sequel 2.11.0 → 2.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (162) hide show
  1. data/CHANGELOG +168 -0
  2. data/README.rdoc +77 -95
  3. data/Rakefile +100 -80
  4. data/bin/sequel +2 -1
  5. data/doc/advanced_associations.rdoc +23 -32
  6. data/doc/cheat_sheet.rdoc +23 -40
  7. data/doc/dataset_filtering.rdoc +6 -6
  8. data/doc/prepared_statements.rdoc +22 -22
  9. data/doc/release_notes/2.12.0.txt +534 -0
  10. data/doc/schema.rdoc +3 -1
  11. data/doc/sharding.rdoc +8 -8
  12. data/doc/virtual_rows.rdoc +65 -0
  13. data/lib/sequel.rb +1 -1
  14. data/lib/{sequel_core → sequel}/adapters/ado.rb +3 -3
  15. data/lib/{sequel_core → sequel}/adapters/db2.rb +0 -0
  16. data/lib/{sequel_core → sequel}/adapters/dbi.rb +1 -1
  17. data/lib/{sequel_core → sequel}/adapters/do.rb +9 -5
  18. data/lib/{sequel_core → sequel}/adapters/do/mysql.rb +1 -1
  19. data/lib/{sequel_core → sequel}/adapters/do/postgres.rb +1 -1
  20. data/lib/{sequel_core → sequel}/adapters/do/sqlite.rb +1 -1
  21. data/lib/{sequel_core → sequel}/adapters/firebird.rb +84 -80
  22. data/lib/{sequel_core → sequel}/adapters/informix.rb +1 -1
  23. data/lib/{sequel_core → sequel}/adapters/jdbc.rb +21 -14
  24. data/lib/{sequel_core → sequel}/adapters/jdbc/h2.rb +14 -13
  25. data/lib/{sequel_core → sequel}/adapters/jdbc/mysql.rb +1 -1
  26. data/lib/{sequel_core → sequel}/adapters/jdbc/oracle.rb +1 -1
  27. data/lib/{sequel_core → sequel}/adapters/jdbc/postgresql.rb +1 -1
  28. data/lib/{sequel_core → sequel}/adapters/jdbc/sqlite.rb +1 -1
  29. data/lib/{sequel_core → sequel}/adapters/mysql.rb +60 -39
  30. data/lib/{sequel_core → sequel}/adapters/odbc.rb +8 -4
  31. data/lib/{sequel_core → sequel}/adapters/openbase.rb +0 -0
  32. data/lib/{sequel_core → sequel}/adapters/oracle.rb +38 -7
  33. data/lib/{sequel_core → sequel}/adapters/postgres.rb +24 -24
  34. data/lib/{sequel_core → sequel}/adapters/shared/mssql.rb +5 -5
  35. data/lib/{sequel_core → sequel}/adapters/shared/mysql.rb +126 -71
  36. data/lib/{sequel_core → sequel}/adapters/shared/oracle.rb +7 -10
  37. data/lib/{sequel_core → sequel}/adapters/shared/postgres.rb +159 -125
  38. data/lib/{sequel_core → sequel}/adapters/shared/progress.rb +1 -2
  39. data/lib/{sequel_core → sequel}/adapters/shared/sqlite.rb +72 -67
  40. data/lib/{sequel_core → sequel}/adapters/sqlite.rb +11 -7
  41. data/lib/{sequel_core → sequel}/adapters/utils/date_format.rb +0 -0
  42. data/lib/{sequel_core → sequel}/adapters/utils/stored_procedures.rb +0 -0
  43. data/lib/{sequel_core → sequel}/adapters/utils/unsupported.rb +19 -0
  44. data/lib/{sequel_core → sequel}/connection_pool.rb +7 -5
  45. data/lib/sequel/core.rb +221 -0
  46. data/lib/{sequel_core → sequel}/core_sql.rb +91 -49
  47. data/lib/{sequel_core → sequel}/database.rb +264 -149
  48. data/lib/{sequel_core/schema/generator.rb → sequel/database/schema_generator.rb} +6 -2
  49. data/lib/{sequel_core/database/schema.rb → sequel/database/schema_methods.rb} +12 -12
  50. data/lib/sequel/database/schema_sql.rb +224 -0
  51. data/lib/{sequel_core → sequel}/dataset.rb +78 -236
  52. data/lib/{sequel_core → sequel}/dataset/convenience.rb +99 -61
  53. data/lib/{sequel_core/object_graph.rb → sequel/dataset/graph.rb} +16 -14
  54. data/lib/{sequel_core → sequel}/dataset/prepared_statements.rb +1 -1
  55. data/lib/{sequel_core → sequel}/dataset/sql.rb +150 -99
  56. data/lib/sequel/deprecated.rb +593 -0
  57. data/lib/sequel/deprecated_migration.rb +91 -0
  58. data/lib/sequel/exceptions.rb +48 -0
  59. data/lib/sequel/extensions/blank.rb +42 -0
  60. data/lib/{sequel_model → sequel/extensions}/inflector.rb +8 -1
  61. data/lib/{sequel_core → sequel/extensions}/migration.rb +1 -1
  62. data/lib/{sequel_core/dataset → sequel/extensions}/pagination.rb +0 -0
  63. data/lib/{sequel_core → sequel/extensions}/pretty_table.rb +7 -0
  64. data/lib/{sequel_core/dataset → sequel/extensions}/query.rb +7 -0
  65. data/lib/sequel/extensions/string_date_time.rb +47 -0
  66. data/lib/sequel/metaprogramming.rb +43 -0
  67. data/lib/sequel/model.rb +110 -0
  68. data/lib/sequel/model/associations.rb +1300 -0
  69. data/lib/sequel/model/base.rb +937 -0
  70. data/lib/sequel/model/deprecated.rb +204 -0
  71. data/lib/sequel/model/deprecated_hooks.rb +103 -0
  72. data/lib/sequel/model/deprecated_inflector.rb +335 -0
  73. data/lib/sequel/model/deprecated_validations.rb +388 -0
  74. data/lib/sequel/model/errors.rb +39 -0
  75. data/lib/{sequel_model → sequel/model}/exceptions.rb +4 -4
  76. data/lib/sequel/model/inflections.rb +208 -0
  77. data/lib/sequel/model/plugins.rb +76 -0
  78. data/lib/sequel/plugins/caching.rb +122 -0
  79. data/lib/sequel/plugins/hook_class_methods.rb +122 -0
  80. data/lib/sequel/plugins/schema.rb +53 -0
  81. data/lib/sequel/plugins/serialization.rb +117 -0
  82. data/lib/sequel/plugins/single_table_inheritance.rb +63 -0
  83. data/lib/sequel/plugins/validation_class_methods.rb +384 -0
  84. data/lib/sequel/plugins/validation_helpers.rb +150 -0
  85. data/lib/{sequel_core → sequel}/sql.rb +125 -190
  86. data/lib/{sequel_core → sequel}/version.rb +2 -1
  87. data/lib/sequel_core.rb +1 -172
  88. data/lib/sequel_model.rb +1 -91
  89. data/spec/adapters/firebird_spec.rb +5 -5
  90. data/spec/adapters/informix_spec.rb +1 -1
  91. data/spec/adapters/mysql_spec.rb +128 -42
  92. data/spec/adapters/oracle_spec.rb +47 -19
  93. data/spec/adapters/postgres_spec.rb +64 -52
  94. data/spec/adapters/spec_helper.rb +1 -1
  95. data/spec/adapters/sqlite_spec.rb +12 -17
  96. data/spec/{sequel_core → core}/connection_pool_spec.rb +10 -10
  97. data/spec/{sequel_core → core}/core_ext_spec.rb +19 -19
  98. data/spec/{sequel_core → core}/core_sql_spec.rb +68 -71
  99. data/spec/{sequel_core → core}/database_spec.rb +135 -99
  100. data/spec/{sequel_core → core}/dataset_spec.rb +398 -242
  101. data/spec/{sequel_core → core}/expression_filters_spec.rb +13 -13
  102. data/spec/core/migration_spec.rb +263 -0
  103. data/spec/{sequel_core → core}/object_graph_spec.rb +10 -10
  104. data/spec/{sequel_core → core}/pretty_table_spec.rb +2 -2
  105. data/spec/{sequel_core → core}/schema_generator_spec.rb +0 -0
  106. data/spec/{sequel_core → core}/schema_spec.rb +8 -10
  107. data/spec/{sequel_core → core}/spec_helper.rb +29 -2
  108. data/spec/{sequel_core → core}/version_spec.rb +0 -0
  109. data/spec/extensions/blank_spec.rb +67 -0
  110. data/spec/extensions/caching_spec.rb +201 -0
  111. data/spec/{sequel_model/hooks_spec.rb → extensions/hook_class_methods_spec.rb} +8 -23
  112. data/spec/{sequel_model → extensions}/inflector_spec.rb +3 -0
  113. data/spec/{sequel_core → extensions}/migration_spec.rb +4 -4
  114. data/spec/extensions/pagination_spec.rb +99 -0
  115. data/spec/extensions/pretty_table_spec.rb +91 -0
  116. data/spec/extensions/query_spec.rb +85 -0
  117. data/spec/{sequel_model → extensions}/schema_spec.rb +22 -1
  118. data/spec/extensions/serialization_spec.rb +109 -0
  119. data/spec/extensions/single_table_inheritance_spec.rb +53 -0
  120. data/spec/{sequel_model → extensions}/spec_helper.rb +13 -4
  121. data/spec/extensions/string_date_time_spec.rb +93 -0
  122. data/spec/{sequel_model/validations_spec.rb → extensions/validation_class_methods_spec.rb} +15 -103
  123. data/spec/extensions/validation_helpers_spec.rb +291 -0
  124. data/spec/integration/dataset_test.rb +31 -0
  125. data/spec/integration/eager_loader_test.rb +17 -30
  126. data/spec/integration/schema_test.rb +8 -5
  127. data/spec/integration/spec_helper.rb +17 -0
  128. data/spec/integration/transaction_test.rb +68 -0
  129. data/spec/{sequel_model → model}/association_reflection_spec.rb +0 -0
  130. data/spec/{sequel_model → model}/associations_spec.rb +23 -10
  131. data/spec/{sequel_model → model}/base_spec.rb +29 -20
  132. data/spec/{sequel_model → model}/caching_spec.rb +16 -14
  133. data/spec/{sequel_model → model}/dataset_methods_spec.rb +0 -0
  134. data/spec/{sequel_model → model}/eager_loading_spec.rb +8 -8
  135. data/spec/model/hooks_spec.rb +472 -0
  136. data/spec/model/inflector_spec.rb +126 -0
  137. data/spec/{sequel_model → model}/model_spec.rb +25 -20
  138. data/spec/model/plugins_spec.rb +142 -0
  139. data/spec/{sequel_model → model}/record_spec.rb +121 -62
  140. data/spec/model/schema_spec.rb +92 -0
  141. data/spec/model/spec_helper.rb +124 -0
  142. data/spec/model/validations_spec.rb +1080 -0
  143. metadata +136 -107
  144. data/lib/sequel_core/core_ext.rb +0 -217
  145. data/lib/sequel_core/dataset/callback.rb +0 -13
  146. data/lib/sequel_core/dataset/schema.rb +0 -15
  147. data/lib/sequel_core/deprecated.rb +0 -26
  148. data/lib/sequel_core/exceptions.rb +0 -44
  149. data/lib/sequel_core/schema.rb +0 -2
  150. data/lib/sequel_core/schema/sql.rb +0 -325
  151. data/lib/sequel_model/association_reflection.rb +0 -267
  152. data/lib/sequel_model/associations.rb +0 -499
  153. data/lib/sequel_model/base.rb +0 -539
  154. data/lib/sequel_model/caching.rb +0 -82
  155. data/lib/sequel_model/dataset_methods.rb +0 -26
  156. data/lib/sequel_model/eager_loading.rb +0 -370
  157. data/lib/sequel_model/hooks.rb +0 -101
  158. data/lib/sequel_model/plugins.rb +0 -62
  159. data/lib/sequel_model/record.rb +0 -568
  160. data/lib/sequel_model/schema.rb +0 -49
  161. data/lib/sequel_model/validations.rb +0 -429
  162. data/spec/sequel_model/plugins_spec.rb +0 -80
