sequel 3.33.0 → 3.34.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (152) hide show
  1. data/CHANGELOG +140 -0
  2. data/Rakefile +7 -0
  3. data/bin/sequel +22 -2
  4. data/doc/dataset_basics.rdoc +1 -1
  5. data/doc/mass_assignment.rdoc +3 -1
  6. data/doc/querying.rdoc +28 -4
  7. data/doc/reflection.rdoc +23 -3
  8. data/doc/release_notes/3.34.0.txt +671 -0
  9. data/doc/schema_modification.rdoc +18 -2
  10. data/doc/virtual_rows.rdoc +49 -0
  11. data/lib/sequel/adapters/do/mysql.rb +0 -5
  12. data/lib/sequel/adapters/ibmdb.rb +9 -4
  13. data/lib/sequel/adapters/jdbc.rb +9 -4
  14. data/lib/sequel/adapters/jdbc/h2.rb +8 -2
  15. data/lib/sequel/adapters/jdbc/mysql.rb +0 -5
  16. data/lib/sequel/adapters/jdbc/postgresql.rb +43 -0
  17. data/lib/sequel/adapters/jdbc/sqlite.rb +19 -0
  18. data/lib/sequel/adapters/mock.rb +24 -3
  19. data/lib/sequel/adapters/mysql.rb +29 -50
  20. data/lib/sequel/adapters/mysql2.rb +13 -28
  21. data/lib/sequel/adapters/oracle.rb +8 -2
  22. data/lib/sequel/adapters/postgres.rb +115 -20
  23. data/lib/sequel/adapters/shared/db2.rb +1 -1
  24. data/lib/sequel/adapters/shared/mssql.rb +14 -3
  25. data/lib/sequel/adapters/shared/mysql.rb +59 -11
  26. data/lib/sequel/adapters/shared/mysql_prepared_statements.rb +6 -0
  27. data/lib/sequel/adapters/shared/oracle.rb +1 -1
  28. data/lib/sequel/adapters/shared/postgres.rb +127 -30
  29. data/lib/sequel/adapters/shared/sqlite.rb +55 -38
  30. data/lib/sequel/adapters/sqlite.rb +9 -3
  31. data/lib/sequel/adapters/swift.rb +2 -2
  32. data/lib/sequel/adapters/swift/mysql.rb +0 -5
  33. data/lib/sequel/adapters/swift/postgres.rb +10 -0
  34. data/lib/sequel/ast_transformer.rb +4 -0
  35. data/lib/sequel/connection_pool.rb +8 -0
  36. data/lib/sequel/connection_pool/sharded_single.rb +5 -0
  37. data/lib/sequel/connection_pool/sharded_threaded.rb +17 -0
  38. data/lib/sequel/connection_pool/single.rb +5 -0
  39. data/lib/sequel/connection_pool/threaded.rb +14 -0
  40. data/lib/sequel/core.rb +24 -3
  41. data/lib/sequel/database/connecting.rb +24 -14
  42. data/lib/sequel/database/dataset_defaults.rb +1 -0
  43. data/lib/sequel/database/misc.rb +16 -25
  44. data/lib/sequel/database/query.rb +20 -2
  45. data/lib/sequel/database/schema_generator.rb +2 -2
  46. data/lib/sequel/database/schema_methods.rb +120 -23
  47. data/lib/sequel/dataset/actions.rb +91 -18
  48. data/lib/sequel/dataset/features.rb +5 -0
  49. data/lib/sequel/dataset/prepared_statements.rb +6 -2
  50. data/lib/sequel/dataset/sql.rb +68 -51
  51. data/lib/sequel/extensions/_pretty_table.rb +79 -0
  52. data/lib/sequel/{core_sql.rb → extensions/core_extensions.rb} +18 -13
  53. data/lib/sequel/extensions/migration.rb +4 -0
  54. data/lib/sequel/extensions/null_dataset.rb +90 -0
  55. data/lib/sequel/extensions/pg_array.rb +460 -0
  56. data/lib/sequel/extensions/pg_array_ops.rb +220 -0
  57. data/lib/sequel/extensions/pg_auto_parameterize.rb +174 -0
  58. data/lib/sequel/extensions/pg_hstore.rb +296 -0
  59. data/lib/sequel/extensions/pg_hstore_ops.rb +259 -0
  60. data/lib/sequel/extensions/pg_statement_cache.rb +316 -0
  61. data/lib/sequel/extensions/pretty_table.rb +5 -71
  62. data/lib/sequel/extensions/query_literals.rb +79 -0
  63. data/lib/sequel/extensions/schema_caching.rb +76 -0
  64. data/lib/sequel/extensions/schema_dumper.rb +227 -31
  65. data/lib/sequel/extensions/select_remove.rb +35 -0
  66. data/lib/sequel/extensions/sql_expr.rb +4 -110
  67. data/lib/sequel/extensions/to_dot.rb +1 -1
  68. data/lib/sequel/model.rb +11 -2
  69. data/lib/sequel/model/associations.rb +35 -7
  70. data/lib/sequel/model/base.rb +159 -36
  71. data/lib/sequel/no_core_ext.rb +2 -0
  72. data/lib/sequel/plugins/caching.rb +25 -18
  73. data/lib/sequel/plugins/composition.rb +1 -1
  74. data/lib/sequel/plugins/hook_class_methods.rb +1 -1
  75. data/lib/sequel/plugins/identity_map.rb +11 -3
  76. data/lib/sequel/plugins/instance_filters.rb +10 -0
  77. data/lib/sequel/plugins/many_to_one_pk_lookup.rb +71 -0
  78. data/lib/sequel/plugins/nested_attributes.rb +4 -3
  79. data/lib/sequel/plugins/prepared_statements.rb +3 -1
  80. data/lib/sequel/plugins/prepared_statements_associations.rb +5 -1
  81. data/lib/sequel/plugins/schema.rb +7 -2
  82. data/lib/sequel/plugins/single_table_inheritance.rb +1 -1
  83. data/lib/sequel/plugins/static_cache.rb +99 -0
  84. data/lib/sequel/plugins/validation_class_methods.rb +1 -1
  85. data/lib/sequel/sql.rb +417 -7
  86. data/lib/sequel/version.rb +1 -1
  87. data/spec/adapters/firebird_spec.rb +1 -1
  88. data/spec/adapters/mssql_spec.rb +12 -15
  89. data/spec/adapters/mysql_spec.rb +81 -23
  90. data/spec/adapters/postgres_spec.rb +444 -77
  91. data/spec/adapters/spec_helper.rb +2 -0
  92. data/spec/adapters/sqlite_spec.rb +8 -8
  93. data/spec/core/connection_pool_spec.rb +85 -0
  94. data/spec/core/database_spec.rb +29 -5
  95. data/spec/core/dataset_spec.rb +171 -3
  96. data/spec/core/expression_filters_spec.rb +364 -0
  97. data/spec/core/mock_adapter_spec.rb +17 -3
  98. data/spec/core/schema_spec.rb +133 -0
  99. data/spec/extensions/association_dependencies_spec.rb +13 -13
  100. data/spec/extensions/caching_spec.rb +26 -3
  101. data/spec/extensions/class_table_inheritance_spec.rb +2 -2
  102. data/spec/{core/core_sql_spec.rb → extensions/core_extensions_spec.rb} +23 -94
  103. data/spec/extensions/force_encoding_spec.rb +4 -2
  104. data/spec/extensions/hook_class_methods_spec.rb +5 -2
  105. data/spec/extensions/identity_map_spec.rb +17 -0
  106. data/spec/extensions/instance_filters_spec.rb +1 -1
  107. data/spec/extensions/lazy_attributes_spec.rb +2 -2
  108. data/spec/extensions/list_spec.rb +4 -4
  109. data/spec/extensions/many_to_one_pk_lookup_spec.rb +140 -0
  110. data/spec/extensions/migration_spec.rb +6 -2
  111. data/spec/extensions/nested_attributes_spec.rb +20 -0
  112. data/spec/extensions/null_dataset_spec.rb +85 -0
  113. data/spec/extensions/optimistic_locking_spec.rb +2 -2
  114. data/spec/extensions/pg_array_ops_spec.rb +105 -0
  115. data/spec/extensions/pg_array_spec.rb +196 -0
  116. data/spec/extensions/pg_auto_parameterize_spec.rb +64 -0
  117. data/spec/extensions/pg_hstore_ops_spec.rb +136 -0
  118. data/spec/extensions/pg_hstore_spec.rb +195 -0
  119. data/spec/extensions/pg_statement_cache_spec.rb +209 -0
  120. data/spec/extensions/prepared_statements_spec.rb +4 -0
  121. data/spec/extensions/pretty_table_spec.rb +6 -0
  122. data/spec/extensions/query_literals_spec.rb +168 -0
  123. data/spec/extensions/schema_caching_spec.rb +41 -0
  124. data/spec/extensions/schema_dumper_spec.rb +231 -11
  125. data/spec/extensions/schema_spec.rb +14 -2
  126. data/spec/extensions/select_remove_spec.rb +38 -0
  127. data/spec/extensions/sharding_spec.rb +6 -6
  128. data/spec/extensions/skip_create_refresh_spec.rb +1 -1
  129. data/spec/extensions/spec_helper.rb +2 -1
  130. data/spec/extensions/sql_expr_spec.rb +28 -19
  131. data/spec/extensions/static_cache_spec.rb +145 -0
  132. data/spec/extensions/touch_spec.rb +1 -1
  133. data/spec/extensions/typecast_on_load_spec.rb +9 -1
  134. data/spec/integration/associations_test.rb +6 -6
  135. data/spec/integration/database_test.rb +1 -1
  136. data/spec/integration/dataset_test.rb +89 -26
  137. data/spec/integration/migrator_test.rb +2 -3
  138. data/spec/integration/model_test.rb +3 -3
  139. data/spec/integration/plugin_test.rb +85 -22
  140. data/spec/integration/prepared_statement_test.rb +28 -8
  141. data/spec/integration/schema_test.rb +78 -7
  142. data/spec/integration/spec_helper.rb +1 -0
  143. data/spec/integration/timezone_test.rb +1 -1
  144. data/spec/integration/transaction_test.rb +4 -6
  145. data/spec/integration/type_test.rb +2 -2
  146. data/spec/model/associations_spec.rb +94 -8
  147. data/spec/model/base_spec.rb +4 -4
  148. data/spec/model/hooks_spec.rb +2 -2
  149. data/spec/model/model_spec.rb +19 -7
  150. data/spec/model/record_spec.rb +135 -58
  151. data/spec/model/spec_helper.rb +1 -0
  152. metadata +35 -7
