composite_primary_keys 0.8.6 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. data/History.txt +9 -0
  2. data/Manifest.txt +37 -0
  3. data/README.txt +34 -9
  4. data/README_DB2.txt +33 -0
  5. data/Rakefile +8 -119
  6. data/init.rb +2 -0
  7. data/lib/adapter_helper/base.rb +63 -0
  8. data/lib/adapter_helper/mysql.rb +13 -0
  9. data/lib/adapter_helper/oracle.rb +13 -0
  10. data/lib/adapter_helper/postgresql.rb +13 -0
  11. data/lib/adapter_helper/sqlite3.rb +13 -0
  12. data/lib/composite_primary_keys.rb +2 -2
  13. data/lib/composite_primary_keys/associations.rb +53 -28
  14. data/lib/composite_primary_keys/base.rb +4 -1
  15. data/lib/composite_primary_keys/connection_adapters/ibm_db_adapter.rb +21 -0
  16. data/lib/composite_primary_keys/connection_adapters/postgresql_adapter.rb +1 -1
  17. data/lib/composite_primary_keys/migration.rb +13 -0
  18. data/lib/composite_primary_keys/version.rb +2 -2
  19. data/loader.rb +24 -0
  20. data/local/database_connections.rb.sample +10 -0
  21. data/local/paths.rb.sample +2 -0
  22. data/local/tasks.rb.sample +2 -0
  23. data/scripts/console.rb +25 -0
  24. data/tasks/activerecord_selection.rake +43 -0
  25. data/tasks/databases.rake +10 -0
  26. data/tasks/databases/mysql.rake +30 -0
  27. data/tasks/databases/oracle.rake +15 -0
  28. data/tasks/databases/postgresql.rake +26 -0
  29. data/tasks/databases/sqlite3.rake +28 -0
  30. data/tasks/deployment.rake +22 -0
  31. data/tasks/local_setup.rake +13 -0
  32. data/tasks/website.rake +18 -0
  33. data/test/README_tests.txt +67 -0
  34. data/test/abstract_unit.rb +2 -4
  35. data/test/connections/native_ibm_db/connection.rb +23 -0
  36. data/test/connections/native_mysql/connection.rb +8 -13
  37. data/test/connections/native_oracle/connection.rb +8 -11
  38. data/test/connections/native_postgresql/connection.rb +3 -9
  39. data/test/connections/native_sqlite/connection.rb +4 -5
  40. data/test/fixtures/comment.rb +5 -0
  41. data/test/fixtures/comments.yml +14 -0
  42. data/test/fixtures/db_definitions/db2-create-tables.sql +92 -0
  43. data/test/fixtures/db_definitions/db2-drop-tables.sql +13 -0
  44. data/test/fixtures/db_definitions/mysql.sql +24 -0
  45. data/test/fixtures/db_definitions/oracle.drop.sql +6 -0
  46. data/test/fixtures/db_definitions/oracle.sql +28 -1
  47. data/test/fixtures/db_definitions/postgresql.sql +24 -0
  48. data/test/fixtures/db_definitions/sqlite.sql +21 -0
  49. data/test/fixtures/department.rb +5 -0
  50. data/test/fixtures/departments.yml +3 -0
  51. data/test/fixtures/employee.rb +4 -0
  52. data/test/fixtures/employees.yml +9 -0
  53. data/test/fixtures/hack.rb +6 -0
  54. data/test/fixtures/hacks.yml +2 -0
  55. data/test/fixtures/product.rb +3 -2
  56. data/test/fixtures/streets.yml +11 -1
  57. data/test/fixtures/suburb.rb +2 -0
  58. data/test/fixtures/suburbs.yml +6 -1
  59. data/test/fixtures/user.rb +1 -0
  60. data/test/test_associations.rb +29 -5
  61. data/test/test_delete.rb +23 -2
  62. data/test/test_polymorphic.rb +24 -0
  63. data/tmp/test.db +0 -0
  64. data/website/index.html +2 -2
  65. data/website/version-raw.js +1 -1
  66. data/website/version.js +1 -1
  67. metadata +43 -3