@@ -48,6 +48,37 @@ describe "Simple Dataset operations" do
48
48
  @ds.first.should == {:id=>1, :number=>10}
49
49
  sqls_should_be('SELECT * FROM items LIMIT 1')
50
50
  end
51
+
52
+ specify "should alias columns correctly" do
53
+ @ds.select(:id___x, :number___n).first.should == {:x=>1, :n=>10}
54
+ sqls_should_be("SELECT id AS 'x', number AS 'n' FROM items LIMIT 1")
55
+ end
56
+ end
57
+
58
+ describe "Simple Dataset operations" do
59
+ before do
60
+ INTEGRATION_DB.create_table!(:items) do
61
+ Integer :number
62
+ TrueClass :flag
63
+ end
64
+ @ds = INTEGRATION_DB[:items]
65
+ end
66
+ after do
67
+ INTEGRATION_DB.drop_table(:items)
68
+ end
69
+
70
+ specify "should deal with boolean conditions correctly" do
71
+ @ds.insert(:number=>1, :flag=>true)
72
+ @ds.insert(:number=>2, :flag=>false)
73
+ @ds.insert(:number=>3, :flag=>nil)
74
+ @ds.order!(:number)
75
+ @ds.filter(:flag=>true).map(:number).should == [1]
76
+ @ds.filter(:flag=>false).map(:number).should == [2]
77
+ @ds.filter(:flag=>nil).map(:number).should == [3]
78
+ @ds.exclude(:flag=>true).map(:number).should == [2, 3]
79
+ @ds.exclude(:flag=>false).map(:number).should == [1, 3]
80
+ @ds.exclude(:flag=>nil).map(:number).should == [1, 2]
81
+ end
51
82
  end
