sequel 5.29.0 → 5.30.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG +8 -1922
- data/doc/release_notes/5.30.0.txt +20 -0
- data/lib/sequel/adapters/shared/sqlite.rb +7 -1
- data/lib/sequel/database/transactions.rb +5 -9
- data/lib/sequel/version.rb +1 -1
- metadata +4 -368
- data/Rakefile +0 -151
- data/doc/release_notes/4.0.0.txt +0 -262
- data/doc/release_notes/4.1.0.txt +0 -85
- data/doc/release_notes/4.10.0.txt +0 -226
- data/doc/release_notes/4.11.0.txt +0 -147
- data/doc/release_notes/4.12.0.txt +0 -105
- data/doc/release_notes/4.13.0.txt +0 -169
- data/doc/release_notes/4.14.0.txt +0 -68
- data/doc/release_notes/4.15.0.txt +0 -56
- data/doc/release_notes/4.16.0.txt +0 -36
- data/doc/release_notes/4.17.0.txt +0 -38
- data/doc/release_notes/4.18.0.txt +0 -36
- data/doc/release_notes/4.19.0.txt +0 -45
- data/doc/release_notes/4.2.0.txt +0 -129
- data/doc/release_notes/4.20.0.txt +0 -79
- data/doc/release_notes/4.21.0.txt +0 -94
- data/doc/release_notes/4.22.0.txt +0 -72
- data/doc/release_notes/4.23.0.txt +0 -65
- data/doc/release_notes/4.24.0.txt +0 -99
- data/doc/release_notes/4.25.0.txt +0 -181
- data/doc/release_notes/4.26.0.txt +0 -44
- data/doc/release_notes/4.27.0.txt +0 -78
- data/doc/release_notes/4.28.0.txt +0 -57
- data/doc/release_notes/4.29.0.txt +0 -41
- data/doc/release_notes/4.3.0.txt +0 -40
- data/doc/release_notes/4.30.0.txt +0 -37
- data/doc/release_notes/4.31.0.txt +0 -57
- data/doc/release_notes/4.32.0.txt +0 -132
- data/doc/release_notes/4.33.0.txt +0 -88
- data/doc/release_notes/4.34.0.txt +0 -86
- data/doc/release_notes/4.35.0.txt +0 -130
- data/doc/release_notes/4.36.0.txt +0 -116
- data/doc/release_notes/4.37.0.txt +0 -50
- data/doc/release_notes/4.38.0.txt +0 -67
- data/doc/release_notes/4.39.0.txt +0 -127
- data/doc/release_notes/4.4.0.txt +0 -92
- data/doc/release_notes/4.40.0.txt +0 -179
- data/doc/release_notes/4.41.0.txt +0 -77
- data/doc/release_notes/4.42.0.txt +0 -221
- data/doc/release_notes/4.43.0.txt +0 -87
- data/doc/release_notes/4.44.0.txt +0 -125
- data/doc/release_notes/4.45.0.txt +0 -370
- data/doc/release_notes/4.46.0.txt +0 -404
- data/doc/release_notes/4.47.0.txt +0 -56
- data/doc/release_notes/4.48.0.txt +0 -293
- data/doc/release_notes/4.49.0.txt +0 -222
- data/doc/release_notes/4.5.0.txt +0 -34
- data/doc/release_notes/4.6.0.txt +0 -30
- data/doc/release_notes/4.7.0.txt +0 -103
- data/doc/release_notes/4.8.0.txt +0 -175
- data/doc/release_notes/4.9.0.txt +0 -190
- data/spec/adapter_spec.rb +0 -4
- data/spec/adapters/db2_spec.rb +0 -170
- data/spec/adapters/mssql_spec.rb +0 -828
- data/spec/adapters/mysql_spec.rb +0 -1060
- data/spec/adapters/oracle_spec.rb +0 -371
- data/spec/adapters/postgres_spec.rb +0 -4476
- data/spec/adapters/spec_helper.rb +0 -44
- data/spec/adapters/sqlanywhere_spec.rb +0 -97
- data/spec/adapters/sqlite_spec.rb +0 -652
- data/spec/bin_spec.rb +0 -278
- data/spec/core/connection_pool_spec.rb +0 -1250
- data/spec/core/database_spec.rb +0 -2915
- data/spec/core/dataset_spec.rb +0 -5544
- data/spec/core/deprecated_spec.rb +0 -70
- data/spec/core/expression_filters_spec.rb +0 -1498
- data/spec/core/mock_adapter_spec.rb +0 -722
- data/spec/core/object_graph_spec.rb +0 -336
- data/spec/core/placeholder_literalizer_spec.rb +0 -166
- data/spec/core/schema_generator_spec.rb +0 -214
- data/spec/core/schema_spec.rb +0 -1844
- data/spec/core/spec_helper.rb +0 -24
- data/spec/core/version_spec.rb +0 -14
- data/spec/core_extensions_spec.rb +0 -763
- data/spec/core_model_spec.rb +0 -2
- data/spec/core_spec.rb +0 -1
- data/spec/deprecation_helper.rb +0 -30
- data/spec/extensions/accessed_columns_spec.rb +0 -51
- data/spec/extensions/active_model_spec.rb +0 -99
- data/spec/extensions/after_initialize_spec.rb +0 -28
- data/spec/extensions/any_not_empty_spec.rb +0 -23
- data/spec/extensions/arbitrary_servers_spec.rb +0 -109
- data/spec/extensions/association_dependencies_spec.rb +0 -125
- data/spec/extensions/association_multi_add_remove_spec.rb +0 -1041
- data/spec/extensions/association_pks_spec.rb +0 -423
- data/spec/extensions/association_proxies_spec.rb +0 -100
- data/spec/extensions/auto_literal_strings_spec.rb +0 -205
- data/spec/extensions/auto_validations_spec.rb +0 -229
- data/spec/extensions/blacklist_security_spec.rb +0 -95
- data/spec/extensions/blank_spec.rb +0 -69
- data/spec/extensions/boolean_readers_spec.rb +0 -93
- data/spec/extensions/boolean_subsets_spec.rb +0 -47
- data/spec/extensions/caching_spec.rb +0 -273
- data/spec/extensions/caller_logging_spec.rb +0 -52
- data/spec/extensions/class_table_inheritance_spec.rb +0 -750
- data/spec/extensions/column_conflicts_spec.rb +0 -75
- data/spec/extensions/column_select_spec.rb +0 -129
- data/spec/extensions/columns_introspection_spec.rb +0 -90
- data/spec/extensions/columns_updated_spec.rb +0 -35
- data/spec/extensions/composition_spec.rb +0 -248
- data/spec/extensions/connection_expiration_spec.rb +0 -151
- data/spec/extensions/connection_validator_spec.rb +0 -144
- data/spec/extensions/constant_sql_override_spec.rb +0 -24
- data/spec/extensions/constraint_validations_plugin_spec.rb +0 -300
- data/spec/extensions/constraint_validations_spec.rb +0 -439
- data/spec/extensions/core_refinements_spec.rb +0 -528
- data/spec/extensions/csv_serializer_spec.rb +0 -183
- data/spec/extensions/current_datetime_timestamp_spec.rb +0 -27
- data/spec/extensions/dataset_associations_spec.rb +0 -365
- data/spec/extensions/dataset_source_alias_spec.rb +0 -51
- data/spec/extensions/date_arithmetic_spec.rb +0 -181
- data/spec/extensions/datetime_parse_to_time_spec.rb +0 -169
- data/spec/extensions/def_dataset_method_spec.rb +0 -100
- data/spec/extensions/defaults_setter_spec.rb +0 -150
- data/spec/extensions/delay_add_association_spec.rb +0 -73
- data/spec/extensions/dirty_spec.rb +0 -222
- data/spec/extensions/duplicate_columns_handler_spec.rb +0 -104
- data/spec/extensions/eager_each_spec.rb +0 -62
- data/spec/extensions/eager_graph_eager_spec.rb +0 -100
- data/spec/extensions/empty_array_consider_nulls_spec.rb +0 -24
- data/spec/extensions/empty_failure_backtraces_spec.rb +0 -60
- data/spec/extensions/error_splitter_spec.rb +0 -18
- data/spec/extensions/error_sql_spec.rb +0 -20
- data/spec/extensions/escaped_like_spec.rb +0 -40
- data/spec/extensions/eval_inspect_spec.rb +0 -81
- data/spec/extensions/exclude_or_null_spec.rb +0 -15
- data/spec/extensions/finder_spec.rb +0 -260
- data/spec/extensions/force_encoding_spec.rb +0 -126
- data/spec/extensions/freeze_datasets_spec.rb +0 -31
- data/spec/extensions/graph_each_spec.rb +0 -113
- data/spec/extensions/hook_class_methods_spec.rb +0 -402
- data/spec/extensions/identifier_mangling_spec.rb +0 -201
- data/spec/extensions/implicit_subquery_spec.rb +0 -58
- data/spec/extensions/index_caching_spec.rb +0 -66
- data/spec/extensions/inflector_spec.rb +0 -183
- data/spec/extensions/input_transformer_spec.rb +0 -69
- data/spec/extensions/insert_conflict_spec.rb +0 -103
- data/spec/extensions/insert_returning_select_spec.rb +0 -72
- data/spec/extensions/instance_filters_spec.rb +0 -79
- data/spec/extensions/instance_hooks_spec.rb +0 -246
- data/spec/extensions/integer64_spec.rb +0 -22
- data/spec/extensions/inverted_subsets_spec.rb +0 -33
- data/spec/extensions/json_serializer_spec.rb +0 -346
- data/spec/extensions/lazy_attributes_spec.rb +0 -183
- data/spec/extensions/list_spec.rb +0 -291
- data/spec/extensions/looser_typecasting_spec.rb +0 -43
- data/spec/extensions/many_through_many_spec.rb +0 -2177
- data/spec/extensions/migration_spec.rb +0 -864
- data/spec/extensions/modification_detection_spec.rb +0 -93
- data/spec/extensions/mssql_optimistic_locking_spec.rb +0 -92
- data/spec/extensions/named_timezones_spec.rb +0 -218
- data/spec/extensions/nested_attributes_spec.rb +0 -815
- data/spec/extensions/null_dataset_spec.rb +0 -85
- data/spec/extensions/optimistic_locking_spec.rb +0 -127
- data/spec/extensions/pagination_spec.rb +0 -116
- data/spec/extensions/pg_array_associations_spec.rb +0 -802
- data/spec/extensions/pg_array_ops_spec.rb +0 -144
- data/spec/extensions/pg_array_spec.rb +0 -398
- data/spec/extensions/pg_auto_constraint_validations_spec.rb +0 -209
- data/spec/extensions/pg_enum_spec.rb +0 -118
- data/spec/extensions/pg_extended_date_support_spec.rb +0 -126
- data/spec/extensions/pg_hstore_ops_spec.rb +0 -238
- data/spec/extensions/pg_hstore_spec.rb +0 -219
- data/spec/extensions/pg_inet_ops_spec.rb +0 -102
- data/spec/extensions/pg_inet_spec.rb +0 -72
- data/spec/extensions/pg_interval_spec.rb +0 -103
- data/spec/extensions/pg_json_ops_spec.rb +0 -356
- data/spec/extensions/pg_json_spec.rb +0 -451
- data/spec/extensions/pg_loose_count_spec.rb +0 -23
- data/spec/extensions/pg_range_ops_spec.rb +0 -60
- data/spec/extensions/pg_range_spec.rb +0 -600
- data/spec/extensions/pg_row_ops_spec.rb +0 -61
- data/spec/extensions/pg_row_plugin_spec.rb +0 -60
- data/spec/extensions/pg_row_spec.rb +0 -363
- data/spec/extensions/pg_static_cache_updater_spec.rb +0 -93
- data/spec/extensions/pg_timestamptz_spec.rb +0 -17
- data/spec/extensions/prepared_statements_safe_spec.rb +0 -66
- data/spec/extensions/prepared_statements_spec.rb +0 -177
- data/spec/extensions/pretty_table_spec.rb +0 -123
- data/spec/extensions/query_spec.rb +0 -94
- data/spec/extensions/rcte_tree_spec.rb +0 -387
- data/spec/extensions/round_timestamps_spec.rb +0 -39
- data/spec/extensions/s_spec.rb +0 -60
- data/spec/extensions/schema_caching_spec.rb +0 -64
- data/spec/extensions/schema_dumper_spec.rb +0 -870
- data/spec/extensions/select_remove_spec.rb +0 -38
- data/spec/extensions/sequel_4_dataset_methods_spec.rb +0 -121
- data/spec/extensions/serialization_modification_detection_spec.rb +0 -98
- data/spec/extensions/serialization_spec.rb +0 -365
- data/spec/extensions/server_block_spec.rb +0 -135
- data/spec/extensions/server_logging_spec.rb +0 -45
- data/spec/extensions/sharding_spec.rb +0 -197
- data/spec/extensions/shared_caching_spec.rb +0 -151
- data/spec/extensions/single_table_inheritance_spec.rb +0 -347
- data/spec/extensions/singular_table_names_spec.rb +0 -22
- data/spec/extensions/skip_create_refresh_spec.rb +0 -18
- data/spec/extensions/spec_helper.rb +0 -70
- data/spec/extensions/split_array_nil_spec.rb +0 -24
- data/spec/extensions/split_values_spec.rb +0 -57
- data/spec/extensions/sql_comments_spec.rb +0 -33
- data/spec/extensions/sql_expr_spec.rb +0 -59
- data/spec/extensions/static_cache_cache_spec.rb +0 -35
- data/spec/extensions/static_cache_spec.rb +0 -471
- data/spec/extensions/string_agg_spec.rb +0 -90
- data/spec/extensions/string_date_time_spec.rb +0 -95
- data/spec/extensions/string_stripper_spec.rb +0 -68
- data/spec/extensions/subclasses_spec.rb +0 -79
- data/spec/extensions/subset_conditions_spec.rb +0 -38
- data/spec/extensions/symbol_aref_refinement_spec.rb +0 -28
- data/spec/extensions/symbol_as_refinement_spec.rb +0 -21
- data/spec/extensions/synchronize_sql_spec.rb +0 -124
- data/spec/extensions/table_select_spec.rb +0 -83
- data/spec/extensions/tactical_eager_loading_spec.rb +0 -402
- data/spec/extensions/thread_local_timezones_spec.rb +0 -67
- data/spec/extensions/throw_failures_spec.rb +0 -74
- data/spec/extensions/timestamps_spec.rb +0 -209
- data/spec/extensions/to_dot_spec.rb +0 -153
- data/spec/extensions/touch_spec.rb +0 -226
- data/spec/extensions/tree_spec.rb +0 -334
- data/spec/extensions/typecast_on_load_spec.rb +0 -86
- data/spec/extensions/unlimited_update_spec.rb +0 -21
- data/spec/extensions/update_or_create_spec.rb +0 -83
- data/spec/extensions/update_primary_key_spec.rb +0 -105
- data/spec/extensions/update_refresh_spec.rb +0 -59
- data/spec/extensions/uuid_spec.rb +0 -101
- data/spec/extensions/validate_associated_spec.rb +0 -52
- data/spec/extensions/validation_class_methods_spec.rb +0 -1040
- data/spec/extensions/validation_contexts_spec.rb +0 -31
- data/spec/extensions/validation_helpers_spec.rb +0 -525
- data/spec/extensions/whitelist_security_spec.rb +0 -157
- data/spec/extensions/xml_serializer_spec.rb +0 -213
- data/spec/files/bad_down_migration/001_create_alt_basic.rb +0 -4
- data/spec/files/bad_down_migration/002_create_alt_advanced.rb +0 -4
- data/spec/files/bad_timestamped_migrations/1273253849_create_sessions.rb +0 -9
- data/spec/files/bad_timestamped_migrations/1273253851_create_nodes.rb +0 -9
- data/spec/files/bad_timestamped_migrations/1273253853_3_create_users.rb +0 -3
- data/spec/files/bad_up_migration/001_create_alt_basic.rb +0 -4
- data/spec/files/bad_up_migration/002_create_alt_advanced.rb +0 -3
- data/spec/files/convert_to_timestamp_migrations/001_create_sessions.rb +0 -9
- data/spec/files/convert_to_timestamp_migrations/002_create_nodes.rb +0 -9
- data/spec/files/convert_to_timestamp_migrations/003_3_create_users.rb +0 -4
- data/spec/files/convert_to_timestamp_migrations/1273253850_create_artists.rb +0 -9
- data/spec/files/convert_to_timestamp_migrations/1273253852_create_albums.rb +0 -9
- data/spec/files/double_migration/001_create_sessions.rb +0 -9
- data/spec/files/double_migration/002_create_nodes.rb +0 -19
- data/spec/files/double_migration/003_3_create_users.rb +0 -4
- data/spec/files/duplicate_integer_migrations/001_create_alt_advanced.rb +0 -4
- data/spec/files/duplicate_integer_migrations/001_create_alt_basic.rb +0 -4
- data/spec/files/duplicate_timestamped_migrations/1273253849_create_sessions.rb +0 -9
- data/spec/files/duplicate_timestamped_migrations/1273253853_create_nodes.rb +0 -9
- data/spec/files/duplicate_timestamped_migrations/1273253853_create_users.rb +0 -4
- data/spec/files/empty_migration/001_create_sessions.rb +0 -9
- data/spec/files/empty_migration/002_create_nodes.rb +0 -0
- data/spec/files/empty_migration/003_3_create_users.rb +0 -4
- data/spec/files/integer_migrations/001_create_sessions.rb +0 -9
- data/spec/files/integer_migrations/002_create_nodes.rb +0 -9
- data/spec/files/integer_migrations/003_3_create_users.rb +0 -4
- data/spec/files/interleaved_timestamped_migrations/1273253849_create_sessions.rb +0 -9
- data/spec/files/interleaved_timestamped_migrations/1273253850_create_artists.rb +0 -9
- data/spec/files/interleaved_timestamped_migrations/1273253851_create_nodes.rb +0 -9
- data/spec/files/interleaved_timestamped_migrations/1273253852_create_albums.rb +0 -9
- data/spec/files/interleaved_timestamped_migrations/1273253853_3_create_users.rb +0 -4
- data/spec/files/missing_integer_migrations/001_create_alt_basic.rb +0 -4
- data/spec/files/missing_integer_migrations/003_create_alt_advanced.rb +0 -4
- data/spec/files/missing_timestamped_migrations/1273253849_create_sessions.rb +0 -9
- data/spec/files/missing_timestamped_migrations/1273253853_3_create_users.rb +0 -4
- data/spec/files/reversible_migrations/001_reversible.rb +0 -5
- data/spec/files/reversible_migrations/002_reversible.rb +0 -5
- data/spec/files/reversible_migrations/003_reversible.rb +0 -5
- data/spec/files/reversible_migrations/004_reversible.rb +0 -5
- data/spec/files/reversible_migrations/005_reversible.rb +0 -10
- data/spec/files/reversible_migrations/006_reversible.rb +0 -10
- data/spec/files/reversible_migrations/007_reversible.rb +0 -10
- data/spec/files/timestamped_migrations/1273253849_create_sessions.rb +0 -9
- data/spec/files/timestamped_migrations/1273253851_create_nodes.rb +0 -9
- data/spec/files/timestamped_migrations/1273253853_3_create_users.rb +0 -4
- data/spec/files/transaction_specified_migrations/001_create_alt_basic.rb +0 -4
- data/spec/files/transaction_specified_migrations/002_create_basic.rb +0 -4
- data/spec/files/transaction_unspecified_migrations/001_create_alt_basic.rb +0 -3
- data/spec/files/transaction_unspecified_migrations/002_create_basic.rb +0 -3
- data/spec/files/uppercase_timestamped_migrations/1273253849_CREATE_SESSIONS.RB +0 -9
- data/spec/files/uppercase_timestamped_migrations/1273253851_CREATE_NODES.RB +0 -9
- data/spec/files/uppercase_timestamped_migrations/1273253853_3_CREATE_USERS.RB +0 -4
- data/spec/guards_helper.rb +0 -59
- data/spec/integration/associations_test.rb +0 -2597
- data/spec/integration/database_test.rb +0 -113
- data/spec/integration/dataset_test.rb +0 -2037
- data/spec/integration/eager_loader_test.rb +0 -687
- data/spec/integration/migrator_test.rb +0 -262
- data/spec/integration/model_test.rb +0 -203
- data/spec/integration/plugin_test.rb +0 -2423
- data/spec/integration/prepared_statement_test.rb +0 -405
- data/spec/integration/schema_test.rb +0 -903
- data/spec/integration/spec_helper.rb +0 -71
- data/spec/integration/timezone_test.rb +0 -86
- data/spec/integration/transaction_test.rb +0 -603
- data/spec/integration/type_test.rb +0 -127
- data/spec/model/association_reflection_spec.rb +0 -803
- data/spec/model/associations_spec.rb +0 -4738
- data/spec/model/base_spec.rb +0 -875
- data/spec/model/class_dataset_methods_spec.rb +0 -146
- data/spec/model/dataset_methods_spec.rb +0 -198
- data/spec/model/eager_loading_spec.rb +0 -2377
- data/spec/model/hooks_spec.rb +0 -370
- data/spec/model/inflector_spec.rb +0 -26
- data/spec/model/model_spec.rb +0 -956
- data/spec/model/plugins_spec.rb +0 -429
- data/spec/model/record_spec.rb +0 -2118
- data/spec/model/spec_helper.rb +0 -46
- data/spec/model/validations_spec.rb +0 -220
- data/spec/model_no_assoc_spec.rb +0 -1
- data/spec/model_spec.rb +0 -1
- data/spec/plugin_spec.rb +0 -1
- data/spec/sequel_coverage.rb +0 -15
- data/spec/sequel_warning.rb +0 -5
- data/spec/spec_config.rb +0 -12
@@ -1,214 +0,0 @@
|
|
1
|
-
require_relative "spec_helper"
|
2
|
-
|
3
|
-
describe Sequel::Schema::CreateTableGenerator do
|
4
|
-
before do
|
5
|
-
@generator = Sequel::Schema::CreateTableGenerator.new(Sequel.mock) do
|
6
|
-
string(:title).must_be_nil
|
7
|
-
column(:body, :text).must_be_nil
|
8
|
-
foreign_key(:parent_id).must_be_nil
|
9
|
-
primary_key(:id).must_be_nil
|
10
|
-
check('price > 100').must_be_nil
|
11
|
-
constraint(:xxx){{:yyy => :zzz}}.must_be_nil
|
12
|
-
index(:title).must_be_nil
|
13
|
-
index([:title, :body], :unique => true).must_be_nil
|
14
|
-
foreign_key(:node_id, :nodes).must_be_nil
|
15
|
-
foreign_key(:deferrable_node_id, :nodes, :deferrable => true).must_be_nil
|
16
|
-
primary_key([:title, :parent_id], :name => :cpk).must_be_nil
|
17
|
-
foreign_key([:node_id, :prop_id], :nodes_props, :name => :cfk).must_be_nil
|
18
|
-
end
|
19
|
-
@columns, @indexes, @constraints = @generator.columns, @generator.indexes, @generator.constraints
|
20
|
-
end
|
21
|
-
|
22
|
-
it "should respond to everything" do
|
23
|
-
@generator.respond_to?(:foo).must_equal true
|
24
|
-
end
|
25
|
-
|
26
|
-
it "should respond adding types" do
|
27
|
-
c = Class.new(Sequel::Schema::CreateTableGenerator)
|
28
|
-
c2 = Class.new
|
29
|
-
def c2.to_s; 'Foo' end
|
30
|
-
c.add_type_method(c2)
|
31
|
-
gen = c.new(Sequel.mock) do
|
32
|
-
Foo :bar
|
33
|
-
end
|
34
|
-
|
35
|
-
gen.columns.first.values_at(:name, :type).must_equal [:bar, c2]
|
36
|
-
end
|
37
|
-
|
38
|
-
it "should primary key column first" do
|
39
|
-
@columns.first[:name].must_equal :id
|
40
|
-
@columns.first[:primary_key].must_equal true
|
41
|
-
@columns[3][:name].must_equal :parent_id
|
42
|
-
@columns[3][:primary_key].must_be_nil
|
43
|
-
end
|
44
|
-
|
45
|
-
it "should respect existing column order if primary_key :keep_order is used" do
|
46
|
-
generator = Sequel::Schema::CreateTableGenerator.new(Sequel.mock) do
|
47
|
-
string :title
|
48
|
-
primary_key :id, :keep_order=>true
|
49
|
-
end
|
50
|
-
|
51
|
-
columns = generator.columns
|
52
|
-
columns.last[:name].must_equal :id
|
53
|
-
columns.last[:primary_key].must_equal true
|
54
|
-
columns.first[:name].must_equal :title
|
55
|
-
columns.first[:primary_key].must_be_nil
|
56
|
-
end
|
57
|
-
|
58
|
-
it "should handle SQL::Identifier and SQL::QualifiedIdentifier as foreign_key arguments" do
|
59
|
-
generator = Sequel::Schema::CreateTableGenerator.new(Sequel.mock) do
|
60
|
-
foreign_key :a_id, Sequel.identifier(:as)
|
61
|
-
foreign_key :b_id, Sequel.qualify(:c, :b)
|
62
|
-
end
|
63
|
-
|
64
|
-
columns = generator.columns
|
65
|
-
columns.first.values_at(:name, :table).must_equal [:a_id, Sequel.identifier(:as)]
|
66
|
-
columns.last.values_at(:name, :table).must_equal [:b_id, Sequel.qualify(:c, :b)]
|
67
|
-
end
|
68
|
-
|
69
|
-
it "counts definitions correctly" do
|
70
|
-
@columns.size.must_equal 6
|
71
|
-
@indexes.size.must_equal 2
|
72
|
-
@constraints.size.must_equal 4
|
73
|
-
end
|
74
|
-
|
75
|
-
it "retrieves primary key name" do
|
76
|
-
@generator.primary_key_name.must_equal :id
|
77
|
-
end
|
78
|
-
|
79
|
-
it "keeps columns in order" do
|
80
|
-
@columns[1][:name].must_equal :title
|
81
|
-
@columns[1][:type].must_equal :string
|
82
|
-
@columns[2][:name].must_equal :body
|
83
|
-
@columns[2][:type].must_equal :text
|
84
|
-
end
|
85
|
-
|
86
|
-
it "creates foreign key column" do
|
87
|
-
@columns[3][:name].must_equal :parent_id
|
88
|
-
@columns[3][:type].must_equal Integer
|
89
|
-
@columns[4][:name].must_equal :node_id
|
90
|
-
@columns[4][:type].must_equal Integer
|
91
|
-
end
|
92
|
-
|
93
|
-
it "creates deferrable altered foreign key column" do
|
94
|
-
@columns[5][:name].must_equal :deferrable_node_id
|
95
|
-
@columns[5][:type].must_equal Integer
|
96
|
-
@columns[5][:deferrable].must_equal true
|
97
|
-
end
|
98
|
-
|
99
|
-
it "uses table for foreign key columns, if specified" do
|
100
|
-
@columns[3][:table].must_be_nil
|
101
|
-
@columns[4][:table].must_equal :nodes
|
102
|
-
@constraints[3][:table].must_equal :nodes_props
|
103
|
-
end
|
104
|
-
|
105
|
-
it "finds columns" do
|
106
|
-
[:title, :body, :parent_id, :id].each do |col|
|
107
|
-
@generator.has_column?(col).must_equal true
|
108
|
-
end
|
109
|
-
@generator.has_column?(:foo).wont_equal true
|
110
|
-
end
|
111
|
-
|
112
|
-
it "creates constraints" do
|
113
|
-
@constraints[0][:name].must_be_nil
|
114
|
-
@constraints[0][:type].must_equal :check
|
115
|
-
@constraints[0][:check].must_equal ['price > 100']
|
116
|
-
|
117
|
-
@constraints[1][:name].must_equal :xxx
|
118
|
-
@constraints[1][:type].must_equal :check
|
119
|
-
@constraints[1][:check].must_be_kind_of Proc
|
120
|
-
|
121
|
-
@constraints[2][:name].must_equal :cpk
|
122
|
-
@constraints[2][:type].must_equal :primary_key
|
123
|
-
@constraints[2][:columns].must_equal [ :title, :parent_id ]
|
124
|
-
|
125
|
-
@constraints[3][:name].must_equal :cfk
|
126
|
-
@constraints[3][:type].must_equal :foreign_key
|
127
|
-
@constraints[3][:columns].must_equal [ :node_id, :prop_id ]
|
128
|
-
@constraints[3][:table].must_equal :nodes_props
|
129
|
-
end
|
130
|
-
|
131
|
-
it "creates indexes" do
|
132
|
-
@indexes[0][:columns].must_equal [:title]
|
133
|
-
@indexes[1][:columns].must_equal [:title, :body]
|
134
|
-
end
|
135
|
-
end
|
136
|
-
|
137
|
-
describe Sequel::Schema::AlterTableGenerator do
|
138
|
-
before do
|
139
|
-
@generator = Sequel::Schema::AlterTableGenerator.new(Sequel.mock) do
|
140
|
-
add_column(:aaa, :text).must_be_nil
|
141
|
-
drop_column(:bbb).must_be_nil
|
142
|
-
rename_column(:ccc, :ho).must_be_nil
|
143
|
-
set_column_type(:ddd, :float).must_be_nil
|
144
|
-
set_column_default(:eee, 1).must_be_nil
|
145
|
-
add_index([:fff, :ggg]).must_be_nil
|
146
|
-
drop_index(:hhh).must_be_nil
|
147
|
-
drop_index(:hhh, :name=>:blah_blah).must_be_nil
|
148
|
-
add_full_text_index(:blah).must_be_nil
|
149
|
-
add_spatial_index(:geom).must_be_nil
|
150
|
-
add_index(:blah, :type => :hash).must_be_nil
|
151
|
-
add_index(:blah, :where => {:something => true}).must_be_nil
|
152
|
-
add_constraint(:con1, 'fred > 100').must_be_nil
|
153
|
-
drop_constraint(:con2).must_be_nil
|
154
|
-
add_unique_constraint([:aaa, :bbb, :ccc], :name => :con3).must_be_nil
|
155
|
-
add_primary_key(:id).must_be_nil
|
156
|
-
add_foreign_key(:node_id, :nodes).must_be_nil
|
157
|
-
add_primary_key([:aaa, :bbb]).must_be_nil
|
158
|
-
add_foreign_key([:node_id, :prop_id], :nodes_props).must_be_nil
|
159
|
-
add_foreign_key([:node_id, :prop_id], :nodes_props, :name => :fkey).must_be_nil
|
160
|
-
drop_foreign_key(:node_id).must_be_nil
|
161
|
-
drop_foreign_key([:node_id, :prop_id]).must_be_nil
|
162
|
-
drop_foreign_key([:node_id, :prop_id], :name => :fkey).must_be_nil
|
163
|
-
end
|
164
|
-
end
|
165
|
-
|
166
|
-
it "should generate operation records" do
|
167
|
-
@generator.operations.must_equal [
|
168
|
-
{:op => :add_column, :name => :aaa, :type => :text},
|
169
|
-
{:op => :drop_column, :name => :bbb},
|
170
|
-
{:op => :rename_column, :name => :ccc, :new_name => :ho},
|
171
|
-
{:op => :set_column_type, :name => :ddd, :type => :float},
|
172
|
-
{:op => :set_column_default, :name => :eee, :default => 1},
|
173
|
-
{:op => :add_index, :columns => [:fff, :ggg]},
|
174
|
-
{:op => :drop_index, :columns => [:hhh]},
|
175
|
-
{:op => :drop_index, :columns => [:hhh], :name=>:blah_blah},
|
176
|
-
{:op => :add_index, :columns => [:blah], :type => :full_text},
|
177
|
-
{:op => :add_index, :columns => [:geom], :type => :spatial},
|
178
|
-
{:op => :add_index, :columns => [:blah], :type => :hash},
|
179
|
-
{:op => :add_index, :columns => [:blah], :where => {:something => true}},
|
180
|
-
{:op => :add_constraint, :type => :check, :name => :con1, :check => ['fred > 100']},
|
181
|
-
{:op => :drop_constraint, :name => :con2},
|
182
|
-
{:op => :add_constraint, :type => :unique, :name => :con3, :columns => [:aaa, :bbb, :ccc]},
|
183
|
-
{:op => :add_column, :name => :id, :type => Integer, :primary_key=>true, :auto_increment=>true},
|
184
|
-
{:op => :add_column, :name => :node_id, :type => Integer, :table=>:nodes},
|
185
|
-
{:op => :add_constraint, :type => :primary_key, :columns => [:aaa, :bbb]},
|
186
|
-
{:op => :add_constraint, :type => :foreign_key, :columns => [:node_id, :prop_id], :table => :nodes_props},
|
187
|
-
{:op => :add_constraint, :type => :foreign_key, :columns => [:node_id, :prop_id], :table => :nodes_props, :name => :fkey},
|
188
|
-
{:op => :drop_constraint, :type => :foreign_key, :columns => [:node_id]},
|
189
|
-
{:op => :drop_column, :name => :node_id},
|
190
|
-
{:op => :drop_constraint, :type => :foreign_key, :columns => [:node_id, :prop_id]},
|
191
|
-
{:op => :drop_constraint, :type => :foreign_key, :columns => [:node_id, :prop_id], :name => :fkey},
|
192
|
-
]
|
193
|
-
end
|
194
|
-
end
|
195
|
-
|
196
|
-
describe "Sequel::Schema::CreateTableGenerator generic type methods" do
|
197
|
-
it "should store the type class in :type for each column" do
|
198
|
-
Sequel::Schema::CreateTableGenerator.new(Sequel.mock) do
|
199
|
-
String :a
|
200
|
-
Integer :b
|
201
|
-
Fixnum :c
|
202
|
-
Bignum :d
|
203
|
-
Float :e
|
204
|
-
BigDecimal :f
|
205
|
-
Date :g
|
206
|
-
DateTime :h
|
207
|
-
Time :i
|
208
|
-
Numeric :j
|
209
|
-
File :k
|
210
|
-
TrueClass :l
|
211
|
-
FalseClass :m
|
212
|
-
end.columns.map{|c| c[:type]}.must_equal [String, Integer, Integer, :Bignum, Float, BigDecimal, Date, DateTime, Time, Numeric, File, TrueClass, FalseClass]
|
213
|
-
end
|
214
|
-
end
|
data/spec/core/schema_spec.rb
DELETED
@@ -1,1844 +0,0 @@
|
|
1
|
-
require_relative "spec_helper"
|
2
|
-
|
3
|
-
describe "DB#create_table" do
|
4
|
-
before do
|
5
|
-
@db = Sequel.mock
|
6
|
-
end
|
7
|
-
|
8
|
-
it "should accept the table name" do
|
9
|
-
@db.create_table(:cats){}.must_be_nil
|
10
|
-
@db.sqls.must_equal ['CREATE TABLE cats ()']
|
11
|
-
end
|
12
|
-
|
13
|
-
with_symbol_splitting "should accept the table name with splittable symbols" do
|
14
|
-
@db.create_table(:cats__cats) {}
|
15
|
-
@db.sqls.must_equal ['CREATE TABLE cats.cats ()']
|
16
|
-
end
|
17
|
-
|
18
|
-
it "should accept the table name in multiple formats" do
|
19
|
-
@db.create_table(Sequel[:cats][:cats]) {}
|
20
|
-
@db.create_table("cats__cats1") {}
|
21
|
-
@db.create_table(Sequel.identifier(:cats__cats2)) {}
|
22
|
-
@db.create_table(Sequel.qualify(:cats3, :cats)) {}
|
23
|
-
@db.sqls.must_equal ['CREATE TABLE cats.cats ()', 'CREATE TABLE cats__cats1 ()', 'CREATE TABLE cats__cats2 ()', 'CREATE TABLE cats3.cats ()']
|
24
|
-
end
|
25
|
-
|
26
|
-
it "should raise an error if the table name argument is not valid" do
|
27
|
-
proc{@db.create_table(1) {}}.must_raise(Sequel::Error)
|
28
|
-
proc{@db.create_table(Sequel.as(:cats, :c)) {}}.must_raise(Sequel::Error)
|
29
|
-
end
|
30
|
-
|
31
|
-
it "should remove cached schema entry" do
|
32
|
-
@db.instance_variable_set(:@schemas, {'cats'=>[]})
|
33
|
-
@db.create_table(:cats){Integer :a}
|
34
|
-
@db.instance_variable_get(:@schemas).must_be :empty?
|
35
|
-
end
|
36
|
-
|
37
|
-
it "should accept multiple columns" do
|
38
|
-
@db.create_table(:cats) do
|
39
|
-
column :id, :integer
|
40
|
-
column :name, :text
|
41
|
-
end
|
42
|
-
@db.sqls.must_equal ['CREATE TABLE cats (id integer, name text)']
|
43
|
-
end
|
44
|
-
|
45
|
-
it "should accept method calls as data types" do
|
46
|
-
@db.create_table(:cats) do
|
47
|
-
integer :id
|
48
|
-
text :name
|
49
|
-
end
|
50
|
-
@db.sqls.must_equal ['CREATE TABLE cats (id integer, name text)']
|
51
|
-
end
|
52
|
-
|
53
|
-
it "should transform types given as ruby classes to database-specific types" do
|
54
|
-
@db.create_table(:cats) do
|
55
|
-
String :a
|
56
|
-
Integer :b
|
57
|
-
Fixnum :c
|
58
|
-
Bignum :d
|
59
|
-
Float :e
|
60
|
-
BigDecimal :f
|
61
|
-
Date :g
|
62
|
-
DateTime :h
|
63
|
-
Time :i
|
64
|
-
Numeric :j
|
65
|
-
File :k
|
66
|
-
TrueClass :l
|
67
|
-
FalseClass :m
|
68
|
-
column :n, Integer
|
69
|
-
primary_key :o, :type=>String
|
70
|
-
foreign_key :p, :f, :type=>Date
|
71
|
-
end
|
72
|
-
@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)']
|
73
|
-
end
|
74
|
-
|
75
|
-
it "should transform types given as ruby classes to database-specific types" do
|
76
|
-
@db.default_string_column_size = 50
|
77
|
-
@db.create_table(:cats) do
|
78
|
-
String :a
|
79
|
-
String :a2, :size=>13
|
80
|
-
String :a3, :fixed=>true
|
81
|
-
String :a4, :size=>13, :fixed=>true
|
82
|
-
String :a5, :text=>true
|
83
|
-
varchar :a6
|
84
|
-
varchar :a7, :size=>13
|
85
|
-
end
|
86
|
-
@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))']
|
87
|
-
end
|
88
|
-
|
89
|
-
it "should allow the use of modifiers with ruby class types" do
|
90
|
-
@db.create_table(:cats) do
|
91
|
-
String :a, :size=>50
|
92
|
-
String :b, :text=>true
|
93
|
-
String :c, :fixed=>true, :size=>40
|
94
|
-
Time :d, :only_time=>true
|
95
|
-
BigDecimal :e, :size=>[11,2]
|
96
|
-
end
|
97
|
-
@db.sqls.must_equal ['CREATE TABLE cats (a varchar(50), b text, c char(40), d time, e numeric(11, 2))']
|
98
|
-
end
|
99
|
-
|
100
|
-
it "should allow the use of modifiers with ruby class types" do
|
101
|
-
c = Class.new
|
102
|
-
def c.name; 'Fixnum'; end
|
103
|
-
@db.create_table(:cats) do
|
104
|
-
column :a, c
|
105
|
-
end
|
106
|
-
@db.sqls.must_equal ['CREATE TABLE cats (a integer)']
|
107
|
-
end
|
108
|
-
|
109
|
-
it "should raise an error if you use a ruby class that isn't handled" do
|
110
|
-
proc{@db.create_table(:cats){column :a, Class}}.must_raise(Sequel::Error)
|
111
|
-
end
|
112
|
-
|
113
|
-
it "should accept primary key definition" do
|
114
|
-
@db.create_table(:cats) do
|
115
|
-
primary_key :id
|
116
|
-
end
|
117
|
-
@db.sqls.must_equal ['CREATE TABLE cats (id integer PRIMARY KEY AUTOINCREMENT)']
|
118
|
-
|
119
|
-
@db.create_table(:cats) do
|
120
|
-
primary_key :id, :serial, :auto_increment => false
|
121
|
-
end
|
122
|
-
@db.sqls.must_equal ['CREATE TABLE cats (id serial PRIMARY KEY)']
|
123
|
-
|
124
|
-
@db.create_table(:cats) do
|
125
|
-
primary_key :id, :type => :serial, :auto_increment => false
|
126
|
-
end
|
127
|
-
@db.sqls.must_equal ['CREATE TABLE cats (id serial PRIMARY KEY)']
|
128
|
-
|
129
|
-
@db.create_table(:cats) do
|
130
|
-
Integer :a
|
131
|
-
primary_key :id
|
132
|
-
end
|
133
|
-
@db.sqls.must_equal ['CREATE TABLE cats (id integer PRIMARY KEY AUTOINCREMENT, a integer)']
|
134
|
-
|
135
|
-
@db.create_table(:cats) do
|
136
|
-
Integer :a
|
137
|
-
primary_key :id, :keep_order=>true
|
138
|
-
end
|
139
|
-
@db.sqls.must_equal ['CREATE TABLE cats (a integer, id integer PRIMARY KEY AUTOINCREMENT)']
|
140
|
-
end
|
141
|
-
|
142
|
-
it "should allow naming primary key constraint with :primary_key_constraint_name option" do
|
143
|
-
@db.create_table(:cats) do
|
144
|
-
primary_key :id, :primary_key_constraint_name=>:foo
|
145
|
-
end
|
146
|
-
@db.sqls.must_equal ['CREATE TABLE cats (id integer CONSTRAINT foo PRIMARY KEY AUTOINCREMENT)']
|
147
|
-
end
|
148
|
-
|
149
|
-
it "should automatically set primary key column NOT NULL if database doesn't do it automatically" do
|
150
|
-
def @db.can_add_primary_key_constraint_on_nullable_columns?; false end
|
151
|
-
@db.create_table(:cats) do
|
152
|
-
primary_key :id
|
153
|
-
end
|
154
|
-
@db.sqls.must_equal ['CREATE TABLE cats (id integer NOT NULL PRIMARY KEY AUTOINCREMENT)']
|
155
|
-
end
|
156
|
-
|
157
|
-
it "should automatically set primary key column NOT NULL when adding constraint if database doesn't do it automatically" do
|
158
|
-
def @db.can_add_primary_key_constraint_on_nullable_columns?; false end
|
159
|
-
@db.create_table(:cats) do
|
160
|
-
String :id
|
161
|
-
primary_key [:id]
|
162
|
-
end
|
163
|
-
@db.sqls.must_equal ['CREATE TABLE cats (id varchar(255) NOT NULL, PRIMARY KEY (id))']
|
164
|
-
end
|
165
|
-
|
166
|
-
it "should handling splitting named column constraints into table constraints if unsupported" do
|
167
|
-
def @db.supports_named_column_constraints?; false end
|
168
|
-
@db.create_table(:cats) do
|
169
|
-
primary_key :id, :primary_key_constraint_name=>:foo
|
170
|
-
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
|
171
|
-
end
|
172
|
-
@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))']
|
173
|
-
end
|
174
|
-
|
175
|
-
it "should accept and literalize default values" do
|
176
|
-
@db.create_table(:cats) do
|
177
|
-
integer :id, :default => 123
|
178
|
-
text :name, :default => "abc'def"
|
179
|
-
end
|
180
|
-
@db.sqls.must_equal ["CREATE TABLE cats (id integer DEFAULT 123, name text DEFAULT 'abc''def')"]
|
181
|
-
end
|
182
|
-
|
183
|
-
it "should accept not null definition" do
|
184
|
-
@db.create_table(:cats) do
|
185
|
-
integer :id
|
186
|
-
text :name, :null => false
|
187
|
-
text :name2, :allow_null => false
|
188
|
-
end
|
189
|
-
@db.sqls.must_equal ["CREATE TABLE cats (id integer, name text NOT NULL, name2 text NOT NULL)"]
|
190
|
-
end
|
191
|
-
|
192
|
-
it "should accept null definition" do
|
193
|
-
@db.create_table(:cats) do
|
194
|
-
integer :id
|
195
|
-
text :name, :null => true
|
196
|
-
text :name2, :allow_null => true
|
197
|
-
end
|
198
|
-
@db.sqls.must_equal ["CREATE TABLE cats (id integer, name text NULL, name2 text NULL)"]
|
199
|
-
end
|
200
|
-
|
201
|
-
it "should accept unique definition" do
|
202
|
-
@db.create_table(:cats) do
|
203
|
-
integer :id
|
204
|
-
text :name, :unique => true
|
205
|
-
end
|
206
|
-
@db.sqls.must_equal ["CREATE TABLE cats (id integer, name text UNIQUE)"]
|
207
|
-
end
|
208
|
-
|
209
|
-
it "should allow naming unique constraint with :unique_constraint_name option" do
|
210
|
-
@db.create_table(:cats) do
|
211
|
-
text :name, :unique => true, :unique_constraint_name=>:foo
|
212
|
-
end
|
213
|
-
@db.sqls.must_equal ["CREATE TABLE cats (name text CONSTRAINT foo UNIQUE)"]
|
214
|
-
end
|
215
|
-
|
216
|
-
it "should handle not deferred unique constraints" do
|
217
|
-
@db.create_table(:cats) do
|
218
|
-
integer :id
|
219
|
-
text :name
|
220
|
-
unique :name, :deferrable=>false
|
221
|
-
end
|
222
|
-
@db.sqls.must_equal ["CREATE TABLE cats (id integer, name text, UNIQUE (name) NOT DEFERRABLE)"]
|
223
|
-
end
|
224
|
-
|
225
|
-
it "should handle deferred unique constraints" do
|
226
|
-
@db.create_table(:cats) do
|
227
|
-
integer :id
|
228
|
-
text :name
|
229
|
-
unique :name, :deferrable=>true
|
230
|
-
end
|
231
|
-
@db.sqls.must_equal ["CREATE TABLE cats (id integer, name text, UNIQUE (name) DEFERRABLE INITIALLY DEFERRED)"]
|
232
|
-
end
|
233
|
-
|
234
|
-
it "should handle deferred initially immediate unique constraints" do
|
235
|
-
@db.create_table(:cats) do
|
236
|
-
integer :id
|
237
|
-
text :name
|
238
|
-
unique :name, :deferrable=>:immediate
|
239
|
-
end
|
240
|
-
@db.sqls.must_equal ["CREATE TABLE cats (id integer, name text, UNIQUE (name) DEFERRABLE INITIALLY IMMEDIATE)"]
|
241
|
-
end
|
242
|
-
|
243
|
-
it "should handle deferred unique column constraints" do
|
244
|
-
@db.create_table(:cats) do
|
245
|
-
integer :id, :unique=>true, :unique_deferrable=>true
|
246
|
-
integer :i, :unique=>true, :unique_deferrable=>:immediate
|
247
|
-
integer :j, :unique=>true, :unique_deferrable=>false
|
248
|
-
end
|
249
|
-
@db.sqls.must_equal ["CREATE TABLE cats (id integer UNIQUE DEFERRABLE INITIALLY DEFERRED, i integer UNIQUE DEFERRABLE INITIALLY IMMEDIATE, j integer UNIQUE NOT DEFERRABLE)"]
|
250
|
-
end
|
251
|
-
|
252
|
-
it "should handle deferred primary key column constraints" do
|
253
|
-
@db.create_table(:cats) do
|
254
|
-
integer :id, :primary_key=>true, :primary_key_deferrable=>true
|
255
|
-
integer :i, :primary_key=>true, :primary_key_deferrable=>:immediate
|
256
|
-
integer :j, :primary_key=>true, :primary_key_deferrable=>false
|
257
|
-
end
|
258
|
-
@db.sqls.must_equal ["CREATE TABLE cats (id integer PRIMARY KEY DEFERRABLE INITIALLY DEFERRED, i integer PRIMARY KEY DEFERRABLE INITIALLY IMMEDIATE, j integer PRIMARY KEY NOT DEFERRABLE)"]
|
259
|
-
end
|
260
|
-
|
261
|
-
it "should accept unsigned definition" do
|
262
|
-
@db.create_table(:cats) do
|
263
|
-
integer :value, :unsigned => true
|
264
|
-
end
|
265
|
-
@db.sqls.must_equal ["CREATE TABLE cats (value integer UNSIGNED)"]
|
266
|
-
end
|
267
|
-
|
268
|
-
it "should accept [SET|ENUM](...) types" do
|
269
|
-
@db.create_table(:cats) do
|
270
|
-
set :color, :elements => ['black', 'tricolor', 'grey']
|
271
|
-
end
|
272
|
-
@db.sqls.must_equal ["CREATE TABLE cats (color set('black', 'tricolor', 'grey'))"]
|
273
|
-
end
|
274
|
-
|
275
|
-
it "should accept varchar size" do
|
276
|
-
@db.create_table(:cats) do
|
277
|
-
varchar :name
|
278
|
-
end
|
279
|
-
@db.sqls.must_equal ["CREATE TABLE cats (name varchar(255))"]
|
280
|
-
@db.create_table(:cats) do
|
281
|
-
varchar :name, :size => 51
|
282
|
-
end
|
283
|
-
@db.sqls.must_equal ["CREATE TABLE cats (name varchar(51))"]
|
284
|
-
end
|
285
|
-
|
286
|
-
it "should use double precision for double type" do
|
287
|
-
@db.create_table(:cats) do
|
288
|
-
double :name
|
289
|
-
end
|
290
|
-
@db.sqls.must_equal ["CREATE TABLE cats (name double precision)"]
|
291
|
-
end
|
292
|
-
|
293
|
-
it "should accept foreign keys without options" do
|
294
|
-
@db.create_table(:cats) do
|
295
|
-
foreign_key :project_id
|
296
|
-
end
|
297
|
-
@db.sqls.must_equal ["CREATE TABLE cats (project_id integer)"]
|
298
|
-
end
|
299
|
-
|
300
|
-
it "should accept foreign keys with options" do
|
301
|
-
@db.create_table(:cats) do
|
302
|
-
foreign_key :project_id, :table => :projects
|
303
|
-
end
|
304
|
-
@db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects)"]
|
305
|
-
end
|
306
|
-
|
307
|
-
it "should accept foreign keys with separate table argument" do
|
308
|
-
@db.create_table(:cats) do
|
309
|
-
foreign_key :project_id, :projects, :default=>3
|
310
|
-
end
|
311
|
-
@db.sqls.must_equal ["CREATE TABLE cats (project_id integer DEFAULT 3 REFERENCES projects)"]
|
312
|
-
end
|
313
|
-
|
314
|
-
it "should allowing naming foreign key constraint with :foreign_key_constraint_name option" do
|
315
|
-
@db.create_table(:cats) do
|
316
|
-
foreign_key :project_id, :projects, :foreign_key_constraint_name=>:foo
|
317
|
-
end
|
318
|
-
@db.sqls.must_equal ["CREATE TABLE cats (project_id integer CONSTRAINT foo REFERENCES projects)"]
|
319
|
-
end
|
320
|
-
|
321
|
-
it "should raise an error if the table argument to foreign_key isn't a hash, symbol, or nil" do
|
322
|
-
proc{@db.create_table(:cats){foreign_key :project_id, Object.new, :default=>3}}.must_raise(Sequel::Error)
|
323
|
-
end
|
324
|
-
|
325
|
-
it "should accept foreign keys with arbitrary keys" do
|
326
|
-
@db.create_table(:cats) do
|
327
|
-
foreign_key :project_id, :table => :projects, :key => :id
|
328
|
-
end
|
329
|
-
@db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects(id))"]
|
330
|
-
|
331
|
-
@db.create_table(:cats) do
|
332
|
-
foreign_key :project_id, :table => :projects, :key => :zzz
|
333
|
-
end
|
334
|
-
@db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects(zzz))"]
|
335
|
-
end
|
336
|
-
|
337
|
-
it "should accept foreign keys with ON DELETE clause" do
|
338
|
-
@db.create_table(:cats) do
|
339
|
-
foreign_key :project_id, :table => :projects, :on_delete => :restrict
|
340
|
-
end
|
341
|
-
@db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE RESTRICT)"]
|
342
|
-
|
343
|
-
@db.create_table(:cats) do
|
344
|
-
foreign_key :project_id, :table => :projects, :on_delete => :cascade
|
345
|
-
end
|
346
|
-
@db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE CASCADE)"]
|
347
|
-
|
348
|
-
@db.create_table(:cats) do
|
349
|
-
foreign_key :project_id, :table => :projects, :on_delete => :no_action
|
350
|
-
end
|
351
|
-
@db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE NO ACTION)"]
|
352
|
-
|
353
|
-
@db.create_table(:cats) do
|
354
|
-
foreign_key :project_id, :table => :projects, :on_delete => :set_null
|
355
|
-
end
|
356
|
-
@db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE SET NULL)"]
|
357
|
-
|
358
|
-
@db.create_table(:cats) do
|
359
|
-
foreign_key :project_id, :table => :projects, :on_delete => :set_default
|
360
|
-
end
|
361
|
-
@db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE SET DEFAULT)"]
|
362
|
-
|
363
|
-
@db.create_table(:cats) do
|
364
|
-
foreign_key :project_id, :table => :projects, :on_delete => 'NO ACTION FOO'
|
365
|
-
end
|
366
|
-
@db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE NO ACTION FOO)"]
|
367
|
-
end
|
368
|
-
|
369
|
-
it "should accept foreign keys with ON UPDATE clause" do
|
370
|
-
@db.create_table(:cats) do
|
371
|
-
foreign_key :project_id, :table => :projects, :on_update => :restrict
|
372
|
-
end
|
373
|
-
@db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects ON UPDATE RESTRICT)"]
|
374
|
-
|
375
|
-
@db.create_table(:cats) do
|
376
|
-
foreign_key :project_id, :table => :projects, :on_update => :cascade
|
377
|
-
end
|
378
|
-
@db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects ON UPDATE CASCADE)"]
|
379
|
-
|
380
|
-
@db.create_table(:cats) do
|
381
|
-
foreign_key :project_id, :table => :projects, :on_update => :no_action
|
382
|
-
end
|
383
|
-
@db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects ON UPDATE NO ACTION)"]
|
384
|
-
|
385
|
-
@db.create_table(:cats) do
|
386
|
-
foreign_key :project_id, :table => :projects, :on_update => :set_null
|
387
|
-
end
|
388
|
-
@db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects ON UPDATE SET NULL)"]
|
389
|
-
|
390
|
-
@db.create_table(:cats) do
|
391
|
-
foreign_key :project_id, :table => :projects, :on_update => :set_default
|
392
|
-
end
|
393
|
-
@db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects ON UPDATE SET DEFAULT)"]
|
394
|
-
|
395
|
-
@db.create_table(:cats) do
|
396
|
-
foreign_key :project_id, :table => :projects, :on_update => 'SET DEFAULT FOO'
|
397
|
-
end
|
398
|
-
@db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects ON UPDATE SET DEFAULT FOO)"]
|
399
|
-
end
|
400
|
-
|
401
|
-
it "should accept foreign keys with deferrable option" do
|
402
|
-
@db.create_table(:cats) do
|
403
|
-
foreign_key :project_id, :projects, :deferrable=>true
|
404
|
-
end
|
405
|
-
@db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects DEFERRABLE INITIALLY DEFERRED)"]
|
406
|
-
end
|
407
|
-
|
408
|
-
it "should accept collation" do
|
409
|
-
@db.create_table(:cats) do
|
410
|
-
String :name, :collate => :utf8_bin
|
411
|
-
end
|
412
|
-
@db.sqls.must_equal ['CREATE TABLE cats (name varchar(255) COLLATE utf8_bin)']
|
413
|
-
end
|
414
|
-
|
415
|
-
it "should accept collation as a String, treated literally" do
|
416
|
-
@db.create_table(:cats) do
|
417
|
-
String :name, :collate => '"utf8_bin"'
|
418
|
-
end
|
419
|
-
@db.sqls.must_equal ['CREATE TABLE cats (name varchar(255) COLLATE "utf8_bin")']
|
420
|
-
end
|
421
|
-
|
422
|
-
it "should accept inline index definition" do
|
423
|
-
@db.create_table(:cats) do
|
424
|
-
integer :id, :index => true
|
425
|
-
end
|
426
|
-
@db.sqls.must_equal ["CREATE TABLE cats (id integer)", "CREATE INDEX cats_id_index ON cats (id)"]
|
427
|
-
end
|
428
|
-
|
429
|
-
it "should accept inline index definition with a hash of options" do
|
430
|
-
@db.create_table(:cats) do
|
431
|
-
integer :id, :index => {:unique=>true}
|
432
|
-
end
|
433
|
-
@db.sqls.must_equal ["CREATE TABLE cats (id integer)", "CREATE UNIQUE INDEX cats_id_index ON cats (id)"]
|
434
|
-
end
|
435
|
-
|
436
|
-
it "should accept inline index definition for foreign keys" do
|
437
|
-
@db.create_table(:cats) do
|
438
|
-
foreign_key :project_id, :table => :projects, :on_delete => :cascade, :index => true
|
439
|
-
end
|
440
|
-
@db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE CASCADE)",
|
441
|
-
"CREATE INDEX cats_project_id_index ON cats (project_id)"]
|
442
|
-
end
|
443
|
-
|
444
|
-
it "should accept inline index definition for foreign keys with a hash of options" do
|
445
|
-
@db.create_table(:cats) do
|
446
|
-
foreign_key :project_id, :table => :projects, :on_delete => :cascade, :index => {:unique=>true}
|
447
|
-
end
|
448
|
-
@db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE CASCADE)",
|
449
|
-
"CREATE UNIQUE INDEX cats_project_id_index ON cats (project_id)"]
|
450
|
-
end
|
451
|
-
|
452
|
-
it "should accept index definitions" do
|
453
|
-
@db.create_table(:cats) do
|
454
|
-
integer :id
|
455
|
-
index :id
|
456
|
-
end
|
457
|
-
@db.sqls.must_equal ["CREATE TABLE cats (id integer)", "CREATE INDEX cats_id_index ON cats (id)"]
|
458
|
-
end
|
459
|
-
|
460
|
-
it "should accept unique constraint definitions" do
|
461
|
-
@db.create_table(:cats) do
|
462
|
-
text :name
|
463
|
-
unique :name
|
464
|
-
end
|
465
|
-
@db.sqls.must_equal ["CREATE TABLE cats (name text, UNIQUE (name))"]
|
466
|
-
end
|
467
|
-
|
468
|
-
it "should accept partial index definitions" do
|
469
|
-
def @db.supports_partial_indexes?() true end
|
470
|
-
@db.create_table(:cats) do
|
471
|
-
integer :id
|
472
|
-
index :id, :where=>proc{id > 1}
|
473
|
-
end
|
474
|
-
@db.sqls.must_equal ["CREATE TABLE cats (id integer)", "CREATE INDEX cats_id_index ON cats (id) WHERE (id > 1)"]
|
475
|
-
end
|
476
|
-
|
477
|
-
it "should raise an error if partial indexes are not supported" do
|
478
|
-
proc do
|
479
|
-
@db.create_table(:cats) do
|
480
|
-
integer :id
|
481
|
-
index :id, :where=>proc{id > 1}
|
482
|
-
end
|
483
|
-
end.must_raise(Sequel::Error)
|
484
|
-
end
|
485
|
-
|
486
|
-
it "should not raise on index error for unsupported index definitions if ignore_index_errors is used" do
|
487
|
-
@db.create_table(:cats, :ignore_index_errors=>true) do
|
488
|
-
text :name
|
489
|
-
full_text_index :name
|
490
|
-
end
|
491
|
-
end
|
492
|
-
|
493
|
-
it "should raise on full-text index definitions" do
|
494
|
-
proc {
|
495
|
-
@db.create_table(:cats) do
|
496
|
-
text :name
|
497
|
-
full_text_index :name
|
498
|
-
end
|
499
|
-
}.must_raise(Sequel::Error)
|
500
|
-
end
|
501
|
-
|
502
|
-
it "should raise on spatial index definitions" do
|
503
|
-
proc {
|
504
|
-
@db.create_table(:cats) do
|
505
|
-
point :geom
|
506
|
-
spatial_index :geom
|
507
|
-
end
|
508
|
-
}.must_raise(Sequel::Error)
|
509
|
-
end
|
510
|
-
|
511
|
-
it "should raise on partial index definitions" do
|
512
|
-
proc {
|
513
|
-
@db.create_table(:cats) do
|
514
|
-
text :name
|
515
|
-
index :name, :where => {:something => true}
|
516
|
-
end
|
517
|
-
}.must_raise(Sequel::Error)
|
518
|
-
end
|
519
|
-
|
520
|
-
it "should raise index definitions with type" do
|
521
|
-
proc {
|
522
|
-
@db.create_table(:cats) do
|
523
|
-
text :name
|
524
|
-
index :name, :type => :hash
|
525
|
-
end
|
526
|
-
}.must_raise(Sequel::Error)
|
527
|
-
end
|
528
|
-
|
529
|
-
it "should ignore errors if the database raises an error on an index creation statement and the :ignore_index_errors option is used" do
|
530
|
-
@db.define_singleton_method(:execute_ddl){|*a| raise Sequel::DatabaseError if /blah/.match(a.first); super(*a)}
|
531
|
-
lambda{@db.create_table(:cats){Integer :id; index :blah; index :id}}.must_raise(Sequel::DatabaseError)
|
532
|
-
@db.sqls.must_equal ['CREATE TABLE cats (id integer)']
|
533
|
-
@db.create_table(:cats, :ignore_index_errors=>true){Integer :id; index :blah; index :id}
|
534
|
-
@db.sqls.must_equal ['CREATE TABLE cats (id integer)', 'CREATE INDEX cats_id_index ON cats (id)']
|
535
|
-
end
|
536
|
-
|
537
|
-
it "should not use savepoints around index creation if running inside a transaction if :ignore_index_errors option is used" do
|
538
|
-
@db.define_singleton_method(:execute_ddl){|*a| super(*a); raise Sequel::DatabaseError if /blah/.match(a.first)}
|
539
|
-
@db.transaction{@db.create_table(:cats, :ignore_index_errors=>true){Integer :id; index :blah; index :id}}
|
540
|
-
@db.sqls.must_equal ["BEGIN", "CREATE TABLE cats (id integer)", "CREATE INDEX cats_blah_index ON cats (blah)", "CREATE INDEX cats_id_index ON cats (id)", "COMMIT"]
|
541
|
-
end
|
542
|
-
|
543
|
-
it "should use savepoints around index creation if running inside a transaction if :ignore_index_errors option is used and transactional schema modifications are supported" do
|
544
|
-
@db.define_singleton_method(:supports_transactional_ddl?){true}
|
545
|
-
@db.define_singleton_method(:execute_ddl){|*a| super(*a); raise Sequel::DatabaseError if /blah/.match(a.first)}
|
546
|
-
@db.transaction{@db.create_table(:cats, :ignore_index_errors=>true){Integer :id; index :blah; index :id}}
|
547
|
-
@db.sqls.must_equal ["BEGIN", "CREATE TABLE cats (id integer)", "SAVEPOINT autopoint_1", "CREATE INDEX cats_blah_index ON cats (blah)", "ROLLBACK TO SAVEPOINT autopoint_1", "SAVEPOINT autopoint_1", "CREATE INDEX cats_id_index ON cats (id)", "RELEASE SAVEPOINT autopoint_1", "COMMIT"]
|
548
|
-
end
|
549
|
-
|
550
|
-
it "should accept multiple index definitions" do
|
551
|
-
@db.create_table(:cats) do
|
552
|
-
integer :id
|
553
|
-
index :id
|
554
|
-
index :name
|
555
|
-
end
|
556
|
-
@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)"]
|
557
|
-
end
|
558
|
-
|
559
|
-
it "should accept functional indexes" do
|
560
|
-
@db.create_table(:cats) do
|
561
|
-
integer :id
|
562
|
-
index Sequel.function(:lower, :name)
|
563
|
-
end
|
564
|
-
@db.sqls.must_equal ["CREATE TABLE cats (id integer)", "CREATE INDEX cats_lower_name__index ON cats (lower(name))"]
|
565
|
-
end
|
566
|
-
|
567
|
-
it "should accept indexes with identifiers" do
|
568
|
-
@db.create_table(:cats) do
|
569
|
-
integer :id
|
570
|
-
index Sequel.identifier(:lower__name)
|
571
|
-
end
|
572
|
-
@db.sqls.must_equal ["CREATE TABLE cats (id integer)", "CREATE INDEX cats_lower__name_index ON cats (lower__name)"]
|
573
|
-
end
|
574
|
-
|
575
|
-
it "should accept custom index names" do
|
576
|
-
@db.create_table(:cats) do
|
577
|
-
integer :id
|
578
|
-
index :id, :name => 'abc'
|
579
|
-
end
|
580
|
-
@db.sqls.must_equal ["CREATE TABLE cats (id integer)", "CREATE INDEX abc ON cats (id)"]
|
581
|
-
end
|
582
|
-
|
583
|
-
it "should accept unique index definitions" do
|
584
|
-
@db.create_table(:cats) do
|
585
|
-
integer :id
|
586
|
-
index :id, :unique => true
|
587
|
-
end
|
588
|
-
@db.sqls.must_equal ["CREATE TABLE cats (id integer)", "CREATE UNIQUE INDEX cats_id_index ON cats (id)"]
|
589
|
-
end
|
590
|
-
|
591
|
-
it "should accept composite index definitions" do
|
592
|
-
@db.create_table(:cats) do
|
593
|
-
integer :id
|
594
|
-
index [:id, :name], :unique => true
|
595
|
-
end
|
596
|
-
@db.sqls.must_equal ["CREATE TABLE cats (id integer)", "CREATE UNIQUE INDEX cats_id_name_index ON cats (id, name)"]
|
597
|
-
end
|
598
|
-
|
599
|
-
it "should accept unnamed constraint definitions with blocks" do
|
600
|
-
@db.create_table(:cats) do
|
601
|
-
integer :score
|
602
|
-
check{(x > 0) & (y < 1)}
|
603
|
-
end
|
604
|
-
@db.sqls.must_equal ["CREATE TABLE cats (score integer, CHECK ((x > 0) AND (y < 1)))"]
|
605
|
-
end
|
606
|
-
|
607
|
-
it "should accept unnamed constraint definitions with function calls" do
|
608
|
-
@db.create_table(:cats) do
|
609
|
-
integer :score
|
610
|
-
check{f(x)}
|
611
|
-
end
|
612
|
-
@db.sqls.must_equal ["CREATE TABLE cats (score integer, CHECK (f(x)))"]
|
613
|
-
end
|
614
|
-
|
615
|
-
it "should accept unnamed constraint definitions" do
|
616
|
-
@db.create_table(:cats) do
|
617
|
-
check 'price < ?', 100
|
618
|
-
end
|
619
|
-
@db.sqls.must_equal ["CREATE TABLE cats (CHECK (price < 100))"]
|
620
|
-
end
|
621
|
-
|
622
|
-
it "should accept arrays of pairs constraints" do
|
623
|
-
@db.create_table(:cats) do
|
624
|
-
check [[:price, 100]]
|
625
|
-
end
|
626
|
-
@db.sqls.must_equal ["CREATE TABLE cats (CHECK (price = 100))"]
|
627
|
-
end
|
628
|
-
|
629
|
-
it "should accept hash constraints" do
|
630
|
-
@db.create_table(:cats) do
|
631
|
-
check :price=>100
|
632
|
-
end
|
633
|
-
@db.sqls.must_equal ["CREATE TABLE cats (CHECK (price = 100))"]
|
634
|
-
end
|
635
|
-
|
636
|
-
it "should accept array constraints" do
|
637
|
-
@db.create_table(:cats) do
|
638
|
-
check [Sequel.expr(:x) > 0, Sequel.expr(:y) < 1]
|
639
|
-
end
|
640
|
-
@db.sqls.must_equal ["CREATE TABLE cats (CHECK ((x > 0) AND (y < 1)))"]
|
641
|
-
end
|
642
|
-
|
643
|
-
it "should accept expression constraints" do
|
644
|
-
@db.create_table(:cats) do
|
645
|
-
check Sequel.&(Sequel.expr(:x) > 0, Sequel.expr(:y) < 1)
|
646
|
-
end
|
647
|
-
@db.sqls.must_equal ["CREATE TABLE cats (CHECK ((x > 0) AND (y < 1)))"]
|
648
|
-
end
|
649
|
-
|
650
|
-
it "should accept named constraint definitions" do
|
651
|
-
@db.create_table(:cats) do
|
652
|
-
integer :score
|
653
|
-
constraint :valid_score, 'score <= 100'
|
654
|
-
end
|
655
|
-
@db.sqls.must_equal ["CREATE TABLE cats (score integer, CONSTRAINT valid_score CHECK (score <= 100))"]
|
656
|
-
end
|
657
|
-
|
658
|
-
it "should accept named constraint definitions with options" do
|
659
|
-
@db.create_table(:cats) do
|
660
|
-
integer :score
|
661
|
-
constraint({:name=>:valid_score, :deferrable=>true}, 'score <= 100')
|
662
|
-
end
|
663
|
-
@db.sqls.must_equal ["CREATE TABLE cats (score integer, CONSTRAINT valid_score CHECK (score <= 100) DEFERRABLE INITIALLY DEFERRED)"]
|
664
|
-
end
|
665
|
-
|
666
|
-
it "should accept named constraint definitions with block" do
|
667
|
-
@db.create_table(:cats) do
|
668
|
-
constraint(:blah_blah){(x.sql_number > 0) & (y.sql_number < 1)}
|
669
|
-
end
|
670
|
-
@db.sqls.must_equal ["CREATE TABLE cats (CONSTRAINT blah_blah CHECK ((x > 0) AND (y < 1)))"]
|
671
|
-
end
|
672
|
-
|
673
|
-
it "should raise an error if an invalid constraint type is used" do
|
674
|
-
proc{@db.create_table(:cats){unique [:a, :b], :type=>:bb}}.must_raise(Sequel::Error)
|
675
|
-
end
|
676
|
-
|
677
|
-
it "should accept composite primary keys" do
|
678
|
-
@db.create_table(:cats) do
|
679
|
-
integer :a
|
680
|
-
integer :b
|
681
|
-
primary_key [:a, :b]
|
682
|
-
end
|
683
|
-
@db.sqls.must_equal ["CREATE TABLE cats (a integer, b integer, PRIMARY KEY (a, b))"]
|
684
|
-
end
|
685
|
-
|
686
|
-
it "should accept named composite primary keys" do
|
687
|
-
@db.create_table(:cats) do
|
688
|
-
integer :a
|
689
|
-
integer :b
|
690
|
-
primary_key [:a, :b], :name => :cpk
|
691
|
-
end
|
692
|
-
@db.sqls.must_equal ["CREATE TABLE cats (a integer, b integer, CONSTRAINT cpk PRIMARY KEY (a, b))"]
|
693
|
-
end
|
694
|
-
|
695
|
-
it "should accept composite foreign keys" do
|
696
|
-
@db.create_table(:cats) do
|
697
|
-
integer :a
|
698
|
-
integer :b
|
699
|
-
foreign_key [:a, :b], :abc
|
700
|
-
end
|
701
|
-
@db.sqls.must_equal ["CREATE TABLE cats (a integer, b integer, FOREIGN KEY (a, b) REFERENCES abc)"]
|
702
|
-
end
|
703
|
-
|
704
|
-
it "should accept named composite foreign keys" do
|
705
|
-
@db.create_table(:cats) do
|
706
|
-
integer :a
|
707
|
-
integer :b
|
708
|
-
foreign_key [:a, :b], :abc, :name => :cfk
|
709
|
-
end
|
710
|
-
@db.sqls.must_equal ["CREATE TABLE cats (a integer, b integer, CONSTRAINT cfk FOREIGN KEY (a, b) REFERENCES abc)"]
|
711
|
-
end
|
712
|
-
|
713
|
-
it "should accept composite foreign keys with arbitrary keys" do
|
714
|
-
@db.create_table(:cats) do
|
715
|
-
integer :a
|
716
|
-
integer :b
|
717
|
-
foreign_key [:a, :b], :abc, :key => [:real_a, :real_b]
|
718
|
-
end
|
719
|
-
@db.sqls.must_equal ["CREATE TABLE cats (a integer, b integer, FOREIGN KEY (a, b) REFERENCES abc(real_a, real_b))"]
|
720
|
-
|
721
|
-
@db.create_table(:cats) do
|
722
|
-
integer :a
|
723
|
-
integer :b
|
724
|
-
foreign_key [:a, :b], :abc, :key => [:z, :x]
|
725
|
-
end
|
726
|
-
@db.sqls.must_equal ["CREATE TABLE cats (a integer, b integer, FOREIGN KEY (a, b) REFERENCES abc(z, x))"]
|
727
|
-
end
|
728
|
-
|
729
|
-
it "should accept composite foreign keys with on delete and on update clauses" do
|
730
|
-
@db.create_table(:cats) do
|
731
|
-
integer :a
|
732
|
-
integer :b
|
733
|
-
foreign_key [:a, :b], :abc, :on_delete => :cascade
|
734
|
-
end
|
735
|
-
@db.sqls.must_equal ["CREATE TABLE cats (a integer, b integer, FOREIGN KEY (a, b) REFERENCES abc ON DELETE CASCADE)"]
|
736
|
-
|
737
|
-
@db.create_table(:cats) do
|
738
|
-
integer :a
|
739
|
-
integer :b
|
740
|
-
foreign_key [:a, :b], :abc, :on_update => :no_action
|
741
|
-
end
|
742
|
-
@db.sqls.must_equal ["CREATE TABLE cats (a integer, b integer, FOREIGN KEY (a, b) REFERENCES abc ON UPDATE NO ACTION)"]
|
743
|
-
|
744
|
-
@db.create_table(:cats) do
|
745
|
-
integer :a
|
746
|
-
integer :b
|
747
|
-
foreign_key [:a, :b], :abc, :on_delete => :restrict, :on_update => :set_default
|
748
|
-
end
|
749
|
-
@db.sqls.must_equal ["CREATE TABLE cats (a integer, b integer, FOREIGN KEY (a, b) REFERENCES abc ON DELETE RESTRICT ON UPDATE SET DEFAULT)"]
|
750
|
-
|
751
|
-
@db.create_table(:cats) do
|
752
|
-
integer :a
|
753
|
-
integer :b
|
754
|
-
foreign_key [:a, :b], :abc, :key => [:x, :y], :on_delete => :set_null, :on_update => :set_null
|
755
|
-
end
|
756
|
-
@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)"]
|
757
|
-
end
|
758
|
-
|
759
|
-
it "should accept an :as option to create a table from the results of a dataset" do
|
760
|
-
@db.create_table(:cats, :as=>@db[:a])
|
761
|
-
@db.sqls.must_equal ['CREATE TABLE cats AS SELECT * FROM a']
|
762
|
-
end
|
763
|
-
|
764
|
-
it "should accept an :as option to create a table from a SELECT string" do
|
765
|
-
@db.create_table(:cats, :as=>'SELECT * FROM a')
|
766
|
-
@db.sqls.must_equal ['CREATE TABLE cats AS SELECT * FROM a']
|
767
|
-
end
|
768
|
-
|
769
|
-
it "should raise an Error if both a block and an :as argument are given" do
|
770
|
-
proc{@db.create_table(:cats, :as=>@db[:a]){}}.must_raise(Sequel::Error)
|
771
|
-
end
|
772
|
-
end
|
773
|
-
|
774
|
-
describe "DB#create_table!" do
|
775
|
-
before do
|
776
|
-
@db = Sequel.mock
|
777
|
-
end
|
778
|
-
|
779
|
-
it "should create the table if it does not exist" do
|
780
|
-
@db.define_singleton_method(:table_exists?){|a| false}
|
781
|
-
@db.create_table!(:cats){|*a|}.must_be_nil
|
782
|
-
@db.sqls.must_equal ['CREATE TABLE cats ()']
|
783
|
-
end
|
784
|
-
|
785
|
-
it "should drop the table before creating it if it already exists" do
|
786
|
-
@db.define_singleton_method(:table_exists?){|a| true}
|
787
|
-
@db.create_table!(:cats){|*a|}
|
788
|
-
@db.sqls.must_equal ['DROP TABLE cats', 'CREATE TABLE cats ()']
|
789
|
-
end
|
790
|
-
|
791
|
-
it "should use IF EXISTS if the database supports it" do
|
792
|
-
@db.define_singleton_method(:supports_drop_table_if_exists?){true}
|
793
|
-
@db.create_table!(:cats){|*a|}
|
794
|
-
@db.sqls.must_equal ['DROP TABLE IF EXISTS cats', 'CREATE TABLE cats ()']
|
795
|
-
end
|
796
|
-
end
|
797
|
-
|
798
|
-
describe "DB#create_table?" do
|
799
|
-
before do
|
800
|
-
@db = Sequel.mock
|
801
|
-
end
|
802
|
-
|
803
|
-
it "should not create the table if the table already exists" do
|
804
|
-
@db.define_singleton_method(:table_exists?){|a| true}
|
805
|
-
@db.create_table?(:cats){|*a|}.must_be_nil
|
806
|
-
@db.sqls.must_equal []
|
807
|
-
end
|
808
|
-
|
809
|
-
it "should create the table if the table doesn't already exist" do
|
810
|
-
@db.define_singleton_method(:table_exists?){|a| false}
|
811
|
-
@db.create_table?(:cats){|*a|}
|
812
|
-
@db.sqls.must_equal ['CREATE TABLE cats ()']
|
813
|
-
end
|
814
|
-
|
815
|
-
it "should use IF NOT EXISTS if the database supports that" do
|
816
|
-
@db.define_singleton_method(:supports_create_table_if_not_exists?){true}
|
817
|
-
@db.create_table?(:cats){|*a|}
|
818
|
-
@db.sqls.must_equal ['CREATE TABLE IF NOT EXISTS cats ()']
|
819
|
-
end
|
820
|
-
|
821
|
-
it "should not use IF NOT EXISTS if the indexes are created" do
|
822
|
-
@db.define_singleton_method(:table_exists?){|a| false}
|
823
|
-
@db.define_singleton_method(:supports_create_table_if_not_exists?){true}
|
824
|
-
@db.create_table?(:cats){|*a| Integer :a, :index=>true}
|
825
|
-
@db.sqls.must_equal ['CREATE TABLE cats (a integer)', 'CREATE INDEX cats_a_index ON cats (a)']
|
826
|
-
|
827
|
-
@db.define_singleton_method(:table_exists?){|a| true}
|
828
|
-
@db.create_table?(:cats){|*a| Integer :a, :index=>true}
|
829
|
-
@db.sqls.must_equal []
|
830
|
-
end
|
831
|
-
end
|
832
|
-
|
833
|
-
describe "DB#create_join_table" do
|
834
|
-
before do
|
835
|
-
@db = Sequel.mock
|
836
|
-
end
|
837
|
-
|
838
|
-
it "should take a hash with foreign keys and table name values" do
|
839
|
-
@db.create_join_table(:cat_id=>:cats, :dog_id=>:dogs).must_be_nil
|
840
|
-
@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)']
|
841
|
-
end
|
842
|
-
|
843
|
-
it "should be able to have values be a hash of options" do
|
844
|
-
@db.create_join_table(:cat_id=>{:table=>:cats, :null=>true}, :dog_id=>{:table=>:dogs, :default=>0})
|
845
|
-
@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)']
|
846
|
-
end
|
847
|
-
|
848
|
-
it "should be able to pass a second hash of table options" do
|
849
|
-
@db.create_join_table({:cat_id=>:cats, :dog_id=>:dogs}, :temp=>true)
|
850
|
-
@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)']
|
851
|
-
end
|
852
|
-
|
853
|
-
it "should recognize :name option in table options" do
|
854
|
-
@db.create_join_table({:cat_id=>:cats, :dog_id=>:dogs}, :name=>:f)
|
855
|
-
@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)']
|
856
|
-
end
|
857
|
-
|
858
|
-
it "should recognize :index_options option in table options" do
|
859
|
-
@db.create_join_table({:cat_id=>:cats, :dog_id=>:dogs}, :index_options=>{:name=>:foo_index})
|
860
|
-
@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)']
|
861
|
-
end
|
862
|
-
|
863
|
-
it "should recognize :no_index option in table options" do
|
864
|
-
@db.create_join_table({:cat_id=>:cats, :dog_id=>:dogs}, :no_index=>true)
|
865
|
-
@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))']
|
866
|
-
end
|
867
|
-
|
868
|
-
it "should recognize :no_primary_key option in table options" do
|
869
|
-
@db.create_join_table({:cat_id=>:cats, :dog_id=>:dogs}, :no_primary_key=>true)
|
870
|
-
@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)']
|
871
|
-
end
|
872
|
-
|
873
|
-
it "should raise an error if the hash doesn't have 2 entries with table names" do
|
874
|
-
proc{@db.create_join_table({})}.must_raise(Sequel::Error)
|
875
|
-
proc{@db.create_join_table({:cat_id=>:cats})}.must_raise(Sequel::Error)
|
876
|
-
proc{@db.create_join_table({:cat_id=>:cats, :human_id=>:humans, :dog_id=>:dog})}.must_raise(Sequel::Error)
|
877
|
-
proc{@db.create_join_table({:cat_id=>:cats, :dog_id=>{}})}.must_raise(Sequel::Error)
|
878
|
-
end
|
879
|
-
end
|
880
|
-
|
881
|
-
describe "DB#create_join_table?" do
|
882
|
-
before do
|
883
|
-
@db = Sequel.mock
|
884
|
-
end
|
885
|
-
|
886
|
-
it "should create the table if it does not already exist" do
|
887
|
-
@db.define_singleton_method(:table_exists?){|a| false}
|
888
|
-
@db.create_join_table?(:cat_id=>:cats, :dog_id=>:dogs).must_be_nil
|
889
|
-
@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)']
|
890
|
-
end
|
891
|
-
|
892
|
-
it "should not create the table if it already exists" do
|
893
|
-
@db.define_singleton_method(:table_exists?){|a| true}
|
894
|
-
@db.create_join_table?(:cat_id=>:cats, :dog_id=>:dogs)
|
895
|
-
@db.sqls.must_equal []
|
896
|
-
end
|
897
|
-
|
898
|
-
it "should not use IF NOT EXISTS" do
|
899
|
-
@db.define_singleton_method(:table_exists?){|a| false}
|
900
|
-
@db.define_singleton_method(:supports_create_table_if_not_exists?){true}
|
901
|
-
@db.create_join_table?(:cat_id=>:cats, :dog_id=>:dogs)
|
902
|
-
@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)']
|
903
|
-
|
904
|
-
@db.define_singleton_method(:table_exists?){|a| true}
|
905
|
-
@db.create_join_table?(:cat_id=>:cats, :dog_id=>:dogs)
|
906
|
-
@db.sqls.must_equal []
|
907
|
-
end
|
908
|
-
|
909
|
-
it "should not use IF NOT EXISTS if no_index is used" do
|
910
|
-
@db.define_singleton_method(:supports_create_table_if_not_exists?){true}
|
911
|
-
@db.create_join_table?({:cat_id=>:cats, :dog_id=>:dogs}, :no_index=>true)
|
912
|
-
@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))']
|
913
|
-
end
|
914
|
-
end
|
915
|
-
|
916
|
-
describe "DB#create_join_table!" do
|
917
|
-
before do
|
918
|
-
@db = Sequel.mock
|
919
|
-
end
|
920
|
-
|
921
|
-
it "should drop the table first if it already exists" do
|
922
|
-
@db.define_singleton_method(:table_exists?){|a| true}
|
923
|
-
@db.create_join_table!(:cat_id=>:cats, :dog_id=>:dogs).must_be_nil
|
924
|
-
@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)']
|
925
|
-
end
|
926
|
-
|
927
|
-
it "should not drop the table if it doesn't exists" do
|
928
|
-
@db.define_singleton_method(:table_exists?){|a| false}
|
929
|
-
@db.create_join_table!(:cat_id=>:cats, :dog_id=>:dogs)
|
930
|
-
@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)']
|
931
|
-
end
|
932
|
-
|
933
|
-
it "should use IF EXISTS if the database supports it" do
|
934
|
-
@db.define_singleton_method(:supports_drop_table_if_exists?){true}
|
935
|
-
@db.create_join_table!(:cat_id=>:cats, :dog_id=>:dogs)
|
936
|
-
@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)']
|
937
|
-
end
|
938
|
-
end
|
939
|
-
|
940
|
-
describe "DB#drop_join_table" do
|
941
|
-
before do
|
942
|
-
@db = Sequel.mock
|
943
|
-
end
|
944
|
-
|
945
|
-
it "should take a hash with foreign keys and table name values and drop the table" do
|
946
|
-
@db.drop_join_table(:cat_id=>:cats, :dog_id=>:dogs).must_be_nil
|
947
|
-
@db.sqls.must_equal ['DROP TABLE cats_dogs']
|
948
|
-
end
|
949
|
-
|
950
|
-
it "should be able to have values be a hash of options" do
|
951
|
-
@db.drop_join_table(:cat_id=>{:table=>:cats, :null=>true}, :dog_id=>{:table=>:dogs, :default=>0})
|
952
|
-
@db.sqls.must_equal ['DROP TABLE cats_dogs']
|
953
|
-
end
|
954
|
-
|
955
|
-
it "should respect a second hash of table options" do
|
956
|
-
@db.drop_join_table({:cat_id=>:cats, :dog_id=>:dogs}, :cascade=>true)
|
957
|
-
@db.sqls.must_equal ['DROP TABLE cats_dogs CASCADE']
|
958
|
-
end
|
959
|
-
|
960
|
-
it "should respect :name option for table name" do
|
961
|
-
@db.drop_join_table({:cat_id=>:cats, :dog_id=>:dogs}, :name=>:f)
|
962
|
-
@db.sqls.must_equal ['DROP TABLE f']
|
963
|
-
end
|
964
|
-
|
965
|
-
it "should raise an error if the hash doesn't have 2 entries with table names" do
|
966
|
-
proc{@db.drop_join_table({})}.must_raise(Sequel::Error)
|
967
|
-
proc{@db.drop_join_table({:cat_id=>:cats})}.must_raise(Sequel::Error)
|
968
|
-
proc{@db.drop_join_table({:cat_id=>:cats, :human_id=>:humans, :dog_id=>:dog})}.must_raise(Sequel::Error)
|
969
|
-
proc{@db.drop_join_table({:cat_id=>:cats, :dog_id=>{}})}.must_raise(Sequel::Error)
|
970
|
-
end
|
971
|
-
end
|
972
|
-
|
973
|
-
describe "DB#drop_table" do
|
974
|
-
before do
|
975
|
-
@db = Sequel.mock
|
976
|
-
end
|
977
|
-
|
978
|
-
it "should generate a DROP TABLE statement" do
|
979
|
-
@db.drop_table(:cats).must_be_nil
|
980
|
-
@db.sqls.must_equal ['DROP TABLE cats']
|
981
|
-
end
|
982
|
-
|
983
|
-
it "should drop multiple tables at once" do
|
984
|
-
@db.drop_table :cats, :dogs
|
985
|
-
@db.sqls.must_equal ['DROP TABLE cats', 'DROP TABLE dogs']
|
986
|
-
end
|
987
|
-
|
988
|
-
it "should take an options hash and support the :cascade option" do
|
989
|
-
@db.drop_table :cats, :dogs, :cascade=>true
|
990
|
-
@db.sqls.must_equal ['DROP TABLE cats CASCADE', 'DROP TABLE dogs CASCADE']
|
991
|
-
end
|
992
|
-
end
|
993
|
-
|
994
|
-
describe "DB#drop_table?" do
|
995
|
-
before do
|
996
|
-
@db = Sequel.mock
|
997
|
-
end
|
998
|
-
|
999
|
-
it "should drop the table if it exists" do
|
1000
|
-
@db.define_singleton_method(:table_exists?){|a| true}
|
1001
|
-
@db.drop_table?(:cats).must_be_nil
|
1002
|
-
@db.sqls.must_equal ["DROP TABLE cats"]
|
1003
|
-
end
|
1004
|
-
|
1005
|
-
it "should do nothing if the table does not exist" do
|
1006
|
-
@db.define_singleton_method(:table_exists?){|a| false}
|
1007
|
-
@db.drop_table?(:cats)
|
1008
|
-
@db.sqls.must_equal []
|
1009
|
-
end
|
1010
|
-
|
1011
|
-
it "should operate on multiple tables at once" do
|
1012
|
-
@db.define_singleton_method(:table_exists?){|a| a == :cats}
|
1013
|
-
@db.drop_table? :cats, :dogs
|
1014
|
-
@db.sqls.must_equal ['DROP TABLE cats']
|
1015
|
-
end
|
1016
|
-
|
1017
|
-
it "should take an options hash and support the :cascade option" do
|
1018
|
-
@db.define_singleton_method(:table_exists?){|a| true}
|
1019
|
-
@db.drop_table? :cats, :dogs, :cascade=>true
|
1020
|
-
@db.sqls.must_equal ['DROP TABLE cats CASCADE', 'DROP TABLE dogs CASCADE']
|
1021
|
-
end
|
1022
|
-
|
1023
|
-
it "should use IF NOT EXISTS if the database supports that" do
|
1024
|
-
@db.define_singleton_method(:supports_drop_table_if_exists?){true}
|
1025
|
-
@db.drop_table? :cats, :dogs
|
1026
|
-
@db.sqls.must_equal ['DROP TABLE IF EXISTS cats', 'DROP TABLE IF EXISTS dogs']
|
1027
|
-
end
|
1028
|
-
|
1029
|
-
it "should use IF NOT EXISTS with CASCADE if the database supports that" do
|
1030
|
-
@db.define_singleton_method(:supports_drop_table_if_exists?){true}
|
1031
|
-
@db.drop_table? :cats, :dogs, :cascade=>true
|
1032
|
-
@db.sqls.must_equal ['DROP TABLE IF EXISTS cats CASCADE', 'DROP TABLE IF EXISTS dogs CASCADE']
|
1033
|
-
end
|
1034
|
-
end
|
1035
|
-
|
1036
|
-
describe "DB#alter_table" do
|
1037
|
-
before do
|
1038
|
-
@db = Sequel.mock
|
1039
|
-
end
|
1040
|
-
|
1041
|
-
it "should allow adding not null constraint via set_column_allow_null with false argument" do
|
1042
|
-
@db.alter_table(:cats) do
|
1043
|
-
set_column_allow_null :score, false
|
1044
|
-
end.must_be_nil
|
1045
|
-
@db.sqls.must_equal ["ALTER TABLE cats ALTER COLUMN score SET NOT NULL"]
|
1046
|
-
end
|
1047
|
-
|
1048
|
-
it "should allow removing not null constraint via set_column_allow_null with true argument" do
|
1049
|
-
@db.alter_table(:cats) do
|
1050
|
-
set_column_allow_null :score, true
|
1051
|
-
end
|
1052
|
-
@db.sqls.must_equal ["ALTER TABLE cats ALTER COLUMN score DROP NOT NULL"]
|
1053
|
-
end
|
1054
|
-
|
1055
|
-
it "should allow adding not null constraint via set_column_not_null" do
|
1056
|
-
@db.alter_table(:cats) do
|
1057
|
-
set_column_not_null :score
|
1058
|
-
end
|
1059
|
-
@db.sqls.must_equal ["ALTER TABLE cats ALTER COLUMN score SET NOT NULL"]
|
1060
|
-
end
|
1061
|
-
|
1062
|
-
it "should allow removing not null constraint via set_column_allow_null without argument" do
|
1063
|
-
@db.alter_table(:cats) do
|
1064
|
-
set_column_allow_null :score
|
1065
|
-
end
|
1066
|
-
@db.sqls.must_equal ["ALTER TABLE cats ALTER COLUMN score DROP NOT NULL"]
|
1067
|
-
end
|
1068
|
-
|
1069
|
-
it "should support add_column" do
|
1070
|
-
@db.alter_table(:cats) do
|
1071
|
-
add_column :score, :integer
|
1072
|
-
end
|
1073
|
-
@db.sqls.must_equal ["ALTER TABLE cats ADD COLUMN score integer"]
|
1074
|
-
end
|
1075
|
-
|
1076
|
-
it "should support add_constraint" do
|
1077
|
-
@db.alter_table(:cats) do
|
1078
|
-
add_constraint :valid_score, 'score <= 100'
|
1079
|
-
end
|
1080
|
-
@db.sqls.must_equal ["ALTER TABLE cats ADD CONSTRAINT valid_score CHECK (score <= 100)"]
|
1081
|
-
end
|
1082
|
-
|
1083
|
-
it "should support add_constraint with options" do
|
1084
|
-
@db.alter_table(:cats) do
|
1085
|
-
add_constraint({:name=>:valid_score, :deferrable=>true}, 'score <= 100')
|
1086
|
-
end
|
1087
|
-
@db.sqls.must_equal ["ALTER TABLE cats ADD CONSTRAINT valid_score CHECK (score <= 100) DEFERRABLE INITIALLY DEFERRED"]
|
1088
|
-
end
|
1089
|
-
|
1090
|
-
it "should support add_constraint with block" do
|
1091
|
-
@db.alter_table(:cats) do
|
1092
|
-
add_constraint(:blah_blah){(x.sql_number > 0) & (y.sql_number < 1)}
|
1093
|
-
end
|
1094
|
-
@db.sqls.must_equal ["ALTER TABLE cats ADD CONSTRAINT blah_blah CHECK ((x > 0) AND (y < 1))"]
|
1095
|
-
end
|
1096
|
-
|
1097
|
-
it "should support add_unique_constraint" do
|
1098
|
-
@db.alter_table(:cats) do
|
1099
|
-
add_unique_constraint [:a, :b]
|
1100
|
-
end
|
1101
|
-
@db.sqls.must_equal ["ALTER TABLE cats ADD UNIQUE (a, b)"]
|
1102
|
-
|
1103
|
-
@db.alter_table(:cats) do
|
1104
|
-
add_unique_constraint [:a, :b], :name => :ab_uniq
|
1105
|
-
end
|
1106
|
-
@db.sqls.must_equal ["ALTER TABLE cats ADD CONSTRAINT ab_uniq UNIQUE (a, b)"]
|
1107
|
-
end
|
1108
|
-
|
1109
|
-
it "should support add_foreign_key" do
|
1110
|
-
@db.alter_table(:cats) do
|
1111
|
-
add_foreign_key :node_id, :nodes
|
1112
|
-
end
|
1113
|
-
@db.sqls.must_equal ["ALTER TABLE cats ADD COLUMN node_id integer REFERENCES nodes"]
|
1114
|
-
end
|
1115
|
-
|
1116
|
-
it "should support add_foreign_key with composite foreign keys" do
|
1117
|
-
@db.alter_table(:cats) do
|
1118
|
-
add_foreign_key [:node_id, :prop_id], :nodes_props
|
1119
|
-
end
|
1120
|
-
@db.sqls.must_equal ["ALTER TABLE cats ADD FOREIGN KEY (node_id, prop_id) REFERENCES nodes_props"]
|
1121
|
-
|
1122
|
-
@db.alter_table(:cats) do
|
1123
|
-
add_foreign_key [:node_id, :prop_id], :nodes_props, :name => :cfk
|
1124
|
-
end
|
1125
|
-
@db.sqls.must_equal ["ALTER TABLE cats ADD CONSTRAINT cfk FOREIGN KEY (node_id, prop_id) REFERENCES nodes_props"]
|
1126
|
-
|
1127
|
-
@db.alter_table(:cats) do
|
1128
|
-
add_foreign_key [:node_id, :prop_id], :nodes_props, :key => [:nid, :pid]
|
1129
|
-
end
|
1130
|
-
@db.sqls.must_equal ["ALTER TABLE cats ADD FOREIGN KEY (node_id, prop_id) REFERENCES nodes_props(nid, pid)"]
|
1131
|
-
|
1132
|
-
@db.alter_table(:cats) do
|
1133
|
-
add_foreign_key [:node_id, :prop_id], :nodes_props, :on_delete => :restrict, :on_update => :cascade
|
1134
|
-
end
|
1135
|
-
@db.sqls.must_equal ["ALTER TABLE cats ADD FOREIGN KEY (node_id, prop_id) REFERENCES nodes_props ON DELETE RESTRICT ON UPDATE CASCADE"]
|
1136
|
-
end
|
1137
|
-
|
1138
|
-
it "should support add_index" do
|
1139
|
-
@db.alter_table(:cats) do
|
1140
|
-
add_index :name
|
1141
|
-
end
|
1142
|
-
@db.sqls.must_equal ["CREATE INDEX cats_name_index ON cats (name)"]
|
1143
|
-
end
|
1144
|
-
|
1145
|
-
it "should ignore errors if the database raises an error on an add_index call and the :ignore_errors option is used" do
|
1146
|
-
@db.define_singleton_method(:execute_ddl){|*a| raise Sequel::DatabaseError}
|
1147
|
-
lambda{@db.add_index(:cats, :id)}.must_raise(Sequel::DatabaseError)
|
1148
|
-
@db.add_index(:cats, :id, :ignore_errors=>true)
|
1149
|
-
@db.sqls.must_equal []
|
1150
|
-
end
|
1151
|
-
|
1152
|
-
it "should support add_primary_key" do
|
1153
|
-
@db.alter_table(:cats) do
|
1154
|
-
add_primary_key :id
|
1155
|
-
end
|
1156
|
-
@db.sqls.must_equal ["ALTER TABLE cats ADD COLUMN id integer PRIMARY KEY AUTOINCREMENT"]
|
1157
|
-
end
|
1158
|
-
|
1159
|
-
it "should support add_primary_key with composite primary keys" do
|
1160
|
-
@db.alter_table(:cats) do
|
1161
|
-
add_primary_key [:id, :type]
|
1162
|
-
end
|
1163
|
-
@db.sqls.must_equal ["ALTER TABLE cats ADD PRIMARY KEY (id, type)"]
|
1164
|
-
|
1165
|
-
@db.alter_table(:cats) do
|
1166
|
-
add_primary_key [:id, :type], :name => :cpk
|
1167
|
-
end
|
1168
|
-
@db.sqls.must_equal ["ALTER TABLE cats ADD CONSTRAINT cpk PRIMARY KEY (id, type)"]
|
1169
|
-
end
|
1170
|
-
|
1171
|
-
it "should set primary key column NOT NULL when using add_primary_key if database doesn't handle it" do
|
1172
|
-
def @db.can_add_primary_key_constraint_on_nullable_columns?; false end
|
1173
|
-
@db.alter_table(:cats) do
|
1174
|
-
add_primary_key :id
|
1175
|
-
end
|
1176
|
-
@db.sqls.must_equal ["ALTER TABLE cats ADD COLUMN id integer NOT NULL PRIMARY KEY AUTOINCREMENT"]
|
1177
|
-
end
|
1178
|
-
|
1179
|
-
it "should set primary key column NOT NULL when adding primary key constraint if database doesn't handle it" do
|
1180
|
-
def @db.can_add_primary_key_constraint_on_nullable_columns?; false end
|
1181
|
-
@db.alter_table(:cats) do
|
1182
|
-
add_primary_key [:id, :type]
|
1183
|
-
end
|
1184
|
-
@db.sqls.must_equal ["ALTER TABLE cats ALTER COLUMN id SET NOT NULL", "ALTER TABLE cats ALTER COLUMN type SET NOT NULL", "ALTER TABLE cats ADD PRIMARY KEY (id, type)"]
|
1185
|
-
end
|
1186
|
-
|
1187
|
-
it "should support drop_column" do
|
1188
|
-
@db.alter_table(:cats) do
|
1189
|
-
drop_column :score
|
1190
|
-
end
|
1191
|
-
@db.sqls.must_equal ["ALTER TABLE cats DROP COLUMN score"]
|
1192
|
-
end
|
1193
|
-
|
1194
|
-
it "should support drop_column with :cascade=>true option" do
|
1195
|
-
@db.alter_table(:cats) do
|
1196
|
-
drop_column :score, :cascade=>true
|
1197
|
-
end
|
1198
|
-
@db.sqls.must_equal ["ALTER TABLE cats DROP COLUMN score CASCADE"]
|
1199
|
-
end
|
1200
|
-
|
1201
|
-
it "should support drop_constraint" do
|
1202
|
-
@db.alter_table(:cats) do
|
1203
|
-
drop_constraint :valid_score
|
1204
|
-
end
|
1205
|
-
@db.sqls.must_equal ["ALTER TABLE cats DROP CONSTRAINT valid_score"]
|
1206
|
-
end
|
1207
|
-
|
1208
|
-
it "should support drop_constraint with :cascade=>true option" do
|
1209
|
-
@db.alter_table(:cats) do
|
1210
|
-
drop_constraint :valid_score, :cascade=>true
|
1211
|
-
end
|
1212
|
-
@db.sqls.must_equal ["ALTER TABLE cats DROP CONSTRAINT valid_score CASCADE"]
|
1213
|
-
end
|
1214
|
-
|
1215
|
-
it "should support drop_foreign_key" do
|
1216
|
-
def @db.foreign_key_list(table_name)
|
1217
|
-
[{:name=>:cats_node_id_fkey, :columns=>[:node_id]}]
|
1218
|
-
end
|
1219
|
-
@db.alter_table(:cats) do
|
1220
|
-
drop_foreign_key :node_id
|
1221
|
-
end
|
1222
|
-
@db.sqls.must_equal ["ALTER TABLE cats DROP CONSTRAINT cats_node_id_fkey", "ALTER TABLE cats DROP COLUMN node_id"]
|
1223
|
-
end
|
1224
|
-
|
1225
|
-
it "should support drop_foreign_key with :foreign_key_constraint_name option" do
|
1226
|
-
@db.alter_table(:cats) do
|
1227
|
-
drop_foreign_key :node_id, :foreign_key_constraint_name=>:foo
|
1228
|
-
end
|
1229
|
-
@db.sqls.must_equal ["ALTER TABLE cats DROP CONSTRAINT foo", "ALTER TABLE cats DROP COLUMN node_id"]
|
1230
|
-
end
|
1231
|
-
|
1232
|
-
it "should support drop_foreign_key with :name option" do
|
1233
|
-
@db.alter_table(:cats) do
|
1234
|
-
drop_foreign_key :node_id, :name=>:foo
|
1235
|
-
end
|
1236
|
-
@db.sqls.must_equal ["ALTER TABLE cats DROP CONSTRAINT foo", "ALTER TABLE cats DROP COLUMN node_id"]
|
1237
|
-
end
|
1238
|
-
|
1239
|
-
it "should support drop_foreign_key with composite foreign keys" do
|
1240
|
-
def @db.foreign_key_list(table_name)
|
1241
|
-
[{:name=>:cats_node_id_prop_id_fkey, :columns=>[:node_id, :prop_id]}]
|
1242
|
-
end
|
1243
|
-
@db.alter_table(:cats) do
|
1244
|
-
drop_foreign_key [:node_id, :prop_id]
|
1245
|
-
end
|
1246
|
-
@db.sqls.must_equal ["ALTER TABLE cats DROP CONSTRAINT cats_node_id_prop_id_fkey"]
|
1247
|
-
|
1248
|
-
@db.alter_table(:cats) do
|
1249
|
-
drop_foreign_key [:node_id, :prop_id], :name => :cfk
|
1250
|
-
end
|
1251
|
-
@db.sqls.must_equal ["ALTER TABLE cats DROP CONSTRAINT cfk"]
|
1252
|
-
end
|
1253
|
-
|
1254
|
-
it "should have drop_foreign_key raise Error if no name is found" do
|
1255
|
-
def @db.foreign_key_list(table_name)
|
1256
|
-
[{:name=>:cats_node_id_fkey, :columns=>[:foo_id]}]
|
1257
|
-
end
|
1258
|
-
lambda{@db.alter_table(:cats){drop_foreign_key :node_id}}.must_raise(Sequel::Error)
|
1259
|
-
end
|
1260
|
-
|
1261
|
-
it "should have drop_foreign_key raise Error if multiple foreign keys found" do
|
1262
|
-
def @db.foreign_key_list(table_name)
|
1263
|
-
[{:name=>:cats_node_id_fkey, :columns=>[:node_id]}, {:name=>:cats_node_id_fkey2, :columns=>[:node_id]}]
|
1264
|
-
end
|
1265
|
-
lambda{@db.alter_table(:cats){drop_foreign_key :node_id}}.must_raise(Sequel::Error)
|
1266
|
-
end
|
1267
|
-
|
1268
|
-
it "should support drop_index" do
|
1269
|
-
@db.alter_table(:cats) do
|
1270
|
-
drop_index :name
|
1271
|
-
end
|
1272
|
-
@db.sqls.must_equal ["DROP INDEX cats_name_index"]
|
1273
|
-
end
|
1274
|
-
|
1275
|
-
it "should support drop_index with a given name" do
|
1276
|
-
@db.alter_table(:cats) do
|
1277
|
-
drop_index :name, :name=>:blah_blah
|
1278
|
-
end
|
1279
|
-
@db.sqls.must_equal ["DROP INDEX blah_blah"]
|
1280
|
-
end
|
1281
|
-
|
1282
|
-
it "should support rename_column" do
|
1283
|
-
@db.alter_table(:cats) do
|
1284
|
-
rename_column :name, :old_name
|
1285
|
-
end
|
1286
|
-
@db.sqls.must_equal ["ALTER TABLE cats RENAME COLUMN name TO old_name"]
|
1287
|
-
end
|
1288
|
-
|
1289
|
-
it "should support set_column_default" do
|
1290
|
-
@db.alter_table(:cats) do
|
1291
|
-
set_column_default :score, 3
|
1292
|
-
end
|
1293
|
-
@db.sqls.must_equal ["ALTER TABLE cats ALTER COLUMN score SET DEFAULT 3"]
|
1294
|
-
end
|
1295
|
-
|
1296
|
-
it "should support set_column_type" do
|
1297
|
-
@db.alter_table(:cats) do
|
1298
|
-
set_column_type :score, :real
|
1299
|
-
end
|
1300
|
-
@db.sqls.must_equal ["ALTER TABLE cats ALTER COLUMN score TYPE real"]
|
1301
|
-
end
|
1302
|
-
|
1303
|
-
it "should support set_column_type with options" do
|
1304
|
-
@db.alter_table(:cats) do
|
1305
|
-
set_column_type :score, :integer, :unsigned=>true
|
1306
|
-
set_column_type :score, :varchar, :size=>30
|
1307
|
-
set_column_type :score, :enum, :elements=>['a', 'b']
|
1308
|
-
end
|
1309
|
-
@db.sqls.must_equal ["ALTER TABLE cats ALTER COLUMN score TYPE integer UNSIGNED",
|
1310
|
-
"ALTER TABLE cats ALTER COLUMN score TYPE varchar(30)",
|
1311
|
-
"ALTER TABLE cats ALTER COLUMN score TYPE enum('a', 'b')"]
|
1312
|
-
end
|
1313
|
-
|
1314
|
-
it "should combine operations into a single query if the database supports it" do
|
1315
|
-
@db.define_singleton_method(:supports_combining_alter_table_ops?){true}
|
1316
|
-
@db.alter_table(:cats) do
|
1317
|
-
add_column :a, Integer
|
1318
|
-
drop_column :b
|
1319
|
-
set_column_not_null :c
|
1320
|
-
rename_column :d, :e
|
1321
|
-
set_column_default :f, 'g'
|
1322
|
-
set_column_type :h, Integer
|
1323
|
-
add_constraint(:i){a > 1}
|
1324
|
-
drop_constraint :j
|
1325
|
-
end
|
1326
|
-
@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"]
|
1327
|
-
end
|
1328
|
-
|
1329
|
-
it "should combine operations into consecutive groups of combinable operations if the database supports combining operations" do
|
1330
|
-
@db.define_singleton_method(:supports_combining_alter_table_ops?){true}
|
1331
|
-
@db.alter_table(:cats) do
|
1332
|
-
add_column :a, Integer
|
1333
|
-
drop_column :b
|
1334
|
-
set_column_not_null :c
|
1335
|
-
rename_column :d, :e
|
1336
|
-
add_index :e
|
1337
|
-
set_column_default :f, 'g'
|
1338
|
-
set_column_type :h, Integer
|
1339
|
-
add_constraint(:i){a > 1}
|
1340
|
-
drop_constraint :j
|
1341
|
-
end
|
1342
|
-
@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",
|
1343
|
-
"CREATE INDEX cats_e_index ON cats (e)",
|
1344
|
-
"ALTER TABLE cats ALTER COLUMN f SET DEFAULT 'g', ALTER COLUMN h TYPE integer, ADD CONSTRAINT i CHECK (a > 1), DROP CONSTRAINT j"]
|
1345
|
-
end
|
1346
|
-
|
1347
|
-
end
|
1348
|
-
|
1349
|
-
describe "Database#create_table" do
|
1350
|
-
before do
|
1351
|
-
@db = Sequel.mock
|
1352
|
-
end
|
1353
|
-
|
1354
|
-
it "should construct proper SQL" do
|
1355
|
-
@db.create_table :test do
|
1356
|
-
primary_key :id, :integer, :null => false
|
1357
|
-
column :name, :text
|
1358
|
-
index :name, :unique => true
|
1359
|
-
end
|
1360
|
-
@db.sqls.must_equal ['CREATE TABLE test (id integer NOT NULL PRIMARY KEY AUTOINCREMENT, name text)',
|
1361
|
-
'CREATE UNIQUE INDEX test_name_index ON test (name)']
|
1362
|
-
end
|
1363
|
-
|
1364
|
-
it "should create a temporary table" do
|
1365
|
-
@db.create_table :test_tmp, :temp => true do
|
1366
|
-
primary_key :id, :integer, :null => false
|
1367
|
-
column :name, :text
|
1368
|
-
index :name, :unique => true
|
1369
|
-
end
|
1370
|
-
|
1371
|
-
@db.sqls.must_equal ['CREATE TEMPORARY TABLE test_tmp (id integer NOT NULL PRIMARY KEY AUTOINCREMENT, name text)',
|
1372
|
-
'CREATE UNIQUE INDEX test_tmp_name_index ON test_tmp (name)']
|
1373
|
-
end
|
1374
|
-
end
|
1375
|
-
|
1376
|
-
describe "Database#alter_table" do
|
1377
|
-
before do
|
1378
|
-
@db = Sequel.mock
|
1379
|
-
end
|
1380
|
-
|
1381
|
-
it "should construct proper SQL" do
|
1382
|
-
@db.alter_table :xyz do
|
1383
|
-
add_column :aaa, :text, :null => false, :unique => true
|
1384
|
-
drop_column :bbb
|
1385
|
-
rename_column :ccc, :ddd
|
1386
|
-
set_column_type :eee, :integer
|
1387
|
-
set_column_default :hhh, 'abcd'
|
1388
|
-
add_index :fff, :unique => true
|
1389
|
-
drop_index :ggg
|
1390
|
-
end
|
1391
|
-
|
1392
|
-
@db.sqls.must_equal ['ALTER TABLE xyz ADD COLUMN aaa text NOT NULL UNIQUE',
|
1393
|
-
'ALTER TABLE xyz DROP COLUMN bbb',
|
1394
|
-
'ALTER TABLE xyz RENAME COLUMN ccc TO ddd',
|
1395
|
-
'ALTER TABLE xyz ALTER COLUMN eee TYPE integer',
|
1396
|
-
"ALTER TABLE xyz ALTER COLUMN hhh SET DEFAULT 'abcd'",
|
1397
|
-
'CREATE UNIQUE INDEX xyz_fff_index ON xyz (fff)',
|
1398
|
-
'DROP INDEX xyz_ggg_index']
|
1399
|
-
end
|
1400
|
-
end
|
1401
|
-
|
1402
|
-
describe "Database#add_column" do
|
1403
|
-
it "should construct proper SQL" do
|
1404
|
-
db = Sequel.mock
|
1405
|
-
db.add_column(:test, :name, :text, :unique => true).must_be_nil
|
1406
|
-
db.sqls.must_equal ['ALTER TABLE test ADD COLUMN name text UNIQUE']
|
1407
|
-
end
|
1408
|
-
end
|
1409
|
-
|
1410
|
-
describe "Database#drop_column" do
|
1411
|
-
before do
|
1412
|
-
@db = Sequel.mock
|
1413
|
-
end
|
1414
|
-
|
1415
|
-
it "should construct proper SQL" do
|
1416
|
-
@db.drop_column(:test, :name).must_be_nil
|
1417
|
-
@db.sqls.must_equal ['ALTER TABLE test DROP COLUMN name']
|
1418
|
-
end
|
1419
|
-
|
1420
|
-
it "should use CASCADE for :cascade=>true option" do
|
1421
|
-
@db.drop_column :test, :name, :cascade=>true
|
1422
|
-
@db.sqls.must_equal ['ALTER TABLE test DROP COLUMN name CASCADE']
|
1423
|
-
end
|
1424
|
-
end
|
1425
|
-
|
1426
|
-
describe "Database#rename_column" do
|
1427
|
-
before do
|
1428
|
-
@db = Sequel.mock
|
1429
|
-
end
|
1430
|
-
|
1431
|
-
it "should construct proper SQL" do
|
1432
|
-
@db.rename_column(:test, :abc, :def).must_be_nil
|
1433
|
-
@db.sqls.must_equal ['ALTER TABLE test RENAME COLUMN abc TO def']
|
1434
|
-
end
|
1435
|
-
end
|
1436
|
-
|
1437
|
-
describe "Database#set_column_type" do
|
1438
|
-
before do
|
1439
|
-
@db = Sequel.mock
|
1440
|
-
end
|
1441
|
-
|
1442
|
-
it "should construct proper SQL" do
|
1443
|
-
@db.set_column_type(:test, :name, :integer).must_be_nil
|
1444
|
-
@db.sqls.must_equal ['ALTER TABLE test ALTER COLUMN name TYPE integer']
|
1445
|
-
end
|
1446
|
-
end
|
1447
|
-
|
1448
|
-
describe "Database#set_column_default" do
|
1449
|
-
before do
|
1450
|
-
@db = Sequel.mock
|
1451
|
-
end
|
1452
|
-
|
1453
|
-
it "should construct proper SQL" do
|
1454
|
-
@db.set_column_default(:test, :name, 'zyx').must_be_nil
|
1455
|
-
@db.sqls.must_equal ["ALTER TABLE test ALTER COLUMN name SET DEFAULT 'zyx'"]
|
1456
|
-
end
|
1457
|
-
end
|
1458
|
-
|
1459
|
-
describe "Database#add_index" do
|
1460
|
-
before do
|
1461
|
-
@db = Sequel.mock
|
1462
|
-
end
|
1463
|
-
|
1464
|
-
it "should construct proper SQL" do
|
1465
|
-
@db.add_index(:test, :name, :unique => true).must_be_nil
|
1466
|
-
@db.sqls.must_equal ['CREATE UNIQUE INDEX test_name_index ON test (name)']
|
1467
|
-
end
|
1468
|
-
|
1469
|
-
it "should accept multiple columns" do
|
1470
|
-
@db.add_index :test, [:one, :two]
|
1471
|
-
@db.sqls.must_equal ['CREATE INDEX test_one_two_index ON test (one, two)']
|
1472
|
-
end
|
1473
|
-
end
|
1474
|
-
|
1475
|
-
describe "Database#drop_index" do
|
1476
|
-
before do
|
1477
|
-
@db = Sequel.mock
|
1478
|
-
end
|
1479
|
-
|
1480
|
-
it "should construct proper SQL" do
|
1481
|
-
@db.drop_index(:test, :name).must_be_nil
|
1482
|
-
@db.sqls.must_equal ['DROP INDEX test_name_index']
|
1483
|
-
end
|
1484
|
-
|
1485
|
-
end
|
1486
|
-
|
1487
|
-
describe "Database#drop_table" do
|
1488
|
-
before do
|
1489
|
-
@db = Sequel.mock
|
1490
|
-
end
|
1491
|
-
|
1492
|
-
it "should construct proper SQL" do
|
1493
|
-
@db.drop_table(:test).must_be_nil
|
1494
|
-
@db.sqls.must_equal ['DROP TABLE test']
|
1495
|
-
end
|
1496
|
-
|
1497
|
-
it "should accept multiple table names" do
|
1498
|
-
@db.drop_table :a, :bb, :ccc
|
1499
|
-
@db.sqls.must_equal ['DROP TABLE a', 'DROP TABLE bb', 'DROP TABLE ccc']
|
1500
|
-
end
|
1501
|
-
end
|
1502
|
-
|
1503
|
-
describe "Database#rename_table" do
|
1504
|
-
before do
|
1505
|
-
@db = Sequel.mock
|
1506
|
-
end
|
1507
|
-
|
1508
|
-
it "should construct proper SQL" do
|
1509
|
-
@db.rename_table(:abc, :xyz).must_be_nil
|
1510
|
-
@db.sqls.must_equal ['ALTER TABLE abc RENAME TO xyz']
|
1511
|
-
end
|
1512
|
-
end
|
1513
|
-
|
1514
|
-
describe "Database#create_view" do
|
1515
|
-
before do
|
1516
|
-
@db = Sequel.mock
|
1517
|
-
end
|
1518
|
-
|
1519
|
-
it "should construct proper SQL with raw SQL" do
|
1520
|
-
@db.create_view(:test, "SELECT * FROM xyz").must_be_nil
|
1521
|
-
@db.sqls.must_equal ['CREATE VIEW test AS SELECT * FROM xyz']
|
1522
|
-
@db.create_view Sequel.identifier(:test), "SELECT * FROM xyz"
|
1523
|
-
@db.sqls.must_equal ['CREATE VIEW test AS SELECT * FROM xyz']
|
1524
|
-
end
|
1525
|
-
|
1526
|
-
it "should construct proper SQL with dataset" do
|
1527
|
-
@db.create_view :test, @db[:items].select(:a, :b).order(:c)
|
1528
|
-
@db.sqls.must_equal ['CREATE VIEW test AS SELECT a, b FROM items ORDER BY c']
|
1529
|
-
end
|
1530
|
-
|
1531
|
-
it "should handle :columns option" do
|
1532
|
-
@db.create_view :test, @db[:items].select(:a, :b).order(:c), :columns=>[:d, :e]
|
1533
|
-
@db.sqls.must_equal ['CREATE VIEW test (d, e) AS SELECT a, b FROM items ORDER BY c']
|
1534
|
-
@db.create_view :test, @db[:items].select(:a, :b).order(:c), :columns=>%w'd e'
|
1535
|
-
@db.sqls.must_equal ['CREATE VIEW test (d, e) AS SELECT a, b FROM items ORDER BY c']
|
1536
|
-
@db.create_view :test, @db[:items].select(:a, :b).order(:c), :columns=>[Sequel.identifier('d'), Sequel.lit('e')]
|
1537
|
-
@db.sqls.must_equal ['CREATE VIEW test (d, e) AS SELECT a, b FROM items ORDER BY c']
|
1538
|
-
end
|
1539
|
-
|
1540
|
-
it "should handle :check option" do
|
1541
|
-
@db.create_view :test, @db[:items].select(:a, :b).order(:c), :check=>true
|
1542
|
-
@db.sqls.must_equal ['CREATE VIEW test AS SELECT a, b FROM items ORDER BY c WITH CHECK OPTION']
|
1543
|
-
@db.create_view :test, @db[:items].select(:a, :b).order(:c), :check=>:local
|
1544
|
-
@db.sqls.must_equal ['CREATE VIEW test AS SELECT a, b FROM items ORDER BY c WITH LOCAL CHECK OPTION']
|
1545
|
-
end
|
1546
|
-
|
1547
|
-
with_symbol_splitting "should handle create_or_replace_view with splittable symbols" do
|
1548
|
-
@db.create_or_replace_view :sch__test, "SELECT * FROM xyz"
|
1549
|
-
@db.sqls.must_equal ['DROP VIEW sch.test', 'CREATE VIEW sch.test AS SELECT * FROM xyz']
|
1550
|
-
end
|
1551
|
-
|
1552
|
-
it "should handle create_or_replace_view" do
|
1553
|
-
@db.create_or_replace_view :test, @db[:items].select(:a, :b).order(:c)
|
1554
|
-
@db.sqls.must_equal ['DROP VIEW test', 'CREATE VIEW test AS SELECT a, b FROM items ORDER BY c']
|
1555
|
-
@db.create_or_replace_view Sequel.identifier(:test), @db[:items].select(:a, :b).order(:c)
|
1556
|
-
@db.sqls.must_equal ['DROP VIEW test', 'CREATE VIEW test AS SELECT a, b FROM items ORDER BY c']
|
1557
|
-
end
|
1558
|
-
|
1559
|
-
it "should use CREATE OR REPLACE VIEW if such syntax is supported" do
|
1560
|
-
def @db.supports_create_or_replace_view?() true end
|
1561
|
-
@db.create_or_replace_view :test, @db[:items]
|
1562
|
-
@db.sqls.must_equal ['CREATE OR REPLACE VIEW test AS SELECT * FROM items']
|
1563
|
-
end
|
1564
|
-
end
|
1565
|
-
|
1566
|
-
describe "Database#drop_view" do
|
1567
|
-
before do
|
1568
|
-
@db = Sequel.mock
|
1569
|
-
end
|
1570
|
-
|
1571
|
-
with_symbol_splitting "should construct proper SQL for splittable symbols" do
|
1572
|
-
@db.drop_view(:sch__test).must_be_nil
|
1573
|
-
@db.sqls.must_equal ['DROP VIEW sch.test']
|
1574
|
-
end
|
1575
|
-
|
1576
|
-
it "should construct proper SQL" do
|
1577
|
-
@db.drop_view :test
|
1578
|
-
@db.drop_view Sequel.identifier(:test)
|
1579
|
-
@db.drop_view Sequel.qualify(:sch, :test)
|
1580
|
-
@db.sqls.must_equal ['DROP VIEW test', 'DROP VIEW test', 'DROP VIEW sch.test']
|
1581
|
-
end
|
1582
|
-
|
1583
|
-
it "should drop multiple views at once" do
|
1584
|
-
@db.drop_view :cats, :dogs
|
1585
|
-
@db.sqls.must_equal ['DROP VIEW cats', 'DROP VIEW dogs']
|
1586
|
-
end
|
1587
|
-
|
1588
|
-
it "should support the :cascade option" do
|
1589
|
-
@db.drop_view :cats, :dogs, :cascade=>true
|
1590
|
-
@db.sqls.must_equal ['DROP VIEW cats CASCADE', 'DROP VIEW dogs CASCADE']
|
1591
|
-
end
|
1592
|
-
|
1593
|
-
it "should support the :if_exists option" do
|
1594
|
-
@db.drop_view :cats, :dogs, :if_exists=>true
|
1595
|
-
@db.sqls.must_equal ['DROP VIEW IF EXISTS cats', 'DROP VIEW IF EXISTS dogs']
|
1596
|
-
end
|
1597
|
-
end
|
1598
|
-
|
1599
|
-
describe "Database#alter_table_sql" do
|
1600
|
-
it "should raise error for an invalid op" do
|
1601
|
-
proc {Sequel.mock.send(:alter_table_sql, :mau, :op => :blah)}.must_raise(Sequel::Error)
|
1602
|
-
end
|
1603
|
-
end
|
1604
|
-
|
1605
|
-
describe "Schema Parser" do
|
1606
|
-
before do
|
1607
|
-
@sqls = []
|
1608
|
-
@db = Sequel::Database.new
|
1609
|
-
end
|
1610
|
-
|
1611
|
-
it "should raise an error if there are no columns" do
|
1612
|
-
@db.define_singleton_method(:schema_parse_table) do |t, opts|
|
1613
|
-
[]
|
1614
|
-
end
|
1615
|
-
proc{@db.schema(:x)}.must_raise(Sequel::Error)
|
1616
|
-
end
|
1617
|
-
|
1618
|
-
it "should cache data by default" do
|
1619
|
-
@db.define_singleton_method(:schema_parse_table) do |t, opts|
|
1620
|
-
[[:a, {}]]
|
1621
|
-
end
|
1622
|
-
@db.schema(:x).must_be_same_as(@db.schema(:x))
|
1623
|
-
end
|
1624
|
-
|
1625
|
-
it "should not cache data if :reload=>true is given" do
|
1626
|
-
@db.define_singleton_method(:schema_parse_table) do |t, opts|
|
1627
|
-
[[:a, {}]]
|
1628
|
-
end
|
1629
|
-
@db.schema(:x).wont_be_same_as(@db.schema(:x, :reload=>true))
|
1630
|
-
end
|
1631
|
-
|
1632
|
-
it "should not cache schema metadata if cache_schema is false" do
|
1633
|
-
@db.cache_schema = false
|
1634
|
-
@db.define_singleton_method(:schema_parse_table) do |t, opts|
|
1635
|
-
[[:a, {}]]
|
1636
|
-
end
|
1637
|
-
@db.schema(:x).wont_be_same_as(@db.schema(:x))
|
1638
|
-
end
|
1639
|
-
|
1640
|
-
it "should freeze string values in resulting hash" do
|
1641
|
-
@db.define_singleton_method(:schema_parse_table) do |t, opts|
|
1642
|
-
[[:a, {:oid=>1, :db_type=>'integer'.dup, :default=>"'a'".dup, :ruby_default=>'a'.dup}]]
|
1643
|
-
end
|
1644
|
-
c = @db.schema(:x)[0][1]
|
1645
|
-
c[:db_type].frozen?.must_equal true
|
1646
|
-
c[:default].frozen?.must_equal true
|
1647
|
-
c[:ruby_default].frozen?.must_equal true
|
1648
|
-
end
|
1649
|
-
|
1650
|
-
it "should provide options if given a table name" do
|
1651
|
-
c = nil
|
1652
|
-
@db.define_singleton_method(:schema_parse_table) do |t, opts|
|
1653
|
-
c = [t, opts]
|
1654
|
-
[[:a, {:db_type=>t.to_s}]]
|
1655
|
-
end
|
1656
|
-
@db.schema(:x)
|
1657
|
-
c.must_equal ["x", {}]
|
1658
|
-
@db.schema(Sequel[:s][:x])
|
1659
|
-
c.must_equal ["x", {:schema=>"s"}]
|
1660
|
-
ds = @db[Sequel[:s][:y]]
|
1661
|
-
@db.schema(ds)
|
1662
|
-
c.must_equal ["y", {:schema=>"s", :dataset=>ds}]
|
1663
|
-
end
|
1664
|
-
|
1665
|
-
with_symbol_splitting "should provide options if given a table name with splittable symbols" do
|
1666
|
-
c = nil
|
1667
|
-
@db.define_singleton_method(:schema_parse_table) do |t, opts|
|
1668
|
-
c = [t, opts]
|
1669
|
-
[[:a, {:db_type=>t.to_s}]]
|
1670
|
-
end
|
1671
|
-
@db.schema(:s__x)
|
1672
|
-
c.must_equal ["x", {:schema=>"s"}]
|
1673
|
-
ds = @db[:s__y]
|
1674
|
-
@db.schema(ds)
|
1675
|
-
c.must_equal ["y", {:schema=>"s", :dataset=>ds}]
|
1676
|
-
end
|
1677
|
-
|
1678
|
-
it "should parse the schema correctly for a single table" do
|
1679
|
-
sqls = @sqls
|
1680
|
-
proc{@db.schema(:x)}.must_raise(Sequel::Error)
|
1681
|
-
@db.define_singleton_method(:schema_parse_table) do |t, opts|
|
1682
|
-
sqls << t
|
1683
|
-
[[:a, {:db_type=>t.to_s}]]
|
1684
|
-
end
|
1685
|
-
@db.schema(:x).must_equal [[:a, {:db_type=>"x", :ruby_default=>nil}]]
|
1686
|
-
@sqls.must_equal ['x']
|
1687
|
-
@db.schema(:x).must_equal [[:a, {:db_type=>"x", :ruby_default=>nil}]]
|
1688
|
-
@sqls.must_equal ['x']
|
1689
|
-
@db.schema(:x, :reload=>true).must_equal [[:a, {:db_type=>"x", :ruby_default=>nil}]]
|
1690
|
-
@sqls.must_equal ['x', 'x']
|
1691
|
-
end
|
1692
|
-
|
1693
|
-
it "should dedup :db_type strings" do
|
1694
|
-
@db.define_singleton_method(:schema_parse_table) do |t, opts|
|
1695
|
-
[[:a, {:db_type=>t.to_s.dup}], [:b, {:db_type=>t.to_s.dup}]]
|
1696
|
-
end
|
1697
|
-
sch = @db.schema(:x)
|
1698
|
-
sch.must_equal [[:a, {:db_type=>"x", :ruby_default=>nil}], [:b, {:db_type=>"x", :ruby_default=>nil}]]
|
1699
|
-
sch[0][1][:db_type].must_be_same_as(sch[1][1][:db_type])
|
1700
|
-
end if RUBY_VERSION >= '2.5'
|
1701
|
-
|
1702
|
-
it "should set :auto_increment to true by default if unset and a single integer primary key is used" do
|
1703
|
-
@db.define_singleton_method(:schema_parse_table){|*| [[:a, {:primary_key=>true, :db_type=>'integer'}]]}
|
1704
|
-
@db.schema(:x).first.last[:auto_increment].must_equal true
|
1705
|
-
end
|
1706
|
-
|
1707
|
-
it "should not set :auto_increment if already set" do
|
1708
|
-
@db.define_singleton_method(:schema_parse_table){|*| [[:a, {:primary_key=>true, :db_type=>'integer', :auto_increment=>false}]]}
|
1709
|
-
@db.schema(:x).first.last[:auto_increment].must_equal false
|
1710
|
-
end
|
1711
|
-
|
1712
|
-
it "should set :auto_increment to false by default if unset and a single nonintegery primary key is used" do
|
1713
|
-
@db.define_singleton_method(:schema_parse_table){|*| [[:a, {:primary_key=>true, :db_type=>'varchar'}]]}
|
1714
|
-
@db.schema(:x).first.last[:auto_increment].must_equal false
|
1715
|
-
end
|
1716
|
-
|
1717
|
-
it "should set :auto_increment to false by default if unset and a composite primary key" do
|
1718
|
-
@db.define_singleton_method(:schema_parse_table){|*| [[:a, {:primary_key=>true, :db_type=>'integer'}], [:b, {:primary_key=>true, :db_type=>'integer'}]]}
|
1719
|
-
@db.schema(:x).first.last[:auto_increment].must_equal false
|
1720
|
-
@db.schema(:x).last.last[:auto_increment].must_equal false
|
1721
|
-
end
|
1722
|
-
|
1723
|
-
it "should set :auto_increment to true by default if set and not the first column" do
|
1724
|
-
@db.define_singleton_method(:schema_parse_table){|*| [[:b, {}], [:a, {:primary_key=>true, :db_type=>'integer'}]]}
|
1725
|
-
@db.schema(:x).last.last[:auto_increment].must_equal true
|
1726
|
-
end
|
1727
|
-
|
1728
|
-
it "should convert various types of table name arguments" do
|
1729
|
-
@db.define_singleton_method(:schema_parse_table) do |t, opts|
|
1730
|
-
[[t, opts]]
|
1731
|
-
end
|
1732
|
-
s1 = @db.schema(:x)
|
1733
|
-
s1.must_equal [['x', {:ruby_default=>nil}]]
|
1734
|
-
@db.schema(:x).object_id.must_equal s1.object_id
|
1735
|
-
@db.schema(Sequel.identifier(:x)).object_id.must_equal s1.object_id
|
1736
|
-
|
1737
|
-
s2 = @db.schema(Sequel[:x][:y])
|
1738
|
-
s2.must_equal [['y', {:schema=>'x', :ruby_default=>nil}]]
|
1739
|
-
@db.schema(Sequel[:x][:y]).object_id.must_equal s2.object_id
|
1740
|
-
@db.schema(Sequel.qualify(:x, :y)).object_id.must_equal s2.object_id
|
1741
|
-
|
1742
|
-
s2 = @db.schema(Sequel.qualify(:v, Sequel[:x][:y]))
|
1743
|
-
s2.must_equal [['y', {:schema=>'x', :ruby_default=>nil, :information_schema_schema=>Sequel.identifier('v')}]]
|
1744
|
-
@db.schema(Sequel.qualify(:v, Sequel[:x][:y])).object_id.must_equal s2.object_id
|
1745
|
-
@db.schema(Sequel.qualify(Sequel[:v][:x], :y)).object_id.must_equal s2.object_id
|
1746
|
-
|
1747
|
-
s2 = @db.schema(Sequel.qualify(Sequel[:u][:v], Sequel[:x][:y]))
|
1748
|
-
s2.must_equal [['y', {:schema=>'x', :ruby_default=>nil, :information_schema_schema=>Sequel.qualify('u', 'v')}]]
|
1749
|
-
@db.schema(Sequel.qualify(Sequel[:u][:v], Sequel[:x][:y])).object_id.must_equal s2.object_id
|
1750
|
-
@db.schema(Sequel.qualify(Sequel.qualify(:u, :v), Sequel.qualify(:x, :y))).object_id.must_equal s2.object_id
|
1751
|
-
end
|
1752
|
-
|
1753
|
-
with_symbol_splitting "should convert splittable symbol arguments" do
|
1754
|
-
@db.define_singleton_method(:schema_parse_table) do |t, opts|
|
1755
|
-
[[t, opts]]
|
1756
|
-
end
|
1757
|
-
s1 = @db.schema(:x)
|
1758
|
-
s1.must_equal [['x', {:ruby_default=>nil}]]
|
1759
|
-
@db.schema(:x).object_id.must_equal s1.object_id
|
1760
|
-
@db.schema(Sequel.identifier(:x)).object_id.must_equal s1.object_id
|
1761
|
-
|
1762
|
-
s2 = @db.schema(:x__y)
|
1763
|
-
s2.must_equal [['y', {:schema=>'x', :ruby_default=>nil}]]
|
1764
|
-
@db.schema(:x__y).object_id.must_equal s2.object_id
|
1765
|
-
@db.schema(Sequel.qualify(:x, :y)).object_id.must_equal s2.object_id
|
1766
|
-
|
1767
|
-
s2 = @db.schema(Sequel.qualify(:v, :x__y))
|
1768
|
-
s2.must_equal [['y', {:schema=>'x', :ruby_default=>nil, :information_schema_schema=>Sequel.identifier('v')}]]
|
1769
|
-
@db.schema(Sequel.qualify(:v, :x__y)).object_id.must_equal s2.object_id
|
1770
|
-
@db.schema(Sequel.qualify(:v__x, :y)).object_id.must_equal s2.object_id
|
1771
|
-
|
1772
|
-
s2 = @db.schema(Sequel.qualify(:u__v, :x__y))
|
1773
|
-
s2.must_equal [['y', {:schema=>'x', :ruby_default=>nil, :information_schema_schema=>Sequel.qualify('u', 'v')}]]
|
1774
|
-
@db.schema(Sequel.qualify(:u__v, :x__y)).object_id.must_equal s2.object_id
|
1775
|
-
@db.schema(Sequel.qualify(Sequel.qualify(:u, :v), Sequel.qualify(:x, :y))).object_id.must_equal s2.object_id
|
1776
|
-
end
|
1777
|
-
|
1778
|
-
it "should correctly parse all supported data types" do
|
1779
|
-
sm = Module.new do
|
1780
|
-
def schema_parse_table(t, opts)
|
1781
|
-
[[:x, {:db_type=>t.to_s, :type=>schema_column_type(t.to_s)}]]
|
1782
|
-
end
|
1783
|
-
end
|
1784
|
-
@db.extend(sm)
|
1785
|
-
@db.schema(:tinyint).first.last[:type].must_equal :integer
|
1786
|
-
@db.schema(:int).first.last[:type].must_equal :integer
|
1787
|
-
@db.schema(:integer).first.last[:type].must_equal :integer
|
1788
|
-
@db.schema(:bigint).first.last[:type].must_equal :integer
|
1789
|
-
@db.schema(:smallint).first.last[:type].must_equal :integer
|
1790
|
-
@db.schema(:character).first.last[:type].must_equal :string
|
1791
|
-
@db.schema(:"character varying").first.last[:type].must_equal :string
|
1792
|
-
@db.schema(:varchar).first.last[:type].must_equal :string
|
1793
|
-
@db.schema(:"varchar(255)").first.last[:type].must_equal :string
|
1794
|
-
@db.schema(:"varchar(255)").first.last[:max_length].must_equal 255
|
1795
|
-
@db.schema(:text).first.last[:type].must_equal :string
|
1796
|
-
@db.schema(:date).first.last[:type].must_equal :date
|
1797
|
-
@db.schema(:datetime).first.last[:type].must_equal :datetime
|
1798
|
-
@db.schema(:smalldatetime).first.last[:type].must_equal :datetime
|
1799
|
-
@db.schema(:timestamp).first.last[:type].must_equal :datetime
|
1800
|
-
@db.schema(:"timestamp with time zone").first.last[:type].must_equal :datetime
|
1801
|
-
@db.schema(:"timestamp without time zone").first.last[:type].must_equal :datetime
|
1802
|
-
@db.schema(:time).first.last[:type].must_equal :time
|
1803
|
-
@db.schema(:"time with time zone").first.last[:type].must_equal :time
|
1804
|
-
@db.schema(:"time without time zone").first.last[:type].must_equal :time
|
1805
|
-
@db.schema(:bool).first.last[:type].must_equal :boolean
|
1806
|
-
@db.schema(:boolean).first.last[:type].must_equal :boolean
|
1807
|
-
@db.schema(:real).first.last[:type].must_equal :float
|
1808
|
-
@db.schema(:float).first.last[:type].must_equal :float
|
1809
|
-
@db.schema(:"float unsigned").first.last[:type].must_equal :float
|
1810
|
-
@db.schema(:double).first.last[:type].must_equal :float
|
1811
|
-
@db.schema(:"double(1,2)").first.last[:type].must_equal :float
|
1812
|
-
@db.schema(:"double(1,2) unsigned").first.last[:type].must_equal :float
|
1813
|
-
@db.schema(:"double precision").first.last[:type].must_equal :float
|
1814
|
-
@db.schema(:number).first.last[:type].must_equal :decimal
|
1815
|
-
@db.schema(:numeric).first.last[:type].must_equal :decimal
|
1816
|
-
@db.schema(:decimal).first.last[:type].must_equal :decimal
|
1817
|
-
@db.schema(:"number(10,0)").first.last[:type].must_equal :integer
|
1818
|
-
@db.schema(:"numeric(10, 10)").first.last[:type].must_equal :decimal
|
1819
|
-
@db.schema(:"decimal(10,1)").first.last[:type].must_equal :decimal
|
1820
|
-
@db.schema(:bytea).first.last[:type].must_equal :blob
|
1821
|
-
@db.schema(:blob).first.last[:type].must_equal :blob
|
1822
|
-
@db.schema(:image).first.last[:type].must_equal :blob
|
1823
|
-
@db.schema(:nchar).first.last[:type].must_equal :string
|
1824
|
-
@db.schema(:nvarchar).first.last[:type].must_equal :string
|
1825
|
-
@db.schema(:ntext).first.last[:type].must_equal :string
|
1826
|
-
@db.schema(:clob).first.last[:type].must_equal :string
|
1827
|
-
@db.schema(:ntext).first.last[:type].must_equal :string
|
1828
|
-
@db.schema(:smalldatetime).first.last[:type].must_equal :datetime
|
1829
|
-
@db.schema(:binary).first.last[:type].must_equal :blob
|
1830
|
-
@db.schema(:varbinary).first.last[:type].must_equal :blob
|
1831
|
-
@db.schema(:enum).first.last[:type].must_equal :enum
|
1832
|
-
|
1833
|
-
@db = Sequel.mock(:host=>'postgres')
|
1834
|
-
@db.extend(sm)
|
1835
|
-
@db.schema(:interval).first.last[:type].must_equal :interval
|
1836
|
-
@db.schema(:citext).first.last[:type].must_equal :string
|
1837
|
-
|
1838
|
-
@db = Sequel.mock(:host=>'mysql')
|
1839
|
-
@db.extend(sm)
|
1840
|
-
@db.schema(:set).first.last[:type].must_equal :set
|
1841
|
-
@db.schema(:mediumint).first.last[:type].must_equal :integer
|
1842
|
-
@db.schema(:mediumtext).first.last[:type].must_equal :string
|
1843
|
-
end
|
1844
|
-
end
|