sequel 3.21.0 → 3.39.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +1061 -3
- data/MIT-LICENSE +1 -1
- data/README.rdoc +103 -63
- data/Rakefile +59 -27
- data/bin/sequel +50 -26
- data/doc/active_record.rdoc +67 -51
- data/doc/advanced_associations.rdoc +189 -75
- data/doc/association_basics.rdoc +327 -79
- data/doc/cheat_sheet.rdoc +21 -21
- data/doc/core_extensions.rdoc +374 -0
- data/doc/dataset_basics.rdoc +10 -10
- data/doc/dataset_filtering.rdoc +54 -42
- data/doc/mass_assignment.rdoc +56 -0
- data/doc/migration.rdoc +89 -557
- data/doc/model_hooks.rdoc +80 -29
- data/doc/object_model.rdoc +547 -0
- data/doc/opening_databases.rdoc +114 -53
- data/doc/prepared_statements.rdoc +41 -15
- data/doc/querying.rdoc +137 -56
- data/doc/reflection.rdoc +36 -10
- data/doc/release_notes/3.22.0.txt +39 -0
- data/doc/release_notes/3.23.0.txt +172 -0
- data/doc/release_notes/3.24.0.txt +420 -0
- data/doc/release_notes/3.25.0.txt +88 -0
- data/doc/release_notes/3.26.0.txt +88 -0
- data/doc/release_notes/3.27.0.txt +82 -0
- data/doc/release_notes/3.28.0.txt +304 -0
- data/doc/release_notes/3.29.0.txt +459 -0
- data/doc/release_notes/3.30.0.txt +135 -0
- data/doc/release_notes/3.31.0.txt +146 -0
- data/doc/release_notes/3.32.0.txt +202 -0
- data/doc/release_notes/3.33.0.txt +157 -0
- data/doc/release_notes/3.34.0.txt +671 -0
- data/doc/release_notes/3.35.0.txt +144 -0
- data/doc/release_notes/3.36.0.txt +245 -0
- data/doc/release_notes/3.37.0.txt +338 -0
- data/doc/release_notes/3.38.0.txt +234 -0
- data/doc/release_notes/3.39.0.txt +237 -0
- data/doc/schema_modification.rdoc +585 -0
- data/doc/sharding.rdoc +99 -8
- data/doc/sql.rdoc +154 -112
- data/doc/testing.rdoc +169 -0
- data/doc/thread_safety.rdoc +17 -0
- data/doc/transactions.rdoc +137 -0
- data/doc/validations.rdoc +1 -1
- data/doc/virtual_rows.rdoc +78 -43
- data/lib/sequel/adapters/ado/mssql.rb +18 -6
- data/lib/sequel/adapters/ado.rb +40 -18
- data/lib/sequel/adapters/amalgalite.rb +17 -9
- data/lib/sequel/adapters/db2.rb +175 -86
- data/lib/sequel/adapters/dbi.rb +15 -15
- data/lib/sequel/adapters/do/mysql.rb +17 -11
- data/lib/sequel/adapters/do/postgres.rb +2 -61
- data/lib/sequel/adapters/do/sqlite.rb +0 -10
- data/lib/sequel/adapters/do.rb +18 -36
- data/lib/sequel/adapters/firebird.rb +27 -208
- data/lib/sequel/adapters/ibmdb.rb +453 -0
- data/lib/sequel/adapters/informix.rb +6 -23
- data/lib/sequel/adapters/jdbc/as400.rb +16 -34
- data/lib/sequel/adapters/jdbc/db2.rb +56 -0
- data/lib/sequel/adapters/jdbc/derby.rb +325 -0
- data/lib/sequel/adapters/jdbc/firebird.rb +24 -0
- data/lib/sequel/adapters/jdbc/h2.rb +73 -31
- data/lib/sequel/adapters/jdbc/hsqldb.rb +184 -0
- data/lib/sequel/adapters/jdbc/informix.rb +21 -0
- data/lib/sequel/adapters/jdbc/jtds.rb +40 -0
- data/lib/sequel/adapters/jdbc/mssql.rb +3 -33
- data/lib/sequel/adapters/jdbc/mysql.rb +7 -24
- data/lib/sequel/adapters/jdbc/oracle.rb +88 -25
- data/lib/sequel/adapters/jdbc/postgresql.rb +77 -52
- data/lib/sequel/adapters/jdbc/progress.rb +21 -0
- data/lib/sequel/adapters/jdbc/sqlite.rb +20 -10
- data/lib/sequel/adapters/jdbc/sqlserver.rb +66 -0
- data/lib/sequel/adapters/jdbc/transactions.rb +83 -0
- data/lib/sequel/adapters/jdbc.rb +279 -97
- data/lib/sequel/adapters/mock.rb +372 -0
- data/lib/sequel/adapters/mysql.rb +125 -222
- data/lib/sequel/adapters/mysql2.rb +65 -56
- data/lib/sequel/adapters/odbc/mssql.rb +17 -9
- data/lib/sequel/adapters/odbc.rb +28 -9
- data/lib/sequel/adapters/openbase.rb +3 -5
- data/lib/sequel/adapters/oracle.rb +349 -53
- data/lib/sequel/adapters/postgres.rb +368 -138
- data/lib/sequel/adapters/shared/access.rb +32 -9
- data/lib/sequel/adapters/shared/db2.rb +343 -0
- data/lib/sequel/adapters/shared/firebird.rb +221 -0
- data/lib/sequel/adapters/shared/informix.rb +53 -0
- data/lib/sequel/adapters/shared/mssql.rb +401 -116
- data/lib/sequel/adapters/shared/mysql.rb +424 -89
- data/lib/sequel/adapters/shared/mysql_prepared_statements.rb +155 -0
- data/lib/sequel/adapters/shared/oracle.rb +257 -45
- data/lib/sequel/adapters/shared/postgres.rb +675 -279
- data/lib/sequel/adapters/shared/progress.rb +5 -8
- data/lib/sequel/adapters/shared/sqlite.rb +285 -96
- data/lib/sequel/adapters/sqlite.rb +101 -39
- data/lib/sequel/adapters/swift/mysql.rb +10 -12
- data/lib/sequel/adapters/swift/postgres.rb +13 -79
- data/lib/sequel/adapters/swift/sqlite.rb +9 -6
- data/lib/sequel/adapters/swift.rb +28 -19
- data/lib/sequel/adapters/tinytds.rb +174 -22
- data/lib/sequel/adapters/utils/emulate_offset_with_row_number.rb +77 -0
- data/lib/sequel/adapters/utils/pg_types.rb +81 -0
- data/lib/sequel/adapters/utils/stored_procedures.rb +1 -11
- data/lib/sequel/ast_transformer.rb +194 -0
- data/lib/sequel/connection_pool/sharded_single.rb +11 -1
- data/lib/sequel/connection_pool/sharded_threaded.rb +33 -2
- data/lib/sequel/connection_pool/single.rb +5 -0
- data/lib/sequel/connection_pool/threaded.rb +43 -14
- data/lib/sequel/connection_pool.rb +10 -2
- data/lib/sequel/core.rb +143 -11
- data/lib/sequel/database/connecting.rb +38 -28
- data/lib/sequel/database/dataset.rb +4 -4
- data/lib/sequel/database/dataset_defaults.rb +63 -2
- data/lib/sequel/database/logging.rb +7 -2
- data/lib/sequel/database/misc.rb +190 -46
- data/lib/sequel/database/query.rb +272 -110
- data/lib/sequel/database/schema_generator.rb +103 -36
- data/lib/sequel/database/schema_methods.rb +300 -67
- data/lib/sequel/dataset/actions.rb +344 -90
- data/lib/sequel/dataset/features.rb +101 -8
- data/lib/sequel/dataset/graph.rb +42 -15
- data/lib/sequel/dataset/misc.rb +52 -25
- data/lib/sequel/dataset/mutation.rb +12 -7
- data/lib/sequel/dataset/prepared_statements.rb +68 -30
- data/lib/sequel/dataset/query.rb +305 -54
- data/lib/sequel/dataset/sql.rb +720 -292
- data/lib/sequel/dataset.rb +8 -0
- data/lib/sequel/exceptions.rb +4 -0
- data/lib/sequel/extensions/_pretty_table.rb +83 -0
- data/lib/sequel/extensions/arbitrary_servers.rb +109 -0
- data/lib/sequel/extensions/blank.rb +4 -0
- data/lib/sequel/extensions/columns_introspection.rb +72 -0
- data/lib/sequel/extensions/constraint_validations.rb +451 -0
- data/lib/sequel/{core_sql.rb → extensions/core_extensions.rb} +30 -46
- data/lib/sequel/extensions/eval_inspect.rb +173 -0
- data/lib/sequel/extensions/inflector.rb +4 -0
- data/lib/sequel/extensions/looser_typecasting.rb +5 -4
- data/lib/sequel/extensions/migration.rb +100 -11
- data/lib/sequel/extensions/named_timezones.rb +9 -0
- data/lib/sequel/extensions/null_dataset.rb +94 -0
- data/lib/sequel/extensions/pagination.rb +4 -0
- data/lib/sequel/extensions/pg_array.rb +537 -0
- data/lib/sequel/extensions/pg_array_ops.rb +263 -0
- data/lib/sequel/extensions/pg_auto_parameterize.rb +175 -0
- data/lib/sequel/extensions/pg_hstore.rb +313 -0
- data/lib/sequel/extensions/pg_hstore_ops.rb +293 -0
- data/lib/sequel/extensions/pg_inet.rb +113 -0
- data/lib/sequel/extensions/pg_interval.rb +191 -0
- data/lib/sequel/extensions/pg_json.rb +208 -0
- data/lib/sequel/extensions/pg_range.rb +512 -0
- data/lib/sequel/extensions/pg_range_ops.rb +150 -0
- data/lib/sequel/extensions/pg_row.rb +572 -0
- data/lib/sequel/extensions/pg_row_ops.rb +182 -0
- data/lib/sequel/extensions/pg_statement_cache.rb +317 -0
- data/lib/sequel/extensions/pretty_table.rb +17 -72
- data/lib/sequel/extensions/query.rb +8 -4
- data/lib/sequel/extensions/query_literals.rb +79 -0
- data/lib/sequel/extensions/schema_caching.rb +76 -0
- data/lib/sequel/extensions/schema_dumper.rb +282 -76
- data/lib/sequel/extensions/select_remove.rb +39 -0
- data/lib/sequel/extensions/server_block.rb +140 -0
- data/lib/sequel/extensions/split_array_nil.rb +65 -0
- data/lib/sequel/extensions/sql_expr.rb +8 -110
- data/lib/sequel/extensions/string_date_time.rb +4 -0
- data/lib/sequel/extensions/thread_local_timezones.rb +10 -4
- data/lib/sequel/extensions/to_dot.rb +99 -83
- data/lib/sequel/model/associations.rb +1263 -409
- data/lib/sequel/model/base.rb +624 -171
- data/lib/sequel/model/errors.rb +1 -1
- data/lib/sequel/model/exceptions.rb +24 -2
- data/lib/sequel/model/inflections.rb +1 -1
- data/lib/sequel/model.rb +30 -11
- data/lib/sequel/no_core_ext.rb +2 -0
- data/lib/sequel/plugins/active_model.rb +13 -1
- data/lib/sequel/plugins/association_pks.rb +22 -4
- data/lib/sequel/plugins/caching.rb +25 -18
- data/lib/sequel/plugins/class_table_inheritance.rb +4 -4
- data/lib/sequel/plugins/composition.rb +44 -12
- data/lib/sequel/plugins/constraint_validations.rb +198 -0
- data/lib/sequel/plugins/dataset_associations.rb +100 -0
- data/lib/sequel/plugins/defaults_setter.rb +72 -0
- data/lib/sequel/plugins/dirty.rb +214 -0
- data/lib/sequel/plugins/eager_each.rb +59 -0
- data/lib/sequel/plugins/force_encoding.rb +6 -6
- data/lib/sequel/plugins/hook_class_methods.rb +1 -1
- data/lib/sequel/plugins/identity_map.rb +134 -15
- data/lib/sequel/plugins/instance_filters.rb +10 -0
- data/lib/sequel/plugins/instance_hooks.rb +1 -1
- data/lib/sequel/plugins/json_serializer.rb +58 -6
- data/lib/sequel/plugins/list.rb +13 -2
- data/lib/sequel/plugins/many_through_many.rb +103 -51
- data/lib/sequel/plugins/many_to_one_pk_lookup.rb +71 -0
- data/lib/sequel/plugins/nested_attributes.rb +150 -66
- data/lib/sequel/plugins/optimistic_locking.rb +8 -0
- data/lib/sequel/plugins/pg_row.rb +121 -0
- data/lib/sequel/plugins/pg_typecast_on_load.rb +65 -0
- data/lib/sequel/plugins/prepared_statements.rb +167 -0
- data/lib/sequel/plugins/prepared_statements_associations.rb +87 -0
- data/lib/sequel/plugins/prepared_statements_safe.rb +82 -0
- data/lib/sequel/plugins/prepared_statements_with_pk.rb +59 -0
- data/lib/sequel/plugins/rcte_tree.rb +31 -17
- data/lib/sequel/plugins/schema.rb +8 -3
- data/lib/sequel/plugins/serialization.rb +98 -49
- data/lib/sequel/plugins/serialization_modification_detection.rb +63 -0
- data/lib/sequel/plugins/sharding.rb +21 -54
- data/lib/sequel/plugins/single_table_inheritance.rb +5 -3
- data/lib/sequel/plugins/static_cache.rb +99 -0
- data/lib/sequel/plugins/subclasses.rb +29 -3
- data/lib/sequel/plugins/tactical_eager_loading.rb +7 -7
- data/lib/sequel/plugins/timestamps.rb +1 -1
- data/lib/sequel/plugins/tree.rb +3 -3
- data/lib/sequel/plugins/typecast_on_load.rb +9 -12
- data/lib/sequel/plugins/update_primary_key.rb +2 -2
- data/lib/sequel/plugins/validation_class_methods.rb +1 -1
- data/lib/sequel/plugins/validation_helpers.rb +55 -4
- data/lib/sequel/plugins/xml_serializer.rb +15 -4
- data/lib/sequel/sql.rb +649 -122
- data/lib/sequel/timezones.rb +67 -40
- data/lib/sequel/version.rb +1 -1
- data/spec/adapters/db2_spec.rb +146 -0
- data/spec/adapters/firebird_spec.rb +1 -1
- data/spec/adapters/mssql_spec.rb +194 -66
- data/spec/adapters/mysql_spec.rb +475 -306
- data/spec/adapters/oracle_spec.rb +92 -117
- data/spec/adapters/postgres_spec.rb +1982 -325
- data/spec/adapters/spec_helper.rb +9 -6
- data/spec/adapters/sqlite_spec.rb +203 -75
- data/spec/core/connection_pool_spec.rb +218 -93
- data/spec/core/database_spec.rb +956 -522
- data/spec/core/dataset_spec.rb +1631 -1242
- data/spec/core/expression_filters_spec.rb +690 -334
- data/spec/core/mock_adapter_spec.rb +453 -0
- data/spec/core/object_graph_spec.rb +88 -129
- data/spec/core/schema_generator_spec.rb +4 -4
- data/spec/core/schema_spec.rb +556 -53
- data/spec/core/spec_helper.rb +7 -48
- data/spec/core_extensions_spec.rb +626 -0
- data/spec/extensions/active_model_spec.rb +13 -0
- data/spec/extensions/arbitrary_servers_spec.rb +110 -0
- data/spec/extensions/association_autoreloading_spec.rb +18 -10
- data/spec/extensions/association_dependencies_spec.rb +15 -25
- data/spec/extensions/association_pks_spec.rb +66 -32
- data/spec/extensions/association_proxies_spec.rb +4 -4
- data/spec/extensions/boolean_readers_spec.rb +25 -25
- data/spec/extensions/caching_spec.rb +47 -51
- data/spec/extensions/class_table_inheritance_spec.rb +31 -83
- data/spec/extensions/columns_introspection_spec.rb +91 -0
- data/spec/extensions/composition_spec.rb +18 -13
- data/spec/extensions/constraint_validations_plugin_spec.rb +196 -0
- data/spec/extensions/constraint_validations_spec.rb +316 -0
- data/spec/extensions/dataset_associations_spec.rb +199 -0
- data/spec/extensions/defaults_setter_spec.rb +88 -0
- data/spec/extensions/dirty_spec.rb +155 -0
- data/spec/extensions/eager_each_spec.rb +34 -0
- data/spec/extensions/eval_inspect_spec.rb +67 -0
- data/spec/extensions/force_encoding_spec.rb +4 -2
- data/spec/extensions/hook_class_methods_spec.rb +97 -128
- data/spec/extensions/identity_map_spec.rb +142 -24
- data/spec/extensions/inflector_spec.rb +0 -4
- data/spec/extensions/instance_filters_spec.rb +11 -21
- data/spec/extensions/instance_hooks_spec.rb +72 -0
- data/spec/extensions/json_serializer_spec.rb +49 -12
- data/spec/extensions/lazy_attributes_spec.rb +16 -20
- data/spec/extensions/list_spec.rb +49 -40
- data/spec/extensions/looser_typecasting_spec.rb +7 -7
- data/spec/extensions/many_through_many_spec.rb +368 -254
- data/spec/extensions/many_to_one_pk_lookup_spec.rb +140 -0
- data/spec/extensions/migration_spec.rb +140 -35
- data/spec/extensions/named_timezones_spec.rb +29 -11
- data/spec/extensions/nested_attributes_spec.rb +268 -89
- data/spec/extensions/null_dataset_spec.rb +85 -0
- data/spec/extensions/optimistic_locking_spec.rb +24 -21
- data/spec/extensions/pg_array_ops_spec.rb +112 -0
- data/spec/extensions/pg_array_spec.rb +320 -0
- data/spec/extensions/pg_auto_parameterize_spec.rb +65 -0
- data/spec/extensions/pg_hstore_ops_spec.rb +140 -0
- data/spec/extensions/pg_hstore_spec.rb +194 -0
- data/spec/extensions/pg_inet_spec.rb +47 -0
- data/spec/extensions/pg_interval_spec.rb +72 -0
- data/spec/extensions/pg_json_spec.rb +99 -0
- data/spec/extensions/pg_range_ops_spec.rb +56 -0
- data/spec/extensions/pg_range_spec.rb +395 -0
- data/spec/extensions/pg_row_ops_spec.rb +58 -0
- data/spec/extensions/pg_row_plugin_spec.rb +49 -0
- data/spec/extensions/pg_row_spec.rb +323 -0
- data/spec/extensions/pg_statement_cache_spec.rb +208 -0
- data/spec/extensions/pg_typecast_on_load_spec.rb +58 -0
- data/spec/extensions/prepared_statements_associations_spec.rb +111 -0
- data/spec/extensions/prepared_statements_safe_spec.rb +61 -0
- data/spec/extensions/prepared_statements_spec.rb +87 -0
- data/spec/extensions/prepared_statements_with_pk_spec.rb +31 -0
- data/spec/extensions/pretty_table_spec.rb +7 -6
- data/spec/extensions/query_literals_spec.rb +167 -0
- data/spec/extensions/query_spec.rb +9 -3
- data/spec/extensions/rcte_tree_spec.rb +50 -43
- data/spec/extensions/schema_caching_spec.rb +41 -0
- data/spec/extensions/schema_dumper_spec.rb +434 -49
- data/spec/extensions/schema_spec.rb +30 -49
- data/spec/extensions/select_remove_spec.rb +38 -0
- data/spec/extensions/serialization_modification_detection_spec.rb +72 -0
- data/spec/extensions/serialization_spec.rb +86 -48
- data/spec/extensions/server_block_spec.rb +90 -0
- data/spec/extensions/sharding_spec.rb +69 -143
- data/spec/extensions/single_table_inheritance_spec.rb +23 -29
- data/spec/extensions/skip_create_refresh_spec.rb +1 -1
- data/spec/extensions/spec_helper.rb +34 -67
- data/spec/extensions/split_array_nil_spec.rb +24 -0
- data/spec/extensions/sql_expr_spec.rb +29 -60
- data/spec/extensions/static_cache_spec.rb +145 -0
- data/spec/extensions/subclasses_spec.rb +14 -0
- data/spec/extensions/tactical_eager_loading_spec.rb +17 -19
- data/spec/extensions/thread_local_timezones_spec.rb +22 -2
- data/spec/extensions/timestamps_spec.rb +6 -6
- data/spec/extensions/to_dot_spec.rb +8 -11
- data/spec/extensions/touch_spec.rb +13 -14
- data/spec/extensions/tree_spec.rb +11 -26
- data/spec/extensions/typecast_on_load_spec.rb +9 -6
- data/spec/extensions/update_primary_key_spec.rb +30 -24
- data/spec/extensions/validation_class_methods_spec.rb +58 -67
- data/spec/extensions/validation_helpers_spec.rb +31 -37
- data/spec/extensions/xml_serializer_spec.rb +29 -4
- data/spec/files/transaction_specified_migrations/001_create_alt_basic.rb +4 -0
- data/spec/files/transaction_specified_migrations/002_create_basic.rb +4 -0
- data/spec/files/transaction_unspecified_migrations/001_create_alt_basic.rb +3 -0
- data/spec/files/transaction_unspecified_migrations/002_create_basic.rb +3 -0
- data/spec/integration/associations_test.rb +659 -21
- data/spec/integration/database_test.rb +11 -5
- data/spec/integration/dataset_test.rb +717 -129
- data/spec/integration/eager_loader_test.rb +38 -54
- data/spec/integration/migrator_test.rb +2 -3
- data/spec/integration/model_test.rb +81 -2
- data/spec/integration/plugin_test.rb +604 -147
- data/spec/integration/prepared_statement_test.rb +272 -128
- data/spec/integration/schema_test.rb +367 -57
- data/spec/integration/spec_helper.rb +42 -32
- data/spec/integration/timezone_test.rb +38 -12
- data/spec/integration/transaction_test.rb +183 -8
- data/spec/integration/type_test.rb +31 -8
- data/spec/model/association_reflection_spec.rb +207 -8
- data/spec/model/associations_spec.rb +1206 -693
- data/spec/model/base_spec.rb +334 -100
- data/spec/model/dataset_methods_spec.rb +45 -27
- data/spec/model/eager_loading_spec.rb +828 -692
- data/spec/model/hooks_spec.rb +355 -67
- data/spec/model/model_spec.rb +251 -179
- data/spec/model/plugins_spec.rb +24 -13
- data/spec/model/record_spec.rb +506 -232
- data/spec/model/spec_helper.rb +16 -77
- data/spec/model/validations_spec.rb +38 -2
- data/spec/rcov.opts +2 -0
- metadata +209 -80
- data/spec/core/core_sql_spec.rb +0 -451
data/doc/migration.rdoc
CHANGED
|
@@ -13,19 +13,21 @@ create the necessary database structure manually using Sequel's schema
|
|
|
13
13
|
modification methods or another database tool. However, if you are dealing
|
|
14
14
|
with other developers, you'll have to send them all of the changes you are
|
|
15
15
|
making. Even if you aren't dealing with other developers, you generally have
|
|
16
|
-
to make the schema changes in 3 places (development, testing, and
|
|
16
|
+
to make the schema changes in 3 places (development, testing, and
|
|
17
17
|
production), and it's probably easier to use the migrations system to apply
|
|
18
18
|
the schema changes than it is to keep track of the changes manually and
|
|
19
19
|
execute them manually at the appropriate time.
|
|
20
20
|
|
|
21
21
|
Sequel tracks which migrations you have already run, so to apply migrations
|
|
22
|
-
you generally need to
|
|
22
|
+
you generally need to run Sequel's migrator with <tt>bin/sequel -m</tt>:
|
|
23
23
|
|
|
24
24
|
sequel -m path/to/migrations postgres://host/database
|
|
25
25
|
|
|
26
26
|
Migrations in Sequel use a very simple DSL via the <tt>Sequel.migration</tt>
|
|
27
27
|
method, and inside the DSL, use the <tt>Sequel::Database</tt> schema
|
|
28
28
|
modification methods such as +create_table+ and +alter_table+.
|
|
29
|
+
See the {schema modification guide}[link:files/doc/schema_modification_rdoc.html]
|
|
30
|
+
for details on the schema modification methods you can use.
|
|
29
31
|
|
|
30
32
|
== A Basic Migration
|
|
31
33
|
|
|
@@ -45,7 +47,7 @@ Here is a fairly basic Sequel migration:
|
|
|
45
47
|
end
|
|
46
48
|
|
|
47
49
|
This migration has an +up+ block which adds an artist table with an integer primary key named id,
|
|
48
|
-
and a varchar or text column (depending on the database) named name that doesn't accept NULL values.
|
|
50
|
+
and a varchar or text column (depending on the database) named +name+ that doesn't accept +NULL+ values.
|
|
49
51
|
Migrations should include both up and +down+ blocks, with the +down+ block reversing
|
|
50
52
|
the change made by up. However, if you never need to be able to migrate down
|
|
51
53
|
(i.e. you are one of the people that doesn't make mistakes), you can leave out
|
|
@@ -117,7 +119,8 @@ This looks a little weird, but you need to be aware that inside an up or +down+
|
|
|
117
119
|
self always refers to the <tt>Sequel::Database</tt> object that the migration is being applied to.
|
|
118
120
|
Since <tt>Database#[]</tt> creates datasets, using <tt>self[:artists]</tt> inside the +up+ block creates
|
|
119
121
|
a dataset on the database representing all columns in the +artists+ table, and updates it to set the
|
|
120
|
-
+location+ column to <tt>'Sacramento'</tt>.
|
|
122
|
+
+location+ column to <tt>'Sacramento'</tt>. You should avoid referencing the <tt>Sequel::Database</tt>
|
|
123
|
+
object directly in your migration, and always use self to reference it, otherwise you may run into problems.
|
|
121
124
|
|
|
122
125
|
It is possible to use model classes inside migrations, as long as they are loaded into the ruby interpreter,
|
|
123
126
|
but it's a bad habit as changes to your model classes can then break old migrations, and this breakage is
|
|
@@ -141,7 +144,7 @@ Migrations themselves do not contain any schema modification methods, but they m
|
|
|
141
144
|
any of the <tt>Sequel::Database</tt> modification methods, of which there are many. The main
|
|
142
145
|
ones are +create_table+ and +alter_table+, but Sequel also comes with numerous other schema
|
|
143
146
|
modification methods, most of which are shortcuts for +alter_table+ (all of these methods are
|
|
144
|
-
described in more detail
|
|
147
|
+
described in more detail in the {schema modification guide}[link:files/doc/schema_modification_rdoc.html]):
|
|
145
148
|
|
|
146
149
|
* add_column
|
|
147
150
|
* add_index
|
|
@@ -170,14 +173,50 @@ which just executes the code on the underlying database.
|
|
|
170
173
|
|
|
171
174
|
== Errors when running migrations
|
|
172
175
|
|
|
173
|
-
Sequel attempts to run migrations inside of a transaction
|
|
174
|
-
|
|
175
|
-
|
|
176
|
+
Sequel attempts to run migrations inside of a transaction if the database supports
|
|
177
|
+
transactional DDL statements. On the databases that don't support transactional DDL
|
|
178
|
+
statements, if there is an error while running a migration, it will not rollback the
|
|
179
|
+
previous schema changes made by the migration. In that case, you will
|
|
176
180
|
need to update the database by hand.
|
|
177
181
|
|
|
178
182
|
It's recommended to always run migrations on a test database and ensure they work
|
|
179
183
|
before running them on any production database.
|
|
180
184
|
|
|
185
|
+
== Transactions
|
|
186
|
+
|
|
187
|
+
You can manually specify to use transactions on a per migration basis. For example,
|
|
188
|
+
if you want to force transaction use for a particular migration, call the transaction
|
|
189
|
+
method in the Sequel.migration block:
|
|
190
|
+
|
|
191
|
+
Sequel.migration do
|
|
192
|
+
transaction
|
|
193
|
+
change do
|
|
194
|
+
# ...
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
Likewise, you can disable transaction use via no_transaction:
|
|
199
|
+
|
|
200
|
+
Sequel.migration do
|
|
201
|
+
no_transaction
|
|
202
|
+
change do
|
|
203
|
+
# ...
|
|
204
|
+
end
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
This is necessary in some cases, such as when attempting to use CREATE INDEX CONCURRENTLY
|
|
208
|
+
on PostgreSQL (which supports transactional schema, but not that statement inside a
|
|
209
|
+
transaction).
|
|
210
|
+
|
|
211
|
+
You can also override the transactions setting at the migrator level, either by forcing
|
|
212
|
+
transactions even if no_transaction is set, or by disabling transactions all together:
|
|
213
|
+
|
|
214
|
+
# Force transaction use
|
|
215
|
+
Sequel::Migrator.run(DB, '/path/to/migrations/dir', :use_transactions=>true)
|
|
216
|
+
|
|
217
|
+
# Disable use of transactions
|
|
218
|
+
Sequel::Migrator.run(DB, '/path/to/migrations/dir', :use_transactions=>false)
|
|
219
|
+
|
|
181
220
|
== Migration files
|
|
182
221
|
|
|
183
222
|
While you can create migration objects yourself and apply them manually, most of the
|
|
@@ -263,7 +302,7 @@ is important though, as it is used to mean that all migrations should be unappli
|
|
|
263
302
|
=== +TimestampMigrator+ Filenames
|
|
264
303
|
|
|
265
304
|
With the +TimestampMigrator+, the version integer should represent a timestamp, though this isn't strictly
|
|
266
|
-
required.
|
|
305
|
+
required.
|
|
267
306
|
|
|
268
307
|
For example, for <tt>5/10/2010 12:00:00pm</tt>, you could use any of the following formats:
|
|
269
308
|
|
|
@@ -283,6 +322,9 @@ be difficult to unapply some the affected migrations correctly.
|
|
|
283
322
|
The +TimestampMigrator+ will be used if any filename in the migrations directory has a version
|
|
284
323
|
greater than 20000101. Otherwise, the +IntegerMigrator+ will be used.
|
|
285
324
|
|
|
325
|
+
You can force the use of the +TimestampMigrator+ in the API by calling TimestampMigrator.apply
|
|
326
|
+
instead of Migrator.apply.
|
|
327
|
+
|
|
286
328
|
=== How to choose
|
|
287
329
|
|
|
288
330
|
Basically, unless you need the features provided by the +TimestampMigrator+, stick with the
|
|
@@ -318,8 +360,8 @@ you should give it some thought before using it.
|
|
|
318
360
|
|
|
319
361
|
Just don't do it.
|
|
320
362
|
|
|
321
|
-
In general, you should not modify any migration that has been run on the database and been committed
|
|
322
|
-
the source control repository, unless the migration contains
|
|
363
|
+
In general, you should not modify any migration that has been run on the database and been committed to
|
|
364
|
+
the source control repository, unless the migration contains an error that causes data loss. As long
|
|
323
365
|
as it is possible to undo the migration without losing data, you should just add another migration
|
|
324
366
|
that undoes the actions of the previous bad migration, and maybe does the correct action afterward.
|
|
325
367
|
|
|
@@ -343,545 +385,18 @@ reverses the changes made by the +up+ block:
|
|
|
343
385
|
down{...}
|
|
344
386
|
end
|
|
345
387
|
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
Inside your migration's down and +up+ blocks is where you will call the +Database+ schema modification methods.
|
|
349
|
-
Here's a brief description of the most common schema modification methods:
|
|
350
|
-
|
|
351
|
-
=== +create_table+
|
|
352
|
-
|
|
353
|
-
+create_table+ is the most common schema modification method, and it's used for adding new tables
|
|
354
|
-
to the schema. You provide it with the name of the table as a symbol, as well a block:
|
|
355
|
-
|
|
356
|
-
create_table(:artists) do
|
|
357
|
-
primary_key :id
|
|
358
|
-
String :name
|
|
359
|
-
end
|
|
360
|
-
|
|
361
|
-
Not that if you want a primary key for the table, you need to specify it, Sequel does not create one
|
|
362
|
-
by default.
|
|
363
|
-
|
|
364
|
-
==== Column types
|
|
365
|
-
|
|
366
|
-
Most method calls inside the create_table block will create columns, since +method_missing+ calls +column+
|
|
367
|
-
Columns are generally created by specifying the column type as the method
|
|
368
|
-
name, followed by the column name symbol to use, and after that any options that should be used.
|
|
369
|
-
If the method is a ruby class name that Sequel recognizes, Sequel will transform it into the appropriate
|
|
370
|
-
type for the given database. So while you specified +String+, Sequel will actually use +varchar+ or
|
|
371
|
-
+text+ depending on the underlying database. Here's a list of all of ruby classes that Sequel will
|
|
372
|
-
convert to database types:
|
|
373
|
-
|
|
374
|
-
create_table(:columns_types) do # common database type used
|
|
375
|
-
Integer :a0 # integer
|
|
376
|
-
String :a1 # varchar(255)
|
|
377
|
-
String :a2, :size=>50 # varchar(50)
|
|
378
|
-
String :a3, :fixed=>true # char(255)
|
|
379
|
-
String :a4, :fixed=>true, :size=>50 # char(50)
|
|
380
|
-
String :a5, :text=>true # text
|
|
381
|
-
File :b, # blob
|
|
382
|
-
Fixnum :c # integer
|
|
383
|
-
Bignum :d # bigint
|
|
384
|
-
Float :e # double precision
|
|
385
|
-
BigDecimal :f # numeric
|
|
386
|
-
BigDecimal :f2, :size=>10 # numeric(10)
|
|
387
|
-
BigDecimal :f3, :size=>[10, 2] # numeric(10, 2)
|
|
388
|
-
Date :g # date
|
|
389
|
-
DateTime :h # timestamp
|
|
390
|
-
Time :i # timestamp
|
|
391
|
-
Time :i2, :only_time=>true # time
|
|
392
|
-
Numeric :j # numeric
|
|
393
|
-
TrueClass :k # boolean
|
|
394
|
-
FalseClass :l # boolean
|
|
395
|
-
end
|
|
396
|
-
|
|
397
|
-
Note that in addition to the ruby class name, Sequel also pays attention to the column options when
|
|
398
|
-
determining which database type to use.
|
|
399
|
-
|
|
400
|
-
Also note that this conversion is only done if you use a supported ruby class name. In all other
|
|
401
|
-
cases, Sequel uses the type specified verbatim:
|
|
402
|
-
|
|
403
|
-
create_table(:columns_types) do # database type used
|
|
404
|
-
string :a1 # string
|
|
405
|
-
datetime :a2 # datetime
|
|
406
|
-
blob :a3 # blob
|
|
407
|
-
inet :a4 # inet
|
|
408
|
-
end
|
|
409
|
-
|
|
410
|
-
In addition to specifying the types as methods, you can use the +column+ method and specify the types
|
|
411
|
-
as the second argument, either as ruby classes, symbols, or strings:
|
|
412
|
-
|
|
413
|
-
create_table(:columns_types) do # database type used
|
|
414
|
-
column :a1, :string # string
|
|
415
|
-
column :a2, String # varchar(255)
|
|
416
|
-
column :a3, 'string' # string
|
|
417
|
-
column :a4, :datetime # datetime
|
|
418
|
-
column :a5, DateTime # timestamp
|
|
419
|
-
column :a6, 'timestamp(6)' # timestamp(6)
|
|
420
|
-
end
|
|
421
|
-
|
|
422
|
-
==== Column options
|
|
423
|
-
|
|
424
|
-
When using the type name as method, the third argument is an options hash, and when using the +column+
|
|
425
|
-
method, the fourth argument is the options hash. The following options are supported:
|
|
426
|
-
|
|
427
|
-
:default :: The default value for the column.
|
|
428
|
-
:index :: Create an index on this column.
|
|
429
|
-
:null :: Mark the column as allowing NULL values (if true),
|
|
430
|
-
or not allowing NULL values (if false). If unspecified, will default
|
|
431
|
-
to whatever the database default is.
|
|
432
|
-
:size :: The size of the column, generally used with string
|
|
433
|
-
columns to specify the maximum number of characters the column will hold.
|
|
434
|
-
An array of two integers can be provided to set the size and the
|
|
435
|
-
precision, respectively, of decimal columns.
|
|
436
|
-
:unique :: Mark the column as unique, generally has the same effect as
|
|
437
|
-
creating a unique index on the column.
|
|
438
|
-
:unsigned :: Make the column type unsigned, only useful for integer
|
|
439
|
-
columns.
|
|
440
|
-
|
|
441
|
-
==== Other methods
|
|
442
|
-
|
|
443
|
-
In addition to the +column+ method and other methods that create columns, there are a other methods that can be used:
|
|
444
|
-
|
|
445
|
-
==== +primary_key+
|
|
446
|
-
|
|
447
|
-
You've seen this one used already. It's used to create an autoincrementing integer primary key column.
|
|
448
|
-
|
|
449
|
-
create_table(:a0){primary_key :id}
|
|
450
|
-
|
|
451
|
-
If you want to create a primary key column that doesn't use an autoincrementing integer, you should
|
|
452
|
-
not use this method. Instead, you should use the :primary_key option to the +column+ method or type
|
|
453
|
-
method:
|
|
454
|
-
|
|
455
|
-
create_table(:a1){Integer :id, :primary_key=>true} # Non autoincrementing integer primary key
|
|
456
|
-
create_table(:a2){String :name, :primary_key=>true} # varchar(255) primary key
|
|
457
|
-
|
|
458
|
-
If you want to create a composite primary key, you should call the +primary_key+ method with an
|
|
459
|
-
array of column symbols:
|
|
460
|
-
|
|
461
|
-
create_table(:items) do
|
|
462
|
-
Integer :group_id
|
|
463
|
-
Integer :position
|
|
464
|
-
primary_key [:group_id, :position]
|
|
465
|
-
end
|
|
466
|
-
|
|
467
|
-
If provided with an array, +primary_key+ does not create a column, it just sets up the primary key constraint.
|
|
468
|
-
|
|
469
|
-
==== +foreign_key+
|
|
470
|
-
|
|
471
|
-
+foreign_key+ is used to create a foreign key column that references a column in another table (or the same table).
|
|
472
|
-
It takes the column name as the first argument, the table it references as the second argument, and an options hash
|
|
473
|
-
as it's third argument. A simple example is:
|
|
474
|
-
|
|
475
|
-
create_table(:albums) do
|
|
476
|
-
primary_key :id
|
|
477
|
-
foreign_key :artist_id, :artists
|
|
478
|
-
String :name
|
|
479
|
-
end
|
|
480
|
-
|
|
481
|
-
+foreign_key+ accepts some specific options:
|
|
482
|
-
|
|
483
|
-
:deferrable :: Makes the foreign key constraint checks deferrable, so they aren't checked
|
|
484
|
-
until the end of the transaction.
|
|
485
|
-
:key :: For foreign key columns, the column in the associated table
|
|
486
|
-
that this column references. Unnecessary if this column
|
|
487
|
-
references the primary key of the associated table, at least
|
|
488
|
-
on most databases.
|
|
489
|
-
:on_delete :: Specify the behavior of this foreign key column when the row with the primary key
|
|
490
|
-
it references is deleted , can be :restrict, :cascade, :set_null, or :set_default.
|
|
491
|
-
:on_update :: Specify the behavior of this foreign key column when the row with the primary key
|
|
492
|
-
it references modifies the value of the primary key, can be
|
|
493
|
-
:restrict, :cascade, :set_null, or :set_default.
|
|
494
|
-
|
|
495
|
-
Like +primary_key+, if you provide +foreign_key+ with an array of symbols, it will not create a
|
|
496
|
-
column, but create a foreign key constraint:
|
|
497
|
-
|
|
498
|
-
create_table(:artists) do
|
|
499
|
-
String :name
|
|
500
|
-
String :location
|
|
501
|
-
primary_key [:name, :location]
|
|
502
|
-
end
|
|
503
|
-
create_table(:albums) do
|
|
504
|
-
String :artist_name
|
|
505
|
-
String :artist_location
|
|
506
|
-
String :name
|
|
507
|
-
foreign_key [:artist_name, :artist_location], :artists
|
|
508
|
-
end
|
|
509
|
-
|
|
510
|
-
==== +index+
|
|
511
|
-
|
|
512
|
-
+index+ creates indexes on the table. For single columns, calling index is the same as using the
|
|
513
|
-
<tt>:index</tt> option when creating the column:
|
|
514
|
-
|
|
515
|
-
create_table(:a){Integer :id, :index=>true}
|
|
516
|
-
# Same as:
|
|
517
|
-
create_table(:a) do
|
|
518
|
-
Integer :id
|
|
519
|
-
index :id
|
|
520
|
-
end
|
|
521
|
-
|
|
522
|
-
Similar to the +primary_key+ and +foreign_key+ methods, calling +index+ with an array of symbols
|
|
523
|
-
will create a multiple column index:
|
|
524
|
-
|
|
525
|
-
create_table(:albums) do
|
|
526
|
-
primary_key :id
|
|
527
|
-
foreign_key :artist_id, :artists
|
|
528
|
-
Integer :position
|
|
529
|
-
index [:artist_id, :position]
|
|
530
|
-
end
|
|
531
|
-
|
|
532
|
-
The +index+ method also accepts some options:
|
|
533
|
-
|
|
534
|
-
:name :: The name of the index (generated based on the table and column names if not provided).
|
|
535
|
-
:type :: The type of index to use (only supported by some databases)
|
|
536
|
-
:unique :: Make the index unique, so duplicate values are not allowed.
|
|
537
|
-
:where :: Create a partial index (only supported by some databases)
|
|
538
|
-
|
|
539
|
-
==== +unique+
|
|
540
|
-
|
|
541
|
-
The +unique+ method creates a unique constraint on the table. A unique constraint generally
|
|
542
|
-
operates identically to a unique index, so the following three +create_table+ blocks are
|
|
543
|
-
pretty much identical:
|
|
544
|
-
|
|
545
|
-
create_table(:a){Integer :a, :unique=>true}
|
|
546
|
-
|
|
547
|
-
create_table(:a) do
|
|
548
|
-
Integer :a
|
|
549
|
-
index :a, :unique=>true
|
|
550
|
-
end
|
|
551
|
-
|
|
552
|
-
create_table(:a) do
|
|
553
|
-
Integer :a
|
|
554
|
-
unique :a
|
|
555
|
-
end
|
|
556
|
-
|
|
557
|
-
Just like +index+, +unique+ can set up a multiple column unique constraint, where the
|
|
558
|
-
combination of the columns must be unique:
|
|
559
|
-
|
|
560
|
-
create_table(:a) do
|
|
561
|
-
Integer :a
|
|
562
|
-
Integer :b
|
|
563
|
-
unique [:a, :b]
|
|
564
|
-
end
|
|
565
|
-
|
|
566
|
-
==== +full_text_index+ and +spatial_index+
|
|
567
|
-
|
|
568
|
-
Both of these create specialized index types supported by some databases. They
|
|
569
|
-
both take the same options as +index+.
|
|
570
|
-
|
|
571
|
-
==== +constraint+
|
|
572
|
-
|
|
573
|
-
+constraint+ creates a named table constraint:
|
|
574
|
-
|
|
575
|
-
create_table(:artists) do
|
|
576
|
-
primary_key :id
|
|
577
|
-
String :name
|
|
578
|
-
constraint(:name_min_length){char_length(name) > 2}
|
|
579
|
-
end
|
|
580
|
-
|
|
581
|
-
Instead of using a block, you can use arguments that will be handled similarly
|
|
582
|
-
to <tt>Dataset#filter</tt>:
|
|
583
|
-
|
|
584
|
-
create_table(:artists) do
|
|
585
|
-
primary_key :id
|
|
586
|
-
String :name
|
|
587
|
-
constraint(:name_length_range, :char_length.sql_function(:name)=>3..50)
|
|
588
|
-
end
|
|
589
|
-
|
|
590
|
-
==== +check+
|
|
591
|
-
|
|
592
|
-
+check+ operates just like +constraint+, except that it doesn't take a name
|
|
593
|
-
and it creates an unnamed constraint
|
|
594
|
-
|
|
595
|
-
create_table(:artists) do
|
|
596
|
-
primary_key :id
|
|
597
|
-
String :name
|
|
598
|
-
check{char_length(name) > 2}
|
|
599
|
-
end
|
|
600
|
-
|
|
601
|
-
=== +alter_table+
|
|
602
|
-
|
|
603
|
-
+alter_table+ is used to alter existing tables, changing their columns, indexes,
|
|
604
|
-
or constraints. It it used just like +create_table+, accepting a block which
|
|
605
|
-
is instance_evaled, and providing its own methods:
|
|
606
|
-
|
|
607
|
-
==== +add_column+
|
|
608
|
-
|
|
609
|
-
One of the most common methods, +add_column+ is used to add a column to the table.
|
|
610
|
-
Its API is similar to that of +create_table+'s +column+ method, where the first
|
|
611
|
-
argument is the column name, the second is the type, and the third is an options
|
|
612
|
-
hash:
|
|
613
|
-
|
|
614
|
-
alter_table(:albums) do
|
|
615
|
-
add_column :copies_sold, Integer, :default=>0
|
|
616
|
-
end
|
|
617
|
-
|
|
618
|
-
When adding a column, it's a good idea to provide a default value, unless you
|
|
619
|
-
want the value for all rows to be set to NULL.
|
|
620
|
-
|
|
621
|
-
==== +drop_column+
|
|
622
|
-
|
|
623
|
-
As you may expect, +drop_column+ takes a column name and drops the column. It's
|
|
624
|
-
often used in the +down+ block of a migration to drop a column added in an +up+ block:
|
|
625
|
-
|
|
626
|
-
alter_table(:albums) do
|
|
627
|
-
drop_column :copies_sold
|
|
628
|
-
end
|
|
629
|
-
|
|
630
|
-
==== +rename_column+
|
|
631
|
-
|
|
632
|
-
+rename_column+ is used to rename a column. It takes the old column name as the first
|
|
633
|
-
argument, and the new column name as the second argument:
|
|
634
|
-
|
|
635
|
-
alter_table(:albums) do
|
|
636
|
-
rename_column :copies_sold, :total_sales
|
|
637
|
-
end
|
|
638
|
-
|
|
639
|
-
==== +add_primary_key+
|
|
640
|
-
|
|
641
|
-
If you forgot to include a primary key on the table, and want to add one later, you
|
|
642
|
-
can use +add_primary_key+. A common use of this is to make many_to_many association
|
|
643
|
-
join tables into real models:
|
|
644
|
-
|
|
645
|
-
alter_table(:albums_artists) do
|
|
646
|
-
add_primary_key :id
|
|
647
|
-
end
|
|
648
|
-
|
|
649
|
-
Just like +create_table+'s +primary_key+ method, if you provide an array of symbols,
|
|
650
|
-
Sequel will not add a column, but will add a composite primary key constraint:
|
|
651
|
-
|
|
652
|
-
alter_table(:albums_artists) do
|
|
653
|
-
add_primary_key [:album_id, :artist_id]
|
|
654
|
-
end
|
|
655
|
-
|
|
656
|
-
If you just want to take an existing single column and make it a primary key, call
|
|
657
|
-
+add_primary_key+ with an array with a single symbol:
|
|
658
|
-
|
|
659
|
-
alter_table(:artists) do
|
|
660
|
-
add_primary_key [:id]
|
|
661
|
-
end
|
|
662
|
-
|
|
663
|
-
==== +add_foreign_key+
|
|
664
|
-
|
|
665
|
-
+add_foreign_key+ can be used to add a new foreign key column or constraint to a table.
|
|
666
|
-
Like +add_primary_key+, if you provide it with a symbol as the first argument, it
|
|
667
|
-
creates a new column:
|
|
668
|
-
|
|
669
|
-
alter_table(:albums) do
|
|
670
|
-
add_foreign_key :artist_id, :artists
|
|
671
|
-
end
|
|
672
|
-
|
|
673
|
-
If you want to add a new foreign key constraint to an existing column, you provide an
|
|
674
|
-
array with a single element:
|
|
675
|
-
|
|
676
|
-
alter_table(:albums) do
|
|
677
|
-
add_foreign_key [:artist_id], :artists
|
|
678
|
-
end
|
|
679
|
-
|
|
680
|
-
To set up a multiple column foreign key constraint, use an array with multiple column
|
|
681
|
-
symbols:
|
|
682
|
-
|
|
683
|
-
alter_table(:albums) do
|
|
684
|
-
add_foreign_key [:artist_name, :artist_location], :artists
|
|
685
|
-
end
|
|
686
|
-
|
|
687
|
-
==== +add_index+
|
|
688
|
-
|
|
689
|
-
+add_index+ works just like +create_table+'s +index+ method, creating a new index on
|
|
690
|
-
the table:
|
|
691
|
-
|
|
692
|
-
alter_table(:albums) do
|
|
693
|
-
add_index :artist_id
|
|
694
|
-
end
|
|
695
|
-
|
|
696
|
-
It accepts the same options as +create_table+'s +index+ method, and you can set up
|
|
697
|
-
a multiple column index using an array:
|
|
698
|
-
|
|
699
|
-
alter_table(:albums_artists) do
|
|
700
|
-
add_index [:album_id, :artist_id], :unique=>true
|
|
701
|
-
end
|
|
702
|
-
|
|
703
|
-
==== +drop_index+
|
|
704
|
-
|
|
705
|
-
As you may expect, +drop_index+ drops an existing index:
|
|
706
|
-
|
|
707
|
-
alter_table(:albums) do
|
|
708
|
-
drop_index :artist_id
|
|
709
|
-
end
|
|
710
|
-
|
|
711
|
-
Just like +drop_column+, it is often used in the +down+ block of a migration.
|
|
712
|
-
|
|
713
|
-
==== +add_full_text_index+, +add_spatial_index+
|
|
714
|
-
|
|
715
|
-
Corresponding to +create_table+'s +full_text_index+ and +spatial_index+ methods,
|
|
716
|
-
these two methods create new indexes on the table.
|
|
717
|
-
|
|
718
|
-
==== +add_constraint+
|
|
719
|
-
|
|
720
|
-
This adds a named constraint to the table, similar to +create_table+'s +constraint+
|
|
721
|
-
method:
|
|
722
|
-
|
|
723
|
-
alter_table(:albums) do
|
|
724
|
-
add_constraint(:name_min_length){char_length(name) > 2}
|
|
725
|
-
end
|
|
726
|
-
|
|
727
|
-
There is no method to add an unnamed constraint, but you can pass nil as the first
|
|
728
|
-
argument of +add_constraint+ to do so. However, it's not recommend to do that
|
|
729
|
-
as it is difficult to drop such a constraint.
|
|
730
|
-
|
|
731
|
-
==== +add_unique_constraint+
|
|
732
|
-
|
|
733
|
-
This adds a unique constraint to the table, similar to +create_table+'s +unique+
|
|
734
|
-
method. This usually has the same effect as adding a unique index.
|
|
735
|
-
|
|
736
|
-
alter_table(:albums) do
|
|
737
|
-
add_unique_constraint [:artist_id, :name]
|
|
738
|
-
end
|
|
739
|
-
|
|
740
|
-
==== +drop_constraint+
|
|
741
|
-
|
|
742
|
-
This method drops an existing named constraint:
|
|
743
|
-
|
|
744
|
-
alter_table(:albums) do
|
|
745
|
-
drop_constraint(:name_min_length)
|
|
746
|
-
end
|
|
747
|
-
|
|
748
|
-
There is no database independent method to drop an unnamed constraint. Generally, the
|
|
749
|
-
database will give it a name automatically, and you will have to figure out what it is.
|
|
750
|
-
For that reason, you should not add unnamed constraints that you ever might need to remove.
|
|
751
|
-
|
|
752
|
-
==== +set_column_default+
|
|
753
|
-
|
|
754
|
-
This modifies the default value of a column:
|
|
755
|
-
|
|
756
|
-
alter_table(:albums) do
|
|
757
|
-
set_column_default :copies_sold, 0
|
|
758
|
-
end
|
|
759
|
-
|
|
760
|
-
==== +set_column_type+
|
|
388
|
+
or they should use the reversible migrations feature with a +change+ block:
|
|
761
389
|
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
alter_table(:albums) do
|
|
766
|
-
set_column_type :copies_sold, Bignum
|
|
767
|
-
end
|
|
768
|
-
|
|
769
|
-
You can specify the type as a string or symbol, in which case it is used verbatim, or as a supported
|
|
770
|
-
ruby class, in which case it gets converted to an appropriate database type.
|
|
771
|
-
|
|
772
|
-
==== +set_column_allow_null+
|
|
773
|
-
|
|
774
|
-
This changes the NULL or NOT NULL setting of a column:
|
|
775
|
-
|
|
776
|
-
alter_table(:albums) do
|
|
777
|
-
set_column_allow_null :artist_id, true # NULL
|
|
778
|
-
set_column_allow_null :copies_sold, false # NOT NULL
|
|
779
|
-
end
|
|
780
|
-
|
|
781
|
-
=== Other +Database+ schema modification methods
|
|
782
|
-
|
|
783
|
-
<tt>Sequel::Database</tt> has many schema modification instance methods,
|
|
784
|
-
most of which are shortcuts to the same methods in +alter_table+. The
|
|
785
|
-
following +Database+ instance methods just call +alter_table+ with a
|
|
786
|
-
block that calls the method with the same name inside the +alter_table+
|
|
787
|
-
block with all arguments after the first argument (which is used as
|
|
788
|
-
the table name):
|
|
789
|
-
|
|
790
|
-
* +add_column+
|
|
791
|
-
* +drop_column+
|
|
792
|
-
* +rename_column+
|
|
793
|
-
* +add_index+
|
|
794
|
-
* +drop_index+
|
|
795
|
-
* +set_column_default+
|
|
796
|
-
* +set_column_type+
|
|
797
|
-
|
|
798
|
-
For example, the following two method calls do the same thing:
|
|
799
|
-
|
|
800
|
-
alter_table(:artists){add_column :copies_sold, Integer}
|
|
801
|
-
add_column :artists, :copies_sold, Integer
|
|
802
|
-
|
|
803
|
-
There are some other schema modification methods that have no +alter_table+
|
|
804
|
-
counterpart:
|
|
805
|
-
|
|
806
|
-
==== +drop_table+
|
|
807
|
-
|
|
808
|
-
+drop_table+ takes multiple arguments and treats all arguments as a
|
|
809
|
-
table name to drop:
|
|
810
|
-
|
|
811
|
-
drop_table(:albums_artists, :albums, :artists)
|
|
812
|
-
|
|
813
|
-
Note that when dropping tables, you may need to drop them in a specific order
|
|
814
|
-
if you are using foreign keys and the database is enforcing referential
|
|
815
|
-
integrity. In general, you need to drop the tables containing the foreign
|
|
816
|
-
keys before the tables containing the primary keys they reference.
|
|
817
|
-
|
|
818
|
-
==== +rename_table+
|
|
819
|
-
|
|
820
|
-
You can rename an existing table using +rename_table+. Like +rename_column+,
|
|
821
|
-
the first argument is the current name, and the second is the new name:
|
|
822
|
-
|
|
823
|
-
rename_table(:artist, :artists)
|
|
824
|
-
|
|
825
|
-
==== <tt>create_table!</tt>
|
|
826
|
-
|
|
827
|
-
<tt>create_table!</tt> with the bang drops the table unconditionally (swallowing
|
|
828
|
-
any errors) before attempting to create it, so:
|
|
829
|
-
|
|
830
|
-
create_table!(:artists)
|
|
831
|
-
primary_key :id
|
|
832
|
-
end
|
|
833
|
-
|
|
834
|
-
is the same as:
|
|
835
|
-
|
|
836
|
-
drop_table(:artists) rescue nil
|
|
837
|
-
create_table(:artists)
|
|
838
|
-
primary_key :id
|
|
839
|
-
end
|
|
840
|
-
|
|
841
|
-
It should not be used inside migrations, as if the table does not exist, it may
|
|
842
|
-
mess up the migration.
|
|
843
|
-
|
|
844
|
-
==== <tt>create_table?</tt>
|
|
845
|
-
|
|
846
|
-
<tt>create_table?</tt> with a question mark only creates the table if it does
|
|
847
|
-
not already exist, so:
|
|
848
|
-
|
|
849
|
-
create_table!(:artists)
|
|
850
|
-
primary_key :id
|
|
390
|
+
Sequel.migration do
|
|
391
|
+
change{...}
|
|
851
392
|
end
|
|
852
393
|
|
|
853
|
-
is the same as:
|
|
854
|
-
|
|
855
|
-
create_table(:artists)
|
|
856
|
-
primary_key :id
|
|
857
|
-
end unless table_exists?(:artists)
|
|
858
|
-
|
|
859
|
-
Like <tt>create_table!</tt>, it should not be used inside migrations.
|
|
860
|
-
|
|
861
|
-
==== +create_view+ and +create_or_replace_view+
|
|
862
|
-
|
|
863
|
-
These can be used to create views. The difference between them is that
|
|
864
|
-
+create_or_replace_view+ will unconditionally replace an existing view of
|
|
865
|
-
the same name, while +create_view+ will probably raise an error. Both methods
|
|
866
|
-
take the name as the first argument, and either an string or a dataset as the
|
|
867
|
-
second argument:
|
|
868
|
-
|
|
869
|
-
create_view(:gold_albums, DB[:albums].filter{copies_sold > 500000})
|
|
870
|
-
create_or_replace_view(:gold_albums, "SELECT * FROM albums WHERE copies_sold > 500000")
|
|
871
|
-
|
|
872
|
-
==== +drop_view+
|
|
873
|
-
|
|
874
|
-
+drop_view+ drops existing views. Just like +drop_table+, it can accept multiple
|
|
875
|
-
arguments:
|
|
876
|
-
|
|
877
|
-
drop_view(:gold_albums, :platinum_albums)
|
|
878
|
-
|
|
879
394
|
== What to put in your migration's +down+ block
|
|
880
395
|
|
|
881
396
|
It's usually easy to determine what you should put in your migration's +up+ block,
|
|
882
397
|
as it's whatever change you want to make to the database. The +down+ block is
|
|
883
398
|
less obvious. In general, it should reverse the changes made by the +up+ block, which means
|
|
884
|
-
it should execute the opposite of what the +up+ block does in the reverse order in which
|
|
399
|
+
it should execute the opposite of what the +up+ block does in the reverse order in which
|
|
885
400
|
the +up+ block does it. Here's an example where you are switching from having a single
|
|
886
401
|
artist per album to multiple artists per album:
|
|
887
402
|
|
|
@@ -897,8 +412,8 @@ artist per album to multiple artists per album:
|
|
|
897
412
|
# Insert one row in the albums_artists table
|
|
898
413
|
# for each row in the albums table where there
|
|
899
414
|
# is an associated artist
|
|
900
|
-
|
|
901
|
-
|
|
415
|
+
self[:albums_artists].insert([:album_id, :artist_id],
|
|
416
|
+
self[:albums].select(:id, :artist_id).exclude(:artist_id=>nil))
|
|
902
417
|
|
|
903
418
|
# Drop the now unnecesssary column from the albums table
|
|
904
419
|
drop_column :albums, :artist_id
|
|
@@ -910,12 +425,12 @@ artist per album to multiple artists per album:
|
|
|
910
425
|
# If possible, associate each album with one of the artists
|
|
911
426
|
# it was associated with. This loses information, but
|
|
912
427
|
# there's no way around that.
|
|
913
|
-
|
|
428
|
+
self[:albums_artists].
|
|
914
429
|
group(:album_id).
|
|
915
430
|
select{[album_id, max(artist_id).as(artist_id)]}.
|
|
916
431
|
having{artist_id > 0}.
|
|
917
432
|
all do |r|
|
|
918
|
-
|
|
433
|
+
self[:artists].
|
|
919
434
|
filter(:id=>r[:album_id]).
|
|
920
435
|
update(:artist_id=>r[:artist_id])
|
|
921
436
|
end
|
|
@@ -925,12 +440,12 @@ artist per album to multiple artists per album:
|
|
|
925
440
|
end
|
|
926
441
|
end
|
|
927
442
|
|
|
928
|
-
Note that the
|
|
929
|
-
reverse order
|
|
930
|
-
isn't always possible to reverse exactly what was done in the +up+ block.
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
443
|
+
Note that the operations performed in the +down+ block are performed in the
|
|
444
|
+
reverse order of how they are performed in the +up+ block. Also note how it
|
|
445
|
+
isn't always possible to reverse exactly what was done in the +up+ block. You
|
|
446
|
+
should try to do so as much as possible, but if you can't, you may want to have
|
|
447
|
+
your +down+ block raise a <tt>Sequel::Error</tt> exception saying why the
|
|
448
|
+
migration cannot be reverted.
|
|
934
449
|
|
|
935
450
|
== Running migrations
|
|
936
451
|
|
|
@@ -1008,17 +523,34 @@ The main difference between the two is that <tt>-d</tt> will use the type method
|
|
|
1008
523
|
with the database independent ruby class types, while <tt>-D</tt> will use
|
|
1009
524
|
the +column+ method with string types.
|
|
1010
525
|
|
|
1011
|
-
Note that Sequel cannot dump constraints other than primary key
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
dumper.
|
|
526
|
+
Note that Sequel cannot dump constraints other than primary key and possibly
|
|
527
|
+
foreign key constraints constraints. If you are using database features such
|
|
528
|
+
as constraints or triggers, you should use your database's dump and restore
|
|
529
|
+
programs instead of Sequel's schema dumper.
|
|
1016
530
|
|
|
1017
531
|
You can take the migration created by the schema dumper to another computer
|
|
1018
532
|
with an empty database, and attempt to recreate the schema using:
|
|
1019
533
|
|
|
1020
534
|
sequel -m db/migrations postgres://host/database
|
|
1021
535
|
|
|
536
|
+
== Checking for Current Migrations
|
|
537
|
+
|
|
538
|
+
In your application code, you may want to check that you are up to date in
|
|
539
|
+
regards to migrations (i.e. you don't have any unapplied migrations). Sequel
|
|
540
|
+
offers two separate methods to do that. The first is Sequel::Migrator.check_current.
|
|
541
|
+
This method raises an exception if there are outstanding migrations that need to
|
|
542
|
+
be run. The second is Sequel::Migrator.is_current?, which returns true if there
|
|
543
|
+
are no outstanding migrations, and false if there are outstanding migrations.
|
|
544
|
+
|
|
545
|
+
If you want to ensure that your application code is up to date, you may want to
|
|
546
|
+
add the following code after connecting to your database:
|
|
547
|
+
|
|
548
|
+
Sequel.extension :migration
|
|
549
|
+
Sequel::Migrator.check_current(DB, '/path/to/migrations')
|
|
550
|
+
|
|
551
|
+
This will cause your application to raise an error when you start it if you have
|
|
552
|
+
any outstanding migrations.
|
|
553
|
+
|
|
1022
554
|
== Old-style migration classes
|
|
1023
555
|
|
|
1024
556
|
Before the <tt>Sequel.migration</tt> DSL was introduced, Sequel used classes
|