composite_primary_keys 8.1.6 → 9.0.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (153) hide show
  1. checksums.yaml +5 -5
  2. data/History.rdoc +59 -7
  3. data/README.rdoc +62 -1
  4. data/Rakefile +4 -1
  5. data/lib/composite_primary_keys/arel/in.rb +6 -0
  6. data/lib/composite_primary_keys/arel/sqlserver.rb +36 -0
  7. data/lib/composite_primary_keys/arel/to_sql.rb +26 -0
  8. data/lib/composite_primary_keys/associations/association.rb +14 -12
  9. data/lib/composite_primary_keys/associations/association_scope.rb +22 -27
  10. data/lib/composite_primary_keys/associations/collection_association.rb +39 -8
  11. data/lib/composite_primary_keys/associations/has_many_association.rb +17 -40
  12. data/lib/composite_primary_keys/associations/has_many_through_association.rb +58 -58
  13. data/lib/composite_primary_keys/associations/join_dependency/join_association.rb +13 -11
  14. data/lib/composite_primary_keys/associations/join_dependency.rb +73 -56
  15. data/lib/composite_primary_keys/associations/preloader/association.rb +75 -73
  16. data/lib/composite_primary_keys/attribute_methods/primary_key.rb +13 -11
  17. data/lib/composite_primary_keys/attribute_methods/read.rb +18 -15
  18. data/lib/composite_primary_keys/attribute_methods/write.rb +21 -19
  19. data/lib/composite_primary_keys/attribute_methods.rb +6 -4
  20. data/lib/composite_primary_keys/autosave_association.rb +19 -54
  21. data/lib/composite_primary_keys/base.rb +18 -82
  22. data/lib/composite_primary_keys/composite_arrays.rb +9 -1
  23. data/lib/composite_primary_keys/composite_predicates.rb +1 -0
  24. data/lib/composite_primary_keys/connection_adapters/abstract_adapter.rb +10 -10
  25. data/lib/composite_primary_keys/connection_adapters/abstract_mysql_adapter.rb +5 -6
  26. data/lib/composite_primary_keys/connection_adapters/sqlite3_adapter.rb +23 -0
  27. data/lib/composite_primary_keys/core.rb +46 -45
  28. data/lib/composite_primary_keys/fixtures.rb +19 -17
  29. data/lib/composite_primary_keys/locking/optimistic.rb +53 -49
  30. data/lib/composite_primary_keys/nested_attributes.rb +60 -53
  31. data/lib/composite_primary_keys/persistence.rb +49 -41
  32. data/lib/composite_primary_keys/relation/batches.rb +35 -32
  33. data/lib/composite_primary_keys/relation/calculations.rb +3 -7
  34. data/lib/composite_primary_keys/relation/finder_methods.rb +122 -55
  35. data/lib/composite_primary_keys/relation/predicate_builder.rb +18 -29
  36. data/lib/composite_primary_keys/relation/query_methods.rb +25 -36
  37. data/lib/composite_primary_keys/relation/where_clause.rb +33 -0
  38. data/lib/composite_primary_keys/relation.rb +96 -43
  39. data/lib/composite_primary_keys/sanitization.rb +6 -15
  40. data/lib/composite_primary_keys/version.rb +3 -3
  41. data/lib/composite_primary_keys.rb +10 -19
  42. data/scripts/console.rb +48 -48
  43. data/scripts/txt2html +76 -76
  44. data/scripts/txt2js +65 -65
  45. data/tasks/databases/mysql.rake +17 -19
  46. data/tasks/databases/oracle.rake +29 -15
  47. data/tasks/databases/postgresql.rake +20 -29
  48. data/tasks/databases/sqlite.rake +25 -0
  49. data/tasks/databases/sqlserver.rake +32 -16
  50. data/tasks/website.rake +18 -18
  51. data/test/README_tests.rdoc +56 -56
  52. data/test/abstract_unit.rb +24 -18
  53. data/test/connections/connection_spec.rb +11 -2
  54. data/test/connections/databases.ci.yml +5 -4
  55. data/test/connections/databases.example.yml +19 -4
  56. data/test/connections/databases.yml +40 -30
  57. data/test/db_test.rb +52 -52
  58. data/test/fixtures/article.rb +1 -0
  59. data/test/fixtures/articles.yml +6 -6
  60. data/test/fixtures/capitol.rb +3 -3
  61. data/test/fixtures/capitols.yml +16 -16
  62. data/test/fixtures/comments.yml +15 -15
  63. data/test/fixtures/db_definitions/mysql.sql +16 -17
  64. data/test/fixtures/db_definitions/oracle.drop.sql +2 -0
  65. data/test/fixtures/db_definitions/oracle.sql +26 -17
  66. data/test/fixtures/db_definitions/postgresql.sql +2 -3
  67. data/test/fixtures/db_definitions/sqlite.sql +0 -1
  68. data/test/fixtures/db_definitions/sqlserver.sql +20 -35
  69. data/test/fixtures/department.rb +5 -5
  70. data/test/fixtures/departments.yml +15 -15
  71. data/test/fixtures/dorms.yml +4 -4
  72. data/test/fixtures/employee.rb +5 -7
  73. data/test/fixtures/employees.yml +1 -5
  74. data/test/fixtures/group.rb +2 -2
  75. data/test/fixtures/groups.yml +6 -6
  76. data/test/fixtures/hack.rb +4 -4
  77. data/test/fixtures/hacks.yml +2 -2
  78. data/test/fixtures/membership_status.rb +2 -2
  79. data/test/fixtures/product.rb +9 -9
  80. data/test/fixtures/product_tariff.rb +5 -5
  81. data/test/fixtures/products.yml +11 -11
  82. data/test/fixtures/reading.rb +4 -4
  83. data/test/fixtures/readings.yml +10 -10
  84. data/test/fixtures/reference_code_using_composite_key_alias.rb +8 -8
  85. data/test/fixtures/reference_code_using_simple_key_alias.rb +8 -8
  86. data/test/fixtures/reference_codes.yml +28 -28
  87. data/test/fixtures/reference_type.rb +1 -1
  88. data/test/fixtures/reference_types.yml +9 -9
  89. data/test/fixtures/restaurant.rb +9 -9
  90. data/test/fixtures/restaurants.yml +14 -14
  91. data/test/fixtures/restaurants_suburbs.yml +10 -10
  92. data/test/fixtures/room.rb +11 -11
  93. data/test/fixtures/room_assignment.rb +13 -13
  94. data/test/fixtures/room_assignments.yml +24 -24
  95. data/test/fixtures/room_attribute.rb +2 -2
  96. data/test/fixtures/room_attribute_assignment.rb +4 -4
  97. data/test/fixtures/room_attribute_assignments.yml +4 -4
  98. data/test/fixtures/room_attributes.yml +2 -2
  99. data/test/fixtures/rooms.yml +12 -12
  100. data/test/fixtures/seat.rb +5 -5
  101. data/test/fixtures/seats.yml +8 -8
  102. data/test/fixtures/street.rb +2 -2
  103. data/test/fixtures/streets.yml +16 -16
  104. data/test/fixtures/student.rb +3 -3
  105. data/test/fixtures/students.yml +15 -15
  106. data/test/fixtures/suburbs.yml +14 -14
  107. data/test/fixtures/tariff.rb +1 -1
  108. data/test/fixtures/tariffs.yml +14 -14
  109. data/test/fixtures/user.rb +0 -1
  110. data/test/plugins/pagination.rb +405 -405
  111. data/test/plugins/pagination_helper.rb +135 -135
  112. data/test/setup.rb +50 -50
  113. data/test/test_aliases.rb +18 -18
  114. data/test/test_associations.rb +18 -17
  115. data/test/test_composite_arrays.rb +24 -24
  116. data/test/test_counter_cache.rb +30 -30
  117. data/test/test_create.rb +14 -6
  118. data/test/test_delete.rb +36 -34
  119. data/test/test_dup.rb +37 -37
  120. data/test/test_exists.rb +39 -39
  121. data/test/test_find.rb +16 -4
  122. data/test/test_habtm.rb +27 -3
  123. data/test/test_miscellaneous.rb +32 -32
  124. data/test/test_nested_attributes.rb +6 -6
  125. data/test/test_pagination.rb +35 -35
  126. data/test/test_polymorphic.rb +0 -7
  127. data/test/test_predicates.rb +20 -20
  128. data/test/test_preload.rb +94 -0
  129. data/test/test_suite.rb +1 -1
  130. data/test/test_update.rb +10 -7
  131. data/test/test_validations.rb +13 -13
  132. metadata +30 -39
  133. data/README_DB2.rdoc +0 -33
  134. data/lib/composite_primary_keys/arel/visitors/to_sql.rb +0 -36
  135. data/lib/composite_primary_keys/associations/singular_association.rb +0 -18
  136. data/lib/composite_primary_keys/attribute_methods/dirty.rb +0 -29
  137. data/lib/composite_primary_keys/attribute_set/builder.rb +0 -20
  138. data/lib/composite_primary_keys/connection_adapters/abstract/connection_specification_changes.rb +0 -70
  139. data/lib/composite_primary_keys/connection_adapters/postgresql_adapter.rb +0 -19
  140. data/lib/composite_primary_keys/dirty.rb +0 -19
  141. data/lib/composite_primary_keys/validations/uniqueness.rb +0 -41
  142. data/tasks/databases/oracle_enhanced.rake +0 -27
  143. data/tasks/databases/sqlite3.rake +0 -27
  144. data/test/connections/native_ibm_db/connection.rb +0 -19
  145. data/test/connections/native_mysql/connection.rb +0 -17
  146. data/test/connections/native_oracle/connection.rb +0 -11
  147. data/test/connections/native_oracle_enhanced/connection.rb +0 -16
  148. data/test/connections/native_postgresql/connection.rb +0 -13
  149. data/test/connections/native_sqlite3/connection.rb +0 -9
  150. data/test/connections/native_sqlserver/connection.rb +0 -11
  151. data/test/fixtures/db_definitions/sqlserver.drop.sql +0 -92
  152. data/test/test_delete_all.rb +0 -35
  153. data/test/test_find_in_batches.rb +0 -30
