sequel 4.22.0 → 4.23.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG +22 -0
- data/README.rdoc +6 -0
- data/Rakefile +59 -81
- data/doc/migration.rdoc +2 -0
- data/doc/release_notes/4.23.0.txt +65 -0
- data/doc/sharding.rdoc +16 -14
- data/doc/testing.rdoc +61 -77
- data/lib/sequel/adapters/jdbc.rb +1 -0
- data/lib/sequel/adapters/mock.rb +0 -1
- data/lib/sequel/adapters/postgres.rb +1 -0
- data/lib/sequel/adapters/postgresql.rb +1 -0
- data/lib/sequel/adapters/shared/postgres.rb +3 -3
- data/lib/sequel/connection_pool/sharded_threaded.rb +5 -0
- data/lib/sequel/connection_pool/threaded.rb +9 -1
- data/lib/sequel/database/connecting.rb +1 -1
- data/lib/sequel/database/transactions.rb +2 -1
- data/lib/sequel/dataset/prepared_statements.rb +1 -1
- data/lib/sequel/extensions/constraint_validations.rb +12 -12
- data/lib/sequel/extensions/date_arithmetic.rb +0 -4
- data/lib/sequel/extensions/pagination.rb +14 -2
- data/lib/sequel/extensions/pg_enum.rb +2 -2
- data/lib/sequel/extensions/pg_hstore.rb +1 -1
- data/lib/sequel/extensions/pg_json_ops.rb +2 -2
- data/lib/sequel/plugins/csv_serializer.rb +2 -0
- data/lib/sequel/plugins/delay_add_association.rb +50 -0
- data/lib/sequel/plugins/list.rb +2 -2
- data/lib/sequel/plugins/nested_attributes.rb +8 -28
- data/lib/sequel/plugins/update_refresh.rb +50 -0
- data/lib/sequel/plugins/validate_associated.rb +55 -0
- data/lib/sequel/version.rb +1 -1
- data/spec/adapters/db2_spec.rb +29 -29
- data/spec/adapters/firebird_spec.rb +97 -103
- data/spec/adapters/informix_spec.rb +25 -25
- data/spec/adapters/mssql_spec.rb +156 -172
- data/spec/adapters/mysql_spec.rb +334 -359
- data/spec/adapters/oracle_spec.rb +67 -69
- data/spec/adapters/postgres_spec.rb +1298 -1249
- data/spec/adapters/spec_helper.rb +2 -35
- data/spec/adapters/sqlanywhere_spec.rb +39 -39
- data/spec/adapters/sqlite_spec.rb +203 -200
- data/spec/bin_spec.rb +57 -59
- data/spec/core/connection_pool_spec.rb +402 -401
- data/spec/core/database_spec.rb +953 -944
- data/spec/core/dataset_spec.rb +2178 -2168
- data/spec/core/deprecated_spec.rb +19 -19
- data/spec/core/expression_filters_spec.rb +415 -415
- data/spec/core/mock_adapter_spec.rb +212 -212
- data/spec/core/object_graph_spec.rb +73 -73
- data/spec/core/placeholder_literalizer_spec.rb +71 -71
- data/spec/core/schema_generator_spec.rb +44 -44
- data/spec/core/schema_spec.rb +470 -472
- data/spec/core/spec_helper.rb +5 -20
- data/spec/core/version_spec.rb +2 -2
- data/spec/core_extensions_spec.rb +320 -320
- data/spec/extensions/accessed_columns_spec.rb +12 -12
- data/spec/extensions/active_model_spec.rb +3 -3
- data/spec/extensions/after_initialize_spec.rb +2 -2
- data/spec/extensions/arbitrary_servers_spec.rb +23 -23
- data/spec/extensions/association_dependencies_spec.rb +34 -34
- data/spec/extensions/association_pks_spec.rb +98 -98
- data/spec/extensions/association_proxies_spec.rb +33 -33
- data/spec/extensions/auto_validations_spec.rb +46 -46
- data/spec/extensions/blacklist_security_spec.rb +19 -18
- data/spec/extensions/blank_spec.rb +36 -36
- data/spec/extensions/boolean_readers_spec.rb +36 -36
- data/spec/extensions/caching_spec.rb +82 -82
- data/spec/extensions/class_table_inheritance_spec.rb +72 -72
- data/spec/extensions/column_conflicts_spec.rb +19 -14
- data/spec/extensions/column_select_spec.rb +19 -19
- data/spec/extensions/columns_introspection_spec.rb +43 -43
- data/spec/extensions/composition_spec.rb +64 -64
- data/spec/extensions/connection_validator_spec.rb +92 -90
- data/spec/extensions/constraint_validations_plugin_spec.rb +92 -92
- data/spec/extensions/constraint_validations_spec.rb +80 -80
- data/spec/extensions/core_refinements_spec.rb +220 -220
- data/spec/extensions/csv_serializer_spec.rb +44 -44
- data/spec/extensions/current_datetime_timestamp_spec.rb +8 -8
- data/spec/extensions/dataset_associations_spec.rb +65 -65
- data/spec/extensions/dataset_source_alias_spec.rb +16 -16
- data/spec/extensions/date_arithmetic_spec.rb +51 -58
- data/spec/extensions/defaults_setter_spec.rb +19 -19
- data/spec/extensions/delay_add_association_spec.rb +52 -0
- data/spec/extensions/dirty_spec.rb +51 -51
- data/spec/extensions/eager_each_spec.rb +8 -8
- data/spec/extensions/empty_array_ignore_nulls_spec.rb +10 -10
- data/spec/extensions/error_splitter_spec.rb +2 -2
- data/spec/extensions/error_sql_spec.rb +4 -4
- data/spec/extensions/eval_inspect_spec.rb +3 -3
- data/spec/extensions/filter_having_spec.rb +8 -8
- data/spec/extensions/force_encoding_spec.rb +30 -30
- data/spec/extensions/from_block_spec.rb +7 -7
- data/spec/extensions/graph_each_spec.rb +19 -19
- data/spec/extensions/hash_aliases_spec.rb +5 -5
- data/spec/extensions/hook_class_methods_spec.rb +100 -100
- data/spec/extensions/inflector_spec.rb +54 -54
- data/spec/extensions/input_transformer_spec.rb +10 -10
- data/spec/extensions/insert_returning_select_spec.rb +8 -8
- data/spec/extensions/instance_filters_spec.rb +26 -26
- data/spec/extensions/instance_hooks_spec.rb +85 -85
- data/spec/extensions/json_serializer_spec.rb +68 -68
- data/spec/extensions/lazy_attributes_spec.rb +49 -49
- data/spec/extensions/list_spec.rb +77 -75
- data/spec/extensions/looser_typecasting_spec.rb +16 -16
- data/spec/extensions/many_through_many_spec.rb +627 -627
- data/spec/extensions/meta_def_spec.rb +7 -7
- data/spec/extensions/migration_spec.rb +217 -217
- data/spec/extensions/modification_detection_spec.rb +20 -20
- data/spec/extensions/mssql_optimistic_locking_spec.rb +21 -21
- data/spec/extensions/named_timezones_spec.rb +18 -18
- data/spec/extensions/nested_attributes_spec.rb +107 -107
- data/spec/extensions/null_dataset_spec.rb +24 -24
- data/spec/extensions/optimistic_locking_spec.rb +21 -21
- data/spec/extensions/pagination_spec.rb +52 -52
- data/spec/extensions/pg_array_associations_spec.rb +273 -273
- data/spec/extensions/pg_array_ops_spec.rb +52 -52
- data/spec/extensions/pg_array_spec.rb +152 -152
- data/spec/extensions/pg_enum_spec.rb +13 -13
- data/spec/extensions/pg_hstore_ops_spec.rb +63 -63
- data/spec/extensions/pg_hstore_spec.rb +84 -84
- data/spec/extensions/pg_inet_spec.rb +15 -15
- data/spec/extensions/pg_interval_spec.rb +29 -29
- data/spec/extensions/pg_json_ops_spec.rb +86 -84
- data/spec/extensions/pg_json_spec.rb +104 -104
- data/spec/extensions/pg_loose_count_spec.rb +6 -6
- data/spec/extensions/pg_range_ops_spec.rb +24 -24
- data/spec/extensions/pg_range_spec.rb +143 -143
- data/spec/extensions/pg_row_ops_spec.rb +14 -14
- data/spec/extensions/pg_row_plugin_spec.rb +12 -12
- data/spec/extensions/pg_row_spec.rb +118 -118
- data/spec/extensions/pg_static_cache_updater_spec.rb +28 -28
- data/spec/extensions/pg_typecast_on_load_spec.rb +21 -21
- data/spec/extensions/prepared_statements_associations_spec.rb +42 -42
- data/spec/extensions/prepared_statements_safe_spec.rb +18 -18
- data/spec/extensions/prepared_statements_spec.rb +28 -28
- data/spec/extensions/prepared_statements_with_pk_spec.rb +11 -11
- data/spec/extensions/pretty_table_spec.rb +16 -16
- data/spec/extensions/query_literals_spec.rb +37 -37
- data/spec/extensions/query_spec.rb +32 -32
- data/spec/extensions/rcte_tree_spec.rb +141 -141
- data/spec/extensions/round_timestamps_spec.rb +21 -21
- data/spec/extensions/schema_caching_spec.rb +8 -8
- data/spec/extensions/schema_dumper_spec.rb +78 -78
- data/spec/extensions/schema_spec.rb +31 -27
- data/spec/extensions/scissors_spec.rb +3 -3
- data/spec/extensions/select_remove_spec.rb +14 -14
- data/spec/extensions/sequel_3_dataset_methods_spec.rb +28 -28
- data/spec/extensions/serialization_modification_detection_spec.rb +33 -33
- data/spec/extensions/serialization_spec.rb +79 -78
- data/spec/extensions/server_block_spec.rb +17 -17
- data/spec/extensions/set_overrides_spec.rb +30 -30
- data/spec/extensions/sharding_spec.rb +65 -65
- data/spec/extensions/shared_caching_spec.rb +29 -29
- data/spec/extensions/single_table_inheritance_spec.rb +79 -79
- data/spec/extensions/skip_create_refresh_spec.rb +3 -3
- data/spec/extensions/spec_helper.rb +4 -29
- data/spec/extensions/split_array_nil_spec.rb +9 -9
- data/spec/extensions/split_values_spec.rb +7 -7
- data/spec/extensions/sql_expr_spec.rb +32 -32
- data/spec/extensions/static_cache_spec.rb +123 -123
- data/spec/extensions/string_date_time_spec.rb +34 -34
- data/spec/extensions/string_stripper_spec.rb +15 -15
- data/spec/extensions/subclasses_spec.rb +31 -31
- data/spec/extensions/table_select_spec.rb +15 -15
- data/spec/extensions/tactical_eager_loading_spec.rb +23 -23
- data/spec/extensions/thread_local_timezones_spec.rb +13 -13
- data/spec/extensions/timestamps_spec.rb +40 -40
- data/spec/extensions/to_dot_spec.rb +34 -34
- data/spec/extensions/touch_spec.rb +52 -52
- data/spec/extensions/tree_spec.rb +72 -72
- data/spec/extensions/typecast_on_load_spec.rb +25 -25
- data/spec/extensions/unlimited_update_spec.rb +2 -2
- data/spec/extensions/update_or_create_spec.rb +36 -36
- data/spec/extensions/update_primary_key_spec.rb +35 -35
- data/spec/extensions/update_refresh_spec.rb +41 -0
- data/spec/extensions/validate_associated_spec.rb +52 -0
- data/spec/extensions/validation_class_methods_spec.rb +314 -317
- data/spec/extensions/validation_helpers_spec.rb +195 -195
- data/spec/extensions/xml_serializer_spec.rb +48 -48
- data/spec/guards_helper.rb +55 -0
- data/spec/integration/associations_test.rb +1089 -1088
- data/spec/integration/database_test.rb +29 -29
- data/spec/integration/dataset_test.rb +661 -661
- data/spec/integration/eager_loader_test.rb +147 -147
- data/spec/integration/migrator_test.rb +122 -122
- data/spec/integration/model_test.rb +70 -70
- data/spec/integration/plugin_test.rb +682 -640
- data/spec/integration/prepared_statement_test.rb +172 -172
- data/spec/integration/schema_test.rb +245 -245
- data/spec/integration/spec_helper.rb +1 -64
- data/spec/integration/timezone_test.rb +17 -17
- data/spec/integration/transaction_test.rb +87 -87
- data/spec/integration/type_test.rb +33 -33
- data/spec/model/association_reflection_spec.rb +130 -121
- data/spec/model/associations_spec.rb +1112 -1113
- data/spec/model/base_spec.rb +197 -196
- data/spec/model/class_dataset_methods_spec.rb +118 -118
- data/spec/model/dataset_methods_spec.rb +49 -49
- data/spec/model/eager_loading_spec.rb +705 -702
- data/spec/model/hooks_spec.rb +169 -168
- data/spec/model/inflector_spec.rb +5 -5
- data/spec/model/model_spec.rb +287 -297
- data/spec/model/plugins_spec.rb +47 -47
- data/spec/model/record_spec.rb +534 -535
- data/spec/model/spec_helper.rb +3 -21
- data/spec/model/validations_spec.rb +72 -70
- data/spec/spec_config.rb +8 -0
- metadata +41 -9
- data/lib/sequel/adapters/fdbsql.rb +0 -286
- data/lib/sequel/adapters/jdbc/fdbsql.rb +0 -66
- data/lib/sequel/adapters/openbase.rb +0 -54
- data/lib/sequel/adapters/shared/fdbsql.rb +0 -550
- data/spec/adapters/fdbsql_spec.rb +0 -429
- data/spec/rspec_helper.rb +0 -22
@@ -13,20 +13,20 @@ describe "Database schema parser" do
|
|
13
13
|
DB.drop_table?(:items)
|
14
14
|
end
|
15
15
|
|
16
|
-
|
16
|
+
it "should handle a database with a identifier methods" do
|
17
17
|
DB.identifier_output_method = :reverse
|
18
18
|
DB.identifier_input_method = :reverse
|
19
19
|
DB.quote_identifiers = true
|
20
20
|
DB.create_table!(:items){Integer :number}
|
21
21
|
begin
|
22
|
-
DB.schema(:items, :reload=>true).
|
23
|
-
DB.schema(:items, :reload=>true).first.first.
|
22
|
+
DB.schema(:items, :reload=>true).must_be_kind_of(Array)
|
23
|
+
DB.schema(:items, :reload=>true).first.first.must_equal :number
|
24
24
|
ensure
|
25
25
|
DB.drop_table(:items)
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
-
|
29
|
+
it "should handle a dataset with identifier methods different than the database's" do
|
30
30
|
DB.identifier_output_method = :reverse
|
31
31
|
DB.identifier_input_method = :reverse
|
32
32
|
DB.quote_identifiers = true
|
@@ -37,8 +37,8 @@ describe "Database schema parser" do
|
|
37
37
|
ds.identifier_output_method = :reverse
|
38
38
|
ds.identifier_input_method = :reverse
|
39
39
|
begin
|
40
|
-
DB.schema(ds, :reload=>true).
|
41
|
-
DB.schema(ds, :reload=>true).first.first.
|
40
|
+
DB.schema(ds, :reload=>true).must_be_kind_of(Array)
|
41
|
+
DB.schema(ds, :reload=>true).first.first.must_equal :number
|
42
42
|
ensure
|
43
43
|
DB.identifier_output_method = :reverse
|
44
44
|
DB.identifier_input_method = :reverse
|
@@ -46,124 +46,124 @@ describe "Database schema parser" do
|
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
49
|
-
|
49
|
+
it "should not issue an sql query if the schema has been loaded unless :reload is true" do
|
50
50
|
DB.create_table!(:items){Integer :number}
|
51
51
|
DB.schema(:items, :reload=>true)
|
52
52
|
DB.schema(:items)
|
53
53
|
DB.schema(:items, :reload=>true)
|
54
54
|
end
|
55
55
|
|
56
|
-
|
56
|
+
it "Model schema should include columns in the table, even if they aren't selected" do
|
57
57
|
DB.create_table!(:items){String :a; Integer :number}
|
58
58
|
m = Sequel::Model(DB[:items].select(:a))
|
59
|
-
m.columns.
|
60
|
-
m.db_schema[:number][:type].
|
59
|
+
m.columns.must_equal [:a]
|
60
|
+
m.db_schema[:number][:type].must_equal :integer
|
61
61
|
end
|
62
62
|
|
63
|
-
|
64
|
-
proc{DB.schema(:no_table)}.
|
63
|
+
it "should raise an error when the table doesn't exist" do
|
64
|
+
proc{DB.schema(:no_table)}.must_raise(Sequel::Error, Sequel::DatabaseError)
|
65
65
|
end
|
66
66
|
|
67
|
-
|
67
|
+
it "should return the schema correctly" do
|
68
68
|
DB.create_table!(:items){Integer :number}
|
69
69
|
schema = DB.schema(:items, :reload=>true)
|
70
|
-
schema.
|
71
|
-
schema.length.
|
70
|
+
schema.must_be_kind_of(Array)
|
71
|
+
schema.length.must_equal 1
|
72
72
|
col = schema.first
|
73
|
-
col.
|
74
|
-
col.length.
|
75
|
-
col.first.
|
73
|
+
col.must_be_kind_of(Array)
|
74
|
+
col.length.must_equal 2
|
75
|
+
col.first.must_equal :number
|
76
76
|
col_info = col.last
|
77
|
-
col_info.
|
78
|
-
col_info[:type].
|
77
|
+
col_info.must_be_kind_of(Hash)
|
78
|
+
col_info[:type].must_equal :integer
|
79
79
|
DB.schema(:items)
|
80
80
|
end
|
81
81
|
|
82
|
-
|
82
|
+
it "should parse primary keys from the schema properly" do
|
83
83
|
DB.create_table!(:items){Integer :number}
|
84
|
-
DB.schema(:items).collect{|k,v| k if v[:primary_key]}.compact.
|
84
|
+
DB.schema(:items).collect{|k,v| k if v[:primary_key]}.compact.must_equal []
|
85
85
|
DB.create_table!(:items){primary_key :number}
|
86
|
-
DB.schema(:items).collect{|k,v| k if v[:primary_key]}.compact.
|
86
|
+
DB.schema(:items).collect{|k,v| k if v[:primary_key]}.compact.must_equal [:number]
|
87
87
|
DB.create_table!(:items){Integer :number1; Integer :number2; primary_key [:number1, :number2]}
|
88
|
-
DB.schema(:items).collect{|k,v| k if v[:primary_key]}.compact.
|
88
|
+
DB.schema(:items).collect{|k,v| k if v[:primary_key]}.compact.must_equal [:number1, :number2]
|
89
89
|
end
|
90
90
|
|
91
|
-
cspecify "should parse autoincrementing primary keys from the schema properly", :sqlite, :oracle
|
91
|
+
cspecify "should parse autoincrementing primary keys from the schema properly", :sqlite, :oracle do
|
92
92
|
DB.create_table!(:items){Integer :number}
|
93
|
-
DB.schema(:items).collect{|k,v| k if v[:primary_key] && v[:auto_increment]}.compact.
|
93
|
+
DB.schema(:items).collect{|k,v| k if v[:primary_key] && v[:auto_increment]}.compact.must_equal []
|
94
94
|
DB.create_table!(:items){primary_key :number}
|
95
|
-
DB.schema(:items).collect{|k,v| k if v[:primary_key] && v[:auto_increment]}.compact.
|
95
|
+
DB.schema(:items).collect{|k,v| k if v[:primary_key] && v[:auto_increment]}.compact.must_equal [:number]
|
96
96
|
DB.create_table!(:items){Integer :number, :primary_key=>true}
|
97
|
-
DB.schema(:items).collect{|k,v| k if v[:primary_key] && v[:auto_increment]}.compact.
|
97
|
+
DB.schema(:items).collect{|k,v| k if v[:primary_key] && v[:auto_increment]}.compact.must_equal []
|
98
98
|
end
|
99
99
|
|
100
|
-
|
100
|
+
it "should parse NULL/NOT NULL from the schema properly" do
|
101
101
|
DB.create_table!(:items){Integer :number, :null=>true}
|
102
|
-
DB.schema(:items).first.last[:allow_null].
|
102
|
+
DB.schema(:items).first.last[:allow_null].must_equal true
|
103
103
|
DB.create_table!(:items){Integer :number, :null=>false}
|
104
|
-
DB.schema(:items).first.last[:allow_null].
|
104
|
+
DB.schema(:items).first.last[:allow_null].must_equal false
|
105
105
|
end
|
106
106
|
|
107
|
-
|
107
|
+
it "should parse defaults from the schema properly" do
|
108
108
|
DB.create_table!(:items){Integer :number}
|
109
|
-
DB.schema(:items).first.last[:ruby_default].
|
109
|
+
DB.schema(:items).first.last[:ruby_default].must_equal nil
|
110
110
|
DB.create_table!(:items){Integer :number, :default=>0}
|
111
|
-
DB.schema(:items).first.last[:ruby_default].
|
111
|
+
DB.schema(:items).first.last[:ruby_default].must_equal 0
|
112
112
|
DB.create_table!(:items){String :a, :default=>"blah"}
|
113
|
-
DB.schema(:items).first.last[:ruby_default].
|
113
|
+
DB.schema(:items).first.last[:ruby_default].must_equal 'blah'
|
114
114
|
end
|
115
115
|
|
116
|
-
|
116
|
+
it "should make :default nil for a NULL default" do
|
117
117
|
DB.create_table!(:items){Integer :number}
|
118
|
-
DB.schema(:items).first.last[:default].
|
118
|
+
DB.schema(:items).first.last[:default].must_equal nil
|
119
119
|
DB.create_table!(:items){Integer :number, :default=>0}
|
120
|
-
DB.schema(:items).first.last[:default].
|
120
|
+
DB.schema(:items).first.last[:default].wont_equal nil
|
121
121
|
end
|
122
122
|
|
123
|
-
|
123
|
+
it "should parse current timestamp defaults from the schema properly" do
|
124
124
|
DB.create_table!(:items){Time :a, :default=>Sequel::CURRENT_TIMESTAMP}
|
125
|
-
DB.schema(:items).first.last[:ruby_default].
|
125
|
+
DB.schema(:items).first.last[:ruby_default].must_equal Sequel::CURRENT_TIMESTAMP
|
126
126
|
end
|
127
127
|
|
128
128
|
cspecify "should parse current date defaults from the schema properly", :mysql, :oracle do
|
129
129
|
DB.create_table!(:items){Date :a, :default=>Sequel::CURRENT_DATE}
|
130
|
-
DB.schema(:items).first.last[:ruby_default].
|
130
|
+
DB.schema(:items).first.last[:ruby_default].must_equal Sequel::CURRENT_DATE
|
131
131
|
end
|
132
132
|
|
133
133
|
cspecify "should parse types from the schema properly", [:jdbc, :db2], :oracle do
|
134
134
|
DB.create_table!(:items){Integer :number}
|
135
|
-
DB.schema(:items).first.last[:type].
|
135
|
+
DB.schema(:items).first.last[:type].must_equal :integer
|
136
136
|
DB.create_table!(:items){Fixnum :number}
|
137
|
-
DB.schema(:items).first.last[:type].
|
137
|
+
DB.schema(:items).first.last[:type].must_equal :integer
|
138
138
|
DB.create_table!(:items){Bignum :number}
|
139
|
-
DB.schema(:items).first.last[:type].
|
139
|
+
DB.schema(:items).first.last[:type].must_equal :integer
|
140
140
|
DB.create_table!(:items){Float :number}
|
141
|
-
DB.schema(:items).first.last[:type].
|
141
|
+
DB.schema(:items).first.last[:type].must_equal :float
|
142
142
|
DB.create_table!(:items){BigDecimal :number, :size=>[11, 2]}
|
143
|
-
DB.schema(:items).first.last[:type].
|
143
|
+
DB.schema(:items).first.last[:type].must_equal :decimal
|
144
144
|
DB.create_table!(:items){Numeric :number, :size=>[12, 0]}
|
145
|
-
DB.schema(:items).first.last[:type].
|
145
|
+
DB.schema(:items).first.last[:type].must_equal :integer
|
146
146
|
DB.create_table!(:items){String :number}
|
147
|
-
DB.schema(:items).first.last[:type].
|
147
|
+
DB.schema(:items).first.last[:type].must_equal :string
|
148
148
|
DB.create_table!(:items){Date :number}
|
149
|
-
DB.schema(:items).first.last[:type].
|
149
|
+
DB.schema(:items).first.last[:type].must_equal :date
|
150
150
|
DB.create_table!(:items){Time :number}
|
151
|
-
DB.schema(:items).first.last[:type].
|
151
|
+
DB.schema(:items).first.last[:type].must_equal :datetime
|
152
152
|
DB.create_table!(:items){DateTime :number}
|
153
|
-
DB.schema(:items).first.last[:type].
|
153
|
+
DB.schema(:items).first.last[:type].must_equal :datetime
|
154
154
|
DB.create_table!(:items){File :number}
|
155
|
-
DB.schema(:items).first.last[:type].
|
155
|
+
DB.schema(:items).first.last[:type].must_equal :blob
|
156
156
|
DB.create_table!(:items){TrueClass :number}
|
157
|
-
DB.schema(:items).first.last[:type].
|
157
|
+
DB.schema(:items).first.last[:type].must_equal :boolean
|
158
158
|
DB.create_table!(:items){FalseClass :number}
|
159
|
-
DB.schema(:items).first.last[:type].
|
159
|
+
DB.schema(:items).first.last[:type].must_equal :boolean
|
160
160
|
end
|
161
161
|
|
162
|
-
|
162
|
+
it "should parse maximum length for string columns" do
|
163
163
|
DB.create_table!(:items){String :a, :size=>4}
|
164
|
-
DB.schema(:items).first.last[:max_length].
|
164
|
+
DB.schema(:items).first.last[:max_length].must_equal 4
|
165
165
|
DB.create_table!(:items){String :a, :fixed=>true, :size=>3}
|
166
|
-
DB.schema(:items).first.last[:max_length].
|
166
|
+
DB.schema(:items).first.last[:max_length].must_equal 3
|
167
167
|
end
|
168
168
|
end if DB.supports_schema_parsing?
|
169
169
|
|
@@ -172,36 +172,36 @@ describe "Database index parsing" do
|
|
172
172
|
DB.drop_table?(:items)
|
173
173
|
end
|
174
174
|
|
175
|
-
|
175
|
+
it "should parse indexes into a hash" do
|
176
176
|
# Delete :deferrable entry, since not all adapters implement it
|
177
177
|
f = lambda{h = DB.indexes(:items); h.values.each{|h2| h2.delete(:deferrable)}; h}
|
178
178
|
|
179
179
|
DB.create_table!(:items){Integer :n; Integer :a}
|
180
|
-
f.call.
|
180
|
+
f.call.must_equal({})
|
181
181
|
DB.add_index(:items, :n)
|
182
|
-
f.call.
|
182
|
+
f.call.must_equal(:items_n_index=>{:columns=>[:n], :unique=>false})
|
183
183
|
DB.drop_index(:items, :n)
|
184
|
-
f.call.
|
184
|
+
f.call.must_equal({})
|
185
185
|
DB.add_index(:items, :n, :unique=>true, :name=>:blah_blah_index)
|
186
|
-
f.call.
|
186
|
+
f.call.must_equal(:blah_blah_index=>{:columns=>[:n], :unique=>true})
|
187
187
|
DB.add_index(:items, [:n, :a])
|
188
|
-
f.call.
|
188
|
+
f.call.must_equal(:blah_blah_index=>{:columns=>[:n], :unique=>true}, :items_n_a_index=>{:columns=>[:n, :a], :unique=>false})
|
189
189
|
DB.drop_index(:items, :n, :name=>:blah_blah_index)
|
190
|
-
f.call.
|
190
|
+
f.call.must_equal(:items_n_a_index=>{:columns=>[:n, :a], :unique=>false})
|
191
191
|
DB.drop_index(:items, [:n, :a])
|
192
|
-
f.call.
|
192
|
+
f.call.must_equal({})
|
193
193
|
end
|
194
194
|
|
195
|
-
|
195
|
+
it "should not include a primary key index" do
|
196
196
|
DB.create_table!(:items){primary_key :n}
|
197
|
-
DB.indexes(:items).
|
197
|
+
DB.indexes(:items).must_equal({})
|
198
198
|
DB.create_table!(:items){Integer :n; Integer :a; primary_key [:n, :a]}
|
199
|
-
DB.indexes(:items).
|
199
|
+
DB.indexes(:items).must_equal({})
|
200
200
|
end
|
201
201
|
|
202
202
|
cspecify "should not include partial indexes", :sqlite do
|
203
203
|
DB.create_table!(:items){Integer :n; Integer :a; index :n, :where=>proc{n > 10}}
|
204
|
-
DB.indexes(:items).
|
204
|
+
DB.indexes(:items).must_equal({})
|
205
205
|
end if DB.supports_partial_indexes?
|
206
206
|
end if DB.supports_index_parsing?
|
207
207
|
|
@@ -219,16 +219,16 @@ describe "Database foreign key parsing" do
|
|
219
219
|
e.last.shift
|
220
220
|
end
|
221
221
|
end
|
222
|
-
a.
|
222
|
+
a.must_equal e
|
223
223
|
end
|
224
|
-
actual.length.
|
224
|
+
actual.length.must_equal expected.length
|
225
225
|
end
|
226
226
|
end
|
227
227
|
after do
|
228
228
|
@db.drop_table?(:b, :a)
|
229
229
|
end
|
230
230
|
|
231
|
-
|
231
|
+
it "should parse foreign key information into an array of hashes" do
|
232
232
|
@db.create_table!(:a, :engine=>:InnoDB){primary_key :c; Integer :d, :null => false, :unique => true}
|
233
233
|
@db.create_table!(:b, :engine=>:InnoDB){foreign_key :e, :a}
|
234
234
|
@pr[:a]
|
@@ -250,11 +250,11 @@ describe "Database foreign key parsing" do
|
|
250
250
|
@db.alter_table(:b){drop_foreign_key :e}
|
251
251
|
@pr[:b, [[:f], :a, [:c]], [[:f], :a, [:d]]]
|
252
252
|
|
253
|
-
proc{@db.alter_table(:b){drop_foreign_key :f}}.
|
253
|
+
proc{@db.alter_table(:b){drop_foreign_key :f}}.must_raise(Sequel::Error, Sequel::DatabaseError)
|
254
254
|
@pr[:b, [[:f], :a, [:c]], [[:f], :a, [:d]]]
|
255
255
|
end
|
256
256
|
|
257
|
-
|
257
|
+
it "should handle composite foreign and primary keys" do
|
258
258
|
@db.create_table!(:a, :engine=>:InnoDB){Integer :b, :null=>false; Integer :c, :null=>false; Integer :d, :null=>false; primary_key [:b, :c]; unique [:d, :c]}
|
259
259
|
@db.create_table!(:b, :engine=>:InnoDB){Integer :e, :null=>false; Integer :f, :null=>false; Integer :g, :null=>false; foreign_key [:e, :f], :a; foreign_key [:g, :f], :a, :key=>[:d, :c]}
|
260
260
|
@pr[:b, [[:e, :f], :a, [:pk, :b, :c]], [[:g, :f], :a, [:d, :c]]]
|
@@ -272,32 +272,32 @@ describe "Database schema modifiers" do
|
|
272
272
|
@db.drop_table(:items2) rescue nil
|
273
273
|
end
|
274
274
|
|
275
|
-
|
275
|
+
it "should create tables correctly" do
|
276
276
|
@db.create_table!(:items){Integer :number}
|
277
|
-
@db.table_exists?(:items).
|
278
|
-
@db.schema(:items, :reload=>true).map{|x| x.first}.
|
277
|
+
@db.table_exists?(:items).must_equal true
|
278
|
+
@db.schema(:items, :reload=>true).map{|x| x.first}.must_equal [:number]
|
279
279
|
@ds.insert([10])
|
280
|
-
@ds.columns!.
|
280
|
+
@ds.columns!.must_equal [:number]
|
281
281
|
end
|
282
282
|
|
283
|
-
|
283
|
+
it "should create tables from select statements correctly" do
|
284
284
|
@db.create_table!(:items){Integer :number}
|
285
285
|
@ds.insert([10])
|
286
286
|
@db.create_table(:items2, :as=>@db[:items])
|
287
|
-
@db.schema(:items2, :reload=>true).map{|x| x.first}.
|
288
|
-
@db[:items2].columns.
|
289
|
-
@db[:items2].all.
|
287
|
+
@db.schema(:items2, :reload=>true).map{|x| x.first}.must_equal [:number]
|
288
|
+
@db[:items2].columns.must_equal [:number]
|
289
|
+
@db[:items2].all.must_equal [{:number=>10}]
|
290
290
|
end
|
291
291
|
|
292
|
-
|
293
|
-
|
292
|
+
it "should not raise an error if table doesn't exist when using drop_table :if_exists" do
|
293
|
+
@db.drop_table(:items, :if_exists=>true)
|
294
294
|
end if DB.supports_drop_table_if_exists?
|
295
295
|
|
296
296
|
describe "views" do
|
297
297
|
before do
|
298
298
|
@db.drop_view(:items_view2) rescue nil
|
299
299
|
@db.drop_view(:items_view) rescue nil
|
300
|
-
@db.create_table(:items){Integer :number}
|
300
|
+
@db.create_table!(:items){Integer :number}
|
301
301
|
@ds.insert(:number=>1)
|
302
302
|
@ds.insert(:number=>2)
|
303
303
|
end
|
@@ -306,228 +306,228 @@ describe "Database schema modifiers" do
|
|
306
306
|
@db.drop_view(:items_view) rescue nil
|
307
307
|
end
|
308
308
|
|
309
|
-
|
309
|
+
it "should create views correctly" do
|
310
310
|
@db.create_view(:items_view, @ds.where(:number=>1))
|
311
|
-
@db[:items_view].map(:number).
|
311
|
+
@db[:items_view].map(:number).must_equal [1]
|
312
312
|
end
|
313
313
|
|
314
|
-
|
314
|
+
it "should create views with check options correctly" do
|
315
315
|
@db.create_view(:items_view, @ds.where{number > 2}, :check=>true)
|
316
|
-
proc{@db[:items_view].insert(1)}.
|
316
|
+
proc{@db[:items_view].insert(1)}.must_raise(Sequel::DatabaseError)
|
317
317
|
@db[:items_view].insert(3)
|
318
|
-
@db[:items_view].select_order_map(:number).
|
318
|
+
@db[:items_view].select_order_map(:number).must_equal [3]
|
319
319
|
@db.create_view(:items_view2, @db[:items_view].where{number > 1}, :check=>true)
|
320
|
-
proc{@db[:items_view2].insert(1)}.
|
321
|
-
proc{@db[:items_view2].insert(2)}.
|
320
|
+
proc{@db[:items_view2].insert(1)}.must_raise(Sequel::DatabaseError)
|
321
|
+
proc{@db[:items_view2].insert(2)}.must_raise(Sequel::DatabaseError)
|
322
322
|
@db[:items_view2].insert(4)
|
323
|
-
@db[:items_view2].select_order_map(:number).
|
324
|
-
@ds.select_order_map(:number).
|
323
|
+
@db[:items_view2].select_order_map(:number).must_equal [3, 4]
|
324
|
+
@ds.select_order_map(:number).must_equal [1, 2, 3, 4]
|
325
325
|
end if DB.supports_views_with_check_option?
|
326
326
|
|
327
|
-
|
327
|
+
it "should create views with local check options correctly" do
|
328
328
|
@db.create_view(:items_view, @ds.where{number > 2})
|
329
329
|
@db[:items_view].insert(3)
|
330
|
-
@db[:items_view].select_order_map(:number).
|
330
|
+
@db[:items_view].select_order_map(:number).must_equal [3]
|
331
331
|
@db.create_view(:items_view2, @db[:items_view].where{number > 1}, :check=>:local)
|
332
|
-
proc{@db[:items_view2].insert(1)}.
|
332
|
+
proc{@db[:items_view2].insert(1)}.must_raise(Sequel::DatabaseError)
|
333
333
|
@db[:items_view2].insert(2)
|
334
334
|
@db[:items_view2].insert(4)
|
335
|
-
@db[:items_view2].select_order_map(:number).
|
336
|
-
@ds.select_order_map(:number).
|
335
|
+
@db[:items_view2].select_order_map(:number).must_equal [3, 4]
|
336
|
+
@ds.select_order_map(:number).must_equal [1, 2, 2, 3, 4]
|
337
337
|
end if DB.supports_views_with_local_check_option?
|
338
338
|
|
339
339
|
cspecify "should create views with explicit columns correctly", :sqlite do
|
340
340
|
@db.create_view(:items_view, @ds.where(:number=>1), :columns=>[:n])
|
341
|
-
@db[:items_view].map(:n).
|
341
|
+
@db[:items_view].map(:n).must_equal [1]
|
342
342
|
end
|
343
343
|
|
344
|
-
|
344
|
+
it "should drop views correctly" do
|
345
345
|
@db.create_view(:items_view, @ds.where(:number=>1))
|
346
346
|
@db.drop_view(:items_view)
|
347
|
-
proc{@db[:items_view].map(:number)}.
|
347
|
+
proc{@db[:items_view].map(:number)}.must_raise(Sequel::DatabaseError)
|
348
348
|
end
|
349
349
|
|
350
|
-
|
351
|
-
|
350
|
+
it "should not raise an error if view doesn't exist when using drop_view :if_exists" do
|
351
|
+
@db.drop_view(:items_view, :if_exists=>true)
|
352
352
|
end if DB.supports_drop_table_if_exists?
|
353
353
|
|
354
|
-
|
354
|
+
it "should create or replace views correctly" do
|
355
355
|
@db.create_or_replace_view(:items_view, @ds.where(:number=>1))
|
356
|
-
@db[:items_view].map(:number).
|
356
|
+
@db[:items_view].map(:number).must_equal [1]
|
357
357
|
@db.create_or_replace_view(:items_view, @ds.where(:number=>2))
|
358
|
-
@db[:items_view].map(:number).
|
358
|
+
@db[:items_view].map(:number).must_equal [2]
|
359
359
|
end
|
360
360
|
end
|
361
361
|
|
362
|
-
|
362
|
+
it "should handle create table in a rolled back transaction" do
|
363
363
|
@db.drop_table?(:items)
|
364
364
|
@db.transaction(:rollback=>:always){@db.create_table(:items){Integer :number}}
|
365
|
-
@db.table_exists?(:items).
|
365
|
+
@db.table_exists?(:items).must_equal false
|
366
366
|
end if DB.supports_transactional_ddl?
|
367
367
|
|
368
368
|
describe "join tables" do
|
369
369
|
after do
|
370
370
|
@db.drop_join_table(:cat_id=>:cats, :dog_id=>:dogs) if @db.table_exists?(:cats_dogs)
|
371
371
|
@db.drop_table(:cats, :dogs)
|
372
|
-
@db.table_exists?(:cats_dogs).
|
372
|
+
@db.table_exists?(:cats_dogs).must_equal false
|
373
373
|
end
|
374
374
|
|
375
|
-
|
375
|
+
it "should create join tables correctly" do
|
376
376
|
@db.create_table!(:cats){primary_key :id}
|
377
377
|
@db.create_table!(:dogs){primary_key :id}
|
378
378
|
@db.create_join_table(:cat_id=>:cats, :dog_id=>:dogs)
|
379
|
-
@db.table_exists?(:cats_dogs).
|
379
|
+
@db.table_exists?(:cats_dogs).must_equal true
|
380
380
|
end
|
381
381
|
end
|
382
382
|
|
383
|
-
|
383
|
+
it "should create temporary tables without raising an exception" do
|
384
384
|
@db.create_table!(:items_temp, :temp=>true){Integer :number}
|
385
385
|
end
|
386
386
|
|
387
|
-
|
387
|
+
it "should have create_table? only create the table if it doesn't already exist" do
|
388
388
|
@db.create_table!(:items){String :a}
|
389
389
|
@db.create_table?(:items){String :b}
|
390
|
-
@db[:items].columns.
|
390
|
+
@db[:items].columns.must_equal [:a]
|
391
391
|
@db.drop_table?(:items)
|
392
392
|
@db.create_table?(:items){String :b}
|
393
|
-
@db[:items].columns.
|
393
|
+
@db[:items].columns.must_equal [:b]
|
394
394
|
end
|
395
395
|
|
396
|
-
|
396
|
+
it "should have create_table? work correctly with indexes" do
|
397
397
|
@db.create_table!(:items){String :a, :index=>true}
|
398
398
|
@db.create_table?(:items){String :b, :index=>true}
|
399
|
-
@db[:items].columns.
|
399
|
+
@db[:items].columns.must_equal [:a]
|
400
400
|
@db.drop_table?(:items)
|
401
401
|
@db.create_table?(:items){String :b, :index=>true}
|
402
|
-
@db[:items].columns.
|
402
|
+
@db[:items].columns.must_equal [:b]
|
403
403
|
end
|
404
404
|
|
405
|
-
|
405
|
+
it "should rename tables correctly" do
|
406
406
|
@db.drop_table?(:items)
|
407
407
|
@db.create_table!(:items2){Integer :number}
|
408
408
|
@db.rename_table(:items2, :items)
|
409
|
-
@db.table_exists?(:items).
|
410
|
-
@db.table_exists?(:items2).
|
411
|
-
@db.schema(:items, :reload=>true).map{|x| x.first}.
|
409
|
+
@db.table_exists?(:items).must_equal true
|
410
|
+
@db.table_exists?(:items2).must_equal false
|
411
|
+
@db.schema(:items, :reload=>true).map{|x| x.first}.must_equal [:number]
|
412
412
|
@ds.insert([10])
|
413
|
-
@ds.columns!.
|
413
|
+
@ds.columns!.must_equal [:number]
|
414
414
|
end
|
415
415
|
|
416
|
-
|
416
|
+
it "should allow creating indexes with tables" do
|
417
417
|
@db.create_table!(:items){Integer :number; index :number}
|
418
|
-
@db.table_exists?(:items).
|
419
|
-
@db.schema(:items, :reload=>true).map{|x| x.first}.
|
418
|
+
@db.table_exists?(:items).must_equal true
|
419
|
+
@db.schema(:items, :reload=>true).map{|x| x.first}.must_equal [:number]
|
420
420
|
@ds.insert([10])
|
421
|
-
@ds.columns!.
|
421
|
+
@ds.columns!.must_equal [:number]
|
422
422
|
end
|
423
423
|
|
424
|
-
|
424
|
+
it "should allow creating partial indexes with tables" do
|
425
425
|
@db.create_table!(:items){Integer :number; index :number, :where=>proc{number > 10}}
|
426
|
-
@db.table_exists?(:items).
|
427
|
-
@db.schema(:items, :reload=>true).map{|x| x.first}.
|
426
|
+
@db.table_exists?(:items).must_equal true
|
427
|
+
@db.schema(:items, :reload=>true).map{|x| x.first}.must_equal [:number]
|
428
428
|
@ds.insert([10])
|
429
|
-
@ds.columns!.
|
429
|
+
@ds.columns!.must_equal [:number]
|
430
430
|
end if DB.supports_partial_indexes?
|
431
431
|
|
432
|
-
|
432
|
+
it "should handle combination of default, unique, and not null" do
|
433
433
|
@db.create_table!(:items){Integer :number, :default=>0, :null=>false, :unique=>true}
|
434
|
-
@db.table_exists?(:items).
|
435
|
-
@db.schema(:items, :reload=>true).map{|x| x.last}.first.values_at(:ruby_default, :allow_null).
|
434
|
+
@db.table_exists?(:items).must_equal true
|
435
|
+
@db.schema(:items, :reload=>true).map{|x| x.last}.first.values_at(:ruby_default, :allow_null).must_equal [0, false]
|
436
436
|
@ds.insert([10])
|
437
437
|
end
|
438
438
|
|
439
|
-
|
439
|
+
it "should be able to specify constraint names for column constraints" do
|
440
440
|
@db.create_table!(:items2){primary_key :id, :primary_key_constraint_name=>:foo_pk}
|
441
441
|
@db.create_table!(:items){foreign_key :id, :items2, :unique=>true, :foreign_key_constraint_name => :foo_fk, :unique_constraint_name => :foo_uk, :null=>false}
|
442
442
|
@db.alter_table(:items){drop_constraint :foo_fk, :type=>:foreign_key; drop_constraint :foo_uk, :type=>:unique}
|
443
443
|
@db.alter_table(:items2){drop_constraint :foo_pk, :type=>:primary_key}
|
444
444
|
end
|
445
445
|
|
446
|
-
|
446
|
+
it "should handle foreign keys correctly when creating tables" do
|
447
447
|
@db.create_table!(:items) do
|
448
448
|
primary_key :id
|
449
449
|
foreign_key :item_id, :items
|
450
450
|
unique [:item_id, :id]
|
451
451
|
foreign_key [:id, :item_id], :items, :key=>[:item_id, :id]
|
452
452
|
end
|
453
|
-
@db.table_exists?(:items).
|
454
|
-
@db.schema(:items, :reload=>true).map{|x| x.first}.
|
455
|
-
@ds.columns!.
|
453
|
+
@db.table_exists?(:items).must_equal true
|
454
|
+
@db.schema(:items, :reload=>true).map{|x| x.first}.must_equal [:id, :item_id]
|
455
|
+
@ds.columns!.must_equal [:id, :item_id]
|
456
456
|
end
|
457
457
|
|
458
|
-
|
458
|
+
it "should add columns to tables correctly" do
|
459
459
|
@db.create_table!(:items){Integer :number}
|
460
460
|
@ds.insert(:number=>10)
|
461
461
|
@db.alter_table(:items){add_column :name, String}
|
462
|
-
@db.schema(:items, :reload=>true).map{|x| x.first}.
|
463
|
-
@ds.columns!.
|
464
|
-
@ds.all.
|
462
|
+
@db.schema(:items, :reload=>true).map{|x| x.first}.must_equal [:number, :name]
|
463
|
+
@ds.columns!.must_equal [:number, :name]
|
464
|
+
@ds.all.must_equal [{:number=>10, :name=>nil}]
|
465
465
|
end
|
466
466
|
|
467
467
|
cspecify "should add primary key columns to tables correctly", :derby do
|
468
468
|
@db.create_table!(:items){Integer :number}
|
469
469
|
@ds.insert(:number=>10)
|
470
470
|
@db.alter_table(:items){add_primary_key :id}
|
471
|
-
@db.schema(:items, :reload=>true).map{|x| x.first}.
|
472
|
-
@ds.columns!.
|
473
|
-
@ds.map(:number).
|
474
|
-
proc{@ds.insert(:id=>@ds.map(:id).first)}.
|
471
|
+
@db.schema(:items, :reload=>true).map{|x| x.first}.must_equal [:number, :id]
|
472
|
+
@ds.columns!.must_equal [:number, :id]
|
473
|
+
@ds.map(:number).must_equal [10]
|
474
|
+
proc{@ds.insert(:id=>@ds.map(:id).first)}.must_raise Sequel::UniqueConstraintViolation, Sequel::ConstraintViolation, Sequel::DatabaseError
|
475
475
|
end
|
476
476
|
|
477
|
-
|
477
|
+
it "should drop primary key constraints from tables correctly" do
|
478
478
|
@db.create_table!(:items){Integer :number; primary_key [:number], :name=>:items_pk}
|
479
479
|
@ds.insert(:number=>10)
|
480
480
|
@db.alter_table(:items){drop_constraint :items_pk, :type=>:primary_key}
|
481
|
-
@ds.map(:number).
|
482
|
-
|
481
|
+
@ds.map(:number).must_equal [10]
|
482
|
+
@ds.insert(10)
|
483
483
|
end
|
484
484
|
|
485
|
-
|
485
|
+
it "should add foreign key columns to tables correctly" do
|
486
486
|
@db.create_table!(:items){primary_key :id}
|
487
487
|
@ds.insert
|
488
488
|
i = @ds.get(:id)
|
489
489
|
@db.alter_table(:items){add_foreign_key :item_id, :items}
|
490
|
-
@db.schema(:items, :reload=>true).map{|x| x.first}.
|
491
|
-
@ds.columns!.
|
492
|
-
@ds.all.
|
490
|
+
@db.schema(:items, :reload=>true).map{|x| x.first}.must_equal [:id, :item_id]
|
491
|
+
@ds.columns!.must_equal [:id, :item_id]
|
492
|
+
@ds.all.must_equal [{:id=>i, :item_id=>nil}]
|
493
493
|
end
|
494
494
|
|
495
|
-
|
495
|
+
it "should not allow NULLs in a primary key" do
|
496
496
|
@db.create_table!(:items){String :id, :primary_key=>true}
|
497
|
-
proc{@ds.insert(:id=>nil)}.
|
497
|
+
proc{@ds.insert(:id=>nil)}.must_raise(Sequel::NotNullConstraintViolation, Sequel::ConstraintViolation, Sequel::DatabaseError)
|
498
498
|
end
|
499
499
|
|
500
|
-
|
500
|
+
it "should rename columns correctly" do
|
501
501
|
@db.create_table!(:items){Integer :id}
|
502
502
|
@ds.insert(:id=>10)
|
503
503
|
@db.alter_table(:items){rename_column :id, :id2}
|
504
|
-
@db.schema(:items, :reload=>true).map{|x| x.first}.
|
505
|
-
@ds.columns!.
|
506
|
-
@ds.all.
|
504
|
+
@db.schema(:items, :reload=>true).map{|x| x.first}.must_equal [:id2]
|
505
|
+
@ds.columns!.must_equal [:id2]
|
506
|
+
@ds.all.must_equal [{:id2=>10}]
|
507
507
|
end
|
508
508
|
|
509
|
-
|
509
|
+
it "should rename columns with defaults correctly" do
|
510
510
|
@db.create_table!(:items){String :n, :default=>'blah'}
|
511
511
|
@ds.insert
|
512
512
|
@db.alter_table(:items){rename_column :n, :n2}
|
513
|
-
@db.schema(:items, :reload=>true).map{|x| x.first}.
|
514
|
-
@ds.columns!.
|
513
|
+
@db.schema(:items, :reload=>true).map{|x| x.first}.must_equal [:n2]
|
514
|
+
@ds.columns!.must_equal [:n2]
|
515
515
|
@ds.insert
|
516
|
-
@ds.all.
|
516
|
+
@ds.all.must_equal [{:n2=>'blah'}, {:n2=>'blah'}]
|
517
517
|
end
|
518
518
|
|
519
|
-
|
519
|
+
it "should rename columns with not null constraints" do
|
520
520
|
@db.create_table!(:items, :engine=>:InnoDB){String :n, :null=>false}
|
521
521
|
@ds.insert(:n=>'blah')
|
522
522
|
@db.alter_table(:items){rename_column :n, :n2}
|
523
|
-
@db.schema(:items, :reload=>true).map{|x| x.first}.
|
524
|
-
@ds.columns!.
|
523
|
+
@db.schema(:items, :reload=>true).map{|x| x.first}.must_equal [:n2]
|
524
|
+
@ds.columns!.must_equal [:n2]
|
525
525
|
@ds.insert(:n2=>'blah')
|
526
|
-
@ds.all.
|
527
|
-
proc{@ds.insert(:n=>nil)}.
|
526
|
+
@ds.all.must_equal [{:n2=>'blah'}, {:n2=>'blah'}]
|
527
|
+
proc{@ds.insert(:n=>nil)}.must_raise(Sequel::DatabaseError)
|
528
528
|
end
|
529
529
|
|
530
|
-
|
530
|
+
it "should rename columns when the table is referenced by a foreign key" do
|
531
531
|
@db.create_table!(:items2){primary_key :id; Integer :a}
|
532
532
|
@db.create_table!(:items){Integer :id, :primary_key=>true; foreign_key :items_id, :items2}
|
533
533
|
@db[:items2].insert(:a=>10)
|
@@ -535,106 +535,106 @@ describe "Database schema modifiers" do
|
|
535
535
|
@db.alter_table(:items2){rename_column :a, :b}
|
536
536
|
@db[:items2].insert(:b=>20)
|
537
537
|
@ds.insert(:id=>2)
|
538
|
-
@db[:items2].select_order_map([:id, :b]).
|
538
|
+
@db[:items2].select_order_map([:id, :b]).must_equal [[1, 10], [2, 20]]
|
539
539
|
end
|
540
540
|
|
541
541
|
cspecify "should rename primary_key columns correctly", :db2 do
|
542
542
|
@db.create_table!(:items){Integer :id, :primary_key=>true}
|
543
543
|
@ds.insert(:id=>10)
|
544
544
|
@db.alter_table(:items){rename_column :id, :id2}
|
545
|
-
@db.schema(:items, :reload=>true).map{|x| x.first}.
|
546
|
-
@ds.columns!.
|
547
|
-
@ds.all.
|
545
|
+
@db.schema(:items, :reload=>true).map{|x| x.first}.must_equal [:id2]
|
546
|
+
@ds.columns!.must_equal [:id2]
|
547
|
+
@ds.all.must_equal [{:id2=>10}]
|
548
548
|
end
|
549
549
|
|
550
550
|
cspecify "should set column NULL/NOT NULL correctly", [:jdbc, :db2], [:db2] do
|
551
551
|
@db.create_table!(:items, :engine=>:InnoDB){Integer :id}
|
552
552
|
@ds.insert(:id=>10)
|
553
553
|
@db.alter_table(:items){set_column_allow_null :id, false}
|
554
|
-
@db.schema(:items, :reload=>true).map{|x| x.first}.
|
555
|
-
@ds.columns!.
|
556
|
-
proc{@ds.insert(:id=>nil)}.
|
554
|
+
@db.schema(:items, :reload=>true).map{|x| x.first}.must_equal [:id]
|
555
|
+
@ds.columns!.must_equal [:id]
|
556
|
+
proc{@ds.insert(:id=>nil)}.must_raise(Sequel::NotNullConstraintViolation, Sequel::ConstraintViolation, Sequel::DatabaseError)
|
557
557
|
@db.alter_table(:items){set_column_allow_null :id, true}
|
558
558
|
@ds.insert(:id=>nil)
|
559
|
-
@ds.all.
|
559
|
+
@ds.all.must_equal [{:id=>10}, {:id=>nil}]
|
560
560
|
end
|
561
561
|
|
562
|
-
|
562
|
+
it "should set column defaults correctly" do
|
563
563
|
@db.create_table!(:items){Integer :id}
|
564
564
|
@ds.insert(:id=>10)
|
565
565
|
@db.alter_table(:items){set_column_default :id, 20}
|
566
|
-
@db.schema(:items, :reload=>true).map{|x| x.first}.
|
567
|
-
@ds.columns!.
|
566
|
+
@db.schema(:items, :reload=>true).map{|x| x.first}.must_equal [:id]
|
567
|
+
@ds.columns!.must_equal [:id]
|
568
568
|
@ds.insert
|
569
|
-
@ds.all.
|
569
|
+
@ds.all.must_equal [{:id=>10}, {:id=>20}]
|
570
570
|
end
|
571
571
|
|
572
572
|
cspecify "should set column types correctly", [:jdbc, :db2], [:db2], :oracle do
|
573
573
|
@db.create_table!(:items){Integer :id}
|
574
574
|
@ds.insert(:id=>10)
|
575
575
|
@db.alter_table(:items){set_column_type :id, String}
|
576
|
-
@db.schema(:items, :reload=>true).map{|x| x.first}.
|
577
|
-
@ds.columns!.
|
576
|
+
@db.schema(:items, :reload=>true).map{|x| x.first}.must_equal [:id]
|
577
|
+
@ds.columns!.must_equal [:id]
|
578
578
|
@ds.insert(:id=>'20')
|
579
|
-
@ds.all.
|
579
|
+
@ds.all.must_equal [{:id=>"10"}, {:id=>"20"}]
|
580
580
|
end
|
581
581
|
|
582
582
|
cspecify "should set column types without modifying NULL/NOT NULL", [:jdbc, :db2], [:db2], :oracle, :derby do
|
583
583
|
@db.create_table!(:items){Integer :id, :null=>false, :default=>2}
|
584
|
-
proc{@ds.insert(:id=>nil)}.
|
584
|
+
proc{@ds.insert(:id=>nil)}.must_raise(Sequel::NotNullConstraintViolation, Sequel::ConstraintViolation, Sequel::DatabaseError)
|
585
585
|
@db.alter_table(:items){set_column_type :id, String}
|
586
|
-
proc{@ds.insert(:id=>nil)}.
|
586
|
+
proc{@ds.insert(:id=>nil)}.must_raise(Sequel::NotNullConstraintViolation, Sequel::ConstraintViolation, Sequel::DatabaseError)
|
587
587
|
|
588
588
|
@db.create_table!(:items){Integer :id}
|
589
589
|
@ds.insert(:id=>nil)
|
590
590
|
@db.alter_table(:items){set_column_type :id, String}
|
591
591
|
@ds.insert(:id=>nil)
|
592
|
-
@ds.map(:id).
|
592
|
+
@ds.map(:id).must_equal [nil, nil]
|
593
593
|
end
|
594
594
|
|
595
595
|
cspecify "should set column types without modifying defaults", [:jdbc, :db2], [:db2], :oracle, :derby do
|
596
596
|
@db.create_table!(:items){Integer :id, :default=>0}
|
597
597
|
@ds.insert
|
598
|
-
@ds.map(:id).
|
598
|
+
@ds.map(:id).must_equal [0]
|
599
599
|
@db.alter_table(:items){set_column_type :id, String}
|
600
600
|
@ds.insert
|
601
|
-
@ds.map(:id).
|
601
|
+
@ds.map(:id).must_equal ['0', '0']
|
602
602
|
|
603
603
|
@db.create_table!(:items){String :id, :default=>'a'}
|
604
604
|
@ds.insert
|
605
|
-
@ds.map(:id).
|
605
|
+
@ds.map(:id).must_equal %w'a'
|
606
606
|
@db.alter_table(:items){set_column_type :id, String, :size=>1}
|
607
607
|
@ds.insert
|
608
|
-
@ds.map(:id).
|
608
|
+
@ds.map(:id).must_equal %w'a a'
|
609
609
|
end
|
610
610
|
|
611
|
-
|
611
|
+
it "should add unnamed unique constraints and foreign key table constraints correctly" do
|
612
612
|
@db.create_table!(:items, :engine=>:InnoDB){Integer :id, :null => false; Integer :item_id, :null => false}
|
613
613
|
@db.alter_table(:items) do
|
614
614
|
add_unique_constraint [:item_id, :id]
|
615
615
|
add_foreign_key [:id, :item_id], :items, :key=>[:item_id, :id]
|
616
616
|
end
|
617
|
-
@db.schema(:items, :reload=>true).map{|x| x.first}.
|
618
|
-
@ds.columns!.
|
619
|
-
|
620
|
-
proc{@ds.insert(1, 1)}.
|
621
|
-
proc{@ds.insert(1, 2)}.
|
617
|
+
@db.schema(:items, :reload=>true).map{|x| x.first}.must_equal [:id, :item_id]
|
618
|
+
@ds.columns!.must_equal [:id, :item_id]
|
619
|
+
@ds.insert(1, 1)
|
620
|
+
proc{@ds.insert(1, 1)}.must_raise Sequel::UniqueConstraintViolation, Sequel::ConstraintViolation, Sequel::DatabaseError
|
621
|
+
proc{@ds.insert(1, 2)}.must_raise Sequel::ForeignKeyConstraintViolation, Sequel::ConstraintViolation, Sequel::DatabaseError
|
622
622
|
end
|
623
623
|
|
624
|
-
|
624
|
+
it "should add named unique constraints and foreign key table constraints correctly" do
|
625
625
|
@db.create_table!(:items, :engine=>:InnoDB){Integer :id, :null=>false; Integer :item_id, :null=>false}
|
626
626
|
@db.alter_table(:items) do
|
627
627
|
add_unique_constraint [:item_id, :id], :name=>:unique_iii
|
628
628
|
add_foreign_key [:id, :item_id], :items, :key=>[:item_id, :id], :name=>:fk_iii
|
629
629
|
end
|
630
|
-
@db.schema(:items, :reload=>true).map{|x| x.first}.
|
631
|
-
@ds.columns!.
|
632
|
-
|
633
|
-
proc{@ds.insert(1, 1)}.
|
634
|
-
proc{@ds.insert(1, 2)}.
|
630
|
+
@db.schema(:items, :reload=>true).map{|x| x.first}.must_equal [:id, :item_id]
|
631
|
+
@ds.columns!.must_equal [:id, :item_id]
|
632
|
+
@ds.insert(1, 1)
|
633
|
+
proc{@ds.insert(1, 1)}.must_raise Sequel::UniqueConstraintViolation, Sequel::ConstraintViolation, Sequel::DatabaseError
|
634
|
+
proc{@ds.insert(1, 2)}.must_raise Sequel::ForeignKeyConstraintViolation, Sequel::ConstraintViolation, Sequel::DatabaseError
|
635
635
|
end
|
636
636
|
|
637
|
-
|
637
|
+
it "should drop unique constraints and foreign key table constraints correctly" do
|
638
638
|
@db.create_table!(:items) do
|
639
639
|
Integer :id
|
640
640
|
Integer :item_id
|
@@ -645,33 +645,33 @@ describe "Database schema modifiers" do
|
|
645
645
|
drop_constraint(:items_fk, :type=>:foreign_key)
|
646
646
|
drop_constraint(:items_uk, :type=>:unique)
|
647
647
|
end
|
648
|
-
@db.schema(:items, :reload=>true).map{|x| x.first}.
|
649
|
-
@ds.columns!.
|
650
|
-
|
651
|
-
|
648
|
+
@db.schema(:items, :reload=>true).map{|x| x.first}.must_equal [:id, :item_id]
|
649
|
+
@ds.columns!.must_equal [:id, :item_id]
|
650
|
+
@ds.insert(1, 2)
|
651
|
+
@ds.insert(1, 2)
|
652
652
|
end
|
653
653
|
|
654
|
-
|
654
|
+
it "should remove columns from tables correctly" do
|
655
655
|
@db.create_table!(:items) do
|
656
656
|
primary_key :id
|
657
657
|
Integer :i
|
658
658
|
end
|
659
659
|
@ds.insert(:i=>10)
|
660
660
|
@db.drop_column(:items, :i)
|
661
|
-
@db.schema(:items, :reload=>true).map{|x| x.first}.
|
661
|
+
@db.schema(:items, :reload=>true).map{|x| x.first}.must_equal [:id]
|
662
662
|
end
|
663
663
|
|
664
|
-
|
664
|
+
it "should remove columns with defaults from tables correctly" do
|
665
665
|
@db.create_table!(:items) do
|
666
666
|
primary_key :id
|
667
667
|
Integer :i, :default=>20
|
668
668
|
end
|
669
669
|
@ds.insert(:i=>10)
|
670
670
|
@db.drop_column(:items, :i)
|
671
|
-
@db.schema(:items, :reload=>true).map{|x| x.first}.
|
671
|
+
@db.schema(:items, :reload=>true).map{|x| x.first}.must_equal [:id]
|
672
672
|
end
|
673
673
|
|
674
|
-
|
674
|
+
it "should remove foreign key columns from tables correctly" do
|
675
675
|
@db.create_table!(:items, :engine=>:InnoDB) do
|
676
676
|
primary_key :id
|
677
677
|
Integer :i
|
@@ -679,25 +679,25 @@ describe "Database schema modifiers" do
|
|
679
679
|
end
|
680
680
|
@ds.insert(:i=>10)
|
681
681
|
@db.alter_table(:items){drop_foreign_key :item_id}
|
682
|
-
@db.schema(:items, :reload=>true).map{|x| x.first}.
|
682
|
+
@db.schema(:items, :reload=>true).map{|x| x.first}.must_equal [:id, :i]
|
683
683
|
end if DB.supports_foreign_key_parsing?
|
684
684
|
|
685
|
-
|
685
|
+
it "should remove multiple columns in a single alter_table block" do
|
686
686
|
@db.create_table!(:items) do
|
687
687
|
primary_key :id
|
688
688
|
String :name
|
689
689
|
Integer :number
|
690
690
|
end
|
691
691
|
@ds.insert(:number=>10)
|
692
|
-
@db.schema(:items, :reload=>true).map{|x| x.first}.
|
692
|
+
@db.schema(:items, :reload=>true).map{|x| x.first}.must_equal [:id, :name, :number]
|
693
693
|
@db.alter_table(:items) do
|
694
694
|
drop_column :name
|
695
695
|
drop_column :number
|
696
696
|
end
|
697
|
-
@db.schema(:items, :reload=>true).map{|x| x.first}.
|
697
|
+
@db.schema(:items, :reload=>true).map{|x| x.first}.must_equal [:id]
|
698
698
|
end
|
699
699
|
|
700
|
-
cspecify "should work correctly with many operations in a single alter_table call", [:jdbc, :db2], [:db2]
|
700
|
+
cspecify "should work correctly with many operations in a single alter_table call", [:jdbc, :db2], [:db2] do
|
701
701
|
@db.create_table!(:items) do
|
702
702
|
primary_key :id
|
703
703
|
String :name2
|
@@ -714,33 +714,33 @@ describe "Database schema modifiers" do
|
|
714
714
|
set_column_default :name, 'A13'
|
715
715
|
add_constraint :foo, Sequel.like(:name, 'A%')
|
716
716
|
end
|
717
|
-
@db[:items].first.
|
717
|
+
@db[:items].first.must_equal(:id=>1, :name=>'A12', :number=>nil)
|
718
718
|
@db[:items].delete
|
719
|
-
proc{@db[:items].insert(:name=>nil)}.
|
719
|
+
proc{@db[:items].insert(:name=>nil)}.must_raise(Sequel::NotNullConstraintViolation, Sequel::ConstraintViolation, Sequel::DatabaseError)
|
720
720
|
@db[:items].insert(:number=>1)
|
721
|
-
@db[:items].get(:name).
|
721
|
+
@db[:items].get(:name).must_equal 'A13'
|
722
722
|
end
|
723
723
|
|
724
|
-
|
724
|
+
it "should support deferrable foreign key constraints" do
|
725
725
|
@db.create_table!(:items2){Integer :id, :primary_key=>true}
|
726
726
|
@db.create_table!(:items){foreign_key :id, :items2, :deferrable=>true}
|
727
|
-
proc{@db[:items].insert(1)}.
|
728
|
-
|
727
|
+
proc{@db[:items].insert(1)}.must_raise(Sequel::ForeignKeyConstraintViolation, Sequel::ConstraintViolation, Sequel::DatabaseError)
|
728
|
+
@db.transaction{proc{@db[:items].insert(1)}}.must_raise(Sequel::ForeignKeyConstraintViolation, Sequel::ConstraintViolation, Sequel::DatabaseError)
|
729
729
|
end if DB.supports_deferrable_foreign_key_constraints?
|
730
730
|
|
731
|
-
|
731
|
+
it "should support deferrable unique constraints when creating or altering tables" do
|
732
732
|
@db.create_table!(:items){Integer :t; unique [:t], :name=>:atest_def, :deferrable=>true, :using=>:btree}
|
733
733
|
@db[:items].insert(1)
|
734
734
|
@db[:items].insert(2)
|
735
|
-
proc{@db[:items].insert(2)}.
|
736
|
-
|
735
|
+
proc{@db[:items].insert(2)}.must_raise(Sequel::DatabaseError, Sequel::UniqueConstraintViolation, Sequel::ConstraintViolation, Sequel::DatabaseError)
|
736
|
+
@db.transaction{proc{@db[:items].insert(2)}}.must_raise(Sequel::DatabaseError, Sequel::UniqueConstraintViolation, Sequel::ConstraintViolation, Sequel::DatabaseError)
|
737
737
|
|
738
738
|
@db.create_table!(:items){Integer :t}
|
739
739
|
@db.alter_table(:items){add_unique_constraint [:t], :name=>:atest_def, :deferrable=>true, :using=>:btree}
|
740
740
|
@db[:items].insert(1)
|
741
741
|
@db[:items].insert(2)
|
742
|
-
proc{@db[:items].insert(2)}.
|
743
|
-
|
742
|
+
proc{@db[:items].insert(2)}.must_raise(Sequel::DatabaseError, Sequel::UniqueConstraintViolation, Sequel::ConstraintViolation, Sequel::DatabaseError)
|
743
|
+
@db.transaction{proc{@db[:items].insert(2)}}.must_raise(Sequel::DatabaseError, Sequel::UniqueConstraintViolation, Sequel::ConstraintViolation, Sequel::DatabaseError)
|
744
744
|
end if DB.supports_deferrable_constraints?
|
745
745
|
end
|
746
746
|
|
@@ -765,18 +765,18 @@ describe "Database#tables" do
|
|
765
765
|
@db.drop_table :sequel_test_table
|
766
766
|
end
|
767
767
|
|
768
|
-
|
768
|
+
it "should return an array of symbols" do
|
769
769
|
ts = @db.tables
|
770
|
-
ts.
|
771
|
-
ts.each{|t| t.
|
772
|
-
ts.
|
773
|
-
ts.
|
770
|
+
ts.must_be_kind_of(Array)
|
771
|
+
ts.each{|t| t.must_be_kind_of(Symbol)}
|
772
|
+
ts.must_include(:sequel_test_table)
|
773
|
+
ts.wont_include(:sequel_test_view)
|
774
774
|
end
|
775
775
|
|
776
|
-
|
776
|
+
it "should respect the database's identifier_output_method" do
|
777
777
|
@db.identifier_output_method = :xxxxx
|
778
778
|
@db.identifier_input_method = :xxxxx
|
779
|
-
@db.tables.each{|t| t.to_s.
|
779
|
+
@db.tables.each{|t| t.to_s.must_match(/\Ax{5}\d+\z/)}
|
780
780
|
end
|
781
781
|
end if DB.supports_table_listing?
|
782
782
|
|
@@ -801,17 +801,17 @@ describe "Database#views" do
|
|
801
801
|
@db.drop_table :sequel_test_table
|
802
802
|
end
|
803
803
|
|
804
|
-
|
804
|
+
it "should return an array of symbols" do
|
805
805
|
ts = @db.views
|
806
|
-
ts.
|
807
|
-
ts.each{|t| t.
|
808
|
-
ts.
|
809
|
-
ts.
|
806
|
+
ts.must_be_kind_of(Array)
|
807
|
+
ts.each{|t| t.must_be_kind_of(Symbol)}
|
808
|
+
ts.wont_include(:sequel_test_table)
|
809
|
+
ts.must_include(:sequel_test_view)
|
810
810
|
end
|
811
811
|
|
812
|
-
|
812
|
+
it "should respect the database's identifier_output_method" do
|
813
813
|
@db.identifier_output_method = :xxxxx
|
814
814
|
@db.identifier_input_method = :xxxxx
|
815
|
-
@db.views.each{|t| t.to_s.
|
815
|
+
@db.views.each{|t| t.to_s.must_match(/\Ax{5}\d+\z/)}
|
816
816
|
end
|
817
817
|
end if DB.supports_view_listing?
|