52
83
 
53
84
  describe "Simple Dataset operations in transactions" do
@@ -69,7 +69,7 @@ describe "Eagerly loading a tree structure" do
69
69
  clear_sqls
70
70
  end
71
71
  after do
72
- Node.drop_table
72
+ INTEGRATION_DB.drop_table :nodes
73
73
  Object.send(:remove_const, :Node)
74
74
  end
75
75
 
@@ -138,26 +138,18 @@ describe "Association Extensions" do
138
138
  before do
139
139
  module ::FindOrCreate
140
140
  def find_or_create(vals)
141
- # Exploits the fact that Sequel filters are ruby objects that
142
- # can be introspected.
143
- author_id = @opts[:where].args[1]
144
- first(vals) || \
145
- @opts[:models][nil].create(vals.merge(:author_id=>author_id))
141
+ first(vals) || model.create(vals.merge(:author_id=>model_object.pk))
146
142
  end
143
+ def find_or_create_by_name(name)
144
+ first(:name=>name) || model.create(:name=>name, :author_id=>model_object.pk)
145
+ end
147
146
  end
148
147
  INTEGRATION_DB.instance_variable_set(:@schemas, nil)
149
148
  INTEGRATION_DB.create_table!(:authors) do
150
149
  primary_key :id
151
150
  end