@@ -13,7 +13,7 @@ describe Sequel::Database do
13
13
  INTEGRATION_DB[:items].prepare(:first, :a).call
14
14
  INTEGRATION_DB.disconnect
15
15
  INTEGRATION_DB.pool.size.should == 0
16
- INTEGRATION_DB.drop_table(:items) rescue nil
16
+ INTEGRATION_DB.drop_table?(:items)
17
17
  end
18
18
 
19
19
  specify "should raise Sequel::DatabaseError on invalid SQL" do
@@ -11,7 +11,7 @@ describe "Simple Dataset operations" do
11
11
  @ds.insert(:number=>10)
12
12
  end
13
13
  after do
14
- @db.drop_table(:items)
14
+ @db.drop_table?(:items)
15
15
  end
16
16
 
17
17
  specify "should support sequential primary keys" do
@@ -23,6 +23,16 @@ describe "Simple Dataset operations" do
23
23
  {:id => 3, :number=>30} ]
24
24
  end
25
25
 
26
+ specify "should support sequential primary keys with a Bignum" do
27
+ @db.create_table!(:items) do
28
+ primary_key :id, :type=>Bignum
29
+ Integer :number
30
+ end
31
+ @ds << {:number=>20}
32
+ @ds << {:number=>30}
33
+ @ds.order(:number).all.should == [{:id => 1, :number=>20}, {:id => 2, :number=>30}]
34
+ end
35
+
26
36
  cspecify "should insert with a primary key specified", :db2, :mssql do
