sequel 4.22.0 → 4.23.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.
- 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
|
@@ -20,82 +20,82 @@ describe Sequel::Schema::Generator do
|
|
|
20
20
|
end
|
|
21
21
|
|
|
22
22
|
it "should respond to everything" do
|
|
23
|
-
@generator.respond_to?(:foo).
|
|
23
|
+
@generator.respond_to?(:foo).must_equal true
|
|
24
24
|
end if RUBY_VERSION >= '1.9'
|
|
25
25
|
|
|
26
26
|
it "should primary key column first" do
|
|
27
|
-
@columns.first[:name].
|
|
28
|
-
@columns.first[:primary_key].
|
|
29
|
-
@columns[3][:name].
|
|
30
|
-
@columns[3][:primary_key].
|
|
27
|
+
@columns.first[:name].must_equal :id
|
|
28
|
+
@columns.first[:primary_key].must_equal true
|
|
29
|
+
@columns[3][:name].must_equal :parent_id
|
|
30
|
+
@columns[3][:primary_key].must_equal nil
|
|
31
31
|
end
|
|
32
32
|
|
|
33
33
|
it "counts definitions correctly" do
|
|
34
|
-
@columns.size.
|
|
35
|
-
@indexes.size.
|
|
36
|
-
@constraints.size.
|
|
34
|
+
@columns.size.must_equal 6
|
|
35
|
+
@indexes.size.must_equal 2
|
|
36
|
+
@constraints.size.must_equal 4
|
|
37
37
|
end
|
|
38
38
|
|
|
39
39
|
it "retrieves primary key name" do
|
|
40
|
-
@generator.primary_key_name.
|
|
40
|
+
@generator.primary_key_name.must_equal :id
|
|
41
41
|
end
|
|
42
42
|
|
|
43
43
|
it "keeps columns in order" do
|
|
44
|
-
@columns[1][:name].
|
|
45
|
-
@columns[1][:type].
|
|
46
|
-
@columns[2][:name].
|
|
47
|
-
@columns[2][:type].
|
|
44
|
+
@columns[1][:name].must_equal :title
|
|
45
|
+
@columns[1][:type].must_equal :string
|
|
46
|
+
@columns[2][:name].must_equal :body
|
|
47
|
+
@columns[2][:type].must_equal :text
|
|
48
48
|
end
|
|
49
49
|
|
|
50
50
|
it "creates foreign key column" do
|
|
51
|
-
@columns[3][:name].
|
|
52
|
-
@columns[3][:type].
|
|
53
|
-
@columns[4][:name].
|
|
54
|
-
@columns[4][:type].
|
|
51
|
+
@columns[3][:name].must_equal :parent_id
|
|
52
|
+
@columns[3][:type].must_equal Integer
|
|
53
|
+
@columns[4][:name].must_equal :node_id
|
|
54
|
+
@columns[4][:type].must_equal Integer
|
|
55
55
|
end
|
|
56
56
|
|
|
57
57
|
it "creates deferrable altered foreign key column" do
|
|
58
|
-
@columns[5][:name].
|
|
59
|
-
@columns[5][:type].
|
|
60
|
-
@columns[5][:deferrable].
|
|
58
|
+
@columns[5][:name].must_equal :deferrable_node_id
|
|
59
|
+
@columns[5][:type].must_equal Integer
|
|
60
|
+
@columns[5][:deferrable].must_equal true
|
|
61
61
|
end
|
|
62
62
|
|
|
63
63
|
it "uses table for foreign key columns, if specified" do
|
|
64
|
-
@columns[3][:table].
|
|
65
|
-
@columns[4][:table].
|
|
66
|
-
@constraints[3][:table].
|
|
64
|
+
@columns[3][:table].must_equal nil
|
|
65
|
+
@columns[4][:table].must_equal :nodes
|
|
66
|
+
@constraints[3][:table].must_equal :nodes_props
|
|
67
67
|
end
|
|
68
68
|
|
|
69
69
|
it "finds columns" do
|
|
70
70
|
[:title, :body, :parent_id, :id].each do |col|
|
|
71
|
-
@generator.has_column?(col).
|
|
71
|
+
@generator.has_column?(col).must_equal true
|
|
72
72
|
end
|
|
73
|
-
@generator.has_column?(:foo).
|
|
73
|
+
@generator.has_column?(:foo).wont_equal true
|
|
74
74
|
end
|
|
75
75
|
|
|
76
76
|
it "creates constraints" do
|
|
77
|
-
@constraints[0][:name].
|
|
78
|
-
@constraints[0][:type].
|
|
79
|
-
@constraints[0][:check].
|
|
77
|
+
@constraints[0][:name].must_equal nil
|
|
78
|
+
@constraints[0][:type].must_equal :check
|
|
79
|
+
@constraints[0][:check].must_equal ['price > 100']
|
|
80
80
|
|
|
81
|
-
@constraints[1][:name].
|
|
82
|
-
@constraints[1][:type].
|
|
83
|
-
@constraints[1][:check].
|
|
81
|
+
@constraints[1][:name].must_equal :xxx
|
|
82
|
+
@constraints[1][:type].must_equal :check
|
|
83
|
+
@constraints[1][:check].must_be_kind_of Proc
|
|
84
84
|
|
|
85
|
-
@constraints[2][:name].
|
|
86
|
-
@constraints[2][:type].
|
|
87
|
-
@constraints[2][:columns].
|
|
85
|
+
@constraints[2][:name].must_equal :cpk
|
|
86
|
+
@constraints[2][:type].must_equal :primary_key
|
|
87
|
+
@constraints[2][:columns].must_equal [ :title, :parent_id ]
|
|
88
88
|
|
|
89
|
-
@constraints[3][:name].
|
|
90
|
-
@constraints[3][:type].
|
|
91
|
-
@constraints[3][:columns].
|
|
92
|
-
@constraints[3][:table].
|
|
89
|
+
@constraints[3][:name].must_equal :cfk
|
|
90
|
+
@constraints[3][:type].must_equal :foreign_key
|
|
91
|
+
@constraints[3][:columns].must_equal [ :node_id, :prop_id ]
|
|
92
|
+
@constraints[3][:table].must_equal :nodes_props
|
|
93
93
|
end
|
|
94
94
|
|
|
95
95
|
it "creates indexes" do
|
|
96
|
-
@indexes[0][:columns].
|
|
97
|
-
@indexes[1][:columns].
|
|
98
|
-
@indexes[1][:columns].
|
|
96
|
+
@indexes[0][:columns].must_include(:title)
|
|
97
|
+
@indexes[1][:columns].must_include(:title)
|
|
98
|
+
@indexes[1][:columns].must_include(:body)
|
|
99
99
|
end
|
|
100
100
|
end
|
|
101
101
|
|
|
@@ -128,8 +128,8 @@ describe Sequel::Schema::AlterTableGenerator do
|
|
|
128
128
|
end
|
|
129
129
|
end
|
|
130
130
|
|
|
131
|
-
|
|
132
|
-
@generator.operations.
|
|
131
|
+
it "should generate operation records" do
|
|
132
|
+
@generator.operations.must_equal [
|
|
133
133
|
{:op => :add_column, :name => :aaa, :type => :text},
|
|
134
134
|
{:op => :drop_column, :name => :bbb},
|
|
135
135
|
{:op => :rename_column, :name => :ccc, :new_name => :ho},
|
|
@@ -174,6 +174,6 @@ describe "Sequel::Schema::Generator generic type methods" do
|
|
|
174
174
|
File :k
|
|
175
175
|
TrueClass :l
|
|
176
176
|
FalseClass :m
|
|
177
|
-
end.columns.map{|c| c[:type]}.
|
|
177
|
+
end.columns.map{|c| c[:type]}.must_equal [String, Integer, Fixnum, Bignum, Float, BigDecimal, Date, DateTime, Time, Numeric, File, TrueClass, FalseClass]
|
|
178
178
|
end
|
|
179
179
|
end
|
data/spec/core/schema_spec.rb
CHANGED
|
@@ -5,47 +5,47 @@ describe "DB#create_table" do
|
|
|
5
5
|
@db = Sequel.mock
|
|
6
6
|
end
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
it "should accept the table name" do
|
|
9
9
|
@db.create_table(:cats) {}
|
|
10
|
-
@db.sqls.
|
|
10
|
+
@db.sqls.must_equal ['CREATE TABLE cats ()']
|
|
11
11
|
end
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
it "should accept the table name in multiple formats" do
|
|
14
14
|
@db.create_table(:cats__cats) {}
|
|
15
15
|
@db.create_table("cats__cats1") {}
|
|
16
16
|
@db.create_table(Sequel.identifier(:cats__cats2)) {}
|
|
17
17
|
@db.create_table(Sequel.qualify(:cats3, :cats)) {}
|
|
18
|
-
@db.sqls.
|
|
18
|
+
@db.sqls.must_equal ['CREATE TABLE cats.cats ()', 'CREATE TABLE cats__cats1 ()', 'CREATE TABLE cats__cats2 ()', 'CREATE TABLE cats3.cats ()']
|
|
19
19
|
end
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
proc{@db.create_table(1) {}}.
|
|
23
|
-
proc{@db.create_table(Sequel.as(:cats, :c)) {}}.
|
|
21
|
+
it "should raise an error if the table name argument is not valid" do
|
|
22
|
+
proc{@db.create_table(1) {}}.must_raise(Sequel::Error)
|
|
23
|
+
proc{@db.create_table(Sequel.as(:cats, :c)) {}}.must_raise(Sequel::Error)
|
|
24
24
|
end
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
it "should remove cached schema entry" do
|
|
27
27
|
@db.instance_variable_set(:@schemas, {'cats'=>[]})
|
|
28
28
|
@db.create_table(:cats){Integer :a}
|
|
29
|
-
@db.instance_variable_get(:@schemas).
|
|
29
|
+
@db.instance_variable_get(:@schemas).must_be :empty?
|
|
30
30
|
end
|
|
31
31
|
|
|
32
|
-
|
|
32
|
+
it "should accept multiple columns" do
|
|
33
33
|
@db.create_table(:cats) do
|
|
34
34
|
column :id, :integer
|
|
35
35
|
column :name, :text
|
|
36
36
|
end
|
|
37
|
-
@db.sqls.
|
|
37
|
+
@db.sqls.must_equal ['CREATE TABLE cats (id integer, name text)']
|
|
38
38
|
end
|
|
39
39
|
|
|
40
|
-
|
|
40
|
+
it "should accept method calls as data types" do
|
|
41
41
|
@db.create_table(:cats) do
|
|
42
42
|
integer :id
|
|
43
43
|
text :name
|
|
44
44
|
end
|
|
45
|
-
@db.sqls.
|
|
45
|
+
@db.sqls.must_equal ['CREATE TABLE cats (id integer, name text)']
|
|
46
46
|
end
|
|
47
47
|
|
|
48
|
-
|
|
48
|
+
it "should transform types given as ruby classes to database-specific types" do
|
|
49
49
|
@db.create_table(:cats) do
|
|
50
50
|
String :a
|
|
51
51
|
Integer :b
|
|
@@ -64,10 +64,10 @@ describe "DB#create_table" do
|
|
|
64
64
|
primary_key :o, :type=>String
|
|
65
65
|
foreign_key :p, :f, :type=>Date
|
|
66
66
|
end
|
|
67
|
-
@db.sqls.
|
|
67
|
+
@db.sqls.must_equal ['CREATE TABLE cats (o varchar(255) PRIMARY KEY AUTOINCREMENT, a varchar(255), b integer, c integer, d bigint, e double precision, f numeric, g date, h timestamp, i timestamp, j numeric, k blob, l boolean, m boolean, n integer, p date REFERENCES f)']
|
|
68
68
|
end
|
|
69
69
|
|
|
70
|
-
|
|
70
|
+
it "should transform types given as ruby classes to database-specific types" do
|
|
71
71
|
@db.default_string_column_size = 50
|
|
72
72
|
@db.create_table(:cats) do
|
|
73
73
|
String :a
|
|
@@ -78,10 +78,10 @@ describe "DB#create_table" do
|
|
|
78
78
|
varchar :a6
|
|
79
79
|
varchar :a7, :size=>13
|
|
80
80
|
end
|
|
81
|
-
@db.sqls.
|
|
81
|
+
@db.sqls.must_equal ['CREATE TABLE cats (a varchar(50), a2 varchar(13), a3 char(50), a4 char(13), a5 text, a6 varchar(50), a7 varchar(13))']
|
|
82
82
|
end
|
|
83
83
|
|
|
84
|
-
|
|
84
|
+
it "should allow the use of modifiers with ruby class types" do
|
|
85
85
|
@db.create_table(:cats) do
|
|
86
86
|
String :a, :size=>50
|
|
87
87
|
String :b, :text=>true
|
|
@@ -89,599 +89,597 @@ describe "DB#create_table" do
|
|
|
89
89
|
Time :d, :only_time=>true
|
|
90
90
|
BigDecimal :e, :size=>[11,2]
|
|
91
91
|
end
|
|
92
|
-
@db.sqls.
|
|
92
|
+
@db.sqls.must_equal ['CREATE TABLE cats (a varchar(50), b text, c char(40), d time, e numeric(11, 2))']
|
|
93
93
|
end
|
|
94
94
|
|
|
95
|
-
|
|
96
|
-
proc{@db.create_table(:cats){column :a, Class}}.
|
|
95
|
+
it "should raise an error if you use a ruby class that isn't handled" do
|
|
96
|
+
proc{@db.create_table(:cats){column :a, Class}}.must_raise(Sequel::Error)
|
|
97
97
|
end
|
|
98
98
|
|
|
99
|
-
|
|
99
|
+
it "should accept primary key definition" do
|
|
100
100
|
@db.create_table(:cats) do
|
|
101
101
|
primary_key :id
|
|
102
102
|
end
|
|
103
|
-
@db.sqls.
|
|
103
|
+
@db.sqls.must_equal ['CREATE TABLE cats (id integer PRIMARY KEY AUTOINCREMENT)']
|
|
104
104
|
|
|
105
105
|
@db.create_table(:cats) do
|
|
106
106
|
primary_key :id, :serial, :auto_increment => false
|
|
107
107
|
end
|
|
108
|
-
@db.sqls.
|
|
108
|
+
@db.sqls.must_equal ['CREATE TABLE cats (id serial PRIMARY KEY)']
|
|
109
109
|
|
|
110
110
|
@db.create_table(:cats) do
|
|
111
111
|
primary_key :id, :type => :serial, :auto_increment => false
|
|
112
112
|
end
|
|
113
|
-
@db.sqls.
|
|
113
|
+
@db.sqls.must_equal ['CREATE TABLE cats (id serial PRIMARY KEY)']
|
|
114
114
|
end
|
|
115
115
|
|
|
116
|
-
|
|
116
|
+
it "should allow naming primary key constraint with :primary_key_constraint_name option" do
|
|
117
117
|
@db.create_table(:cats) do
|
|
118
118
|
primary_key :id, :primary_key_constraint_name=>:foo
|
|
119
119
|
end
|
|
120
|
-
@db.sqls.
|
|
120
|
+
@db.sqls.must_equal ['CREATE TABLE cats (id integer CONSTRAINT foo PRIMARY KEY AUTOINCREMENT)']
|
|
121
121
|
end
|
|
122
122
|
|
|
123
|
-
|
|
123
|
+
it "should handling splitting named column constraints into table constraints if unsupported" do
|
|
124
124
|
def @db.supports_named_column_constraints?; false end
|
|
125
125
|
@db.create_table(:cats) do
|
|
126
126
|
primary_key :id, :primary_key_constraint_name=>:foo
|
|
127
127
|
foreign_key :cat_id, :cats, :unique=>true, :unique_constraint_name=>:bar, :foreign_key_constraint_name=>:baz, :deferrable=>true, :key=>:foo_id, :on_delete=>:cascade, :on_update=>:restrict
|
|
128
128
|
end
|
|
129
|
-
@db.sqls.
|
|
129
|
+
@db.sqls.must_equal ['CREATE TABLE cats (id integer AUTOINCREMENT, cat_id integer, CONSTRAINT foo PRIMARY KEY (id), CONSTRAINT baz FOREIGN KEY (cat_id) REFERENCES cats(foo_id) ON DELETE CASCADE ON UPDATE RESTRICT DEFERRABLE INITIALLY DEFERRED, CONSTRAINT bar UNIQUE (cat_id))']
|
|
130
130
|
end
|
|
131
131
|
|
|
132
|
-
|
|
132
|
+
it "should accept and literalize default values" do
|
|
133
133
|
@db.create_table(:cats) do
|
|
134
134
|
integer :id, :default => 123
|
|
135
135
|
text :name, :default => "abc'def"
|
|
136
136
|
end
|
|
137
|
-
@db.sqls.
|
|
137
|
+
@db.sqls.must_equal ["CREATE TABLE cats (id integer DEFAULT 123, name text DEFAULT 'abc''def')"]
|
|
138
138
|
end
|
|
139
139
|
|
|
140
|
-
|
|
140
|
+
it "should accept not null definition" do
|
|
141
141
|
@db.create_table(:cats) do
|
|
142
142
|
integer :id
|
|
143
143
|
text :name, :null => false
|
|
144
144
|
text :name2, :allow_null => false
|
|
145
145
|
end
|
|
146
|
-
@db.sqls.
|
|
146
|
+
@db.sqls.must_equal ["CREATE TABLE cats (id integer, name text NOT NULL, name2 text NOT NULL)"]
|
|
147
147
|
end
|
|
148
148
|
|
|
149
|
-
|
|
149
|
+
it "should accept null definition" do
|
|
150
150
|
@db.create_table(:cats) do
|
|
151
151
|
integer :id
|
|
152
152
|
text :name, :null => true
|
|
153
153
|
text :name2, :allow_null => true
|
|
154
154
|
end
|
|
155
|
-
@db.sqls.
|
|
155
|
+
@db.sqls.must_equal ["CREATE TABLE cats (id integer, name text NULL, name2 text NULL)"]
|
|
156
156
|
end
|
|
157
157
|
|
|
158
|
-
|
|
158
|
+
it "should accept unique definition" do
|
|
159
159
|
@db.create_table(:cats) do
|
|
160
160
|
integer :id
|
|
161
161
|
text :name, :unique => true
|
|
162
162
|
end
|
|
163
|
-
@db.sqls.
|
|
163
|
+
@db.sqls.must_equal ["CREATE TABLE cats (id integer, name text UNIQUE)"]
|
|
164
164
|
end
|
|
165
165
|
|
|
166
|
-
|
|
166
|
+
it "should allow naming unique constraint with :unique_constraint_name option" do
|
|
167
167
|
@db.create_table(:cats) do
|
|
168
168
|
text :name, :unique => true, :unique_constraint_name=>:foo
|
|
169
169
|
end
|
|
170
|
-
@db.sqls.
|
|
170
|
+
@db.sqls.must_equal ["CREATE TABLE cats (name text CONSTRAINT foo UNIQUE)"]
|
|
171
171
|
end
|
|
172
172
|
|
|
173
|
-
|
|
173
|
+
it "should handle not deferred unique constraints" do
|
|
174
174
|
@db.create_table(:cats) do
|
|
175
175
|
integer :id
|
|
176
176
|
text :name
|
|
177
177
|
unique :name, :deferrable=>false
|
|
178
178
|
end
|
|
179
|
-
@db.sqls.
|
|
179
|
+
@db.sqls.must_equal ["CREATE TABLE cats (id integer, name text, UNIQUE (name) NOT DEFERRABLE)"]
|
|
180
180
|
end
|
|
181
181
|
|
|
182
|
-
|
|
182
|
+
it "should handle deferred unique constraints" do
|
|
183
183
|
@db.create_table(:cats) do
|
|
184
184
|
integer :id
|
|
185
185
|
text :name
|
|
186
186
|
unique :name, :deferrable=>true
|
|
187
187
|
end
|
|
188
|
-
@db.sqls.
|
|
188
|
+
@db.sqls.must_equal ["CREATE TABLE cats (id integer, name text, UNIQUE (name) DEFERRABLE INITIALLY DEFERRED)"]
|
|
189
189
|
end
|
|
190
190
|
|
|
191
|
-
|
|
191
|
+
it "should handle deferred initially immediate unique constraints" do
|
|
192
192
|
@db.create_table(:cats) do
|
|
193
193
|
integer :id
|
|
194
194
|
text :name
|
|
195
195
|
unique :name, :deferrable=>:immediate
|
|
196
196
|
end
|
|
197
|
-
@db.sqls.
|
|
197
|
+
@db.sqls.must_equal ["CREATE TABLE cats (id integer, name text, UNIQUE (name) DEFERRABLE INITIALLY IMMEDIATE)"]
|
|
198
198
|
end
|
|
199
199
|
|
|
200
|
-
|
|
200
|
+
it "should accept unsigned definition" do
|
|
201
201
|
@db.create_table(:cats) do
|
|
202
202
|
integer :value, :unsigned => true
|
|
203
203
|
end
|
|
204
|
-
@db.sqls.
|
|
204
|
+
@db.sqls.must_equal ["CREATE TABLE cats (value integer UNSIGNED)"]
|
|
205
205
|
end
|
|
206
206
|
|
|
207
|
-
|
|
207
|
+
it "should accept [SET|ENUM](...) types" do
|
|
208
208
|
@db.create_table(:cats) do
|
|
209
209
|
set :color, :elements => ['black', 'tricolor', 'grey']
|
|
210
210
|
end
|
|
211
|
-
@db.sqls.
|
|
211
|
+
@db.sqls.must_equal ["CREATE TABLE cats (color set('black', 'tricolor', 'grey'))"]
|
|
212
212
|
end
|
|
213
213
|
|
|
214
|
-
|
|
214
|
+
it "should accept varchar size" do
|
|
215
215
|
@db.create_table(:cats) do
|
|
216
216
|
varchar :name
|
|
217
217
|
end
|
|
218
|
-
@db.sqls.
|
|
218
|
+
@db.sqls.must_equal ["CREATE TABLE cats (name varchar(255))"]
|
|
219
219
|
@db.create_table(:cats) do
|
|
220
220
|
varchar :name, :size => 51
|
|
221
221
|
end
|
|
222
|
-
@db.sqls.
|
|
222
|
+
@db.sqls.must_equal ["CREATE TABLE cats (name varchar(51))"]
|
|
223
223
|
end
|
|
224
224
|
|
|
225
|
-
|
|
225
|
+
it "should use double precision for double type" do
|
|
226
226
|
@db.create_table(:cats) do
|
|
227
227
|
double :name
|
|
228
228
|
end
|
|
229
|
-
@db.sqls.
|
|
229
|
+
@db.sqls.must_equal ["CREATE TABLE cats (name double precision)"]
|
|
230
230
|
end
|
|
231
231
|
|
|
232
|
-
|
|
232
|
+
it "should accept foreign keys without options" do
|
|
233
233
|
@db.create_table(:cats) do
|
|
234
234
|
foreign_key :project_id
|
|
235
235
|
end
|
|
236
|
-
@db.sqls.
|
|
236
|
+
@db.sqls.must_equal ["CREATE TABLE cats (project_id integer)"]
|
|
237
237
|
end
|
|
238
238
|
|
|
239
|
-
|
|
239
|
+
it "should accept foreign keys with options" do
|
|
240
240
|
@db.create_table(:cats) do
|
|
241
241
|
foreign_key :project_id, :table => :projects
|
|
242
242
|
end
|
|
243
|
-
@db.sqls.
|
|
243
|
+
@db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects)"]
|
|
244
244
|
end
|
|
245
245
|
|
|
246
|
-
|
|
246
|
+
it "should accept foreign keys with separate table argument" do
|
|
247
247
|
@db.create_table(:cats) do
|
|
248
248
|
foreign_key :project_id, :projects, :default=>3
|
|
249
249
|
end
|
|
250
|
-
@db.sqls.
|
|
250
|
+
@db.sqls.must_equal ["CREATE TABLE cats (project_id integer DEFAULT 3 REFERENCES projects)"]
|
|
251
251
|
end
|
|
252
252
|
|
|
253
|
-
|
|
253
|
+
it "should allowing naming foreign key constraint with :foreign_key_constraint_name option" do
|
|
254
254
|
@db.create_table(:cats) do
|
|
255
255
|
foreign_key :project_id, :projects, :foreign_key_constraint_name=>:foo
|
|
256
256
|
end
|
|
257
|
-
@db.sqls.
|
|
257
|
+
@db.sqls.must_equal ["CREATE TABLE cats (project_id integer CONSTRAINT foo REFERENCES projects)"]
|
|
258
258
|
end
|
|
259
259
|
|
|
260
|
-
|
|
261
|
-
proc{@db.create_table(:cats){foreign_key :project_id, Object.new, :default=>3}}.
|
|
260
|
+
it "should raise an error if the table argument to foreign_key isn't a hash, symbol, or nil" do
|
|
261
|
+
proc{@db.create_table(:cats){foreign_key :project_id, Object.new, :default=>3}}.must_raise(Sequel::Error)
|
|
262
262
|
end
|
|
263
263
|
|
|
264
|
-
|
|
264
|
+
it "should accept foreign keys with arbitrary keys" do
|
|
265
265
|
@db.create_table(:cats) do
|
|
266
266
|
foreign_key :project_id, :table => :projects, :key => :id
|
|
267
267
|
end
|
|
268
|
-
@db.sqls.
|
|
268
|
+
@db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects(id))"]
|
|
269
269
|
|
|
270
270
|
@db.create_table(:cats) do
|
|
271
271
|
foreign_key :project_id, :table => :projects, :key => :zzz
|
|
272
272
|
end
|
|
273
|
-
@db.sqls.
|
|
273
|
+
@db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects(zzz))"]
|
|
274
274
|
end
|
|
275
275
|
|
|
276
|
-
|
|
276
|
+
it "should accept foreign keys with ON DELETE clause" do
|
|
277
277
|
@db.create_table(:cats) do
|
|
278
278
|
foreign_key :project_id, :table => :projects, :on_delete => :restrict
|
|
279
279
|
end
|
|
280
|
-
@db.sqls.
|
|
280
|
+
@db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE RESTRICT)"]
|
|
281
281
|
|
|
282
282
|
@db.create_table(:cats) do
|
|
283
283
|
foreign_key :project_id, :table => :projects, :on_delete => :cascade
|
|
284
284
|
end
|
|
285
|
-
@db.sqls.
|
|
285
|
+
@db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE CASCADE)"]
|
|
286
286
|
|
|
287
287
|
@db.create_table(:cats) do
|
|
288
288
|
foreign_key :project_id, :table => :projects, :on_delete => :no_action
|
|
289
289
|
end
|
|
290
|
-
@db.sqls.
|
|
290
|
+
@db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE NO ACTION)"]
|
|
291
291
|
|
|
292
292
|
@db.create_table(:cats) do
|
|
293
293
|
foreign_key :project_id, :table => :projects, :on_delete => :set_null
|
|
294
294
|
end
|
|
295
|
-
@db.sqls.
|
|
295
|
+
@db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE SET NULL)"]
|
|
296
296
|
|
|
297
297
|
@db.create_table(:cats) do
|
|
298
298
|
foreign_key :project_id, :table => :projects, :on_delete => :set_default
|
|
299
299
|
end
|
|
300
|
-
@db.sqls.
|
|
300
|
+
@db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE SET DEFAULT)"]
|
|
301
301
|
|
|
302
302
|
@db.create_table(:cats) do
|
|
303
303
|
foreign_key :project_id, :table => :projects, :on_delete => 'NO ACTION FOO'
|
|
304
304
|
end
|
|
305
|
-
@db.sqls.
|
|
305
|
+
@db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE NO ACTION FOO)"]
|
|
306
306
|
end
|
|
307
307
|
|
|
308
|
-
|
|
308
|
+
it "should accept foreign keys with ON UPDATE clause" do
|
|
309
309
|
@db.create_table(:cats) do
|
|
310
310
|
foreign_key :project_id, :table => :projects, :on_update => :restrict
|
|
311
311
|
end
|
|
312
|
-
@db.sqls.
|
|
312
|
+
@db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects ON UPDATE RESTRICT)"]
|
|
313
313
|
|
|
314
314
|
@db.create_table(:cats) do
|
|
315
315
|
foreign_key :project_id, :table => :projects, :on_update => :cascade
|
|
316
316
|
end
|
|
317
|
-
@db.sqls.
|
|
317
|
+
@db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects ON UPDATE CASCADE)"]
|
|
318
318
|
|
|
319
319
|
@db.create_table(:cats) do
|
|
320
320
|
foreign_key :project_id, :table => :projects, :on_update => :no_action
|
|
321
321
|
end
|
|
322
|
-
@db.sqls.
|
|
322
|
+
@db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects ON UPDATE NO ACTION)"]
|
|
323
323
|
|
|
324
324
|
@db.create_table(:cats) do
|
|
325
325
|
foreign_key :project_id, :table => :projects, :on_update => :set_null
|
|
326
326
|
end
|
|
327
|
-
@db.sqls.
|
|
327
|
+
@db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects ON UPDATE SET NULL)"]
|
|
328
328
|
|
|
329
329
|
@db.create_table(:cats) do
|
|
330
330
|
foreign_key :project_id, :table => :projects, :on_update => :set_default
|
|
331
331
|
end
|
|
332
|
-
@db.sqls.
|
|
332
|
+
@db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects ON UPDATE SET DEFAULT)"]
|
|
333
333
|
|
|
334
334
|
@db.create_table(:cats) do
|
|
335
335
|
foreign_key :project_id, :table => :projects, :on_update => 'SET DEFAULT FOO'
|
|
336
336
|
end
|
|
337
|
-
@db.sqls.
|
|
337
|
+
@db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects ON UPDATE SET DEFAULT FOO)"]
|
|
338
338
|
end
|
|
339
339
|
|
|
340
|
-
|
|
340
|
+
it "should accept foreign keys with deferrable option" do
|
|
341
341
|
@db.create_table(:cats) do
|
|
342
342
|
foreign_key :project_id, :projects, :deferrable=>true
|
|
343
343
|
end
|
|
344
|
-
@db.sqls.
|
|
344
|
+
@db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects DEFERRABLE INITIALLY DEFERRED)"]
|
|
345
345
|
end
|
|
346
346
|
|
|
347
|
-
|
|
347
|
+
it "should accept collation" do
|
|
348
348
|
@db.create_table(:cats) do
|
|
349
349
|
varchar :name, :collate => :utf8_bin
|
|
350
350
|
end
|
|
351
|
-
@db.sqls.
|
|
351
|
+
@db.sqls.must_equal ["CREATE TABLE cats (name varchar(255) COLLATE utf8_bin)"]
|
|
352
352
|
end
|
|
353
353
|
|
|
354
|
-
|
|
354
|
+
it "should accept inline index definition" do
|
|
355
355
|
@db.create_table(:cats) do
|
|
356
356
|
integer :id, :index => true
|
|
357
357
|
end
|
|
358
|
-
@db.sqls.
|
|
358
|
+
@db.sqls.must_equal ["CREATE TABLE cats (id integer)", "CREATE INDEX cats_id_index ON cats (id)"]
|
|
359
359
|
end
|
|
360
360
|
|
|
361
|
-
|
|
361
|
+
it "should accept inline index definition with a hash of options" do
|
|
362
362
|
@db.create_table(:cats) do
|
|
363
363
|
integer :id, :index => {:unique=>true}
|
|
364
364
|
end
|
|
365
|
-
@db.sqls.
|
|
365
|
+
@db.sqls.must_equal ["CREATE TABLE cats (id integer)", "CREATE UNIQUE INDEX cats_id_index ON cats (id)"]
|
|
366
366
|
end
|
|
367
367
|
|
|
368
|
-
|
|
368
|
+
it "should accept inline index definition for foreign keys" do
|
|
369
369
|
@db.create_table(:cats) do
|
|
370
370
|
foreign_key :project_id, :table => :projects, :on_delete => :cascade, :index => true
|
|
371
371
|
end
|
|
372
|
-
@db.sqls.
|
|
372
|
+
@db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE CASCADE)",
|
|
373
373
|
"CREATE INDEX cats_project_id_index ON cats (project_id)"]
|
|
374
374
|
end
|
|
375
375
|
|
|
376
|
-
|
|
376
|
+
it "should accept inline index definition for foreign keys with a hash of options" do
|
|
377
377
|
@db.create_table(:cats) do
|
|
378
378
|
foreign_key :project_id, :table => :projects, :on_delete => :cascade, :index => {:unique=>true}
|
|
379
379
|
end
|
|
380
|
-
@db.sqls.
|
|
380
|
+
@db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE CASCADE)",
|
|
381
381
|
"CREATE UNIQUE INDEX cats_project_id_index ON cats (project_id)"]
|
|
382
382
|
end
|
|
383
383
|
|
|
384
|
-
|
|
384
|
+
it "should accept index definitions" do
|
|
385
385
|
@db.create_table(:cats) do
|
|
386
386
|
integer :id
|
|
387
387
|
index :id
|
|
388
388
|
end
|
|
389
|
-
@db.sqls.
|
|
389
|
+
@db.sqls.must_equal ["CREATE TABLE cats (id integer)", "CREATE INDEX cats_id_index ON cats (id)"]
|
|
390
390
|
end
|
|
391
391
|
|
|
392
|
-
|
|
392
|
+
it "should accept unique constraint definitions" do
|
|
393
393
|
@db.create_table(:cats) do
|
|
394
394
|
text :name
|
|
395
395
|
unique :name
|
|
396
396
|
end
|
|
397
|
-
@db.sqls.
|
|
397
|
+
@db.sqls.must_equal ["CREATE TABLE cats (name text, UNIQUE (name))"]
|
|
398
398
|
end
|
|
399
399
|
|
|
400
|
-
|
|
400
|
+
it "should accept partial index definitions" do
|
|
401
401
|
def @db.supports_partial_indexes?() true end
|
|
402
402
|
@db.create_table(:cats) do
|
|
403
403
|
integer :id
|
|
404
404
|
index :id, :where=>proc{id > 1}
|
|
405
405
|
end
|
|
406
|
-
@db.sqls.
|
|
406
|
+
@db.sqls.must_equal ["CREATE TABLE cats (id integer)", "CREATE INDEX cats_id_index ON cats (id) WHERE (id > 1)"]
|
|
407
407
|
end
|
|
408
408
|
|
|
409
|
-
|
|
409
|
+
it "should raise an error if partial indexes are not supported" do
|
|
410
410
|
proc do
|
|
411
411
|
@db.create_table(:cats) do
|
|
412
412
|
integer :id
|
|
413
413
|
index :id, :where=>proc{id > 1}
|
|
414
414
|
end
|
|
415
|
-
end.
|
|
415
|
+
end.must_raise(Sequel::Error)
|
|
416
416
|
end
|
|
417
417
|
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
end
|
|
424
|
-
}.should_not raise_error
|
|
418
|
+
it "should not raise on index error for unsupported index definitions if ignore_index_errors is used" do
|
|
419
|
+
@db.create_table(:cats, :ignore_index_errors=>true) do
|
|
420
|
+
text :name
|
|
421
|
+
full_text_index :name
|
|
422
|
+
end
|
|
425
423
|
end
|
|
426
424
|
|
|
427
|
-
|
|
425
|
+
it "should raise on full-text index definitions" do
|
|
428
426
|
proc {
|
|
429
427
|
@db.create_table(:cats) do
|
|
430
428
|
text :name
|
|
431
429
|
full_text_index :name
|
|
432
430
|
end
|
|
433
|
-
}.
|
|
431
|
+
}.must_raise(Sequel::Error)
|
|
434
432
|
end
|
|
435
433
|
|
|
436
|
-
|
|
434
|
+
it "should raise on spatial index definitions" do
|
|
437
435
|
proc {
|
|
438
436
|
@db.create_table(:cats) do
|
|
439
437
|
point :geom
|
|
440
438
|
spatial_index :geom
|
|
441
439
|
end
|
|
442
|
-
}.
|
|
440
|
+
}.must_raise(Sequel::Error)
|
|
443
441
|
end
|
|
444
442
|
|
|
445
|
-
|
|
443
|
+
it "should raise on partial index definitions" do
|
|
446
444
|
proc {
|
|
447
445
|
@db.create_table(:cats) do
|
|
448
446
|
text :name
|
|
449
447
|
index :name, :where => {:something => true}
|
|
450
448
|
end
|
|
451
|
-
}.
|
|
449
|
+
}.must_raise(Sequel::Error)
|
|
452
450
|
end
|
|
453
451
|
|
|
454
|
-
|
|
452
|
+
it "should raise index definitions with type" do
|
|
455
453
|
proc {
|
|
456
454
|
@db.create_table(:cats) do
|
|
457
455
|
text :name
|
|
458
456
|
index :name, :type => :hash
|
|
459
457
|
end
|
|
460
|
-
}.
|
|
458
|
+
}.must_raise(Sequel::Error)
|
|
461
459
|
end
|
|
462
460
|
|
|
463
|
-
|
|
461
|
+
it "should ignore errors if the database raises an error on an index creation statement and the :ignore_index_errors option is used" do
|
|
464
462
|
meta_def(@db, :execute_ddl){|*a| raise Sequel::DatabaseError if /blah/.match(a.first); super(*a)}
|
|
465
|
-
lambda{@db.create_table(:cats){Integer :id; index :blah; index :id}}.
|
|
466
|
-
@db.sqls.
|
|
467
|
-
|
|
468
|
-
@db.sqls.
|
|
463
|
+
lambda{@db.create_table(:cats){Integer :id; index :blah; index :id}}.must_raise(Sequel::DatabaseError)
|
|
464
|
+
@db.sqls.must_equal ['CREATE TABLE cats (id integer)']
|
|
465
|
+
@db.create_table(:cats, :ignore_index_errors=>true){Integer :id; index :blah; index :id}
|
|
466
|
+
@db.sqls.must_equal ['CREATE TABLE cats (id integer)', 'CREATE INDEX cats_id_index ON cats (id)']
|
|
469
467
|
end
|
|
470
468
|
|
|
471
|
-
|
|
469
|
+
it "should accept multiple index definitions" do
|
|
472
470
|
@db.create_table(:cats) do
|
|
473
471
|
integer :id
|
|
474
472
|
index :id
|
|
475
473
|
index :name
|
|
476
474
|
end
|
|
477
|
-
@db.sqls.
|
|
475
|
+
@db.sqls.must_equal ["CREATE TABLE cats (id integer)", "CREATE INDEX cats_id_index ON cats (id)", "CREATE INDEX cats_name_index ON cats (name)"]
|
|
478
476
|
end
|
|
479
477
|
|
|
480
|
-
|
|
478
|
+
it "should accept functional indexes" do
|
|
481
479
|
@db.create_table(:cats) do
|
|
482
480
|
integer :id
|
|
483
481
|
index Sequel.function(:lower, :name)
|
|
484
482
|
end
|
|
485
|
-
@db.sqls.
|
|
483
|
+
@db.sqls.must_equal ["CREATE TABLE cats (id integer)", "CREATE INDEX cats_lower_name__index ON cats (lower(name))"]
|
|
486
484
|
end
|
|
487
485
|
|
|
488
|
-
|
|
486
|
+
it "should accept indexes with identifiers" do
|
|
489
487
|
@db.create_table(:cats) do
|
|
490
488
|
integer :id
|
|
491
489
|
index Sequel.identifier(:lower__name)
|
|
492
490
|
end
|
|
493
|
-
@db.sqls.
|
|
491
|
+
@db.sqls.must_equal ["CREATE TABLE cats (id integer)", "CREATE INDEX cats_lower__name_index ON cats (lower__name)"]
|
|
494
492
|
end
|
|
495
493
|
|
|
496
|
-
|
|
494
|
+
it "should accept custom index names" do
|
|
497
495
|
@db.create_table(:cats) do
|
|
498
496
|
integer :id
|
|
499
497
|
index :id, :name => 'abc'
|
|
500
498
|
end
|
|
501
|
-
@db.sqls.
|
|
499
|
+
@db.sqls.must_equal ["CREATE TABLE cats (id integer)", "CREATE INDEX abc ON cats (id)"]
|
|
502
500
|
end
|
|
503
501
|
|
|
504
|
-
|
|
502
|
+
it "should accept unique index definitions" do
|
|
505
503
|
@db.create_table(:cats) do
|
|
506
504
|
integer :id
|
|
507
505
|
index :id, :unique => true
|
|
508
506
|
end
|
|
509
|
-
@db.sqls.
|
|
507
|
+
@db.sqls.must_equal ["CREATE TABLE cats (id integer)", "CREATE UNIQUE INDEX cats_id_index ON cats (id)"]
|
|
510
508
|
end
|
|
511
509
|
|
|
512
|
-
|
|
510
|
+
it "should accept composite index definitions" do
|
|
513
511
|
@db.create_table(:cats) do
|
|
514
512
|
integer :id
|
|
515
513
|
index [:id, :name], :unique => true
|
|
516
514
|
end
|
|
517
|
-
@db.sqls.
|
|
515
|
+
@db.sqls.must_equal ["CREATE TABLE cats (id integer)", "CREATE UNIQUE INDEX cats_id_name_index ON cats (id, name)"]
|
|
518
516
|
end
|
|
519
517
|
|
|
520
|
-
|
|
518
|
+
it "should accept unnamed constraint definitions with blocks" do
|
|
521
519
|
@db.create_table(:cats) do
|
|
522
520
|
integer :score
|
|
523
521
|
check{(x > 0) & (y < 1)}
|
|
524
522
|
end
|
|
525
|
-
@db.sqls.
|
|
523
|
+
@db.sqls.must_equal ["CREATE TABLE cats (score integer, CHECK ((x > 0) AND (y < 1)))"]
|
|
526
524
|
end
|
|
527
525
|
|
|
528
|
-
|
|
526
|
+
it "should accept unnamed constraint definitions with function calls" do
|
|
529
527
|
@db.create_table(:cats) do
|
|
530
528
|
integer :score
|
|
531
529
|
check{f(x)}
|
|
532
530
|
end
|
|
533
|
-
@db.sqls.
|
|
531
|
+
@db.sqls.must_equal ["CREATE TABLE cats (score integer, CHECK (f(x)))"]
|
|
534
532
|
end
|
|
535
533
|
|
|
536
|
-
|
|
534
|
+
it "should accept unnamed constraint definitions" do
|
|
537
535
|
@db.create_table(:cats) do
|
|
538
536
|
check 'price < ?', 100
|
|
539
537
|
end
|
|
540
|
-
@db.sqls.
|
|
538
|
+
@db.sqls.must_equal ["CREATE TABLE cats (CHECK (price < 100))"]
|
|
541
539
|
end
|
|
542
540
|
|
|
543
|
-
|
|
541
|
+
it "should accept arrays of pairs constraints" do
|
|
544
542
|
@db.create_table(:cats) do
|
|
545
543
|
check [[:price, 100]]
|
|
546
544
|
end
|
|
547
|
-
@db.sqls.
|
|
545
|
+
@db.sqls.must_equal ["CREATE TABLE cats (CHECK (price = 100))"]
|
|
548
546
|
end
|
|
549
547
|
|
|
550
|
-
|
|
548
|
+
it "should accept hash constraints" do
|
|
551
549
|
@db.create_table(:cats) do
|
|
552
550
|
check :price=>100
|
|
553
551
|
end
|
|
554
|
-
@db.sqls.
|
|
552
|
+
@db.sqls.must_equal ["CREATE TABLE cats (CHECK (price = 100))"]
|
|
555
553
|
end
|
|
556
554
|
|
|
557
|
-
|
|
555
|
+
it "should accept array constraints" do
|
|
558
556
|
@db.create_table(:cats) do
|
|
559
557
|
check [Sequel.expr(:x) > 0, Sequel.expr(:y) < 1]
|
|
560
558
|
end
|
|
561
|
-
@db.sqls.
|
|
559
|
+
@db.sqls.must_equal ["CREATE TABLE cats (CHECK ((x > 0) AND (y < 1)))"]
|
|
562
560
|
end
|
|
563
561
|
|
|
564
|
-
|
|
562
|
+
it "should accept named constraint definitions" do
|
|
565
563
|
@db.create_table(:cats) do
|
|
566
564
|
integer :score
|
|
567
565
|
constraint :valid_score, 'score <= 100'
|
|
568
566
|
end
|
|
569
|
-
@db.sqls.
|
|
567
|
+
@db.sqls.must_equal ["CREATE TABLE cats (score integer, CONSTRAINT valid_score CHECK (score <= 100))"]
|
|
570
568
|
end
|
|
571
569
|
|
|
572
|
-
|
|
570
|
+
it "should accept named constraint definitions with options" do
|
|
573
571
|
@db.create_table(:cats) do
|
|
574
572
|
integer :score
|
|
575
573
|
constraint({:name=>:valid_score, :deferrable=>true}, 'score <= 100')
|
|
576
574
|
end
|
|
577
|
-
@db.sqls.
|
|
575
|
+
@db.sqls.must_equal ["CREATE TABLE cats (score integer, CONSTRAINT valid_score CHECK (score <= 100) DEFERRABLE INITIALLY DEFERRED)"]
|
|
578
576
|
end
|
|
579
577
|
|
|
580
|
-
|
|
578
|
+
it "should accept named constraint definitions with block" do
|
|
581
579
|
@db.create_table(:cats) do
|
|
582
580
|
constraint(:blah_blah){(x.sql_number > 0) & (y.sql_number < 1)}
|
|
583
581
|
end
|
|
584
|
-
@db.sqls.
|
|
582
|
+
@db.sqls.must_equal ["CREATE TABLE cats (CONSTRAINT blah_blah CHECK ((x > 0) AND (y < 1)))"]
|
|
585
583
|
end
|
|
586
584
|
|
|
587
|
-
|
|
588
|
-
proc{@db.create_table(:cats){unique [:a, :b], :type=>:bb}}.
|
|
585
|
+
it "should raise an error if an invalid constraint type is used" do
|
|
586
|
+
proc{@db.create_table(:cats){unique [:a, :b], :type=>:bb}}.must_raise(Sequel::Error)
|
|
589
587
|
end
|
|
590
588
|
|
|
591
|
-
|
|
589
|
+
it "should accept composite primary keys" do
|
|
592
590
|
@db.create_table(:cats) do
|
|
593
591
|
integer :a
|
|
594
592
|
integer :b
|
|
595
593
|
primary_key [:a, :b]
|
|
596
594
|
end
|
|
597
|
-
@db.sqls.
|
|
595
|
+
@db.sqls.must_equal ["CREATE TABLE cats (a integer, b integer, PRIMARY KEY (a, b))"]
|
|
598
596
|
end
|
|
599
597
|
|
|
600
|
-
|
|
598
|
+
it "should accept named composite primary keys" do
|
|
601
599
|
@db.create_table(:cats) do
|
|
602
600
|
integer :a
|
|
603
601
|
integer :b
|
|
604
602
|
primary_key [:a, :b], :name => :cpk
|
|
605
603
|
end
|
|
606
|
-
@db.sqls.
|
|
604
|
+
@db.sqls.must_equal ["CREATE TABLE cats (a integer, b integer, CONSTRAINT cpk PRIMARY KEY (a, b))"]
|
|
607
605
|
end
|
|
608
606
|
|
|
609
|
-
|
|
607
|
+
it "should accept composite foreign keys" do
|
|
610
608
|
@db.create_table(:cats) do
|
|
611
609
|
integer :a
|
|
612
610
|
integer :b
|
|
613
611
|
foreign_key [:a, :b], :abc
|
|
614
612
|
end
|
|
615
|
-
@db.sqls.
|
|
613
|
+
@db.sqls.must_equal ["CREATE TABLE cats (a integer, b integer, FOREIGN KEY (a, b) REFERENCES abc)"]
|
|
616
614
|
end
|
|
617
615
|
|
|
618
|
-
|
|
616
|
+
it "should accept named composite foreign keys" do
|
|
619
617
|
@db.create_table(:cats) do
|
|
620
618
|
integer :a
|
|
621
619
|
integer :b
|
|
622
620
|
foreign_key [:a, :b], :abc, :name => :cfk
|
|
623
621
|
end
|
|
624
|
-
@db.sqls.
|
|
622
|
+
@db.sqls.must_equal ["CREATE TABLE cats (a integer, b integer, CONSTRAINT cfk FOREIGN KEY (a, b) REFERENCES abc)"]
|
|
625
623
|
end
|
|
626
624
|
|
|
627
|
-
|
|
625
|
+
it "should accept composite foreign keys with arbitrary keys" do
|
|
628
626
|
@db.create_table(:cats) do
|
|
629
627
|
integer :a
|
|
630
628
|
integer :b
|
|
631
629
|
foreign_key [:a, :b], :abc, :key => [:real_a, :real_b]
|
|
632
630
|
end
|
|
633
|
-
@db.sqls.
|
|
631
|
+
@db.sqls.must_equal ["CREATE TABLE cats (a integer, b integer, FOREIGN KEY (a, b) REFERENCES abc(real_a, real_b))"]
|
|
634
632
|
|
|
635
633
|
@db.create_table(:cats) do
|
|
636
634
|
integer :a
|
|
637
635
|
integer :b
|
|
638
636
|
foreign_key [:a, :b], :abc, :key => [:z, :x]
|
|
639
637
|
end
|
|
640
|
-
@db.sqls.
|
|
638
|
+
@db.sqls.must_equal ["CREATE TABLE cats (a integer, b integer, FOREIGN KEY (a, b) REFERENCES abc(z, x))"]
|
|
641
639
|
end
|
|
642
640
|
|
|
643
|
-
|
|
641
|
+
it "should accept composite foreign keys with on delete and on update clauses" do
|
|
644
642
|
@db.create_table(:cats) do
|
|
645
643
|
integer :a
|
|
646
644
|
integer :b
|
|
647
645
|
foreign_key [:a, :b], :abc, :on_delete => :cascade
|
|
648
646
|
end
|
|
649
|
-
@db.sqls.
|
|
647
|
+
@db.sqls.must_equal ["CREATE TABLE cats (a integer, b integer, FOREIGN KEY (a, b) REFERENCES abc ON DELETE CASCADE)"]
|
|
650
648
|
|
|
651
649
|
@db.create_table(:cats) do
|
|
652
650
|
integer :a
|
|
653
651
|
integer :b
|
|
654
652
|
foreign_key [:a, :b], :abc, :on_update => :no_action
|
|
655
653
|
end
|
|
656
|
-
@db.sqls.
|
|
654
|
+
@db.sqls.must_equal ["CREATE TABLE cats (a integer, b integer, FOREIGN KEY (a, b) REFERENCES abc ON UPDATE NO ACTION)"]
|
|
657
655
|
|
|
658
656
|
@db.create_table(:cats) do
|
|
659
657
|
integer :a
|
|
660
658
|
integer :b
|
|
661
659
|
foreign_key [:a, :b], :abc, :on_delete => :restrict, :on_update => :set_default
|
|
662
660
|
end
|
|
663
|
-
@db.sqls.
|
|
661
|
+
@db.sqls.must_equal ["CREATE TABLE cats (a integer, b integer, FOREIGN KEY (a, b) REFERENCES abc ON DELETE RESTRICT ON UPDATE SET DEFAULT)"]
|
|
664
662
|
|
|
665
663
|
@db.create_table(:cats) do
|
|
666
664
|
integer :a
|
|
667
665
|
integer :b
|
|
668
666
|
foreign_key [:a, :b], :abc, :key => [:x, :y], :on_delete => :set_null, :on_update => :set_null
|
|
669
667
|
end
|
|
670
|
-
@db.sqls.
|
|
668
|
+
@db.sqls.must_equal ["CREATE TABLE cats (a integer, b integer, FOREIGN KEY (a, b) REFERENCES abc(x, y) ON DELETE SET NULL ON UPDATE SET NULL)"]
|
|
671
669
|
end
|
|
672
670
|
|
|
673
|
-
|
|
671
|
+
it "should accept an :as option to create a table from the results of a dataset" do
|
|
674
672
|
@db.create_table(:cats, :as=>@db[:a])
|
|
675
|
-
@db.sqls.
|
|
673
|
+
@db.sqls.must_equal ['CREATE TABLE cats AS SELECT * FROM a']
|
|
676
674
|
end
|
|
677
675
|
|
|
678
|
-
|
|
676
|
+
it "should accept an :as option to create a table from a SELECT string" do
|
|
679
677
|
@db.create_table(:cats, :as=>'SELECT * FROM a')
|
|
680
|
-
@db.sqls.
|
|
678
|
+
@db.sqls.must_equal ['CREATE TABLE cats AS SELECT * FROM a']
|
|
681
679
|
end
|
|
682
680
|
|
|
683
|
-
|
|
684
|
-
proc{@db.create_table(:cats, :as=>@db[:a]){}}.
|
|
681
|
+
it "should raise an Error if both a block and an :as argument are given" do
|
|
682
|
+
proc{@db.create_table(:cats, :as=>@db[:a]){}}.must_raise(Sequel::Error)
|
|
685
683
|
end
|
|
686
684
|
end
|
|
687
685
|
|
|
@@ -690,22 +688,22 @@ describe "DB#create_table!" do
|
|
|
690
688
|
@db = Sequel.mock
|
|
691
689
|
end
|
|
692
690
|
|
|
693
|
-
|
|
691
|
+
it "should create the table if it does not exist" do
|
|
694
692
|
meta_def(@db, :table_exists?){|a| false}
|
|
695
693
|
@db.create_table!(:cats){|*a|}
|
|
696
|
-
@db.sqls.
|
|
694
|
+
@db.sqls.must_equal ['CREATE TABLE cats ()']
|
|
697
695
|
end
|
|
698
696
|
|
|
699
|
-
|
|
697
|
+
it "should drop the table before creating it if it already exists" do
|
|
700
698
|
meta_def(@db, :table_exists?){|a| true}
|
|
701
699
|
@db.create_table!(:cats){|*a|}
|
|
702
|
-
@db.sqls.
|
|
700
|
+
@db.sqls.must_equal ['DROP TABLE cats', 'CREATE TABLE cats ()']
|
|
703
701
|
end
|
|
704
702
|
|
|
705
|
-
|
|
703
|
+
it "should use IF EXISTS if the database supports it" do
|
|
706
704
|
meta_def(@db, :supports_drop_table_if_exists?){true}
|
|
707
705
|
@db.create_table!(:cats){|*a|}
|
|
708
|
-
@db.sqls.
|
|
706
|
+
@db.sqls.must_equal ['DROP TABLE IF EXISTS cats', 'CREATE TABLE cats ()']
|
|
709
707
|
end
|
|
710
708
|
end
|
|
711
709
|
|
|
@@ -714,33 +712,33 @@ describe "DB#create_table?" do
|
|
|
714
712
|
@db = Sequel.mock
|
|
715
713
|
end
|
|
716
714
|
|
|
717
|
-
|
|
715
|
+
it "should not create the table if the table already exists" do
|
|
718
716
|
meta_def(@db, :table_exists?){|a| true}
|
|
719
717
|
@db.create_table?(:cats){|*a|}
|
|
720
|
-
@db.sqls.
|
|
718
|
+
@db.sqls.must_equal []
|
|
721
719
|
end
|
|
722
720
|
|
|
723
|
-
|
|
721
|
+
it "should create the table if the table doesn't already exist" do
|
|
724
722
|
meta_def(@db, :table_exists?){|a| false}
|
|
725
723
|
@db.create_table?(:cats){|*a|}
|
|
726
|
-
@db.sqls.
|
|
724
|
+
@db.sqls.must_equal ['CREATE TABLE cats ()']
|
|
727
725
|
end
|
|
728
726
|
|
|
729
|
-
|
|
727
|
+
it "should use IF NOT EXISTS if the database supports that" do
|
|
730
728
|
meta_def(@db, :supports_create_table_if_not_exists?){true}
|
|
731
729
|
@db.create_table?(:cats){|*a|}
|
|
732
|
-
@db.sqls.
|
|
730
|
+
@db.sqls.must_equal ['CREATE TABLE IF NOT EXISTS cats ()']
|
|
733
731
|
end
|
|
734
732
|
|
|
735
|
-
|
|
733
|
+
it "should not use IF NOT EXISTS if the indexes are created" do
|
|
736
734
|
meta_def(@db, :table_exists?){|a| false}
|
|
737
735
|
meta_def(@db, :supports_create_table_if_not_exists?){true}
|
|
738
736
|
@db.create_table?(:cats){|*a| Integer :a, :index=>true}
|
|
739
|
-
@db.sqls.
|
|
737
|
+
@db.sqls.must_equal ['CREATE TABLE cats (a integer)', 'CREATE INDEX cats_a_index ON cats (a)']
|
|
740
738
|
|
|
741
739
|
meta_def(@db, :table_exists?){|a| true}
|
|
742
740
|
@db.create_table?(:cats){|*a| Integer :a, :index=>true}
|
|
743
|
-
@db.sqls.
|
|
741
|
+
@db.sqls.must_equal []
|
|
744
742
|
end
|
|
745
743
|
end
|
|
746
744
|
|
|
@@ -749,46 +747,46 @@ describe "DB#create_join_table" do
|
|
|
749
747
|
@db = Sequel.mock
|
|
750
748
|
end
|
|
751
749
|
|
|
752
|
-
|
|
750
|
+
it "should take a hash with foreign keys and table name values" do
|
|
753
751
|
@db.create_join_table(:cat_id=>:cats, :dog_id=>:dogs)
|
|
754
|
-
@db.sqls.
|
|
752
|
+
@db.sqls.must_equal ['CREATE TABLE cats_dogs (cat_id integer NOT NULL REFERENCES cats, dog_id integer NOT NULL REFERENCES dogs, PRIMARY KEY (cat_id, dog_id))', 'CREATE INDEX cats_dogs_dog_id_cat_id_index ON cats_dogs (dog_id, cat_id)']
|
|
755
753
|
end
|
|
756
754
|
|
|
757
|
-
|
|
755
|
+
it "should be able to have values be a hash of options" do
|
|
758
756
|
@db.create_join_table(:cat_id=>{:table=>:cats, :null=>true}, :dog_id=>{:table=>:dogs, :default=>0})
|
|
759
|
-
@db.sqls.
|
|
757
|
+
@db.sqls.must_equal ['CREATE TABLE cats_dogs (cat_id integer NULL REFERENCES cats, dog_id integer DEFAULT 0 NOT NULL REFERENCES dogs, PRIMARY KEY (cat_id, dog_id))', 'CREATE INDEX cats_dogs_dog_id_cat_id_index ON cats_dogs (dog_id, cat_id)']
|
|
760
758
|
end
|
|
761
759
|
|
|
762
|
-
|
|
760
|
+
it "should be able to pass a second hash of table options" do
|
|
763
761
|
@db.create_join_table({:cat_id=>:cats, :dog_id=>:dogs}, :temp=>true)
|
|
764
|
-
@db.sqls.
|
|
762
|
+
@db.sqls.must_equal ['CREATE TEMPORARY TABLE cats_dogs (cat_id integer NOT NULL REFERENCES cats, dog_id integer NOT NULL REFERENCES dogs, PRIMARY KEY (cat_id, dog_id))', 'CREATE INDEX cats_dogs_dog_id_cat_id_index ON cats_dogs (dog_id, cat_id)']
|
|
765
763
|
end
|
|
766
764
|
|
|
767
|
-
|
|
765
|
+
it "should recognize :name option in table options" do
|
|
768
766
|
@db.create_join_table({:cat_id=>:cats, :dog_id=>:dogs}, :name=>:f)
|
|
769
|
-
@db.sqls.
|
|
767
|
+
@db.sqls.must_equal ['CREATE TABLE f (cat_id integer NOT NULL REFERENCES cats, dog_id integer NOT NULL REFERENCES dogs, PRIMARY KEY (cat_id, dog_id))', 'CREATE INDEX f_dog_id_cat_id_index ON f (dog_id, cat_id)']
|
|
770
768
|
end
|
|
771
769
|
|
|
772
|
-
|
|
770
|
+
it "should recognize :index_options option in table options" do
|
|
773
771
|
@db.create_join_table({:cat_id=>:cats, :dog_id=>:dogs}, :index_options=>{:name=>:foo_index})
|
|
774
|
-
@db.sqls.
|
|
772
|
+
@db.sqls.must_equal ['CREATE TABLE cats_dogs (cat_id integer NOT NULL REFERENCES cats, dog_id integer NOT NULL REFERENCES dogs, PRIMARY KEY (cat_id, dog_id))', 'CREATE INDEX foo_index ON cats_dogs (dog_id, cat_id)']
|
|
775
773
|
end
|
|
776
774
|
|
|
777
|
-
|
|
775
|
+
it "should recognize :no_index option in table options" do
|
|
778
776
|
@db.create_join_table({:cat_id=>:cats, :dog_id=>:dogs}, :no_index=>true)
|
|
779
|
-
@db.sqls.
|
|
777
|
+
@db.sqls.must_equal ['CREATE TABLE cats_dogs (cat_id integer NOT NULL REFERENCES cats, dog_id integer NOT NULL REFERENCES dogs, PRIMARY KEY (cat_id, dog_id))']
|
|
780
778
|
end
|
|
781
779
|
|
|
782
|
-
|
|
780
|
+
it "should recognize :no_primary_key option in table options" do
|
|
783
781
|
@db.create_join_table({:cat_id=>:cats, :dog_id=>:dogs}, :no_primary_key=>true)
|
|
784
|
-
@db.sqls.
|
|
782
|
+
@db.sqls.must_equal ['CREATE TABLE cats_dogs (cat_id integer NOT NULL REFERENCES cats, dog_id integer NOT NULL REFERENCES dogs)', 'CREATE INDEX cats_dogs_dog_id_cat_id_index ON cats_dogs (dog_id, cat_id)']
|
|
785
783
|
end
|
|
786
784
|
|
|
787
|
-
|
|
788
|
-
proc{@db.create_join_table({})}.
|
|
789
|
-
proc{@db.create_join_table({:cat_id=>:cats})}.
|
|
790
|
-
proc{@db.create_join_table({:cat_id=>:cats, :human_id=>:humans, :dog_id=>:dog})}.
|
|
791
|
-
proc{@db.create_join_table({:cat_id=>:cats, :dog_id=>{}})}.
|
|
785
|
+
it "should raise an error if the hash doesn't have 2 entries with table names" do
|
|
786
|
+
proc{@db.create_join_table({})}.must_raise(Sequel::Error)
|
|
787
|
+
proc{@db.create_join_table({:cat_id=>:cats})}.must_raise(Sequel::Error)
|
|
788
|
+
proc{@db.create_join_table({:cat_id=>:cats, :human_id=>:humans, :dog_id=>:dog})}.must_raise(Sequel::Error)
|
|
789
|
+
proc{@db.create_join_table({:cat_id=>:cats, :dog_id=>{}})}.must_raise(Sequel::Error)
|
|
792
790
|
end
|
|
793
791
|
end
|
|
794
792
|
|
|
@@ -797,33 +795,33 @@ describe "DB#create_join_table?" do
|
|
|
797
795
|
@db = Sequel.mock
|
|
798
796
|
end
|
|
799
797
|
|
|
800
|
-
|
|
798
|
+
it "should create the table if it does not already exist" do
|
|
801
799
|
meta_def(@db, :table_exists?){|a| false}
|
|
802
800
|
@db.create_join_table?(:cat_id=>:cats, :dog_id=>:dogs)
|
|
803
|
-
@db.sqls.
|
|
801
|
+
@db.sqls.must_equal ['CREATE TABLE cats_dogs (cat_id integer NOT NULL REFERENCES cats, dog_id integer NOT NULL REFERENCES dogs, PRIMARY KEY (cat_id, dog_id))', 'CREATE INDEX cats_dogs_dog_id_cat_id_index ON cats_dogs (dog_id, cat_id)']
|
|
804
802
|
end
|
|
805
803
|
|
|
806
|
-
|
|
804
|
+
it "should not create the table if it already exists" do
|
|
807
805
|
meta_def(@db, :table_exists?){|a| true}
|
|
808
806
|
@db.create_join_table?(:cat_id=>:cats, :dog_id=>:dogs)
|
|
809
|
-
@db.sqls.
|
|
807
|
+
@db.sqls.must_equal []
|
|
810
808
|
end
|
|
811
809
|
|
|
812
|
-
|
|
810
|
+
it "should not use IF NOT EXISTS" do
|
|
813
811
|
meta_def(@db, :table_exists?){|a| false}
|
|
814
812
|
meta_def(@db, :supports_create_table_if_not_exists?){true}
|
|
815
813
|
@db.create_join_table?(:cat_id=>:cats, :dog_id=>:dogs)
|
|
816
|
-
@db.sqls.
|
|
814
|
+
@db.sqls.must_equal ['CREATE TABLE cats_dogs (cat_id integer NOT NULL REFERENCES cats, dog_id integer NOT NULL REFERENCES dogs, PRIMARY KEY (cat_id, dog_id))', 'CREATE INDEX cats_dogs_dog_id_cat_id_index ON cats_dogs (dog_id, cat_id)']
|
|
817
815
|
|
|
818
816
|
meta_def(@db, :table_exists?){|a| true}
|
|
819
817
|
@db.create_join_table?(:cat_id=>:cats, :dog_id=>:dogs)
|
|
820
|
-
@db.sqls.
|
|
818
|
+
@db.sqls.must_equal []
|
|
821
819
|
end
|
|
822
820
|
|
|
823
|
-
|
|
821
|
+
it "should not use IF NOT EXISTS if no_index is used" do
|
|
824
822
|
meta_def(@db, :supports_create_table_if_not_exists?){true}
|
|
825
823
|
@db.create_join_table?({:cat_id=>:cats, :dog_id=>:dogs}, :no_index=>true)
|
|
826
|
-
@db.sqls.
|
|
824
|
+
@db.sqls.must_equal ['CREATE TABLE IF NOT EXISTS cats_dogs (cat_id integer NOT NULL REFERENCES cats, dog_id integer NOT NULL REFERENCES dogs, PRIMARY KEY (cat_id, dog_id))']
|
|
827
825
|
end
|
|
828
826
|
end
|
|
829
827
|
|
|
@@ -832,22 +830,22 @@ describe "DB#create_join_table!" do
|
|
|
832
830
|
@db = Sequel.mock
|
|
833
831
|
end
|
|
834
832
|
|
|
835
|
-
|
|
833
|
+
it "should drop the table first if it already exists" do
|
|
836
834
|
meta_def(@db, :table_exists?){|a| true}
|
|
837
835
|
@db.create_join_table!(:cat_id=>:cats, :dog_id=>:dogs)
|
|
838
|
-
@db.sqls.
|
|
836
|
+
@db.sqls.must_equal ['DROP TABLE cats_dogs', 'CREATE TABLE cats_dogs (cat_id integer NOT NULL REFERENCES cats, dog_id integer NOT NULL REFERENCES dogs, PRIMARY KEY (cat_id, dog_id))', 'CREATE INDEX cats_dogs_dog_id_cat_id_index ON cats_dogs (dog_id, cat_id)']
|
|
839
837
|
end
|
|
840
838
|
|
|
841
|
-
|
|
839
|
+
it "should not drop the table if it doesn't exists" do
|
|
842
840
|
meta_def(@db, :table_exists?){|a| false}
|
|
843
841
|
@db.create_join_table!(:cat_id=>:cats, :dog_id=>:dogs)
|
|
844
|
-
@db.sqls.
|
|
842
|
+
@db.sqls.must_equal ['CREATE TABLE cats_dogs (cat_id integer NOT NULL REFERENCES cats, dog_id integer NOT NULL REFERENCES dogs, PRIMARY KEY (cat_id, dog_id))', 'CREATE INDEX cats_dogs_dog_id_cat_id_index ON cats_dogs (dog_id, cat_id)']
|
|
845
843
|
end
|
|
846
844
|
|
|
847
|
-
|
|
845
|
+
it "should use IF EXISTS if the database supports it" do
|
|
848
846
|
meta_def(@db, :supports_drop_table_if_exists?){true}
|
|
849
847
|
@db.create_join_table!(:cat_id=>:cats, :dog_id=>:dogs)
|
|
850
|
-
@db.sqls.
|
|
848
|
+
@db.sqls.must_equal ['DROP TABLE IF EXISTS cats_dogs', 'CREATE TABLE cats_dogs (cat_id integer NOT NULL REFERENCES cats, dog_id integer NOT NULL REFERENCES dogs, PRIMARY KEY (cat_id, dog_id))', 'CREATE INDEX cats_dogs_dog_id_cat_id_index ON cats_dogs (dog_id, cat_id)']
|
|
851
849
|
end
|
|
852
850
|
end
|
|
853
851
|
|
|
@@ -856,31 +854,31 @@ describe "DB#drop_join_table" do
|
|
|
856
854
|
@db = Sequel.mock
|
|
857
855
|
end
|
|
858
856
|
|
|
859
|
-
|
|
857
|
+
it "should take a hash with foreign keys and table name values and drop the table" do
|
|
860
858
|
@db.drop_join_table(:cat_id=>:cats, :dog_id=>:dogs)
|
|
861
|
-
@db.sqls.
|
|
859
|
+
@db.sqls.must_equal ['DROP TABLE cats_dogs']
|
|
862
860
|
end
|
|
863
861
|
|
|
864
|
-
|
|
862
|
+
it "should be able to have values be a hash of options" do
|
|
865
863
|
@db.drop_join_table(:cat_id=>{:table=>:cats, :null=>true}, :dog_id=>{:table=>:dogs, :default=>0})
|
|
866
|
-
@db.sqls.
|
|
864
|
+
@db.sqls.must_equal ['DROP TABLE cats_dogs']
|
|
867
865
|
end
|
|
868
866
|
|
|
869
|
-
|
|
867
|
+
it "should respect a second hash of table options" do
|
|
870
868
|
@db.drop_join_table({:cat_id=>:cats, :dog_id=>:dogs}, :cascade=>true)
|
|
871
|
-
@db.sqls.
|
|
869
|
+
@db.sqls.must_equal ['DROP TABLE cats_dogs CASCADE']
|
|
872
870
|
end
|
|
873
871
|
|
|
874
|
-
|
|
872
|
+
it "should respect :name option for table name" do
|
|
875
873
|
@db.drop_join_table({:cat_id=>:cats, :dog_id=>:dogs}, :name=>:f)
|
|
876
|
-
@db.sqls.
|
|
874
|
+
@db.sqls.must_equal ['DROP TABLE f']
|
|
877
875
|
end
|
|
878
876
|
|
|
879
|
-
|
|
880
|
-
proc{@db.drop_join_table({})}.
|
|
881
|
-
proc{@db.drop_join_table({:cat_id=>:cats})}.
|
|
882
|
-
proc{@db.drop_join_table({:cat_id=>:cats, :human_id=>:humans, :dog_id=>:dog})}.
|
|
883
|
-
proc{@db.drop_join_table({:cat_id=>:cats, :dog_id=>{}})}.
|
|
877
|
+
it "should raise an error if the hash doesn't have 2 entries with table names" do
|
|
878
|
+
proc{@db.drop_join_table({})}.must_raise(Sequel::Error)
|
|
879
|
+
proc{@db.drop_join_table({:cat_id=>:cats})}.must_raise(Sequel::Error)
|
|
880
|
+
proc{@db.drop_join_table({:cat_id=>:cats, :human_id=>:humans, :dog_id=>:dog})}.must_raise(Sequel::Error)
|
|
881
|
+
proc{@db.drop_join_table({:cat_id=>:cats, :dog_id=>{}})}.must_raise(Sequel::Error)
|
|
884
882
|
end
|
|
885
883
|
end
|
|
886
884
|
|
|
@@ -889,19 +887,19 @@ describe "DB#drop_table" do
|
|
|
889
887
|
@db = Sequel.mock
|
|
890
888
|
end
|
|
891
889
|
|
|
892
|
-
|
|
890
|
+
it "should generate a DROP TABLE statement" do
|
|
893
891
|
@db.drop_table :cats
|
|
894
|
-
@db.sqls.
|
|
892
|
+
@db.sqls.must_equal ['DROP TABLE cats']
|
|
895
893
|
end
|
|
896
894
|
|
|
897
|
-
|
|
895
|
+
it "should drop multiple tables at once" do
|
|
898
896
|
@db.drop_table :cats, :dogs
|
|
899
|
-
@db.sqls.
|
|
897
|
+
@db.sqls.must_equal ['DROP TABLE cats', 'DROP TABLE dogs']
|
|
900
898
|
end
|
|
901
899
|
|
|
902
|
-
|
|
900
|
+
it "should take an options hash and support the :cascade option" do
|
|
903
901
|
@db.drop_table :cats, :dogs, :cascade=>true
|
|
904
|
-
@db.sqls.
|
|
902
|
+
@db.sqls.must_equal ['DROP TABLE cats CASCADE', 'DROP TABLE dogs CASCADE']
|
|
905
903
|
end
|
|
906
904
|
end
|
|
907
905
|
|
|
@@ -910,40 +908,40 @@ describe "DB#drop_table?" do
|
|
|
910
908
|
@db = Sequel.mock
|
|
911
909
|
end
|
|
912
910
|
|
|
913
|
-
|
|
911
|
+
it "should drop the table if it exists" do
|
|
914
912
|
meta_def(@db, :table_exists?){|a| true}
|
|
915
913
|
@db.drop_table?(:cats)
|
|
916
|
-
@db.sqls.
|
|
914
|
+
@db.sqls.must_equal ["DROP TABLE cats"]
|
|
917
915
|
end
|
|
918
916
|
|
|
919
|
-
|
|
917
|
+
it "should do nothing if the table does not exist" do
|
|
920
918
|
meta_def(@db, :table_exists?){|a| false}
|
|
921
919
|
@db.drop_table?(:cats)
|
|
922
|
-
@db.sqls.
|
|
920
|
+
@db.sqls.must_equal []
|
|
923
921
|
end
|
|
924
922
|
|
|
925
|
-
|
|
923
|
+
it "should operate on multiple tables at once" do
|
|
926
924
|
meta_def(@db, :table_exists?){|a| a == :cats}
|
|
927
925
|
@db.drop_table? :cats, :dogs
|
|
928
|
-
@db.sqls.
|
|
926
|
+
@db.sqls.must_equal ['DROP TABLE cats']
|
|
929
927
|
end
|
|
930
928
|
|
|
931
|
-
|
|
929
|
+
it "should take an options hash and support the :cascade option" do
|
|
932
930
|
meta_def(@db, :table_exists?){|a| true}
|
|
933
931
|
@db.drop_table? :cats, :dogs, :cascade=>true
|
|
934
|
-
@db.sqls.
|
|
932
|
+
@db.sqls.must_equal ['DROP TABLE cats CASCADE', 'DROP TABLE dogs CASCADE']
|
|
935
933
|
end
|
|
936
934
|
|
|
937
|
-
|
|
935
|
+
it "should use IF NOT EXISTS if the database supports that" do
|
|
938
936
|
meta_def(@db, :supports_drop_table_if_exists?){true}
|
|
939
937
|
@db.drop_table? :cats, :dogs
|
|
940
|
-
@db.sqls.
|
|
938
|
+
@db.sqls.must_equal ['DROP TABLE IF EXISTS cats', 'DROP TABLE IF EXISTS dogs']
|
|
941
939
|
end
|
|
942
940
|
|
|
943
|
-
|
|
941
|
+
it "should use IF NOT EXISTS with CASCADE if the database supports that" do
|
|
944
942
|
meta_def(@db, :supports_drop_table_if_exists?){true}
|
|
945
943
|
@db.drop_table? :cats, :dogs, :cascade=>true
|
|
946
|
-
@db.sqls.
|
|
944
|
+
@db.sqls.must_equal ['DROP TABLE IF EXISTS cats CASCADE', 'DROP TABLE IF EXISTS dogs CASCADE']
|
|
947
945
|
end
|
|
948
946
|
end
|
|
949
947
|
|
|
@@ -952,250 +950,250 @@ describe "DB#alter_table" do
|
|
|
952
950
|
@db = Sequel.mock
|
|
953
951
|
end
|
|
954
952
|
|
|
955
|
-
|
|
953
|
+
it "should allow adding not null constraint via set_column_allow_null with false argument" do
|
|
956
954
|
@db.alter_table(:cats) do
|
|
957
955
|
set_column_allow_null :score, false
|
|
958
956
|
end
|
|
959
|
-
@db.sqls.
|
|
957
|
+
@db.sqls.must_equal ["ALTER TABLE cats ALTER COLUMN score SET NOT NULL"]
|
|
960
958
|
end
|
|
961
959
|
|
|
962
|
-
|
|
960
|
+
it "should allow removing not null constraint via set_column_allow_null with true argument" do
|
|
963
961
|
@db.alter_table(:cats) do
|
|
964
962
|
set_column_allow_null :score, true
|
|
965
963
|
end
|
|
966
|
-
@db.sqls.
|
|
964
|
+
@db.sqls.must_equal ["ALTER TABLE cats ALTER COLUMN score DROP NOT NULL"]
|
|
967
965
|
end
|
|
968
966
|
|
|
969
|
-
|
|
967
|
+
it "should allow adding not null constraint via set_column_not_null" do
|
|
970
968
|
@db.alter_table(:cats) do
|
|
971
969
|
set_column_not_null :score
|
|
972
970
|
end
|
|
973
|
-
@db.sqls.
|
|
971
|
+
@db.sqls.must_equal ["ALTER TABLE cats ALTER COLUMN score SET NOT NULL"]
|
|
974
972
|
end
|
|
975
973
|
|
|
976
|
-
|
|
974
|
+
it "should allow removing not null constraint via set_column_allow_null without argument" do
|
|
977
975
|
@db.alter_table(:cats) do
|
|
978
976
|
set_column_allow_null :score
|
|
979
977
|
end
|
|
980
|
-
@db.sqls.
|
|
978
|
+
@db.sqls.must_equal ["ALTER TABLE cats ALTER COLUMN score DROP NOT NULL"]
|
|
981
979
|
end
|
|
982
980
|
|
|
983
|
-
|
|
981
|
+
it "should support add_column" do
|
|
984
982
|
@db.alter_table(:cats) do
|
|
985
983
|
add_column :score, :integer
|
|
986
984
|
end
|
|
987
|
-
@db.sqls.
|
|
985
|
+
@db.sqls.must_equal ["ALTER TABLE cats ADD COLUMN score integer"]
|
|
988
986
|
end
|
|
989
987
|
|
|
990
|
-
|
|
988
|
+
it "should support add_constraint" do
|
|
991
989
|
@db.alter_table(:cats) do
|
|
992
990
|
add_constraint :valid_score, 'score <= 100'
|
|
993
991
|
end
|
|
994
|
-
@db.sqls.
|
|
992
|
+
@db.sqls.must_equal ["ALTER TABLE cats ADD CONSTRAINT valid_score CHECK (score <= 100)"]
|
|
995
993
|
end
|
|
996
994
|
|
|
997
|
-
|
|
995
|
+
it "should support add_constraint with options" do
|
|
998
996
|
@db.alter_table(:cats) do
|
|
999
997
|
add_constraint({:name=>:valid_score, :deferrable=>true}, 'score <= 100')
|
|
1000
998
|
end
|
|
1001
|
-
@db.sqls.
|
|
999
|
+
@db.sqls.must_equal ["ALTER TABLE cats ADD CONSTRAINT valid_score CHECK (score <= 100) DEFERRABLE INITIALLY DEFERRED"]
|
|
1002
1000
|
end
|
|
1003
1001
|
|
|
1004
|
-
|
|
1002
|
+
it "should support add_constraint with block" do
|
|
1005
1003
|
@db.alter_table(:cats) do
|
|
1006
1004
|
add_constraint(:blah_blah){(x.sql_number > 0) & (y.sql_number < 1)}
|
|
1007
1005
|
end
|
|
1008
|
-
@db.sqls.
|
|
1006
|
+
@db.sqls.must_equal ["ALTER TABLE cats ADD CONSTRAINT blah_blah CHECK ((x > 0) AND (y < 1))"]
|
|
1009
1007
|
end
|
|
1010
1008
|
|
|
1011
|
-
|
|
1009
|
+
it "should support add_unique_constraint" do
|
|
1012
1010
|
@db.alter_table(:cats) do
|
|
1013
1011
|
add_unique_constraint [:a, :b]
|
|
1014
1012
|
end
|
|
1015
|
-
@db.sqls.
|
|
1013
|
+
@db.sqls.must_equal ["ALTER TABLE cats ADD UNIQUE (a, b)"]
|
|
1016
1014
|
|
|
1017
1015
|
@db.alter_table(:cats) do
|
|
1018
1016
|
add_unique_constraint [:a, :b], :name => :ab_uniq
|
|
1019
1017
|
end
|
|
1020
|
-
@db.sqls.
|
|
1018
|
+
@db.sqls.must_equal ["ALTER TABLE cats ADD CONSTRAINT ab_uniq UNIQUE (a, b)"]
|
|
1021
1019
|
end
|
|
1022
1020
|
|
|
1023
|
-
|
|
1021
|
+
it "should support add_foreign_key" do
|
|
1024
1022
|
@db.alter_table(:cats) do
|
|
1025
1023
|
add_foreign_key :node_id, :nodes
|
|
1026
1024
|
end
|
|
1027
|
-
@db.sqls.
|
|
1025
|
+
@db.sqls.must_equal ["ALTER TABLE cats ADD COLUMN node_id integer REFERENCES nodes"]
|
|
1028
1026
|
end
|
|
1029
1027
|
|
|
1030
|
-
|
|
1028
|
+
it "should support add_foreign_key with composite foreign keys" do
|
|
1031
1029
|
@db.alter_table(:cats) do
|
|
1032
1030
|
add_foreign_key [:node_id, :prop_id], :nodes_props
|
|
1033
1031
|
end
|
|
1034
|
-
@db.sqls.
|
|
1032
|
+
@db.sqls.must_equal ["ALTER TABLE cats ADD FOREIGN KEY (node_id, prop_id) REFERENCES nodes_props"]
|
|
1035
1033
|
|
|
1036
1034
|
@db.alter_table(:cats) do
|
|
1037
1035
|
add_foreign_key [:node_id, :prop_id], :nodes_props, :name => :cfk
|
|
1038
1036
|
end
|
|
1039
|
-
@db.sqls.
|
|
1037
|
+
@db.sqls.must_equal ["ALTER TABLE cats ADD CONSTRAINT cfk FOREIGN KEY (node_id, prop_id) REFERENCES nodes_props"]
|
|
1040
1038
|
|
|
1041
1039
|
@db.alter_table(:cats) do
|
|
1042
1040
|
add_foreign_key [:node_id, :prop_id], :nodes_props, :key => [:nid, :pid]
|
|
1043
1041
|
end
|
|
1044
|
-
@db.sqls.
|
|
1042
|
+
@db.sqls.must_equal ["ALTER TABLE cats ADD FOREIGN KEY (node_id, prop_id) REFERENCES nodes_props(nid, pid)"]
|
|
1045
1043
|
|
|
1046
1044
|
@db.alter_table(:cats) do
|
|
1047
1045
|
add_foreign_key [:node_id, :prop_id], :nodes_props, :on_delete => :restrict, :on_update => :cascade
|
|
1048
1046
|
end
|
|
1049
|
-
@db.sqls.
|
|
1047
|
+
@db.sqls.must_equal ["ALTER TABLE cats ADD FOREIGN KEY (node_id, prop_id) REFERENCES nodes_props ON DELETE RESTRICT ON UPDATE CASCADE"]
|
|
1050
1048
|
end
|
|
1051
1049
|
|
|
1052
|
-
|
|
1050
|
+
it "should support add_index" do
|
|
1053
1051
|
@db.alter_table(:cats) do
|
|
1054
1052
|
add_index :name
|
|
1055
1053
|
end
|
|
1056
|
-
@db.sqls.
|
|
1054
|
+
@db.sqls.must_equal ["CREATE INDEX cats_name_index ON cats (name)"]
|
|
1057
1055
|
end
|
|
1058
1056
|
|
|
1059
|
-
|
|
1057
|
+
it "should ignore errors if the database raises an error on an add_index call and the :ignore_errors option is used" do
|
|
1060
1058
|
meta_def(@db, :execute_ddl){|*a| raise Sequel::DatabaseError}
|
|
1061
|
-
lambda{@db.add_index(:cats, :id)}.
|
|
1062
|
-
|
|
1063
|
-
@db.sqls.
|
|
1059
|
+
lambda{@db.add_index(:cats, :id)}.must_raise(Sequel::DatabaseError)
|
|
1060
|
+
@db.add_index(:cats, :id, :ignore_errors=>true)
|
|
1061
|
+
@db.sqls.must_equal []
|
|
1064
1062
|
end
|
|
1065
1063
|
|
|
1066
|
-
|
|
1064
|
+
it "should support add_primary_key" do
|
|
1067
1065
|
@db.alter_table(:cats) do
|
|
1068
1066
|
add_primary_key :id
|
|
1069
1067
|
end
|
|
1070
|
-
@db.sqls.
|
|
1068
|
+
@db.sqls.must_equal ["ALTER TABLE cats ADD COLUMN id integer PRIMARY KEY AUTOINCREMENT"]
|
|
1071
1069
|
end
|
|
1072
1070
|
|
|
1073
|
-
|
|
1071
|
+
it "should support add_primary_key with composite primary keys" do
|
|
1074
1072
|
@db.alter_table(:cats) do
|
|
1075
1073
|
add_primary_key [:id, :type]
|
|
1076
1074
|
end
|
|
1077
|
-
@db.sqls.
|
|
1075
|
+
@db.sqls.must_equal ["ALTER TABLE cats ADD PRIMARY KEY (id, type)"]
|
|
1078
1076
|
|
|
1079
1077
|
@db.alter_table(:cats) do
|
|
1080
1078
|
add_primary_key [:id, :type], :name => :cpk
|
|
1081
1079
|
end
|
|
1082
|
-
@db.sqls.
|
|
1080
|
+
@db.sqls.must_equal ["ALTER TABLE cats ADD CONSTRAINT cpk PRIMARY KEY (id, type)"]
|
|
1083
1081
|
end
|
|
1084
1082
|
|
|
1085
|
-
|
|
1083
|
+
it "should support drop_column" do
|
|
1086
1084
|
@db.alter_table(:cats) do
|
|
1087
1085
|
drop_column :score
|
|
1088
1086
|
end
|
|
1089
|
-
@db.sqls.
|
|
1087
|
+
@db.sqls.must_equal ["ALTER TABLE cats DROP COLUMN score"]
|
|
1090
1088
|
end
|
|
1091
1089
|
|
|
1092
|
-
|
|
1090
|
+
it "should support drop_column with :cascade=>true option" do
|
|
1093
1091
|
@db.alter_table(:cats) do
|
|
1094
1092
|
drop_column :score, :cascade=>true
|
|
1095
1093
|
end
|
|
1096
|
-
@db.sqls.
|
|
1094
|
+
@db.sqls.must_equal ["ALTER TABLE cats DROP COLUMN score CASCADE"]
|
|
1097
1095
|
end
|
|
1098
1096
|
|
|
1099
|
-
|
|
1097
|
+
it "should support drop_constraint" do
|
|
1100
1098
|
@db.alter_table(:cats) do
|
|
1101
1099
|
drop_constraint :valid_score
|
|
1102
1100
|
end
|
|
1103
|
-
@db.sqls.
|
|
1101
|
+
@db.sqls.must_equal ["ALTER TABLE cats DROP CONSTRAINT valid_score"]
|
|
1104
1102
|
end
|
|
1105
1103
|
|
|
1106
|
-
|
|
1104
|
+
it "should support drop_constraint with :cascade=>true option" do
|
|
1107
1105
|
@db.alter_table(:cats) do
|
|
1108
1106
|
drop_constraint :valid_score, :cascade=>true
|
|
1109
1107
|
end
|
|
1110
|
-
@db.sqls.
|
|
1108
|
+
@db.sqls.must_equal ["ALTER TABLE cats DROP CONSTRAINT valid_score CASCADE"]
|
|
1111
1109
|
end
|
|
1112
1110
|
|
|
1113
|
-
|
|
1111
|
+
it "should support drop_foreign_key" do
|
|
1114
1112
|
def @db.foreign_key_list(table_name)
|
|
1115
1113
|
[{:name=>:cats_node_id_fkey, :columns=>[:node_id]}]
|
|
1116
1114
|
end
|
|
1117
1115
|
@db.alter_table(:cats) do
|
|
1118
1116
|
drop_foreign_key :node_id
|
|
1119
1117
|
end
|
|
1120
|
-
@db.sqls.
|
|
1118
|
+
@db.sqls.must_equal ["ALTER TABLE cats DROP CONSTRAINT cats_node_id_fkey", "ALTER TABLE cats DROP COLUMN node_id"]
|
|
1121
1119
|
end
|
|
1122
1120
|
|
|
1123
|
-
|
|
1121
|
+
it "should support drop_foreign_key with composite foreign keys" do
|
|
1124
1122
|
def @db.foreign_key_list(table_name)
|
|
1125
1123
|
[{:name=>:cats_node_id_prop_id_fkey, :columns=>[:node_id, :prop_id]}]
|
|
1126
1124
|
end
|
|
1127
1125
|
@db.alter_table(:cats) do
|
|
1128
1126
|
drop_foreign_key [:node_id, :prop_id]
|
|
1129
1127
|
end
|
|
1130
|
-
@db.sqls.
|
|
1128
|
+
@db.sqls.must_equal ["ALTER TABLE cats DROP CONSTRAINT cats_node_id_prop_id_fkey"]
|
|
1131
1129
|
|
|
1132
1130
|
@db.alter_table(:cats) do
|
|
1133
1131
|
drop_foreign_key [:node_id, :prop_id], :name => :cfk
|
|
1134
1132
|
end
|
|
1135
|
-
@db.sqls.
|
|
1133
|
+
@db.sqls.must_equal ["ALTER TABLE cats DROP CONSTRAINT cfk"]
|
|
1136
1134
|
end
|
|
1137
1135
|
|
|
1138
|
-
|
|
1136
|
+
it "should have drop_foreign_key raise Error if no name is found" do
|
|
1139
1137
|
def @db.foreign_key_list(table_name)
|
|
1140
1138
|
[{:name=>:cats_node_id_fkey, :columns=>[:foo_id]}]
|
|
1141
1139
|
end
|
|
1142
|
-
lambda{@db.alter_table(:cats){drop_foreign_key :node_id}}.
|
|
1140
|
+
lambda{@db.alter_table(:cats){drop_foreign_key :node_id}}.must_raise(Sequel::Error)
|
|
1143
1141
|
end
|
|
1144
1142
|
|
|
1145
|
-
|
|
1143
|
+
it "should have drop_foreign_key raise Error if multiple foreign keys found" do
|
|
1146
1144
|
def @db.foreign_key_list(table_name)
|
|
1147
1145
|
[{:name=>:cats_node_id_fkey, :columns=>[:node_id]}, {:name=>:cats_node_id_fkey2, :columns=>[:node_id]}]
|
|
1148
1146
|
end
|
|
1149
|
-
lambda{@db.alter_table(:cats){drop_foreign_key :node_id}}.
|
|
1147
|
+
lambda{@db.alter_table(:cats){drop_foreign_key :node_id}}.must_raise(Sequel::Error)
|
|
1150
1148
|
end
|
|
1151
1149
|
|
|
1152
|
-
|
|
1150
|
+
it "should support drop_index" do
|
|
1153
1151
|
@db.alter_table(:cats) do
|
|
1154
1152
|
drop_index :name
|
|
1155
1153
|
end
|
|
1156
|
-
@db.sqls.
|
|
1154
|
+
@db.sqls.must_equal ["DROP INDEX cats_name_index"]
|
|
1157
1155
|
end
|
|
1158
1156
|
|
|
1159
|
-
|
|
1157
|
+
it "should support drop_index with a given name" do
|
|
1160
1158
|
@db.alter_table(:cats) do
|
|
1161
1159
|
drop_index :name, :name=>:blah_blah
|
|
1162
1160
|
end
|
|
1163
|
-
@db.sqls.
|
|
1161
|
+
@db.sqls.must_equal ["DROP INDEX blah_blah"]
|
|
1164
1162
|
end
|
|
1165
1163
|
|
|
1166
|
-
|
|
1164
|
+
it "should support rename_column" do
|
|
1167
1165
|
@db.alter_table(:cats) do
|
|
1168
1166
|
rename_column :name, :old_name
|
|
1169
1167
|
end
|
|
1170
|
-
@db.sqls.
|
|
1168
|
+
@db.sqls.must_equal ["ALTER TABLE cats RENAME COLUMN name TO old_name"]
|
|
1171
1169
|
end
|
|
1172
1170
|
|
|
1173
|
-
|
|
1171
|
+
it "should support set_column_default" do
|
|
1174
1172
|
@db.alter_table(:cats) do
|
|
1175
1173
|
set_column_default :score, 3
|
|
1176
1174
|
end
|
|
1177
|
-
@db.sqls.
|
|
1175
|
+
@db.sqls.must_equal ["ALTER TABLE cats ALTER COLUMN score SET DEFAULT 3"]
|
|
1178
1176
|
end
|
|
1179
1177
|
|
|
1180
|
-
|
|
1178
|
+
it "should support set_column_type" do
|
|
1181
1179
|
@db.alter_table(:cats) do
|
|
1182
1180
|
set_column_type :score, :real
|
|
1183
1181
|
end
|
|
1184
|
-
@db.sqls.
|
|
1182
|
+
@db.sqls.must_equal ["ALTER TABLE cats ALTER COLUMN score TYPE real"]
|
|
1185
1183
|
end
|
|
1186
1184
|
|
|
1187
|
-
|
|
1185
|
+
it "should support set_column_type with options" do
|
|
1188
1186
|
@db.alter_table(:cats) do
|
|
1189
1187
|
set_column_type :score, :integer, :unsigned=>true
|
|
1190
1188
|
set_column_type :score, :varchar, :size=>30
|
|
1191
1189
|
set_column_type :score, :enum, :elements=>['a', 'b']
|
|
1192
1190
|
end
|
|
1193
|
-
@db.sqls.
|
|
1191
|
+
@db.sqls.must_equal ["ALTER TABLE cats ALTER COLUMN score TYPE integer UNSIGNED",
|
|
1194
1192
|
"ALTER TABLE cats ALTER COLUMN score TYPE varchar(30)",
|
|
1195
1193
|
"ALTER TABLE cats ALTER COLUMN score TYPE enum('a', 'b')"]
|
|
1196
1194
|
end
|
|
1197
1195
|
|
|
1198
|
-
|
|
1196
|
+
it "should combine operations into a single query if the database supports it" do
|
|
1199
1197
|
meta_def(@db, :supports_combining_alter_table_ops?){true}
|
|
1200
1198
|
@db.alter_table(:cats) do
|
|
1201
1199
|
add_column :a, Integer
|
|
@@ -1207,10 +1205,10 @@ describe "DB#alter_table" do
|
|
|
1207
1205
|
add_constraint(:i){a > 1}
|
|
1208
1206
|
drop_constraint :j
|
|
1209
1207
|
end
|
|
1210
|
-
@db.sqls.
|
|
1208
|
+
@db.sqls.must_equal ["ALTER TABLE cats ADD COLUMN a integer, DROP COLUMN b, ALTER COLUMN c SET NOT NULL, RENAME COLUMN d TO e, ALTER COLUMN f SET DEFAULT 'g', ALTER COLUMN h TYPE integer, ADD CONSTRAINT i CHECK (a > 1), DROP CONSTRAINT j"]
|
|
1211
1209
|
end
|
|
1212
1210
|
|
|
1213
|
-
|
|
1211
|
+
it "should combine operations into consecutive groups of combinable operations if the database supports combining operations" do
|
|
1214
1212
|
meta_def(@db, :supports_combining_alter_table_ops?){true}
|
|
1215
1213
|
@db.alter_table(:cats) do
|
|
1216
1214
|
add_column :a, Integer
|
|
@@ -1223,7 +1221,7 @@ describe "DB#alter_table" do
|
|
|
1223
1221
|
add_constraint(:i){a > 1}
|
|
1224
1222
|
drop_constraint :j
|
|
1225
1223
|
end
|
|
1226
|
-
@db.sqls.
|
|
1224
|
+
@db.sqls.must_equal ["ALTER TABLE cats ADD COLUMN a integer, DROP COLUMN b, ALTER COLUMN c SET NOT NULL, RENAME COLUMN d TO e",
|
|
1227
1225
|
"CREATE INDEX cats_e_index ON cats (e)",
|
|
1228
1226
|
"ALTER TABLE cats ALTER COLUMN f SET DEFAULT 'g', ALTER COLUMN h TYPE integer, ADD CONSTRAINT i CHECK (a > 1), DROP CONSTRAINT j"]
|
|
1229
1227
|
end
|
|
@@ -1235,24 +1233,24 @@ describe "Database#create_table" do
|
|
|
1235
1233
|
@db = Sequel.mock
|
|
1236
1234
|
end
|
|
1237
1235
|
|
|
1238
|
-
|
|
1236
|
+
it "should construct proper SQL" do
|
|
1239
1237
|
@db.create_table :test do
|
|
1240
1238
|
primary_key :id, :integer, :null => false
|
|
1241
1239
|
column :name, :text
|
|
1242
1240
|
index :name, :unique => true
|
|
1243
1241
|
end
|
|
1244
|
-
@db.sqls.
|
|
1242
|
+
@db.sqls.must_equal ['CREATE TABLE test (id integer NOT NULL PRIMARY KEY AUTOINCREMENT, name text)',
|
|
1245
1243
|
'CREATE UNIQUE INDEX test_name_index ON test (name)']
|
|
1246
1244
|
end
|
|
1247
1245
|
|
|
1248
|
-
|
|
1246
|
+
it "should create a temporary table" do
|
|
1249
1247
|
@db.create_table :test_tmp, :temp => true do
|
|
1250
1248
|
primary_key :id, :integer, :null => false
|
|
1251
1249
|
column :name, :text
|
|
1252
1250
|
index :name, :unique => true
|
|
1253
1251
|
end
|
|
1254
1252
|
|
|
1255
|
-
@db.sqls.
|
|
1253
|
+
@db.sqls.must_equal ['CREATE TEMPORARY TABLE test_tmp (id integer NOT NULL PRIMARY KEY AUTOINCREMENT, name text)',
|
|
1256
1254
|
'CREATE UNIQUE INDEX test_tmp_name_index ON test_tmp (name)']
|
|
1257
1255
|
end
|
|
1258
1256
|
end
|
|
@@ -1262,7 +1260,7 @@ describe "Database#alter_table" do
|
|
|
1262
1260
|
@db = Sequel.mock
|
|
1263
1261
|
end
|
|
1264
1262
|
|
|
1265
|
-
|
|
1263
|
+
it "should construct proper SQL" do
|
|
1266
1264
|
@db.alter_table :xyz do
|
|
1267
1265
|
add_column :aaa, :text, :null => false, :unique => true
|
|
1268
1266
|
drop_column :bbb
|
|
@@ -1273,7 +1271,7 @@ describe "Database#alter_table" do
|
|
|
1273
1271
|
drop_index :ggg
|
|
1274
1272
|
end
|
|
1275
1273
|
|
|
1276
|
-
@db.sqls.
|
|
1274
|
+
@db.sqls.must_equal ['ALTER TABLE xyz ADD COLUMN aaa text NOT NULL UNIQUE',
|
|
1277
1275
|
'ALTER TABLE xyz DROP COLUMN bbb',
|
|
1278
1276
|
'ALTER TABLE xyz RENAME COLUMN ccc TO ddd',
|
|
1279
1277
|
'ALTER TABLE xyz ALTER COLUMN eee TYPE integer',
|
|
@@ -1284,10 +1282,10 @@ describe "Database#alter_table" do
|
|
|
1284
1282
|
end
|
|
1285
1283
|
|
|
1286
1284
|
describe "Database#add_column" do
|
|
1287
|
-
|
|
1285
|
+
it "should construct proper SQL" do
|
|
1288
1286
|
db = Sequel.mock
|
|
1289
1287
|
db.add_column :test, :name, :text, :unique => true
|
|
1290
|
-
db.sqls.
|
|
1288
|
+
db.sqls.must_equal ['ALTER TABLE test ADD COLUMN name text UNIQUE']
|
|
1291
1289
|
end
|
|
1292
1290
|
end
|
|
1293
1291
|
|
|
@@ -1296,14 +1294,14 @@ describe "Database#drop_column" do
|
|
|
1296
1294
|
@db = Sequel.mock
|
|
1297
1295
|
end
|
|
1298
1296
|
|
|
1299
|
-
|
|
1297
|
+
it "should construct proper SQL" do
|
|
1300
1298
|
@db.drop_column :test, :name
|
|
1301
|
-
@db.sqls.
|
|
1299
|
+
@db.sqls.must_equal ['ALTER TABLE test DROP COLUMN name']
|
|
1302
1300
|
end
|
|
1303
1301
|
|
|
1304
|
-
|
|
1302
|
+
it "should use CASCADE for :cascade=>true option" do
|
|
1305
1303
|
@db.drop_column :test, :name, :cascade=>true
|
|
1306
|
-
@db.sqls.
|
|
1304
|
+
@db.sqls.must_equal ['ALTER TABLE test DROP COLUMN name CASCADE']
|
|
1307
1305
|
end
|
|
1308
1306
|
end
|
|
1309
1307
|
|
|
@@ -1312,9 +1310,9 @@ describe "Database#rename_column" do
|
|
|
1312
1310
|
@db = Sequel.mock
|
|
1313
1311
|
end
|
|
1314
1312
|
|
|
1315
|
-
|
|
1313
|
+
it "should construct proper SQL" do
|
|
1316
1314
|
@db.rename_column :test, :abc, :def
|
|
1317
|
-
@db.sqls.
|
|
1315
|
+
@db.sqls.must_equal ['ALTER TABLE test RENAME COLUMN abc TO def']
|
|
1318
1316
|
end
|
|
1319
1317
|
end
|
|
1320
1318
|
|
|
@@ -1323,9 +1321,9 @@ describe "Database#set_column_type" do
|
|
|
1323
1321
|
@db = Sequel.mock
|
|
1324
1322
|
end
|
|
1325
1323
|
|
|
1326
|
-
|
|
1324
|
+
it "should construct proper SQL" do
|
|
1327
1325
|
@db.set_column_type :test, :name, :integer
|
|
1328
|
-
@db.sqls.
|
|
1326
|
+
@db.sqls.must_equal ['ALTER TABLE test ALTER COLUMN name TYPE integer']
|
|
1329
1327
|
end
|
|
1330
1328
|
end
|
|
1331
1329
|
|
|
@@ -1334,9 +1332,9 @@ describe "Database#set_column_default" do
|
|
|
1334
1332
|
@db = Sequel.mock
|
|
1335
1333
|
end
|
|
1336
1334
|
|
|
1337
|
-
|
|
1335
|
+
it "should construct proper SQL" do
|
|
1338
1336
|
@db.set_column_default :test, :name, 'zyx'
|
|
1339
|
-
@db.sqls.
|
|
1337
|
+
@db.sqls.must_equal ["ALTER TABLE test ALTER COLUMN name SET DEFAULT 'zyx'"]
|
|
1340
1338
|
end
|
|
1341
1339
|
end
|
|
1342
1340
|
|
|
@@ -1345,14 +1343,14 @@ describe "Database#add_index" do
|
|
|
1345
1343
|
@db = Sequel.mock
|
|
1346
1344
|
end
|
|
1347
1345
|
|
|
1348
|
-
|
|
1346
|
+
it "should construct proper SQL" do
|
|
1349
1347
|
@db.add_index :test, :name, :unique => true
|
|
1350
|
-
@db.sqls.
|
|
1348
|
+
@db.sqls.must_equal ['CREATE UNIQUE INDEX test_name_index ON test (name)']
|
|
1351
1349
|
end
|
|
1352
1350
|
|
|
1353
|
-
|
|
1351
|
+
it "should accept multiple columns" do
|
|
1354
1352
|
@db.add_index :test, [:one, :two]
|
|
1355
|
-
@db.sqls.
|
|
1353
|
+
@db.sqls.must_equal ['CREATE INDEX test_one_two_index ON test (one, two)']
|
|
1356
1354
|
end
|
|
1357
1355
|
end
|
|
1358
1356
|
|
|
@@ -1361,9 +1359,9 @@ describe "Database#drop_index" do
|
|
|
1361
1359
|
@db = Sequel.mock
|
|
1362
1360
|
end
|
|
1363
1361
|
|
|
1364
|
-
|
|
1362
|
+
it "should construct proper SQL" do
|
|
1365
1363
|
@db.drop_index :test, :name
|
|
1366
|
-
@db.sqls.
|
|
1364
|
+
@db.sqls.must_equal ['DROP INDEX test_name_index']
|
|
1367
1365
|
end
|
|
1368
1366
|
|
|
1369
1367
|
end
|
|
@@ -1373,14 +1371,14 @@ describe "Database#drop_table" do
|
|
|
1373
1371
|
@db = Sequel.mock
|
|
1374
1372
|
end
|
|
1375
1373
|
|
|
1376
|
-
|
|
1374
|
+
it "should construct proper SQL" do
|
|
1377
1375
|
@db.drop_table :test
|
|
1378
|
-
@db.sqls.
|
|
1376
|
+
@db.sqls.must_equal ['DROP TABLE test']
|
|
1379
1377
|
end
|
|
1380
1378
|
|
|
1381
|
-
|
|
1379
|
+
it "should accept multiple table names" do
|
|
1382
1380
|
@db.drop_table :a, :bb, :ccc
|
|
1383
|
-
@db.sqls.
|
|
1381
|
+
@db.sqls.must_equal ['DROP TABLE a', 'DROP TABLE bb', 'DROP TABLE ccc']
|
|
1384
1382
|
end
|
|
1385
1383
|
end
|
|
1386
1384
|
|
|
@@ -1389,9 +1387,9 @@ describe "Database#rename_table" do
|
|
|
1389
1387
|
@db = Sequel.mock
|
|
1390
1388
|
end
|
|
1391
1389
|
|
|
1392
|
-
|
|
1390
|
+
it "should construct proper SQL" do
|
|
1393
1391
|
@db.rename_table :abc, :xyz
|
|
1394
|
-
@db.sqls.
|
|
1392
|
+
@db.sqls.must_equal ['ALTER TABLE abc RENAME TO xyz']
|
|
1395
1393
|
end
|
|
1396
1394
|
end
|
|
1397
1395
|
|
|
@@ -1400,47 +1398,47 @@ describe "Database#create_view" do
|
|
|
1400
1398
|
@db = Sequel.mock
|
|
1401
1399
|
end
|
|
1402
1400
|
|
|
1403
|
-
|
|
1401
|
+
it "should construct proper SQL with raw SQL" do
|
|
1404
1402
|
@db.create_view :test, "SELECT * FROM xyz"
|
|
1405
|
-
@db.sqls.
|
|
1403
|
+
@db.sqls.must_equal ['CREATE VIEW test AS SELECT * FROM xyz']
|
|
1406
1404
|
@db.create_view Sequel.identifier(:test), "SELECT * FROM xyz"
|
|
1407
|
-
@db.sqls.
|
|
1405
|
+
@db.sqls.must_equal ['CREATE VIEW test AS SELECT * FROM xyz']
|
|
1408
1406
|
end
|
|
1409
1407
|
|
|
1410
|
-
|
|
1408
|
+
it "should construct proper SQL with dataset" do
|
|
1411
1409
|
@db.create_view :test, @db[:items].select(:a, :b).order(:c)
|
|
1412
|
-
@db.sqls.
|
|
1410
|
+
@db.sqls.must_equal ['CREATE VIEW test AS SELECT a, b FROM items ORDER BY c']
|
|
1413
1411
|
end
|
|
1414
1412
|
|
|
1415
|
-
|
|
1413
|
+
it "should handle :columns option" do
|
|
1416
1414
|
@db.create_view :test, @db[:items].select(:a, :b).order(:c), :columns=>[:d, :e]
|
|
1417
|
-
@db.sqls.
|
|
1415
|
+
@db.sqls.must_equal ['CREATE VIEW test (d, e) AS SELECT a, b FROM items ORDER BY c']
|
|
1418
1416
|
@db.create_view :test, @db[:items].select(:a, :b).order(:c), :columns=>%w'd e'
|
|
1419
|
-
@db.sqls.
|
|
1417
|
+
@db.sqls.must_equal ['CREATE VIEW test (d, e) AS SELECT a, b FROM items ORDER BY c']
|
|
1420
1418
|
@db.create_view :test, @db[:items].select(:a, :b).order(:c), :columns=>[Sequel.identifier('d'), Sequel.lit('e')]
|
|
1421
|
-
@db.sqls.
|
|
1419
|
+
@db.sqls.must_equal ['CREATE VIEW test (d, e) AS SELECT a, b FROM items ORDER BY c']
|
|
1422
1420
|
end
|
|
1423
1421
|
|
|
1424
|
-
|
|
1422
|
+
it "should handle :check option" do
|
|
1425
1423
|
@db.create_view :test, @db[:items].select(:a, :b).order(:c), :check=>true
|
|
1426
|
-
@db.sqls.
|
|
1424
|
+
@db.sqls.must_equal ['CREATE VIEW test AS SELECT a, b FROM items ORDER BY c WITH CHECK OPTION']
|
|
1427
1425
|
@db.create_view :test, @db[:items].select(:a, :b).order(:c), :check=>:local
|
|
1428
|
-
@db.sqls.
|
|
1426
|
+
@db.sqls.must_equal ['CREATE VIEW test AS SELECT a, b FROM items ORDER BY c WITH LOCAL CHECK OPTION']
|
|
1429
1427
|
end
|
|
1430
1428
|
|
|
1431
|
-
|
|
1429
|
+
it "should handle create_or_replace_view" do
|
|
1432
1430
|
@db.create_or_replace_view :sch__test, "SELECT * FROM xyz"
|
|
1433
|
-
@db.sqls.
|
|
1431
|
+
@db.sqls.must_equal ['DROP VIEW sch.test', 'CREATE VIEW sch.test AS SELECT * FROM xyz']
|
|
1434
1432
|
@db.create_or_replace_view :test, @db[:items].select(:a, :b).order(:c)
|
|
1435
|
-
@db.sqls.
|
|
1433
|
+
@db.sqls.must_equal ['DROP VIEW test', 'CREATE VIEW test AS SELECT a, b FROM items ORDER BY c']
|
|
1436
1434
|
@db.create_or_replace_view Sequel.identifier(:test), @db[:items].select(:a, :b).order(:c)
|
|
1437
|
-
@db.sqls.
|
|
1435
|
+
@db.sqls.must_equal ['DROP VIEW test', 'CREATE VIEW test AS SELECT a, b FROM items ORDER BY c']
|
|
1438
1436
|
end
|
|
1439
1437
|
|
|
1440
|
-
|
|
1438
|
+
it "should use CREATE OR REPLACE VIEW if such syntax is supported" do
|
|
1441
1439
|
def @db.supports_create_or_replace_view?() true end
|
|
1442
1440
|
@db.create_or_replace_view :test, @db[:items]
|
|
1443
|
-
@db.sqls.
|
|
1441
|
+
@db.sqls.must_equal ['CREATE OR REPLACE VIEW test AS SELECT * FROM items']
|
|
1444
1442
|
end
|
|
1445
1443
|
end
|
|
1446
1444
|
|
|
@@ -1449,33 +1447,33 @@ describe "Database#drop_view" do
|
|
|
1449
1447
|
@db = Sequel.mock
|
|
1450
1448
|
end
|
|
1451
1449
|
|
|
1452
|
-
|
|
1450
|
+
it "should construct proper SQL" do
|
|
1453
1451
|
@db.drop_view :test
|
|
1454
1452
|
@db.drop_view Sequel.identifier(:test)
|
|
1455
1453
|
@db.drop_view :sch__test
|
|
1456
1454
|
@db.drop_view Sequel.qualify(:sch, :test)
|
|
1457
|
-
@db.sqls.
|
|
1455
|
+
@db.sqls.must_equal ['DROP VIEW test', 'DROP VIEW test', 'DROP VIEW sch.test', 'DROP VIEW sch.test']
|
|
1458
1456
|
end
|
|
1459
1457
|
|
|
1460
|
-
|
|
1458
|
+
it "should drop multiple views at once" do
|
|
1461
1459
|
@db.drop_view :cats, :dogs
|
|
1462
|
-
@db.sqls.
|
|
1460
|
+
@db.sqls.must_equal ['DROP VIEW cats', 'DROP VIEW dogs']
|
|
1463
1461
|
end
|
|
1464
1462
|
|
|
1465
|
-
|
|
1463
|
+
it "should support the :cascade option" do
|
|
1466
1464
|
@db.drop_view :cats, :dogs, :cascade=>true
|
|
1467
|
-
@db.sqls.
|
|
1465
|
+
@db.sqls.must_equal ['DROP VIEW cats CASCADE', 'DROP VIEW dogs CASCADE']
|
|
1468
1466
|
end
|
|
1469
1467
|
|
|
1470
|
-
|
|
1468
|
+
it "should support the :if_exists option" do
|
|
1471
1469
|
@db.drop_view :cats, :dogs, :if_exists=>true
|
|
1472
|
-
@db.sqls.
|
|
1470
|
+
@db.sqls.must_equal ['DROP VIEW IF EXISTS cats', 'DROP VIEW IF EXISTS dogs']
|
|
1473
1471
|
end
|
|
1474
1472
|
end
|
|
1475
1473
|
|
|
1476
1474
|
describe "Database#alter_table_sql" do
|
|
1477
|
-
|
|
1478
|
-
proc {Sequel.mock.send(:alter_table_sql, :mau, :op => :blah)}.
|
|
1475
|
+
it "should raise error for an invalid op" do
|
|
1476
|
+
proc {Sequel.mock.send(:alter_table_sql, :mau, :op => :blah)}.must_raise(Sequel::Error)
|
|
1479
1477
|
end
|
|
1480
1478
|
end
|
|
1481
1479
|
|
|
@@ -1485,169 +1483,169 @@ describe "Schema Parser" do
|
|
|
1485
1483
|
@db = Sequel::Database.new
|
|
1486
1484
|
end
|
|
1487
1485
|
|
|
1488
|
-
|
|
1486
|
+
it "should raise an error if there are no columns" do
|
|
1489
1487
|
meta_def(@db, :schema_parse_table) do |t, opts|
|
|
1490
1488
|
[]
|
|
1491
1489
|
end
|
|
1492
|
-
proc{@db.schema(:x)}.
|
|
1490
|
+
proc{@db.schema(:x)}.must_raise(Sequel::Error)
|
|
1493
1491
|
end
|
|
1494
1492
|
|
|
1495
|
-
|
|
1493
|
+
it "should cache data by default" do
|
|
1496
1494
|
meta_def(@db, :schema_parse_table) do |t, opts|
|
|
1497
1495
|
[[:a, {}]]
|
|
1498
1496
|
end
|
|
1499
|
-
@db.schema(:x).
|
|
1497
|
+
@db.schema(:x).must_be_same_as(@db.schema(:x))
|
|
1500
1498
|
end
|
|
1501
1499
|
|
|
1502
|
-
|
|
1500
|
+
it "should not cache data if :reload=>true is given" do
|
|
1503
1501
|
meta_def(@db, :schema_parse_table) do |t, opts|
|
|
1504
1502
|
[[:a, {}]]
|
|
1505
1503
|
end
|
|
1506
|
-
@db.schema(:x).
|
|
1504
|
+
@db.schema(:x).wont_be_same_as(@db.schema(:x, :reload=>true))
|
|
1507
1505
|
end
|
|
1508
1506
|
|
|
1509
|
-
|
|
1507
|
+
it "should not cache schema metadata if cache_schema is false" do
|
|
1510
1508
|
@db.cache_schema = false
|
|
1511
1509
|
meta_def(@db, :schema_parse_table) do |t, opts|
|
|
1512
1510
|
[[:a, {}]]
|
|
1513
1511
|
end
|
|
1514
|
-
@db.schema(:x).
|
|
1512
|
+
@db.schema(:x).wont_be_same_as(@db.schema(:x))
|
|
1515
1513
|
end
|
|
1516
1514
|
|
|
1517
|
-
|
|
1515
|
+
it "should provide options if given a table name" do
|
|
1518
1516
|
c = nil
|
|
1519
1517
|
meta_def(@db, :schema_parse_table) do |t, opts|
|
|
1520
1518
|
c = [t, opts]
|
|
1521
1519
|
[[:a, {:db_type=>t.to_s}]]
|
|
1522
1520
|
end
|
|
1523
1521
|
@db.schema(:x)
|
|
1524
|
-
c.
|
|
1522
|
+
c.must_equal ["x", {}]
|
|
1525
1523
|
@db.schema(:s__x)
|
|
1526
|
-
c.
|
|
1524
|
+
c.must_equal ["x", {:schema=>"s"}]
|
|
1527
1525
|
ds = @db[:s__y]
|
|
1528
1526
|
@db.schema(ds)
|
|
1529
|
-
c.
|
|
1527
|
+
c.must_equal ["y", {:schema=>"s", :dataset=>ds}]
|
|
1530
1528
|
end
|
|
1531
1529
|
|
|
1532
|
-
|
|
1530
|
+
it "should parse the schema correctly for a single table" do
|
|
1533
1531
|
sqls = @sqls
|
|
1534
|
-
proc{@db.schema(:x)}.
|
|
1532
|
+
proc{@db.schema(:x)}.must_raise(Sequel::Error)
|
|
1535
1533
|
meta_def(@db, :schema_parse_table) do |t, opts|
|
|
1536
1534
|
sqls << t
|
|
1537
1535
|
[[:a, {:db_type=>t.to_s}]]
|
|
1538
1536
|
end
|
|
1539
|
-
@db.schema(:x).
|
|
1540
|
-
@sqls.
|
|
1541
|
-
@db.schema(:x).
|
|
1542
|
-
@sqls.
|
|
1543
|
-
@db.schema(:x, :reload=>true).
|
|
1544
|
-
@sqls.
|
|
1537
|
+
@db.schema(:x).must_equal [[:a, {:db_type=>"x", :ruby_default=>nil}]]
|
|
1538
|
+
@sqls.must_equal ['x']
|
|
1539
|
+
@db.schema(:x).must_equal [[:a, {:db_type=>"x", :ruby_default=>nil}]]
|
|
1540
|
+
@sqls.must_equal ['x']
|
|
1541
|
+
@db.schema(:x, :reload=>true).must_equal [[:a, {:db_type=>"x", :ruby_default=>nil}]]
|
|
1542
|
+
@sqls.must_equal ['x', 'x']
|
|
1545
1543
|
end
|
|
1546
1544
|
|
|
1547
|
-
|
|
1545
|
+
it "should set :auto_increment to true by default if unset and a single integer primary key is used" do
|
|
1548
1546
|
meta_def(@db, :schema_parse_table){|*| [[:a, {:primary_key=>true, :db_type=>'integer'}]]}
|
|
1549
|
-
@db.schema(:x).first.last[:auto_increment].
|
|
1547
|
+
@db.schema(:x).first.last[:auto_increment].must_equal true
|
|
1550
1548
|
end
|
|
1551
1549
|
|
|
1552
|
-
|
|
1550
|
+
it "should not set :auto_increment if already set" do
|
|
1553
1551
|
meta_def(@db, :schema_parse_table){|*| [[:a, {:primary_key=>true, :db_type=>'integer', :auto_increment=>false}]]}
|
|
1554
|
-
@db.schema(:x).first.last[:auto_increment].
|
|
1552
|
+
@db.schema(:x).first.last[:auto_increment].must_equal false
|
|
1555
1553
|
end
|
|
1556
1554
|
|
|
1557
|
-
|
|
1555
|
+
it "should set :auto_increment to false by default if unset and a single nonintegery primary key is used" do
|
|
1558
1556
|
meta_def(@db, :schema_parse_table){|*| [[:a, {:primary_key=>true, :db_type=>'varchar'}]]}
|
|
1559
|
-
@db.schema(:x).first.last[:auto_increment].
|
|
1557
|
+
@db.schema(:x).first.last[:auto_increment].must_equal false
|
|
1560
1558
|
end
|
|
1561
1559
|
|
|
1562
|
-
|
|
1560
|
+
it "should set :auto_increment to false by default if unset and a composite primary key" do
|
|
1563
1561
|
meta_def(@db, :schema_parse_table){|*| [[:a, {:primary_key=>true, :db_type=>'integer'}], [:b, {:primary_key=>true, :db_type=>'integer'}]]}
|
|
1564
|
-
@db.schema(:x).first.last[:auto_increment].
|
|
1565
|
-
@db.schema(:x).last.last[:auto_increment].
|
|
1562
|
+
@db.schema(:x).first.last[:auto_increment].must_equal false
|
|
1563
|
+
@db.schema(:x).last.last[:auto_increment].must_equal false
|
|
1566
1564
|
end
|
|
1567
1565
|
|
|
1568
|
-
|
|
1566
|
+
it "should convert various types of table name arguments" do
|
|
1569
1567
|
meta_def(@db, :schema_parse_table) do |t, opts|
|
|
1570
1568
|
[[t, opts]]
|
|
1571
1569
|
end
|
|
1572
1570
|
s1 = @db.schema(:x)
|
|
1573
|
-
s1.
|
|
1574
|
-
@db.schema(:x).object_id.
|
|
1575
|
-
@db.schema(Sequel.identifier(:x)).object_id.
|
|
1571
|
+
s1.must_equal [['x', {:ruby_default=>nil}]]
|
|
1572
|
+
@db.schema(:x).object_id.must_equal s1.object_id
|
|
1573
|
+
@db.schema(Sequel.identifier(:x)).object_id.must_equal s1.object_id
|
|
1576
1574
|
|
|
1577
1575
|
s2 = @db.schema(:x__y)
|
|
1578
|
-
s2.
|
|
1579
|
-
@db.schema(:x__y).object_id.
|
|
1580
|
-
@db.schema(Sequel.qualify(:x, :y)).object_id.
|
|
1576
|
+
s2.must_equal [['y', {:schema=>'x', :ruby_default=>nil}]]
|
|
1577
|
+
@db.schema(:x__y).object_id.must_equal s2.object_id
|
|
1578
|
+
@db.schema(Sequel.qualify(:x, :y)).object_id.must_equal s2.object_id
|
|
1581
1579
|
|
|
1582
1580
|
s2 = @db.schema(Sequel.qualify(:v, :x__y))
|
|
1583
|
-
s2.
|
|
1584
|
-
@db.schema(Sequel.qualify(:v, :x__y)).object_id.
|
|
1585
|
-
@db.schema(Sequel.qualify(:v__x, :y)).object_id.
|
|
1581
|
+
s2.must_equal [['y', {:schema=>'x', :ruby_default=>nil, :information_schema_schema=>Sequel.identifier('v')}]]
|
|
1582
|
+
@db.schema(Sequel.qualify(:v, :x__y)).object_id.must_equal s2.object_id
|
|
1583
|
+
@db.schema(Sequel.qualify(:v__x, :y)).object_id.must_equal s2.object_id
|
|
1586
1584
|
|
|
1587
1585
|
s2 = @db.schema(Sequel.qualify(:u__v, :x__y))
|
|
1588
|
-
s2.
|
|
1589
|
-
@db.schema(Sequel.qualify(:u__v, :x__y)).object_id.
|
|
1590
|
-
@db.schema(Sequel.qualify(Sequel.qualify(:u, :v), Sequel.qualify(:x, :y))).object_id.
|
|
1586
|
+
s2.must_equal [['y', {:schema=>'x', :ruby_default=>nil, :information_schema_schema=>Sequel.qualify('u', 'v')}]]
|
|
1587
|
+
@db.schema(Sequel.qualify(:u__v, :x__y)).object_id.must_equal s2.object_id
|
|
1588
|
+
@db.schema(Sequel.qualify(Sequel.qualify(:u, :v), Sequel.qualify(:x, :y))).object_id.must_equal s2.object_id
|
|
1591
1589
|
end
|
|
1592
1590
|
|
|
1593
|
-
|
|
1591
|
+
it "should correctly parse all supported data types" do
|
|
1594
1592
|
sm = Module.new do
|
|
1595
1593
|
def schema_parse_table(t, opts)
|
|
1596
1594
|
[[:x, {:db_type=>t.to_s, :type=>schema_column_type(t.to_s)}]]
|
|
1597
1595
|
end
|
|
1598
1596
|
end
|
|
1599
1597
|
@db.extend(sm)
|
|
1600
|
-
@db.schema(:tinyint).first.last[:type].
|
|
1601
|
-
@db.schema(:int).first.last[:type].
|
|
1602
|
-
@db.schema(:integer).first.last[:type].
|
|
1603
|
-
@db.schema(:bigint).first.last[:type].
|
|
1604
|
-
@db.schema(:smallint).first.last[:type].
|
|
1605
|
-
@db.schema(:character).first.last[:type].
|
|
1606
|
-
@db.schema(:"character varying").first.last[:type].
|
|
1607
|
-
@db.schema(:varchar).first.last[:type].
|
|
1608
|
-
@db.schema(:"varchar(255)").first.last[:type].
|
|
1609
|
-
@db.schema(:"varchar(255)").first.last[:max_length].
|
|
1610
|
-
@db.schema(:text).first.last[:type].
|
|
1611
|
-
@db.schema(:date).first.last[:type].
|
|
1612
|
-
@db.schema(:datetime).first.last[:type].
|
|
1613
|
-
@db.schema(:timestamp).first.last[:type].
|
|
1614
|
-
@db.schema(:"timestamp with time zone").first.last[:type].
|
|
1615
|
-
@db.schema(:"timestamp without time zone").first.last[:type].
|
|
1616
|
-
@db.schema(:time).first.last[:type].
|
|
1617
|
-
@db.schema(:"time with time zone").first.last[:type].
|
|
1618
|
-
@db.schema(:"time without time zone").first.last[:type].
|
|
1619
|
-
@db.schema(:boolean).first.last[:type].
|
|
1620
|
-
@db.schema(:real).first.last[:type].
|
|
1621
|
-
@db.schema(:float).first.last[:type].
|
|
1622
|
-
@db.schema(:double).first.last[:type].
|
|
1623
|
-
@db.schema(:"double(1,2)").first.last[:type].
|
|
1624
|
-
@db.schema(:"double precision").first.last[:type].
|
|
1625
|
-
@db.schema(:number).first.last[:type].
|
|
1626
|
-
@db.schema(:numeric).first.last[:type].
|
|
1627
|
-
@db.schema(:decimal).first.last[:type].
|
|
1628
|
-
@db.schema(:"number(10,0)").first.last[:type].
|
|
1629
|
-
@db.schema(:"numeric(10, 10)").first.last[:type].
|
|
1630
|
-
@db.schema(:"decimal(10,1)").first.last[:type].
|
|
1631
|
-
@db.schema(:bytea).first.last[:type].
|
|
1632
|
-
@db.schema(:blob).first.last[:type].
|
|
1633
|
-
@db.schema(:image).first.last[:type].
|
|
1634
|
-
@db.schema(:nchar).first.last[:type].
|
|
1635
|
-
@db.schema(:nvarchar).first.last[:type].
|
|
1636
|
-
@db.schema(:ntext).first.last[:type].
|
|
1637
|
-
@db.schema(:smalldatetime).first.last[:type].
|
|
1638
|
-
@db.schema(:binary).first.last[:type].
|
|
1639
|
-
@db.schema(:varbinary).first.last[:type].
|
|
1640
|
-
@db.schema(:enum).first.last[:type].
|
|
1598
|
+
@db.schema(:tinyint).first.last[:type].must_equal :integer
|
|
1599
|
+
@db.schema(:int).first.last[:type].must_equal :integer
|
|
1600
|
+
@db.schema(:integer).first.last[:type].must_equal :integer
|
|
1601
|
+
@db.schema(:bigint).first.last[:type].must_equal :integer
|
|
1602
|
+
@db.schema(:smallint).first.last[:type].must_equal :integer
|
|
1603
|
+
@db.schema(:character).first.last[:type].must_equal :string
|
|
1604
|
+
@db.schema(:"character varying").first.last[:type].must_equal :string
|
|
1605
|
+
@db.schema(:varchar).first.last[:type].must_equal :string
|
|
1606
|
+
@db.schema(:"varchar(255)").first.last[:type].must_equal :string
|
|
1607
|
+
@db.schema(:"varchar(255)").first.last[:max_length].must_equal 255
|
|
1608
|
+
@db.schema(:text).first.last[:type].must_equal :string
|
|
1609
|
+
@db.schema(:date).first.last[:type].must_equal :date
|
|
1610
|
+
@db.schema(:datetime).first.last[:type].must_equal :datetime
|
|
1611
|
+
@db.schema(:timestamp).first.last[:type].must_equal :datetime
|
|
1612
|
+
@db.schema(:"timestamp with time zone").first.last[:type].must_equal :datetime
|
|
1613
|
+
@db.schema(:"timestamp without time zone").first.last[:type].must_equal :datetime
|
|
1614
|
+
@db.schema(:time).first.last[:type].must_equal :time
|
|
1615
|
+
@db.schema(:"time with time zone").first.last[:type].must_equal :time
|
|
1616
|
+
@db.schema(:"time without time zone").first.last[:type].must_equal :time
|
|
1617
|
+
@db.schema(:boolean).first.last[:type].must_equal :boolean
|
|
1618
|
+
@db.schema(:real).first.last[:type].must_equal :float
|
|
1619
|
+
@db.schema(:float).first.last[:type].must_equal :float
|
|
1620
|
+
@db.schema(:double).first.last[:type].must_equal :float
|
|
1621
|
+
@db.schema(:"double(1,2)").first.last[:type].must_equal :float
|
|
1622
|
+
@db.schema(:"double precision").first.last[:type].must_equal :float
|
|
1623
|
+
@db.schema(:number).first.last[:type].must_equal :decimal
|
|
1624
|
+
@db.schema(:numeric).first.last[:type].must_equal :decimal
|
|
1625
|
+
@db.schema(:decimal).first.last[:type].must_equal :decimal
|
|
1626
|
+
@db.schema(:"number(10,0)").first.last[:type].must_equal :integer
|
|
1627
|
+
@db.schema(:"numeric(10, 10)").first.last[:type].must_equal :decimal
|
|
1628
|
+
@db.schema(:"decimal(10,1)").first.last[:type].must_equal :decimal
|
|
1629
|
+
@db.schema(:bytea).first.last[:type].must_equal :blob
|
|
1630
|
+
@db.schema(:blob).first.last[:type].must_equal :blob
|
|
1631
|
+
@db.schema(:image).first.last[:type].must_equal :blob
|
|
1632
|
+
@db.schema(:nchar).first.last[:type].must_equal :string
|
|
1633
|
+
@db.schema(:nvarchar).first.last[:type].must_equal :string
|
|
1634
|
+
@db.schema(:ntext).first.last[:type].must_equal :string
|
|
1635
|
+
@db.schema(:smalldatetime).first.last[:type].must_equal :datetime
|
|
1636
|
+
@db.schema(:binary).first.last[:type].must_equal :blob
|
|
1637
|
+
@db.schema(:varbinary).first.last[:type].must_equal :blob
|
|
1638
|
+
@db.schema(:enum).first.last[:type].must_equal :enum
|
|
1641
1639
|
|
|
1642
1640
|
@db = Sequel.mock(:host=>'postgres')
|
|
1643
1641
|
@db.extend(sm)
|
|
1644
|
-
@db.schema(:interval).first.last[:type].
|
|
1645
|
-
@db.schema(:citext).first.last[:type].
|
|
1642
|
+
@db.schema(:interval).first.last[:type].must_equal :interval
|
|
1643
|
+
@db.schema(:citext).first.last[:type].must_equal :string
|
|
1646
1644
|
|
|
1647
1645
|
@db = Sequel.mock(:host=>'mysql')
|
|
1648
1646
|
@db.extend(sm)
|
|
1649
|
-
@db.schema(:set).first.last[:type].
|
|
1650
|
-
@db.schema(:mediumint).first.last[:type].
|
|
1651
|
-
@db.schema(:mediumtext).first.last[:type].
|
|
1647
|
+
@db.schema(:set).first.last[:type].must_equal :set
|
|
1648
|
+
@db.schema(:mediumint).first.last[:type].must_equal :integer
|
|
1649
|
+
@db.schema(:mediumtext).first.last[:type].must_equal :string
|
|
1652
1650
|
end
|
|
1653
1651
|
end
|