152
151
  class ::Author < Sequel::Model
153
- one_to_many :authorships, :extend=>FindOrCreate, :dataset=>(proc do
154
- key = pk
155
- ds = Authorship.filter(:author_id=>key)
156
- ds.meta_def(:find_or_create_by_name) do |name|
157
- first(:name=>name) || Authorship.create(:name=>name, :author_id=>key)
158
- end
159
- ds
160
- end)
152
+ one_to_many :authorships, :extend=>FindOrCreate
161
153
  end
162
154
  INTEGRATION_DB.create_table!(:authorships) do
163
155
  primary_key :id
@@ -171,8 +163,7 @@ describe "Association Extensions" do
171
163
  clear_sqls
172
164
  end
173
165
  after do
174
- Authorship.drop_table
175
- Author.drop_table
166
+ INTEGRATION_DB.drop_table :authorships, :authors
176
167
  Object.send(:remove_const, :Author)
177
168
  Object.send(:remove_const, :Authorship)
178
169
  end
@@ -181,7 +172,7 @@ describe "Association Extensions" do
181
172
  Authorship.count.should == 0
182
173
  sqls_should_be('SELECT COUNT(*) FROM authorships LIMIT 1')
183
174
  authorship = @author.authorships_dataset.find_or_create_by_name('Bob')