27
37
  @ds.insert(:id=>100, :number=>20)
28
38
  @ds.count.should == 2
@@ -172,6 +182,33 @@ describe "Simple Dataset operations" do
172
182
  end
173
183
  end
174
184
 
185
+ describe "Simple dataset operations with nasty table names" do
186
+ before do
187
+ @db = INTEGRATION_DB
188
+ @table = :"i`t' [e]\"m\\s"
189
+ @qi = @db.quote_identifiers?
190
+ @db.quote_identifiers = true
191
+ end
192
+ after do
193
+ @db.quote_identifiers = @qi
194
+ end
195
+
196
+ cspecify "should work correctly", :mssql, :oracle do
197
+ @db.create_table!(@table) do
198
+ primary_key :id
199
+ Integer :number
200
+ end
201
+ @ds = @db[@table]
202
+ @ds.insert(:number=>10).should == 1
203
+ @ds.all.should == [{:id=>1, :number=>10}]
204
+ @ds.update(:number=>20).should == 1
205
+ @ds.all.should == [{:id=>1, :number=>20}]
206
+ @ds.delete.should == 1
207
+ @ds.count.should == 0
208
+ proc{@db.drop_table?(@table)}.should_not raise_error
209
+ end
210
+ end
211
+
175
212
  describe Sequel::Dataset do
176
213
  before do
177
214
  INTEGRATION_DB.create_table!(:test) do
@@ -181,7 +218,7 @@ describe Sequel::Dataset do
181
218
  @d = INTEGRATION_DB[:test]
182
219
  end
183
220
  after do
184
- INTEGRATION_DB.drop_table(:test)
221
+ INTEGRATION_DB.drop_table?(:test)
185
222
  end
186
223
 
187
224
  specify "should return the correct record count" do
@@ -267,7 +304,7 @@ describe Sequel::Database do
267
304
 
268
305
  specify "should have a working table_exists?" do
269
306
  t = :basdfdsafsaddsaf
270
- INTEGRATION_DB.drop_table(t) rescue nil
307
+ INTEGRATION_DB.drop_table?(t)
271
308
  INTEGRATION_DB.table_exists?(t).should == false
272
309
  INTEGRATION_DB.create_table(t){Integer :a}
273
310
  begin
@@ -290,7 +327,7 @@ describe Sequel::Dataset do
290
327
  @d << {:value => 789}
291
328
  end
292
329
  after do
293
- INTEGRATION_DB.drop_table(:items)
330
+ INTEGRATION_DB.drop_table?(:items)
294
331
  end
295
332
 
296
333
  specify "should correctly return avg" do
@@ -319,7 +356,7 @@ describe "Simple Dataset operations" do
319
356
  @ds = INTEGRATION_DB[:items]
320
357
  end
321
358
  after do
322
- INTEGRATION_DB.drop_table(:items)
359
+ INTEGRATION_DB.drop_table?(:items)
323
360
  end
324
361
 
325
362
  specify "should deal with boolean conditions correctly" do
@@ -345,7 +382,7 @@ describe "Simple Dataset operations in transactions" do
345
382
  @ds = INTEGRATION_DB[:items]
346
383
  end
347
384
  after do
348
- INTEGRATION_DB.drop_table(:items)
385
+ INTEGRATION_DB.drop_table?(:items)
349
386
  end
350
387
 