@@ -1,41 +0,0 @@
1
- module ActiveRecord
2
- module Validations
3
- class UniquenessValidator
4
- def validate_each(record, attribute, value)
5
- finder_class = find_finder_class_for(record)
6
- table = finder_class.arel_table
7
- value = map_enum_attribute(finder_class, attribute, value)
8
-
9
- begin
10
- relation = build_relation(finder_class, table, attribute, value)
11
- # CPK
12
- # relation = relation.and(table[finder_class.primary_key.to_sym].not_eq(record.id)) if record.persisted?
13
- if record.persisted?
14
- not_eq_conditions = Array(finder_class.primary_key).zip(Array(record.send(:id))).map do |name, value|
15
- table[name.to_sym].not_eq(value)
16
- end
17
-
18
- condition = not_eq_conditions.shift
19
- not_eq_conditions.each do |not_eq_condition|
20
- condition = condition.or(not_eq_condition)
21
- end
22
- relation = relation.and(condition)
23
- end
24
- # End CPK
25
- relation = scope_relation(record, table, relation)
26
- relation = finder_class.unscoped.where(relation)
27
- relation = relation.merge(options[:conditions]) if options[:conditions]
28
- rescue RangeError
29
- relation = finder_class.none
30
- end
31
-
32
- if relation.exists?
33
- error_options = options.except(:case_sensitive, :scope, :conditions)
34
- error_options[:value] = value
35
-
36
- record.errors.add(attribute, :taken, error_options)
37
- end
38
- end
39
- end
40
- end
41
- end
@@ -1,27 +0,0 @@
1
- require File.join(PROJECT_ROOT, 'lib', 'composite_primary_keys')
2
- require File.join(PROJECT_ROOT, 'test', 'connections', 'connection_spec')
3
-
4
- namespace :oracle_enhanced do
5
- desc 'Build the Oracle test database'
6
- task :build_database => :load_connection do
7
- options_str = connection_string
8
-
9
- sql = File.join(PROJECT_ROOT, 'test', 'fixtures', 'db_definitions', 'oracle.sql')
10
- sh %( sqlplus #{options_str} < #{sql} )
11
- end
12
-
13
- desc 'Drop the Oracle test database'
14
- task :drop_database => :load_connection do
15
- options_str = connection_string
16
-
17
- sql = File.join(PROJECT_ROOT, 'test', 'fixtures', 'db_definitions', 'oracle.drop.sql')
18
- sh %( sqlplus #{options_str} < #{sql} )
19
- end
20
-
21
- desc 'Rebuild the Oracle test database'
22
- task :rebuild_database => [:drop_database, :build_database]
23
-
24
- task :load_connection do
25
- require File.join(PROJECT_ROOT, "test", "connections", "native_oracle_enhanced", "connection")
26
- end
27
- end
@@ -1,27 +0,0 @@
1
- require File.join(PROJECT_ROOT, 'lib', 'composite_primary_keys')
2
- require File.join(PROJECT_ROOT, 'test', 'connections', 'connection_spec')
3
-
4
- namespace :sqlite3 do
5
- desc 'Build the sqlite test database'
6
- task :build_database => :load_connection do
7
- schema = File.join(PROJECT_ROOT, 'test', 'fixtures', 'db_definitions', 'sqlite.sql')
8
- dbfile = File.join(PROJECT_ROOT, connection_string)
9
- FileUtils.mkdir_p(File.dirname(dbfile))
10
- cmd = "sqlite3 #{dbfile} < #{schema}"
11
- puts cmd
12
- sh %{ #{cmd} }
13
- end
14
-
15
- desc 'Drop the sqlite test database'
16
- task :drop_database => :load_connection do
17
- dbfile = connection_string
18
- sh %{ rm -f #{dbfile} }
19
- end
20
-
21
- desc 'Rebuild the sqlite test database'
22
- task :rebuild_database => [:drop_database, :build_database]
23
-
24
- task :load_connection do
25
- require File.join(PROJECT_ROOT, "test", "connections", "native_sqlite3", "connection")
26
- end
27
- end
@@ -1,19 +0,0 @@
1
- print "Using IBM2 \n"
2
-
3
- gem 'ibm_db'
4
- require 'IBM_DB'
5
-
6
- RAILS_CONNECTION_ADAPTERS = %w( mysql postgresql sqlite firebird sqlserver db2 oracle sybase openbase frontbase ibm_db )
7
-
8
- db1 = 'composite_primary_keys_unittest'
9
-
10
- connection_options = {
11
- :adapter => "ibm_db",
12
- :database => "ocdpdev",
13
- :username => "db2inst1",
14
- :password => "password",
15
- :host => '192.168.2.21'
16
- }
17
-
18
- ActiveRecord::Base.configurations = { db1 => connection_options }
19
- ActiveRecord::Base.establish_connection(connection_options)
@@ -1,17 +0,0 @@
1
- print "Using native MySQL\n"
2
-
3
- def connection_string
4
- options = {}
5
- options['u'] = SPEC['username'] if SPEC['username']
6
- options['p'] = SPEC['password'] if SPEC['password']
7
- options['S'] = SPEC['sock'] if SPEC['sock']
8
- options.map { |key, value| "-#{key}#{value}" }.join(" ")
9
- end
10
-
11
- # Adapter config setup in test/connections/databases.yml
12
- SPEC = CompositePrimaryKeys::ConnectionSpec['mysql']
13
-
14
- def establish_connection
15
- ActiveRecord::Base.establish_connection(SPEC)
16
- end
17
- establish_connection
@@ -1,11 +0,0 @@
1
- print "Using native Oracle\n"
2
-
3
- require File.join(PROJECT_ROOT, 'lib', 'composite_primary_keys')
4
-
5
- def connection_string
6
- "#{SPEC['username']}/#{SPEC['password']}@//#{SPEC['host']}:#{SPEC['port']}/#{SPEC['database']}"
7
- end
8
-
9
- # Adapter config setup in locals/database_connections.rb
10
- SPEC = CompositePrimaryKeys::ConnectionSpec['oracle']
11
- ActiveRecord::Base.establish_connection(SPEC)
@@ -1,16 +0,0 @@
1
- print "Using native Oracle Enhanced\n"
2
-
3
- require File.join(PROJECT_ROOT, 'lib', 'composite_primary_keys')
4
-
5
- def connection_string
6
- "#{SPEC['username']}/#{SPEC['password']}@//#{SPEC['host']}:#{SPEC['port']}/#{SPEC['database']}"
7
- end
8
-
9
- # Adapter config setup in locals/database_connections.rb
10
- SPEC = CompositePrimaryKeys::ConnectionSpec[:oracle_enhanced]
11
- ActiveRecord::Base.establish_connection(SPEC)
12
-
13
- # Change default options for Oracle Enhanced adapter
14
- ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.emulate_dates_by_column_name = true
15
- # Change NLS_DATE_FORMAT to non-default format to verify that all tests should pass
16
- ActiveRecord::Base.connection.execute %q{alter session set nls_date_format = 'DD-MON-YYYY HH24:MI:SS'}
@@ -1,13 +0,0 @@
1
- print "Using native Postgresql\n"
2
-
3
- def connection_string
4
- options = Hash.new
5
- options['U'] = SPEC['username'] if SPEC['username']
6
- options['h'] = SPEC['host'] if SPEC['host']
7
- options['p'] = SPEC['port'] if SPEC['port']
8
- options.map { |key, value| "-#{key} #{value}" }.join(" ")
9
- end
10
-
11
- # Adapter config setup in text/connections/databases.yml
12
- SPEC = CompositePrimaryKeys::ConnectionSpec['postgresql']
13
- ActiveRecord::Base.establish_connection(SPEC)
@@ -1,9 +0,0 @@
1
- print "Using native Sqlite3\n"
2
-
3
- def connection_string
4
- SPEC['database']
5
- end
6
-
7
- # Adapter config setup in text/connections/databases.yml
8
- SPEC = CompositePrimaryKeys::ConnectionSpec['sqlite3']
9
- ActiveRecord::Base.establish_connection(SPEC)
@@ -1,11 +0,0 @@
1
- print "Using native SQL Server\n"
2
-
3
- require File.join(PROJECT_ROOT, 'lib', 'composite_primary_keys')
4
-
5
- def connection_string
6
- "-S #{SPEC['host']} -U #{SPEC['username']} -P\"#{SPEC['password']}\""
7
- end
8
-
9
- # Adapter config setup in locals/database_connections.rb
10
- SPEC = CompositePrimaryKeys::ConnectionSpec['sqlserver']
11
- ActiveRecord::Base.establish_connection(SPEC)
@@ -1,92 +0,0 @@
1
- USE [composite_primary_keys_unittest];
2
- go
3
-
4
- DROP TABLE topics;
5
- go
6
-
7
- DROP TABLE topic_sources;
8
- go
9
-
10
- DROP TABLE reference_types;
11
- go
12
-
13
- DROP TABLE reference_codes;
14
- go
15
-
16
- DROP TABLE products;
17
- go
18
-
19
- DROP TABLE tariffs;
20
- go
21
-
22
- DROP TABLE product_tariffs;
23
- go
24
-
25
- DROP TABLE suburbs;
26
- go
27
-
28
- DROP TABLE streets;
29
- go
30
-
31
- DROP TABLE users;
32
- go
33
-
34
- DROP TABLE articles;
35
- go
36
-
37
- DROP TABLE readings;
38
- go
39
-
40
- DROP TABLE groups;
41
- go
42
-
43
- DROP TABLE memberships;
44
- go
45
-
46
- DROP TABLE membership_statuses;
47
- go
48
-
49
- DROP TABLE departments;
50
- go
51
-
52
- DROP TABLE employees;
53
- go
54
-
55
- DROP TABLE comments;
56
- go
57
-
58
- DROP TABLE hacks;
59
- go
60
-
61
- DROP TABLE restaurants;
62
- go
63
-
64
- DROP TABLE restaurants_suburbs;
65
- go
66
-
67
- DROP TABLE dorms;
68
- go
69
-
70
- DROP TABLE rooms;
71
- go
72
-
73
- DROP TABLE room_attributes;
74
- go
75
-
76
- DROP TABLE room_attribute_assignments;
77
- go
78
-
79
- DROP TABLE students;
80
- go
81
-
82
- DROP TABLE room_assignments;
83
- go
84
-
85
- DROP TABLE seats;
86
- go
87
-
88
- DROP TABLE capitols;
89
- go
90
-
91
- DROP TABLE products_restaurants;
92
- go
@@ -1,35 +0,0 @@
1
- require File.expand_path('../abstract_unit', __FILE__)
2
-
3
- class EmployeesGroup < ActiveRecord::Base
4
-
5
- end
6
-
7
- class TestValidations < ActiveSupport::TestCase
8
- fixtures :employees
9
-
10
- def test_delete_for_model_without_primary_key
11
- EmployeesGroup.create(employee_id: 1, group_id: 100)
12
- EmployeesGroup.create(employee_id: 2, group_id: 102)
13
- EmployeesGroup.create(employee_id: 3, group_id: 103)
14
-
15
- assert_equal(EmployeesGroup.all.size, 3)
16
- exception = assert_raises(ActiveRecord::StatementInvalid) {
17
- EmployeesGroup.where(employee_id: 1).first.destroy
18
- }
19
-
20
- mysql_match = /Unknown column 'employees_groups.' in 'where clause/ =~ exception.message
21
- sqlite3_match = /no such column: employees_groups./ =~ exception.message
22
- postgresql_match = /PG::SyntaxError: ERROR: zero-length delimited identifier/ =~ exception.message
23
- oracle_match = /OCIError: ORA-01741: illegal zero-length identifier/ =~ exception.message
24
-
25
- assert(postgresql_match || mysql_match || sqlite3_match || oracle_match)
26
-
27
- assert(EmployeesGroup.all.size == 3)
28
- end
29
-
30
- def test_delete_all_with_joins
31
- # Let's ignore SQLite for this case since multi-column IN clause like (column1, column2) IN (...) is not allowed.
32
- # It cannot work without some dirty fix.
33
- ReferenceCode.joins(:reference_type).where(:reference_type_id => 1).delete_all unless ReferenceCode.connection.adapter_name == "SQLite"
34
- end
35
- end
@@ -1,30 +0,0 @@
1
- require File.expand_path('../abstract_unit', __FILE__)
2
-
3
- class TestFindInBatches < ActiveSupport::TestCase
4
- fixtures :capitols
5
-
6
- def test_in_batches
7
- capitols = []
8
- Capitol.find_in_batches do |chunk|
9
- capitols += chunk.map(&:country)
10
- end
11
- assert_equal(capitols, ['Canada', 'France', 'Mexico', 'The Netherlands'])
12
- end
13
-
14
- def test_in_small_batches
15
- capitols = []
16
- Capitol.find_in_batches(batch_size: 2) do |chunk|
17
- capitols += chunk.map(&:country)
18
- end
19
-
20
- assert_equal(capitols, ['Canada', 'France', 'Mexico', 'The Netherlands'])
21
- end
22
-
23
- def test_in_one_unit_batch
24
- capitols = []
25
- Capitol.find_in_batches(batch_size: 1) do |chunk|
26
- capitols += chunk.map(&:country)
27
- end
28
- assert_equal(capitols, ['Canada', 'France', 'Mexico', 'The Netherlands'])
29
- end
30
- end