184
- sqls_should_be("SELECT * FROM authorships WHERE ((author_id = 1) AND (name = 'Bob')) LIMIT 1",
175
+ sqls_should_be("SELECT * FROM authorships WHERE ((authorships.author_id = 1) AND (name = 'Bob')) LIMIT 1",
185
176
  /INSERT INTO authorships \((author_id, name|name, author_id)\) VALUES \((1, 'Bob'|'Bob', 1)\)/,
186
177
  "SELECT * FROM authorships WHERE (id = 1) LIMIT 1")
187
178
  Authorship.count.should == 1
@@ -190,11 +181,11 @@ describe "Association Extensions" do
190
181
  authorship.name.should == 'Bob'
191
182
  authorship.author_id.should == @author.id
192
183
  @author.authorships_dataset.find_or_create_by_name('Bob').should == authorship
193
- sqls_should_be("SELECT * FROM authorships WHERE ((author_id = 1) AND (name = 'Bob')) LIMIT 1")
184
+ sqls_should_be("SELECT * FROM authorships WHERE ((authorships.author_id = 1) AND (name = 'Bob')) LIMIT 1")
194
185
  Authorship.count.should == 1
195
186
  sqls_should_be('SELECT COUNT(*) FROM authorships LIMIT 1')
196
187
  authorship2 = @author.authorships_dataset.find_or_create(:name=>'Jim')
197
- sqls_should_be("SELECT * FROM authorships WHERE ((author_id = 1) AND (name = 'Jim')) LIMIT 1",
188
+ sqls_should_be("SELECT * FROM authorships WHERE ((authorships.author_id = 1) AND (name = 'Jim')) LIMIT 1",
198
189
  /INSERT INTO authorships \((author_id, name|name, author_id)\) VALUES \((1, 'Jim'|'Jim', 1)\)/,
199
190
  "SELECT * FROM authorships WHERE (id = 2) LIMIT 1")
200
191
  Authorship.count.should == 2
@@ -204,7 +195,7 @@ describe "Association Extensions" do
204
195
  authorship2.name.should == 'Jim'
205
196
  authorship2.author_id.should == @author.id
206
197
  @author.authorships_dataset.find_or_create(:name=>'Jim').should == authorship2
207
- sqls_should_be("SELECT * FROM authorships WHERE ((author_id = 1) AND (name = 'Jim')) LIMIT 1")
198
+ sqls_should_be("SELECT * FROM authorships WHERE ((authorships.author_id = 1) AND (name = 'Jim')) LIMIT 1")
208
199
  end
209
200
  end
210
201
 
@@ -291,9 +282,7 @@ describe "has_many :through has_many and has_one :through belongs_to" do
291
282
  clear_sqls
292
283
  end
293
284
  after do
294
- Invoice.drop_table
295
- Client.drop_table
296
- Firm.drop_table
285
+ INTEGRATION_DB.drop_table :invoices, :clients, :firms
297
286
  Object.send(:remove_const, :Firm)
298
287
  Object.send(:remove_const, :Client)
299
288
  Object.send(:remove_const, :Invoice)
@@ -379,9 +368,10 @@ describe "Polymorphic Associations" do
379
368
  text :attachable_type
380
369
  end
381
370
  class ::Asset < Sequel::Model
371
+ m = method(:constantize)
382
372
  many_to_one :attachable, :reciprocal=>:assets, \
383
373
  :dataset=>(proc do
384
- klass = attachable_type.constantize
374
+ klass = m.call(attachable_type)
385
375
  klass.filter(klass.primary_key=>attachable_id)
386
376
  end), \
387
377
  :eager_loader=>(proc do |key_hash, assets, associations|
@@ -391,7 +381,7 @@ describe "Polymorphic Associations" do
391
381
  ((id_map[asset.attachable_type] ||= {})[asset.attachable_id] ||= []) << asset
392
382
  end
393
383
  id_map.each do |klass_name, id_map|
394
- klass = klass_name.constantize
384
+ klass = m.call(klass_name)
395
385
  klass.filter(klass.primary_key=>id_map.keys).all do |attach|
396
386
  id_map[attach.pk].each do |asset|
397
387
  asset.associations[:attachable] = attach
@@ -469,9 +459,7 @@ describe "Polymorphic Associations" do
469
459
  clear_sqls
470
460
  end
471
461
  after do
472
- Asset.drop_table
473
- Post.drop_table
474
- Note.drop_table
462
+ INTEGRATION_DB.drop_table :assets, :posts, :notes
475
463
  Object.send(:remove_const, :Asset)
476
464
  Object.send(:remove_const, :Post)
477
465
  Object.send(:remove_const, :Note)
@@ -609,8 +597,7 @@ describe "many_to_one/one_to_many not referencing primary key" do
609
597
  clear_sqls
610
598
  end
611
599
  after do
612
- Invoice.drop_table
613
- Client.drop_table
600
+ INTEGRATION_DB.drop_table :invoices, :clients
614
601
  Object.send(:remove_const, :Client)
615
602
  Object.send(:remove_const, :Invoice)
616
603
  end
@@ -20,11 +20,11 @@ describe "Database schema parser" do
20
20
  INTEGRATION_DB.identifier_input_method = :reverse
21
21
  INTEGRATION_DB.default_schema = nil if INTEGRATION_DB.default_schema
22
22
  INTEGRATION_DB.create_table!(:items){integer :number}
23
- INTEGRATION_DB.schema(nil, :reload=>true)[:items].should be_a_kind_of(Array)
23
+ INTEGRATION_DB.schema(:items, :reload=>true).should be_a_kind_of(Array)
24
24
  INTEGRATION_DB.schema(:items, :reload=>true).first.first.should == :number
25
25
  end
26
26
 
27
- specify "should be a hash with table_names as symbols" do
27
+ deprec_specify "should be a hash with table_names as symbols" do
28
28
  INTEGRATION_DB.create_table!(:items){integer :number}
29
29
  schema = INTEGRATION_DB.schema(nil, :reload=>true)
30
30
  schema.should be_a_kind_of(Hash)
@@ -33,13 +33,16 @@ describe "Database schema parser" do
33
33
 
34
34
  specify "should not issue an sql query if the schema has been loaded unless :reload is true" do
35
35
  INTEGRATION_DB.create_table!(:items){integer :number}
36
- INTEGRATION_DB.schema(nil, :reload=>true)
36
+ INTEGRATION_DB.schema(:items, :reload=>true)
37
37
  clear_sqls
38
- INTEGRATION_DB.schema
38
+ INTEGRATION_DB.schema(:items)
39
39
  sqls_should_be
40
+ clear_sqls
41
+ INTEGRATION_DB.schema(:items, :reload=>true)
42
+ sqls_should_be "PRAGMA table_info('items')"
40
43
  end
41
44
 
42
- specify "should give the same result for a single table regardless of whether schema was called for a single table" do
45
+ deprec_specify "should give the same result for a single table regardless of whether schema was called for a single table" do
43
46
  INTEGRATION_DB.create_table!(:items){integer :number}
44
47
  INTEGRATION_DB.schema(:items, :reload=>true).should == INTEGRATION_DB.schema(nil, :reload=>true)[:items]
45
48
  end
@@ -8,6 +8,23 @@ begin
8
8
  rescue LoadError
9
9
  end
10
10
 
11
+ Sequel.virtual_row_instance_eval = true
12
+ Sequel::Model.use_transactions = false
13
+
14
+ module Spec::Example::ExampleGroupMethods
15
+ def deprec_specify(*args, &block)
16
+ specify(*args) do
17
+ output = Sequel::Deprecation.output
18
+ Sequel::Deprecation.output = nil
19
+ begin
20
+ instance_eval(&block)
21
+ ensure
22
+ Sequel::Deprecation.output = output
23
+ end
24
+ end
25
+ end
26
+ end
27
+
11
28
  $sqls = []
12
29
  def clear_sqls
13
30
  $sqls.clear
@@ -0,0 +1,68 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper.rb')
2
+
3
+ describe "Database transactions" do
4
+ before do
5
+ INTEGRATION_DB.drop_table(:items) if INTEGRATION_DB.table_exists?(:items)
6
+ INTEGRATION_DB.create_table(:items){String :name; Integer :value}
7
+ @d = INTEGRATION_DB[:items]
8
+ clear_sqls
9
+ end
10
+ after do
11
+ INTEGRATION_DB.drop_table(:items) if INTEGRATION_DB.table_exists?(:items)
12
+ end
13
+
14
+ specify "should support transactions" do
15
+ INTEGRATION_DB.transaction do
16
+ @d << {:name => 'abc', :value => 1}
17
+ end
18
+
19
+ @d.count.should == 1
20
+ end
21
+
22
+ specify "should have #transaction yield the connection" do
23
+ INTEGRATION_DB.transaction do |conn|
24
+ conn.should_not == nil
25
+ end
26
+ end
27
+
28
+ specify "should correctly rollback transactions" do
29
+ proc do
30
+ INTEGRATION_DB.transaction do
31
+ @d << {:name => 'abc', :value => 1}
32
+ raise Interrupt, 'asdf'
33
+ end
34
+ end.should raise_error(Interrupt)
35
+
36
+ proc do
37
+ INTEGRATION_DB.transaction do
38
+ @d << {:name => 'abc', :value => 1}
39
+ raise Sequel::Rollback
40
+ end
41
+ end.should_not raise_error
42
+
43
+ @d.count.should == 0
44
+ end
45
+
46
+ specify "should handle returning inside of the block by committing" do
47
+ def INTEGRATION_DB.ret_commit
48
+ transaction do
49
+ self[:items] << {:name => 'abc'}
50
+ return
51
+ self[:items] << {:name => 'd'}
52
+ end
53
+ end
54
+
55
+ @d.count.should == 0
56
+ INTEGRATION_DB.ret_commit
57
+ @d.count.should == 1
58
+ INTEGRATION_DB.ret_commit
59
+ @d.count.should == 2
60
+ proc do
61
+ INTEGRATION_DB.transaction do
62
+ raise Interrupt, 'asdf'
63
+ end
64
+ end.should raise_error(Interrupt)
65
+
66
+ @d.count.should == 2
67
+ end
68
+ end
@@ -392,7 +392,7 @@ describe Sequel::Model, "many_to_one" do
392
392
  proc{c.parent = p}.should raise_error(Sequel::Error)
393
393
  end
394
394
 
395
- it "should have belongs_to alias" do
395
+ deprec_specify "should have belongs_to alias" do
396
396
  @c2.belongs_to :parent, :class => @c2
397
397
 
398
398
  d = @c2.load(:id => 1)
@@ -567,7 +567,7 @@ describe Sequel::Model, "one_to_many" do
567
567
  v = n.historical_values_dataset
568
568
  v.should be_a_kind_of(Sequel::Dataset)
569
569
  v.sql.should == 'SELECT * FROM historical_values WHERE (historical_values.node_id = 1234)'
570
- v.model_classes.should == {nil => HistoricalValue}
570
+ v.model.should == HistoricalValue
571
571
  end
572
572
 
573
573
  it "should use class inside a module if given as a string" do
@@ -582,7 +582,7 @@ describe Sequel::Model, "one_to_many" do
582
582
  v = n.historical_values_dataset
583
583
  v.should be_a_kind_of(Sequel::Dataset)
584
584
  v.sql.should == 'SELECT * FROM values WHERE (values.node_id = 1234)'
585
- v.model_classes.should == {nil => Historical::Value}
585
+ v.model.should == Historical::Value
586
586
  end
587
587
 
588
588
  it "should use explicit key if given" do
@@ -599,7 +599,7 @@ describe Sequel::Model, "one_to_many" do
599
599
 
600
600
  n = @c2.new(:id => 1234)
601
601
  a = @c1.new(:id => 2345)
602
- a.save!
602
+ a.save
603
603
  MODEL_DB.reset
604
604
  a.should == n.add_attribute(a)
605
605
  MODEL_DB.sqls.should == ['UPDATE attributes SET node_id = 1234 WHERE (id = 2345)']
@@ -610,7 +610,7 @@ describe Sequel::Model, "one_to_many" do
610
610
 
611
611
  n = @c2.new(:id => 1234)
612
612
  a = @c1.new(:id => 2345)
613
- a.save!
613
+ a.save
614
614
  MODEL_DB.reset
615
615
  a.should == n.remove_attribute(a)
616
616
  MODEL_DB.sqls.should == ['UPDATE attributes SET node_id = NULL WHERE (id = 2345)']
@@ -621,7 +621,7 @@ describe Sequel::Model, "one_to_many" do
621
621
 
622
622
  n = @c2.new(:id => 1234, :xxx=>5)
623
623
  a = @c1.new(:id => 2345)
624
- a.save!
624
+ a.save
625
625
  MODEL_DB.reset
626
626
  a.should == n.add_attribute(a)
627
627
  MODEL_DB.sqls.should == ['UPDATE attributes SET node_id = 5 WHERE (id = 2345)']
@@ -866,7 +866,7 @@ describe Sequel::Model, "one_to_many" do
866
866
  im2.should_not(include('remove_all_attributes'))
867
867
  end
868
868
 
869
- it "should have has_many alias" do
869
+ deprec_specify "should have has_many alias" do
870
870
  @c2.has_many :attributes, :class => @c1
871
871
 
872
872
  n = @c2.new(:id => 1234)
@@ -936,7 +936,7 @@ describe Sequel::Model, "one_to_many" do
936
936
  d = @c1.dataset
937
937
  def d.fetch_rows(s); end
938
938
  node.attributes.should == []
939
- def attrib.save!; self end
939
+ def attrib.save; self end
940
940
  node.add_attribute(attrib)
941
941
  node.associations[:attributes].should == [attrib]
942
942
  node.remove_all_attributes.should == [attrib]
@@ -958,7 +958,7 @@ describe Sequel::Model, "one_to_many" do
958
958
  node = @c2.new(:id => 1234)
959
959
  node.attributes.should == []
960
960
  attrib.node.should == nil
961
- def attrib.save!; self end
961
+ def attrib.save; self end
962
962
  node.add_attribute(attrib)
963
963
  attrib.associations[:node].should == node
964
964
  node.remove_all_attributes
@@ -1006,6 +1006,19 @@ describe Sequel::Model, "one_to_many" do
1006
1006
  MODEL_DB.sqls.last.should == 'UPDATE attributes SET node_id = NULL WHERE ((node_id = 1234) AND (id != 3))'
1007
1007
  end
1008
1008
 
1009
+ it "should use a transaction in the setter method if the :one_to_one option is true" do
1010
+ @c2.one_to_many :attributes, :class => @c1, :one_to_one=>true
1011
+ @c2.use_transactions = true
1012
+ MODEL_DB.sqls.clear
1013
+ attrib = @c1.load(:id=>3)
1014
+ @c2.new(:id => 1234).attribute = attrib
1015
+ MODEL_DB.sqls.length.should == 4
1016
+ MODEL_DB.sqls.first.should == 'BEGIN'
1017
+ MODEL_DB.sqls[1].should =~ /UPDATE attributes SET (node_id = 1234, id = 3|id = 3, node_id = 1234) WHERE \(id = 3\)/
1018
+ MODEL_DB.sqls[2].should == 'UPDATE attributes SET node_id = NULL WHERE ((node_id = 1234) AND (id != 3))'
1019
+ MODEL_DB.sqls.last.should == 'COMMIT'
1020
+ end
1021
+
1009
1022
  it "should have the setter method for the :one_to_one option respect the :primary_key option" do
1010
1023
  @c2.one_to_many :attributes, :class => @c1, :one_to_one=>true, :primary_key=>:xxx
1011
1024
  attrib = @c1.new(:id=>3)
@@ -1519,7 +1532,7 @@ describe Sequel::Model, "many_to_many" do
1519
1532
  im2.should_not(include('remove_all_attributes'))
1520
1533
  end
1521
1534
 
1522
- it "should have has_and_belongs_to_many alias" do
1535
+ deprec_specify "should have has_and_belongs_to_many alias" do
1523
1536
  @c2.has_and_belongs_to_many :attributes, :class => @c1
1524
1537
 
1525
1538
  n = @c2.new(:id => 1234)
@@ -37,7 +37,7 @@ describe "Model#serialize" do
37
37
  MODEL_DB.reset
38
38
  end
39
39
 
40
- it "should translate values to YAML when creating records" do
40
+ deprec_specify "should translate values to YAML when creating records" do
41
41
  @c = Class.new(Sequel::Model(:items)) do
42
42
  no_primary_key
43
43
  serialize :abc
@@ -53,7 +53,7 @@ describe "Model#serialize" do
53
53
  ]
54
54
  end
55
55
 
56
- it "should support calling after the class is defined" do
56
+ deprec_specify "should support calling after the class is defined" do
57
57
  @c = Class.new(Sequel::Model(:items)) do
58
58
  no_primary_key
59
59
  columns :def
@@ -70,7 +70,7 @@ describe "Model#serialize" do
70
70
  ]
71
71
  end
72
72
 
73
- it "should support using the Marshal format" do
73
+ deprec_specify "should support using the Marshal format" do
74
74
  @c = Class.new(Sequel::Model(:items)) do
75
75
  no_primary_key
76
76
  serialize :abc, :format => :marshal
@@ -87,7 +87,7 @@ describe "Model#serialize" do
87
87
  ]