351
388
  cspecify "should insert correctly with a primary key specified inside a transaction", :db2, :mssql do
@@ -456,7 +493,7 @@ if INTEGRATION_DB.dataset.supports_cte?
456
493
  @ds.insert(:id=>6, :parent_id=>5)
457
494
  end
458
495
  after(:all) do
459
- @db.drop_table(:i1)
496
+ @db.drop_table?(:i1)
460
497
  end
461
498
 
462
499
  specify "should give correct results for WITH" do
@@ -508,7 +545,7 @@ if INTEGRATION_DB.dataset.supports_cte?(:update) # Assume INSERT and DELETE supp
508
545
  @ds.insert(:id=>2)
509
546
  end
510
547
  after do
511
- @db.drop_table(:i1)
548
+ @db.drop_table?(:i1)
512
549
  end
513
550
 
514
551
  specify "should give correct results for WITH" do
@@ -530,7 +567,7 @@ if INTEGRATION_DB.dataset.supports_returning?(:insert)
530
567
  @ds = @db[:i1]
531
568
  end
532
569
  after do
533
- @db.drop_table(:i1)
570
+ @db.drop_table?(:i1)
534
571
  end
535
572
 
536
573
  specify "should give correct results" do
@@ -556,7 +593,7 @@ if INTEGRATION_DB.dataset.supports_returning?(:update) # Assume DELETE support a
556
593
  @ds.insert(1, 2)
557
594
  end
558
595
  after do
559
- @db.drop_table(:i1)
596
+ @db.drop_table?(:i1)
560
597
  end
561
598
 
562
599
  specify "should give correct results" do
@@ -597,7 +634,7 @@ if INTEGRATION_DB.dataset.supports_window_functions?
597
634
  @ds.insert(:id=>6, :group_id=>2, :amount=>100000)
598
635
  end
599
636
  after(:all) do
600
- @db.drop_table(:i1)
637
+ @db.drop_table?(:i1)
601
638
  end
602
639
 
603
640
  specify "should give correct results for aggregate window functions" do
@@ -652,7 +689,7 @@ describe Sequel::SQL::Constants do
652
689
  @c2 = proc{|v| v.is_a?(Date) ? v : Date.parse(v) }
653
690
  end
654
691
  after do
655
- @db.drop_table(:constants) if @db.table_exists?(:constants)
692
+ @db.drop_table?(:constants)
656
693
  end
657
694
 
658
695
  cspecify "should have working CURRENT_DATE", [:odbc, :mssql], [:jdbc, :sqlite], :oracle do
@@ -669,13 +706,13 @@ describe Sequel::SQL::Constants do
669
706
  (Time.now - @c[@ds.get(:t)]).should be_within(2).of(0)
670
707
  end
671
708
 
672
- cspecify "should have working CURRENT_TIMESTAMP", [:jdbc, :sqlite] do
709
+ cspecify "should have working CURRENT_TIMESTAMP", [:jdbc, :sqlite], [:swift] do
673
710
  @db.create_table!(:constants){DateTime :ts}
674
711
  @ds.insert(:ts=>Sequel::CURRENT_TIMESTAMP)
675
712
  (Time.now - @c[@ds.get(:ts)]).should be_within(2).of(0)
676
713
  end
677
714
 
678
- cspecify "should have working CURRENT_TIMESTAMP when used as a column default", [:jdbc, :sqlite] do
715
+ cspecify "should have working CURRENT_TIMESTAMP when used as a column default", [:jdbc, :sqlite], [:swift] do
679
716
  @db.create_table!(:constants){DateTime :ts, :default=>Sequel::CURRENT_TIMESTAMP}
680
717
  @ds.insert
681
718
  (Time.now - @c[@ds.get(:ts)]).should be_within(2).of(0)
@@ -692,7 +729,7 @@ describe "Sequel::Dataset#import and #multi_insert" do
692
729
  @ids.delete
693
730
  end
694
731
  after(:all) do
695
- @db.drop_table(:imp)
732
+ @db.drop_table?(:imp)
696
733
  end
697
734
 
698
735
  it "should import with multi_insert and an array of hashes" do
@@ -732,7 +769,7 @@ describe "Sequel::Dataset#import and #multi_insert :return=>:primary_key " do
732
769
  @ds = @db[:imp]
733
770
  end
734
771
  after do
735
- @db.drop_table(:imp)
772
+ @db.drop_table?(:imp)
736
773
  end
737
774
 
738
775
  specify "should return primary key values " do
@@ -760,7 +797,7 @@ describe "Sequel::Dataset convenience methods" do
760
797
  @ds.insert(2, 4, 6)
761
798
  end
762
799
  after(:all) do
763
- @db.drop_table(:a)
800
+ @db.drop_table?(:a)
764
801
  end
765
802
 
766
803
  it "#group_rollup should include hierarchy of groupings" do
@@ -784,7 +821,7 @@ describe "Sequel::Dataset convenience methods" do
784
821
  @ds.delete
785
822
  end
786
823
  after(:all) do
787
- @db.drop_table(:a)
824
+ @db.drop_table?(:a)
788
825
  end
789
826
 
790
827
  it "#[]= should update matching rows" do
@@ -862,7 +899,7 @@ describe "Sequel::Dataset main SQL methods" do
862
899
  @ds.delete