@@ -0,0 +1,28 @@
1
+ namespace :sqlite3 do
2
+ desc 'Build the sqlite test databases'
3
+ task :build_databases => :load_connection do
4
+ file = File.join(SCHEMA_PATH, 'sqlite.sql')
5
+ dbfile = File.join(PROJECT_ROOT, ENV['cpk_adapter_options_str'])
6
+ cmd = "mkdir -p #{File.dirname(dbfile)}"
7
+ puts cmd
8
+ sh %{ #{cmd} }
9
+ cmd = "sqlite3 #{dbfile} < #{file}"
10
+ puts cmd
11
+ sh %{ #{cmd} }
12
+ end
13
+
14
+ desc 'Drop the sqlite test databases'
15
+ task :drop_databases => :load_connection do
16
+ dbfile = ENV['cpk_adapter_options_str']
17
+ sh %{ rm -f #{dbfile} }
18
+ end
19
+
20
+ desc 'Rebuild the sqlite test databases'
21
+ task :rebuild_databases => [:drop_databases, :build_databases]
22
+
23
+ task :load_connection do
24
+ require File.join(PROJECT_ROOT, %w[lib adapter_helper sqlite3])
25
+ spec = AdapterHelper::Sqlite3.load_connection_from_env
26
+ ENV['cpk_adapter_options_str'] = spec[:dbfile]
27
+ end
28
+ end
@@ -0,0 +1,22 @@
1
+ desc 'Release the website and new gem version'
2
+ task :deploy => [:check_version, :website, :release] do
3
+ puts "Remember to create SVN tag:"
4
+ puts "svn copy svn+ssh://#{RUBYFORGE_USERNAME}@rubyforge.org/var/svn/#{PATH}/trunk " +
5
+ "svn+ssh://#{RUBYFORGE_USERNAME}@rubyforge.org/var/svn/#{PATH}/tags/REL-#{VERS} "
6
+ puts "Suggested comment:"
7
+ puts "Tagging release #{CHANGES}"
8
+ end
9
+
10
+ desc 'Runs tasks website_generate and install_gem as a local deployment of the gem'
11
+ task :local_deploy => [:website_generate, :install_gem]
12
+
13
+ task :check_version do
14
+ unless ENV['VERSION']
15
+ puts 'Must pass a VERSION=x.y.z release version'
16
+ exit
17
+ end
18
+ unless ENV['VERSION'] == VERS
19
+ puts "Please update your version.rb to match the release version, currently #{VERS}"
20
+ exit
21
+ end
22
+ end
@@ -0,0 +1,13 @@
1
+ namespace :local do
2
+ desc 'Copies over the same local files ready for editing'
3
+ task :setup do
4
+ sample_files = Dir[File.join(PROJECT_ROOT, "local/*.rb.sample")]
5
+ sample_files.each do |sample_file|
6
+ file = sample_file.sub(".sample","")
7
+ unless File.exists?(file)
8
+ puts "Copying #{sample_file} -> #{file}"
9
+ sh %{ cp #{sample_file} #{file} }
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,18 @@
1
+ desc 'Generate website files'
2
+ task :website_generate do
3
+ sh %{ ruby scripts/txt2html website/index.txt > website/index.html }
4
+ sh %{ ruby scripts/txt2js website/version.txt > website/version.js }
5
+ sh %{ ruby scripts/txt2js website/version-raw.txt > website/version-raw.js }
6
+ end
7
+
8
+ desc 'Upload website files to rubyforge'
9
+ task :website_upload do
10
+ config = YAML.load(File.read(File.expand_path("~/.rubyforge/user-config.yml")))
11
+ host = "#{config["username"]}@rubyforge.org"
12
+ remote_dir = "/var/www/gforge-projects/#{RUBYFORGE_PROJECT}/"
13
+ local_dir = 'website'
14
+ sh %{rsync -aCv #{local_dir}/ #{host}:#{remote_dir}}
15
+ end
16
+
17
+ desc 'Generate and upload website files'
18
+ task :website => [:website_generate, :website_upload, :publish_docs]
@@ -0,0 +1,67 @@
1
+ = Composite Primary Keys - Testing Readme
2
+
3
+ == Testing an adapter
4
+
5
+ There are tests available for the following adapters:
6
+
7
+ * ibmdb
8
+ * mysql
9
+ * oracle
10
+ * postgresql
11
+ * sqlite
12
+
13
+ To run the tests for on of the adapters, follow these steps (using mysql in the example):
14
+
15
+ * rake -T | grep mysql
16
+
17
+ rake mysql:build_databases # Build the MySQL test databases
18
+ rake mysql:drop_databases # Drop the MySQL test databases
19
+ rake mysql:rebuild_databases # Rebuild the MySQL test databases
20
+ rake test_mysql # Run tests for test_mysql
21
+
22
+ * rake mysql:build_databases
23
+ * rake test_mysql
24
+
25
+ == Testing against different ActiveRecord versions (or Edge Rails)
26
+
27
+ ActiveRecord is a RubyGem within Rails, and is constantly being improved/changed on
28
+ its repository (http://dev.rubyonrails.org). These changes may create errors for the CPK
29
+ gem. So, we need a way to test CPK against Edge Rails, as well as officially released RubyGems.
30
+
31
+ The default test (as above) uses the latest RubyGem in your cache.
32
+
33
+ You can select an older RubyGem version by running the following:
34
+
35
+ * rake ar:set VERSION=1.14.4 test_mysql
36
+
37
+ == Edge Rails
38
+
39
+ Before you can test CPK against Edge Rails, you must checkout a copy of edge rails somewhere (see http://dev.rubyonrails.org for for examples)
40
+
41
+ * cd /path/to/gems
42
+ * svn co http://svn.rubyonrails.org/rails/trunk rails
43
+
44
+ Say the rails folder is /path/to/gems/rails
45
+
46
+ Three ways to run CPK tests for Edge Rails:
47
+
48
+ i) Run:
49
+
50
+ EDGE_RAILS_DIR=/path/to/gems/rails rake ar:edge test_mysql
51
+
52
+ ii) In your .profile, set the environment variable EDGE_RAILS_DIR=/path/to/gems/rails,
53
+ and once you reload your profile, run:
54
+
55
+ rake ar:edge test_mysql
56
+
57
+ iii) Store the path in local/paths.rb. Run:
58
+
59
+ cp local/paths.rb.sample local/paths.rb
60
+ # Now set ENV['EDGE_RAILS_DIR']=/path/to/gems/rails
61
+ rake ar:edge test_mysql
62
+
63
+ These are all variations of the same theme:
64
+
65
+ * Set the environment variable EDGE_RAILS_DIR to the path to Rails (which contains the activerecord/lib folder)
66
+ * Run: rake ar:edge test_<adapter>
67
+
@@ -1,12 +1,10 @@
1
- $:.unshift(File.dirname(__FILE__) + '/../lib')
1
+ $:.unshift(ENV['AR_LOAD_PATH']) if ENV['AR_LOAD_PATH']
2
2
 
3
- require 'rubygems'
4
3
  require 'test/unit'
5
4
  require 'hash_tricks'
5
+ require 'rubygems'
6
6
  require 'active_record'
7
7
  require 'active_record/fixtures'
8
- require 'active_support/binding_of_caller'
9
- require 'active_support/breakpoint'
10
8
  begin
11
9
  require 'connection'
12
10
  rescue MissingSourceFile => e
@@ -0,0 +1,23 @@
1
+ print "Using IBM2 \n"
2
+ require 'logger'
3
+
4
+ gem 'ibm_db'
5
+ require 'IBM_DB'
6
+
7
+ RAILS_CONNECTION_ADAPTERS = %w( mysql postgresql sqlite firebird sqlserver db2 oracle sybase openbase frontbase ibm_db )
8
+
9
+
10
+ ActiveRecord::Base.logger = Logger.new("debug.log")
11
+
12
+ db1 = 'composite_primary_keys_unittest'
13
+
14
+ connection_options = {
15
+ :adapter => "ibm_db",
16
+ :database => "ocdpdev",
17
+ :username => "db2inst1",
18
+ :password => "password",
19
+ :host => '192.168.2.21'
20
+ }
21
+
22
+ ActiveRecord::Base.configurations = { db1 => connection_options }
23
+ ActiveRecord::Base.establish_connection(connection_options)
@@ -1,18 +1,13 @@
1
1
  print "Using native MySQL\n"
2
+ require 'fileutils'
2
3
  require 'logger'
4
+ require 'adapter_helper/mysql'
3
5
 
4
- ActiveRecord::Base.logger = Logger.new("debug.log")
6
+ log_path = File.expand_path(File.join(File.dirname(__FILE__), %w[.. .. .. log]))
7
+ FileUtils.mkdir_p log_path
8
+ puts "Logging to #{log_path}/debug.log"
9
+ ActiveRecord::Base.logger = Logger.new("#{log_path}/debug.log")
5
10
 
6
- db1 = 'composite_primary_keys_unittest'
7
-
8
- connection_options = {
9
- :adapter => "mysql",
10
- :username => "root",
11
- :password => "root",
12
- :socket => '/Applications/MAMP/tmp/mysql/mysql.sock',
13
- :encoding => "utf8",
14
- :database => db1
15
- }
16
-
17
- ActiveRecord::Base.configurations = { db1 => connection_options }
11
+ # Adapter config setup in locals/database_connections.rb
12
+ connection_options = AdapterHelper::MySQL.load_connection_from_env
18
13
  ActiveRecord::Base.establish_connection(connection_options)
@@ -1,16 +1,13 @@
1
1
  print "Using native Oracle\n"
2
+ require 'fileutils'
2
3
  require 'logger'
4
+ require 'adapter_helper/oracle'
3
5
 
4
- ActiveRecord::Base.logger = Logger.new("debug.log")
6
+ log_path = File.expand_path(File.join(File.dirname(__FILE__), %w[.. .. .. log]))
7
+ FileUtils.mkdir_p log_path
8
+ puts "Logging to #{log_path}/debug.log"
9
+ ActiveRecord::Base.logger = Logger.new("#{log_path}/debug.log")
5
10
 
6
- db1 = 'composite_primary_keys_unittest'
7
-
8
- connection_options = {
9
- :adapter => 'oci',
10
- :username => 'holstdl',
11
- :password => 'holstdl',
12
- :host => 'test'
13
- }
14
-
15
- ActiveRecord::Base.configurations = { db1 => connection_options }
11
+ # Adapter config setup in locals/database_connections.rb
12
+ connection_options = AdapterHelper::MySQL.load_connection_from_env
16
13
  ActiveRecord::Base.establish_connection(connection_options)
@@ -1,15 +1,9 @@
1
1
  print "Using native Postgresql\n"
2
2
  require 'logger'
3
+ require 'adapter_helper/postgresql'
3
4
 
4
5
  ActiveRecord::Base.logger = Logger.new("debug.log")
5
6
 
6
- db1 = 'composite_primary_keys_unittest'
7
-
8
- connection_options = {
9
- :adapter => "postgresql",
10
- :encoding => "utf8",
11
- :database => db1
12
- }
13
-
14
- ActiveRecord::Base.configurations = { db1 => connection_options }
7
+ # Adapter config setup in locals/database_connections.rb
8
+ connection_options = AdapterHelper::Postgresql.load_connection_from_env
15
9
  ActiveRecord::Base.establish_connection(connection_options)
@@ -1,10 +1,9 @@
1
1
  print "Using native Sqlite3\n"
2
2
  require 'logger'
3
+ require 'adapter_helper/sqlite3'
3
4
 
4
5
  ActiveRecord::Base.logger = Logger.new("debug.log")
5
6
 
6
- ActiveRecord::Base.establish_connection(
7
- :adapter => "sqlite3",
8
- :dbfile => "test.db"
9
- )
10
-
7
+ # Adapter config setup in locals/database_connections.rb
8
+ connection_options = AdapterHelper::Sqlite3.load_connection_from_env
9
+ ActiveRecord::Base.establish_connection(connection_options)
@@ -0,0 +1,5 @@
1
+ class Comment < ActiveRecord::Base
2
+ set_primary_keys :id
3
+ belongs_to :person, :polymorphic => true
4
+ end
5
+
@@ -0,0 +1,14 @@
1
+ comment1:
2
+ id: 1
3
+ person_id: 1
4
+ person_type: Employee
5
+
6
+ comment2:
7
+ id: 2
8
+ person_id: 1
9
+ person_type: User
10
+
11
+ comment3:
12
+ id: 3
13
+ person_id: andrew
14
+ person_type: Hack
@@ -0,0 +1,92 @@
1
+ CREATE TABLE reference_types (
2
+ reference_type_id integer NOT NULL generated by default as identity (start with 100, increment by 1, no cache),
3
+ type_label varchar(50) default NULL,
4
+ abbreviation varchar(50) default NULL,
5
+ description varchar(50) default NULL,
6
+ PRIMARY KEY (reference_type_id)
7
+ );
8
+
9
+ CREATE TABLE reference_codes (
10
+ reference_type_id integer NOT NULL,
11
+ reference_code integer NOT NULL,
12
+ code_label varchar(50) default NULL,
13
+ abbreviation varchar(50) default NULL,
14
+ description varchar(50) default NULL,
15
+ PRIMARY KEY (reference_type_id,reference_code)
16
+ );
17
+
18
+ CREATE TABLE products (
19
+ id integer NOT NULL,
20
+ name varchar(50) default NULL,
21
+ PRIMARY KEY (id)
22
+ );
23
+
24
+ CREATE TABLE tariffs (
25
+ tariff_id integer NOT NULL,
26
+ start_date date NOT NULL,
27
+ amount integer default NULL,
28
+ PRIMARY KEY (tariff_id,start_date)
29
+ );
30
+
31
+ CREATE TABLE product_tariffs (
32
+ product_id integer NOT NULL,
33
+ tariff_id integer NOT NULL,
34
+ tariff_start_date date NOT NULL,
35
+ PRIMARY KEY (product_id,tariff_id,tariff_start_date)
36
+ );
37
+
38
+ CREATE TABLE suburbs (
39
+ city_id integer NOT NULL,
40
+ suburb_id integer NOT NULL,
41
+ name varchar(50) NOT NULL,
42
+ PRIMARY KEY (city_id,suburb_id)
43
+ );
44
+
45
+ CREATE TABLE streets (
46
+ id integer NOT NULL ,
47
+ city_id integer NOT NULL,
48
+ suburb_id integer NOT NULL,
49
+ name varchar(50) NOT NULL,
50
+ PRIMARY KEY (id)
51
+ );
52
+
53
+ CREATE TABLE users (
54
+ id integer NOT NULL ,
55
+ name varchar(50) NOT NULL,
56
+ PRIMARY KEY (id)
57
+ );
58
+
59
+ CREATE TABLE articles (
60
+ id integer NOT NULL ,
61
+ name varchar(50) NOT NULL,
62
+ PRIMARY KEY (id)
63
+ );
64
+
65
+ CREATE TABLE readings (
66
+ id integer NOT NULL ,
67
+ user_id integer NOT NULL,
68
+ article_id integer NOT NULL,
69
+ rating integer NOT NULL,
70
+ PRIMARY KEY (id)
71
+ );
72
+
73
+ CREATE TABLE groups (
74
+ id integer NOT NULL ,
75
+ name varchar(50) NOT NULL,
76
+ PRIMARY KEY (id)
77
+ );
78
+
79
+ CREATE TABLE memberships (
80
+ user_id integer NOT NULL,
81
+ group_id integer NOT NULL,
82
+ PRIMARY KEY (user_id,group_id)
83
+ );
84
+
85
+ CREATE TABLE membership_statuses (
86
+ id integer NOT NULL ,
87
+ user_id integer NOT NULL,
88
+ group_id integer NOT NULL,
89
+ status varchar(50) NOT NULL,
90
+ PRIMARY KEY (id)
91
+ );
92
+
@@ -0,0 +1,13 @@
1
+ drop table MEMBERSHIPS;
2
+ drop table REFERENCE_CODES;
3
+ drop table TARIFFS;
4
+ drop table ARTICLES;
5
+ drop table GROUPS;
6
+ drop table MEMBERSHIP_STATUSES;
7
+ drop table READINGS;
8
+ drop table REFERENCE_TYPES;
9
+ drop table STREETS;
10
+ drop table PRODUCTS;
11
+ drop table USERS;
12
+ drop table SUBURBS;
13
+ drop table PRODUCT_TARIFFS;
@@ -90,3 +90,27 @@ CREATE TABLE membership_statuses (
90
90
  PRIMARY KEY (id)
91
91
  ) TYPE=InnoDB;
92
92
 
93
+ CREATE TABLE departments (
94
+ department_id int(11) NOT NULL,
95
+ location_id int(11) NOT NULL,
96
+ PRIMARY KEY (department_id, location_id)
97
+ );
98
+
99
+ CREATE TABLE employees (
100
+ id int(11) NOT NULL auto_increment,
101
+ department_id int(11) DEFAULT NULL,
102
+ location_id int(11) DEFAULT NULL,
103
+ PRIMARY KEY (id)
104
+ );
105
+
106
+ CREATE TABLE comments (
107
+ id int(11) NOT NULL auto_increment,
108
+ person_id varchar(100) DEFAULT NULL,
109
+ person_type varchar(100) DEFAULT NULL,
110
+ PRIMARY KEY (id)
111
+ );
112
+
113
+ CREATE TABLE hacks (
114
+ name varchar(50) NOT NULL,
115
+ PRIMARY KEY (name)
116
+ );