88
88
  end
89
89
 
90
- it "should translate values to and from YAML using accessor methods" do
90
+ deprec_specify "should translate values to and from YAML using accessor methods" do
91
91
  @c = Class.new(Sequel::Model(:items)) do
92
92
  serialize :abc, :def
93
93
  columns :abc, :def
@@ -127,7 +127,7 @@ describe "Model#serialize" do
127
127
  o.abc.should == 1
128
128
  o.def.should == "hello"
129
129
 
130
- o.update_values(:abc => 23)
130
+ o.update(:abc => 23)
131
131
  ds.sqls.should == "UPDATE items SET abc = '#{23.to_yaml}' WHERE (id = 1)"
132
132
 
133
133
  ds.raw = {:id => 1, :abc => "--- 1\n", :def => "--- hello\n"}
@@ -184,9 +184,8 @@ describe Sequel::Model, "dataset" do
184
184
  end
185
185
 
186
186
  describe Sequel::Model, ".def_dataset_method" do
187
- setup do
187
+ before do
188
188
  @c = Class.new(Sequel::Model(:items)) do
189
- @dataset = Object.new
190
189
  end
191
190
  end
192
191
 
@@ -233,18 +232,18 @@ describe Sequel::Model, ".def_dataset_method" do
233
232
  end