863
900
  end
864
901
  after(:all) do
865
- @db.drop_table(:a)
902
+ @db.drop_table?(:a)
866
903
  end
867
904
 
868
905
  it "#exists should return a usable exists clause" do
@@ -935,7 +972,7 @@ describe "Sequel::Dataset convenience methods" do
935
972
  @ds.insert(5, 6, 7, 8)
936
973
  end
937
974
  after(:all) do
938
- @db.drop_table(:a)
975
+ @db.drop_table?(:a)
939
976
  end
940
977
 
941
978
  specify "should have working #map" do
@@ -955,6 +992,19 @@ describe "Sequel::Dataset convenience methods" do
955
992
  @ds.to_hash([:a, :c], [:b, :d]).should == {[1, 3]=>[2, 4], [5, 7]=>[6, 8]}
956
993
  end
957
994
 
995
+ specify "should have working #to_hash_groups" do
996
+ ds = @ds.order(*@ds.columns)
997
+ ds.insert(1, 2, 3, 9)
998
+ ds.to_hash_groups(:a).should == {1=>[{:a=>1, :b=>2, :c=>3, :d=>4}, {:a=>1, :b=>2, :c=>3, :d=>9}], 5=>[{:a=>5, :b=>6, :c=>7, :d=>8}]}
999
+ ds.to_hash_groups(:b).should == {2=>[{:a=>1, :b=>2, :c=>3, :d=>4}, {:a=>1, :b=>2, :c=>3, :d=>9}], 6=>[{:a=>5, :b=>6, :c=>7, :d=>8}]}
1000
+ ds.to_hash_groups([:a, :b]).should == {[1, 2]=>[{:a=>1, :b=>2, :c=>3, :d=>4}, {:a=>1, :b=>2, :c=>3, :d=>9}], [5, 6]=>[{:a=>5, :b=>6, :c=>7, :d=>8}]}
1001
+
1002
+ ds.to_hash_groups(:a, :d).should == {1=>[4, 9], 5=>[8]}
1003
+ ds.to_hash_groups([:a, :c], :d).should == {[1, 3]=>[4, 9], [5, 7]=>[8]}
1004
+ ds.to_hash_groups(:a, [:b, :d]).should == {1=>[[2, 4], [2, 9]], 5=>[[6, 8]]}
1005
+ ds.to_hash_groups([:a, :c], [:b, :d]).should == {[1, 3]=>[[2, 4], [2, 9]], [5, 7]=>[[6, 8]]}
1006
+ end
1007
+
958
1008
  specify "should have working #select_map" do
959
1009
  @ds.select_map(:a).should == [1, 5]
960
1010
  @ds.select_map(:b).should == [2, 6]
@@ -997,6 +1047,19 @@ describe "Sequel::Dataset convenience methods" do
997
1047
  @ds.select_hash(:a, [:b, :c]).should == {1=>[2, 3], 5=>[6, 7]}
998
1048
  @ds.select_hash([:a, :c], [:b, :d]).should == {[1, 3]=>[2, 4], [5, 7]=>[6, 8]}
999
1049
  end
1050
+
1051
+ specify "should have working #select_hash_groups" do
1052
+ ds = @ds.order(*@ds.columns)
1053
+ ds.insert(1, 2, 3, 9)
1054
+ ds.select_hash_groups(:a, :d).should == {1=>[4, 9], 5=>[8]}
1055
+ ds.select_hash_groups(:a__a___e, :d).should == {1=>[4, 9], 5=>[8]}
1056
+ ds.select_hash_groups(:a__a.as(:e), :d).should == {1=>[4, 9], 5=>[8]}
1057
+ ds.select_hash_groups(:a.qualify(:a).as(:e), :d).should == {1=>[4, 9], 5=>[8]}
1058
+ ds.select_hash_groups(:a.identifier.qualify(:a).as(:e), :d).should == {1=>[4, 9], 5=>[8]}
1059
+ ds.select_hash_groups([:a, :c], :d).should == {[1, 3]=>[4, 9], [5, 7]=>[8]}
1060
+ ds.select_hash_groups(:a, [:b, :d]).should == {1=>[[2, 4], [2, 9]], 5=>[[6, 8]]}
1061
+ ds.select_hash_groups([:a, :c], [:b, :d]).should == {[1, 3]=>[[2, 4], [2, 9]], [5, 7]=>[[6, 8]]}
1062
+ end
1000
1063
  end
1001
1064
 
1002
1065
  describe "Sequel::Dataset DSL support" do
@@ -1009,7 +1072,7 @@ describe "Sequel::Dataset DSL support" do
1009
1072
  @ds.delete
1010
1073
  end
1011
1074
  after(:all) do
1012
- @db.drop_table(:a)
1075
+ @db.drop_table?(:a)
1013
1076
  end
1014
1077
 
1015
1078
  it "should work with standard mathematical operators" do
@@ -1237,7 +1300,7 @@ describe "SQL Extract Function" do
1237
1300
  @ds = @db[:a].order(:a)
1238
1301
  end
1239
1302
  after do
1240
- @db.drop_table(:a)
1303
+ @db.drop_table?(:a)
1241
1304
  end
1242
1305
 
1243
1306
  specify "should return the part of the datetime asked for" do
@@ -1270,7 +1333,7 @@ describe "Dataset string methods" do
1270
1333
  @ds.delete
1271
1334
  end
1272
1335
  after(:all) do
1273
- @db.drop_table(:a)
1336
+ @db.drop_table?(:a)
1274
1337
  end
1275
1338
 
1276
1339
  it "#grep should return matching rows" do
@@ -1354,7 +1417,7 @@ describe "Dataset identifier methods" do
1354
1417
  @ds = @db[:a].order(:ab)
1355
1418
  end
1356
1419
  after(:all) do
1357
- @db.drop_table(:a)
1420
+ @db.drop_table?(:a)
1358
1421
  end
1359
1422
 
1360
1423
  cspecify "#identifier_output_method should change how identifiers are output", [:swift] do
@@ -1385,7 +1448,7 @@ describe "Dataset defaults and overrides" do
1385
1448
  @ds.delete
1386
1449
  end
1387
1450
  after(:all) do
1388
- @db.drop_table(:a)
1451
+ @db.drop_table?(:a)
1389
1452
  end
1390
1453
 
1391
1454
  it "#set_defaults should set defaults that can be overridden" do
@@ -1419,7 +1482,7 @@ if INTEGRATION_DB.dataset.supports_modifying_joins?
1419
1482
  @db[:c].insert(7, 8)
1420
1483
  end
1421
1484
  after do
1422
- @db.drop_table(:a, :b, :c)
1485
+ @db.drop_table?(:a, :b, :c)
1423
1486
  end
1424
1487
 
1425
1488
  it "#update should allow updating joined datasets" do
@@ -7,7 +7,7 @@ describe Sequel::Migrator do
7
7
  @m = Sequel::Migrator
8
8
  end
9
9
  after do
10
- [:schema_info, :schema_migrations, :sm1111, :sm1122, :sm2222, :sm2233, :sm3333, :sm11111, :sm22222].each{|n| @db.drop_table(n) rescue nil}
10
+ @db.drop_table?(:schema_info, :schema_migrations, :sm1111, :sm1122, :sm2222, :sm2233, :sm3333, :sm11111, :sm22222)
11
11
  end
12
12
 
13
13
  specify "should be able to migrate up and down all the way successfully" do
@@ -187,8 +187,7 @@ describe Sequel::Migrator do
187
187
 
188
188
  specify "should handle reversible migrations" do
189
189
  @dir = 'spec/files/reversible_migrations'
190
- @db.drop_table(:a) rescue nil
191
- @db.drop_table(:b) rescue nil
190
+ @db.drop_table?(:a, :b)
192
191
  @m.apply(@db, @dir, 1)
193
192
  [:schema_info, :a].each{|n| @db.table_exists?(n).should be_true}
194
193
  [:schema_migrations, :b].each{|n| @db.table_exists?(n).should be_false}
@@ -11,7 +11,7 @@ describe "Sequel::Model basic support" do
11
11
  end
12
12
  end
13
13
  after do
14
- @db.drop_table(:items)
14
+ @db.drop_table?(:items)
15
15
  Object.send(:remove_const, :Item)
16
16
  end
17
17
 
@@ -202,8 +202,8 @@ end
202
202
  describe "Sequel::Model with no existing table" do
203
203
  specify "should not raise an error when setting the dataset" do
204
204
  db = INTEGRATION_DB
205
- db.drop_table(:items) if db.table_exists?(:items)
206
- proc{class ::Item < Sequel::Model(db); end}.should_not raise_error
205
+ db.drop_table?(:items)
206
+ proc{class ::Item < Sequel::Model(db); end; Object.send(:remove_const, :Item)}.should_not raise_error
207
207
  proc{c = Class.new(Sequel::Model); c.set_dataset(db[:items])}.should_not raise_error
208
208
  end
209
209
  end
@@ -8,7 +8,7 @@ describe "Class Table Inheritance Plugin" do
8
8
  before(:all) do
9
9
  @db = INTEGRATION_DB
10
10
  @db.instance_variable_set(:@schemas, {})
11
- [:staff, :executives, :managers, :employees].each{|t| @db.drop_table(t) if @db.table_exists?(t)}
11
+ @db.drop_table?(:staff, :executives, :managers, :employees)
12
12
  @db.create_table(:employees) do
13
13
  primary_key :id
14
14
  String :name
@@ -54,7 +54,7 @@ describe "Class Table Inheritance Plugin" do
54
54
  [:Executive, :Manager, :Staff, :Employee].each{|s| Object.send(:remove_const, s)}
55
55
  end
56
56
  after(:all) do
57
- @db.drop_table :staff, :executives, :managers, :employees
57
+ @db.drop_table? :staff, :executives, :managers, :employees
58
58
  end
59
59
 
60
60
  specify "should return rows as subclass instances" do
@@ -151,7 +151,7 @@ describe "Many Through Many Plugin" do
151
151
  before(:all) do
152
152
  @db = INTEGRATION_DB
153
153
  @db.instance_variable_set(:@schemas, {})