234
233
 
235
234
  describe "A model class with implicit table name" do
236
- setup do
235
+ before do
237
236
  class Donkey < Sequel::Model
238
237
  end
239
238
  end
240
239
 
241
240
  specify "should have a dataset associated with the model class" do
242
- Donkey.dataset.model_classes.should == {nil => Donkey}
241
+ Donkey.dataset.model.should == Donkey
243
242
  end
244
243
  end
245
244
 
246
245
  describe "A model inheriting from a model" do
247
- setup do
246
+ before do
248
247
  class Feline < Sequel::Model
249
248
  end
250
249
 
@@ -253,18 +252,18 @@ describe "A model inheriting from a model" do
253
252
  end
254
253
 
255
254
  specify "should have a dataset associated with itself" do
256
- Feline.dataset.model_classes.should == {nil => Feline}
257
- Leopard.dataset.model_classes.should == {nil => Leopard}
255
+ Feline.dataset.model.should == Feline
256
+ Leopard.dataset.model.should == Leopard
258
257
  end
259
258
  end
260
259
 
261
260
  describe "Model.db=" do
262
- setup do
261
+ before do
263
262
  $db1 = MockDatabase.new
264
263
  $db2 = MockDatabase.new
265
264
 
266
265
  class BlueBlue < Sequel::Model(:items)