154
- [:albums_artists, :albums, :artists].each{|t| @db.drop_table(t) if @db.table_exists?(t)}
154
+ @db.drop_table?(:albums_artists, :albums, :artists)
155
155
  @db.create_table(:albums) do
156
156
  primary_key :id
157
157
  String :name
@@ -195,7 +195,7 @@ describe "Many Through Many Plugin" do
195
195
  [:Album, :Artist].each{|s| Object.send(:remove_const, s)}
196
196
  end
197
197
  after(:all) do
198
- @db.drop_table :albums_artists, :albums, :artists
198
+ @db.drop_table? :albums_artists, :albums, :artists
199
199
  end
200
200
 
201
201
  def self_join(c)
@@ -410,7 +410,7 @@ describe "Lazy Attributes plugin" do
410
410
  Item.create(:name=>'J', :num=>1)
411
411
  end
412
412
  after(:all) do
413
- @db.drop_table(:items)
413
+ @db.drop_table?(:items)
414
414
  Object.send(:remove_const, :Item)
415
415
  end
416
416
 
@@ -480,7 +480,7 @@ describe "Tactical Eager Loading Plugin" do
480
480
  [:Album, :Artist].each{|s| Object.send(:remove_const, s)}
481
481
  end
482
482
  after(:all) do
483
- @db.drop_table :albums, :artists
483
+ @db.drop_table? :albums, :artists
484
484
  end
485
485
 
486
486
  specify "should eagerly load associations for all items when accessing any item" do
@@ -510,7 +510,7 @@ describe "Identity Map plugin" do
510
510
  Item.create(:name=>'J', :num=>3)
511
511
  end
512
512
  after do
513
- @db.drop_table(:items)
513
+ @db.drop_table?(:items)
514
514
  Object.send(:remove_const, :Item)
515
515
  end
516
516
 
@@ -561,7 +561,7 @@ describe "Touch plugin" do
561
561
  [:Album, :Artist].each{|s| Object.send(:remove_const, s)}
562
562
  end
563
563
  after(:all) do
564
- @db.drop_table :albums, :artists
564
+ @db.drop_table? :albums, :artists
565
565
  end
566
566
 
567
567
  specify "should update the timestamp column when touching the record" do
@@ -570,7 +570,7 @@ describe "Touch plugin" do
570
570
  @album.updated_at.to_i.should be_within(2).of(Time.now.to_i)
571
571
  end
572
572
 
573
- cspecify "should update the timestamp column for associated records when the record is updated or destroyed", [:do, :sqlite], [:jdbc, :sqlite] do
573
+ cspecify "should update the timestamp column for associated records when the record is updated or destroyed", [:do, :sqlite], [:jdbc, :sqlite], [:swift] do
574
574
  @artist.updated_at.should == nil
575
575
  @album.update(:name=>'B')
576
576
  ua = @artist.reload.updated_at
@@ -601,7 +601,7 @@ describe "Serialization plugin" do
601
601
  end
602
602
  end
603
603
  after do
604
- @db.drop_table(:items)
604
+ @db.drop_table?(:items)
605
605
  Object.send(:remove_const, :Item)
606
606
  end
607
607
 
@@ -633,7 +633,7 @@ describe "OptimisticLocking plugin" do
633
633
  @p = Person.create(:name=>'John')
634
634
  end
635
635
  after(:all) do
636
- @db.drop_table(:people)
636
+ @db.drop_table?(:people)
637
637
  Object.send(:remove_const, :Person)
638
638
  end
639
639
 
@@ -684,7 +684,7 @@ describe "Composition plugin" do
684
684
  @e2 = Event.create(:year=>nil)
685
685
  end
686
686
  after do
687
- @db.drop_table(:events)
687
+ @db.drop_table?(:events)
688
688
  Object.send(:remove_const, :Event)
689
689
  end
690
690
 
@@ -744,7 +744,7 @@ if INTEGRATION_DB.dataset.supports_cte? and !Sequel.guarded?(:db2)
744
744
  @nodes.each{|n| n.associations.clear}
745
745
  end
746
746
  after(:all) do
747
- @db.drop_table :nodes
747
+ @db.drop_table? :nodes
748
748
  Object.send(:remove_const, :Node)
749
749
  end
750
750
 
@@ -965,7 +965,7 @@ describe "Instance Filters plugin" do
965
965
  @i.set(:name=>'K')
966
966
  end
967
967
  after(:all) do
968
- @db.drop_table(:items)
968
+ @db.drop_table?(:items)
969
969
  Object.send(:remove_const, :Item)
970
970
  end
971
971
 
@@ -1026,7 +1026,7 @@ describe "UpdatePrimaryKey plugin" do
1026
1026
  @ds.insert(:a=>1, :b=>3)
1027
1027
  end
1028
1028
  after(:all) do
1029
- @db.drop_table(:t)
1029
+ @db.drop_table?(:t)
1030
1030
  end
1031
1031
 
1032
1032
  specify "should handle regular updates" do
@@ -1069,7 +1069,7 @@ end
1069
1069
  describe "AssociationPks plugin" do
1070
1070
  before(:all) do
1071
1071
  @db = INTEGRATION_DB
1072
- [:albums_tags, :tags, :albums, :artists].each{|t| @db.drop_table(t) if @db.table_exists?(t)}
1072
+ @db.drop_table?(:albums_tags, :tags, :albums, :artists)
1073
1073
  @db.create_table(:artists) do