267
- set_dataset $db1[:blue]
266
+ set_dataset $db1[:blue].filter(:x=>1)
268
267
  end
269
268
  end
270
269
 
@@ -274,10 +273,20 @@ describe "Model.db=" do
274
273
  BlueBlue.dataset.db.should === $db2
275
274
  BlueBlue.dataset.db.should_not === $db1
276
275
  end
276
+
277
+ specify "should keep the same dataset options" do
278
+ BlueBlue.db = $db2
279
+ BlueBlue.dataset.sql.should == 'SELECT * FROM blue WHERE (x = 1)'
280
+ end
281
+
282
+ specify "should use the database for subclasses" do
283
+ BlueBlue.db = $db2
284
+ Class.new(BlueBlue).db.should === $db2
285
+ end
277
286
  end
278
287
 
279
288
  describe Sequel::Model, ".(allowed|restricted)_columns " do
280
- setup do
289
+ before do
281
290
  @c = Class.new(Sequel::Model(:blahblah)) do
282
291
  columns :x, :y, :z
283
292
  def refresh
@@ -340,7 +349,7 @@ describe Sequel::Model, ".(allowed|restricted)_columns " do
340
349
  end
341
350
 
342
351
  describe Sequel::Model, ".(un)?restrict_primary_key\\??" do
343
- setup do
352
+ before do
344
353
  @c = Class.new(Sequel::Model(:blahblah)) do
345
354
  set_primary_key :id
346
355
  columns :x, :y, :z, :id
@@ -382,7 +391,7 @@ describe Sequel::Model, ".(un)?restrict_primary_key\\??" do
382
391
  end
383
392
 
384
393
  describe Sequel::Model, ".strict_param_setting" do
385
- setup do
394
+ before do
386
395
  @c = Class.new(Sequel::Model(:blahblah)) do
387
396
  columns :x, :y, :z, :id
388
397
  set_restricted_columns :z
@@ -419,7 +428,7 @@ describe Sequel::Model, ".strict_param_setting" do
419
428
  end
420
429
 
421
430
  describe Sequel::Model, ".[] optimization" do
422
- setup do
431
+ before do
423
432
  @c = Class.new(Sequel::Model(:a))
424
433
  @c.instance_eval do
425
434
  def simple_table
@@ -466,8 +475,8 @@ describe Sequel::Model, ".[] optimization" do
466
475
  Class.new(@c).simple_table.should == "'b'"
467
476
  end
468
477
 
469
- it "should have simple_table = nil if inheriting and sti_key is set" do
470
- @c.set_sti_key :x
478
+ specify "should have simple_table = nil if inheriting and sti_key is set" do
479
+ @c.plugin :single_table_inheritance, :x
471
480
  Class.new(@c).simple_table.should == nil
472
481
  end
473
482