1074
1074
  primary_key :id
1075
1075
  String :name
@@ -1113,7 +1113,7 @@ describe "AssociationPks plugin" do
1113
1113
  end
1114
1114
  end
1115
1115
  after(:all) do
1116
- @db.drop_table :albums_tags, :tags, :albums, :artists
1116
+ @db.drop_table? :albums_tags, :tags, :albums, :artists
1117
1117
  [:Artist, :Album, :Tag].each{|s| Object.send(:remove_const, s)}
1118
1118
  end
1119
1119
 
@@ -1184,7 +1184,7 @@ describe "List plugin without a scope" do
1184
1184
  @c.create :name => "hig"
1185
1185
  end
1186
1186
  after(:all) do
1187
- @db.drop_table(:sites)
1187
+ @db.drop_table?(:sites)
1188
1188
  end
1189
1189
 
1190
1190
  it "should return rows in order of position" do
@@ -1262,7 +1262,7 @@ describe "List plugin with a scope" do
1262
1262
  @c.create :name => "Au", :parent_id => p1.id
1263
1263
  end
1264
1264
  after(:all) do
1265
- @db.drop_table(:pages)
1265
+ @db.drop_table?(:pages)
1266
1266
  end
1267
1267
 
1268
1268
  it "should return rows in order of position" do
@@ -1350,7 +1350,7 @@ describe "Sequel::Plugins::Tree" do
1350
1350
  end
1351
1351
  end
1352
1352
  after(:all) do
1353
- @db.drop_table(:nodes)
1353
+ @db.drop_table?(:nodes)
1354
1354
  Object.send(:remove_const, :Node)
1355
1355
  end
1356
1356
 
@@ -1466,7 +1466,7 @@ describe "Sequel::Plugins::Tree" do
1466
1466
  end
1467
1467
  end
1468
1468
  after(:all) do
1469
- @db.drop_table(:lorems)
1469
+ @db.drop_table?(:lorems)
1470
1470
  Object.send(:remove_const, :Lorem)
1471
1471
  end
1472
1472
 
@@ -1499,7 +1499,7 @@ describe "Sequel::Plugins::PreparedStatements" do
1499
1499
  @bar = @c.create(:name=>'bar', :i=>20)
1500
1500
  end
1501
1501
  after(:all) do
1502
- @db.drop_table(:ps_test)
1502
+ @db.drop_table?(:ps_test)
1503
1503
  end
1504
1504
 
1505
1505
  it "should work with looking up using Model.[]" do
@@ -1554,3 +1554,66 @@ describe "Sequel::Plugins::PreparedStatements" do
1554
1554
  @c[o.id].should == @c.load(:id=>o.id, :name=>nil, :i=>40)
1555
1555
  end
1556
1556
  end
1557
+
1558
+ describe "Caching plugins" do
1559
+ before(:all) do
1560
+ @db = INTEGRATION_DB
1561
+ @db.drop_table?(:albums, :artists)
1562
+ @db.create_table(:artists) do
1563
+ primary_key :id
1564
+ end
1565
+ @db.create_table(:albums) do
1566
+ primary_key :id
1567
+ foreign_key :artist_id, :artists
1568
+ end
1569
+ @db[:artists].insert
1570
+ @db[:albums].insert(:artist_id=>1)
1571
+ end
1572
+ before do
1573
+ @Album = Class.new(Sequel::Model(@db[:albums]))
1574
+ @Album.plugin :many_to_one_pk_lookup
1575
+ end
1576
+ after(:all) do
1577
+ @db.drop_table?(:albums, :artists)
1578
+ end
1579
+
1580
+ shared_examples_for "a caching plugin" do
1581
+ it "should work with looking up using Model.[]" do
1582
+ @Artist[1].should equal(@Artist[1])
1583
+ @Artist[:id=>1].should == @Artist[1]
1584
+ @Artist[0].should == nil
1585
+ @Artist[nil].should == nil
1586
+ end
1587
+
1588
+ it "should work with lookup up many_to_one associated objects" do
1589
+ a = @Artist[1]
1590
+ @Album.first.artist.should equal(a)
1591
+ end
1592
+ end
1593
+
1594
+ describe "caching plugin" do
1595
+ before do
1596
+ @cache_class = Class.new(Hash) do
1597
+ def set(k, v, ttl) self[k] = v end
1598
+ alias get []
1599
+ end
1600
+ @cache = @cache_class.new
1601
+
1602
+ @Artist = Class.new(Sequel::Model(@db[:artists]))
1603
+ @Artist.plugin :caching, @cache
1604
+ @Album.many_to_one :artist, :class=>@Artist
1605
+ end
1606
+
1607
+ it_should_behave_like "a caching plugin"
1608
+ end
1609
+
1610
+ describe "static_cache plugin" do
1611
+ before do
1612
+ @Artist = Class.new(Sequel::Model(@db[:artists]))
1613
+ @Artist.plugin :static_cache
1614
+ @Album.many_to_one :artist, :class=>@Artist
1615
+ end
1616
+
1617
+ it_should_behave_like "a caching plugin"
1618
+ end
1619
+ end