sequel 3.21.0 → 3.29.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (200) hide show
  1. data/CHANGELOG +413 -3
  2. data/README.rdoc +20 -6
  3. data/Rakefile +23 -16
  4. data/bin/sequel +1 -5
  5. data/doc/association_basics.rdoc +210 -43
  6. data/doc/dataset_basics.rdoc +4 -4
  7. data/doc/mass_assignment.rdoc +54 -0
  8. data/doc/migration.rdoc +15 -538
  9. data/doc/model_hooks.rdoc +72 -27
  10. data/doc/opening_databases.rdoc +86 -50
  11. data/doc/prepared_statements.rdoc +40 -13
  12. data/doc/reflection.rdoc +8 -2
  13. data/doc/release_notes/3.22.0.txt +39 -0
  14. data/doc/release_notes/3.23.0.txt +172 -0
  15. data/doc/release_notes/3.24.0.txt +420 -0
  16. data/doc/release_notes/3.25.0.txt +88 -0
  17. data/doc/release_notes/3.26.0.txt +88 -0
  18. data/doc/release_notes/3.27.0.txt +82 -0
  19. data/doc/release_notes/3.28.0.txt +304 -0
  20. data/doc/release_notes/3.29.0.txt +459 -0
  21. data/doc/schema_modification.rdoc +547 -0
  22. data/doc/sharding.rdoc +7 -1
  23. data/doc/testing.rdoc +115 -0
  24. data/doc/transactions.rdoc +137 -0
  25. data/doc/virtual_rows.rdoc +2 -2
  26. data/lib/sequel/adapters/ado/mssql.rb +18 -6
  27. data/lib/sequel/adapters/ado.rb +40 -18
  28. data/lib/sequel/adapters/amalgalite.rb +15 -7
  29. data/lib/sequel/adapters/db2.rb +175 -86
  30. data/lib/sequel/adapters/dbi.rb +15 -15
  31. data/lib/sequel/adapters/do/mysql.rb +0 -5
  32. data/lib/sequel/adapters/do/postgres.rb +0 -5
  33. data/lib/sequel/adapters/do/sqlite.rb +0 -5
  34. data/lib/sequel/adapters/do.rb +17 -36
  35. data/lib/sequel/adapters/firebird.rb +27 -208
  36. data/lib/sequel/adapters/ibmdb.rb +448 -0
  37. data/lib/sequel/adapters/informix.rb +6 -23
  38. data/lib/sequel/adapters/jdbc/as400.rb +5 -31
  39. data/lib/sequel/adapters/jdbc/db2.rb +44 -0
  40. data/lib/sequel/adapters/jdbc/derby.rb +217 -0
  41. data/lib/sequel/adapters/jdbc/firebird.rb +29 -0
  42. data/lib/sequel/adapters/jdbc/h2.rb +26 -17
  43. data/lib/sequel/adapters/jdbc/hsqldb.rb +166 -0
  44. data/lib/sequel/adapters/jdbc/informix.rb +26 -0
  45. data/lib/sequel/adapters/jdbc/jtds.rb +29 -0
  46. data/lib/sequel/adapters/jdbc/mssql.rb +0 -32
  47. data/lib/sequel/adapters/jdbc/mysql.rb +9 -10
  48. data/lib/sequel/adapters/jdbc/oracle.rb +67 -25
  49. data/lib/sequel/adapters/jdbc/postgresql.rb +6 -11
  50. data/lib/sequel/adapters/jdbc/sqlite.rb +0 -5
  51. data/lib/sequel/adapters/jdbc/sqlserver.rb +41 -0
  52. data/lib/sequel/adapters/jdbc/transactions.rb +83 -0
  53. data/lib/sequel/adapters/jdbc.rb +107 -38
  54. data/lib/sequel/adapters/mock.rb +315 -0
  55. data/lib/sequel/adapters/mysql.rb +78 -182
  56. data/lib/sequel/adapters/mysql2.rb +24 -23
  57. data/lib/sequel/adapters/odbc/db2.rb +17 -0
  58. data/lib/sequel/adapters/odbc/mssql.rb +0 -5
  59. data/lib/sequel/adapters/odbc.rb +28 -9
  60. data/lib/sequel/adapters/openbase.rb +2 -4
  61. data/lib/sequel/adapters/oracle.rb +349 -51
  62. data/lib/sequel/adapters/postgres.rb +169 -21
  63. data/lib/sequel/adapters/shared/access.rb +21 -6
  64. data/lib/sequel/adapters/shared/db2.rb +288 -0
  65. data/lib/sequel/adapters/shared/firebird.rb +214 -0
  66. data/lib/sequel/adapters/shared/informix.rb +45 -0
  67. data/lib/sequel/adapters/shared/mssql.rb +108 -65
  68. data/lib/sequel/adapters/shared/mysql.rb +56 -13
  69. data/lib/sequel/adapters/shared/mysql_prepared_statements.rb +149 -0
  70. data/lib/sequel/adapters/shared/oracle.rb +185 -34
  71. data/lib/sequel/adapters/shared/postgres.rb +107 -58
  72. data/lib/sequel/adapters/shared/progress.rb +0 -6
  73. data/lib/sequel/adapters/shared/sqlite.rb +168 -41
  74. data/lib/sequel/adapters/sqlite.rb +27 -18
  75. data/lib/sequel/adapters/swift/mysql.rb +9 -5
  76. data/lib/sequel/adapters/swift/postgres.rb +0 -5
  77. data/lib/sequel/adapters/swift/sqlite.rb +6 -4
  78. data/lib/sequel/adapters/swift.rb +5 -5
  79. data/lib/sequel/adapters/tinytds.rb +152 -17
  80. data/lib/sequel/adapters/utils/emulate_offset_with_row_number.rb +63 -0
  81. data/lib/sequel/ast_transformer.rb +190 -0
  82. data/lib/sequel/connection_pool/threaded.rb +3 -2
  83. data/lib/sequel/connection_pool.rb +1 -1
  84. data/lib/sequel/core.rb +46 -5
  85. data/lib/sequel/database/connecting.rb +5 -6
  86. data/lib/sequel/database/dataset.rb +4 -4
  87. data/lib/sequel/database/dataset_defaults.rb +59 -1
  88. data/lib/sequel/database/logging.rb +1 -1
  89. data/lib/sequel/database/misc.rb +100 -19
  90. data/lib/sequel/database/query.rb +153 -58
  91. data/lib/sequel/database/schema_generator.rb +8 -5
  92. data/lib/sequel/database/schema_methods.rb +59 -29
  93. data/lib/sequel/dataset/actions.rb +194 -57
  94. data/lib/sequel/dataset/features.rb +81 -8
  95. data/lib/sequel/dataset/graph.rb +8 -7
  96. data/lib/sequel/dataset/misc.rb +50 -23
  97. data/lib/sequel/dataset/mutation.rb +5 -6
  98. data/lib/sequel/dataset/prepared_statements.rb +32 -15
  99. data/lib/sequel/dataset/query.rb +223 -42
  100. data/lib/sequel/dataset/sql.rb +58 -61
  101. data/lib/sequel/dataset.rb +8 -0
  102. data/lib/sequel/exceptions.rb +4 -0
  103. data/lib/sequel/extensions/columns_introspection.rb +61 -0
  104. data/lib/sequel/extensions/migration.rb +6 -4
  105. data/lib/sequel/extensions/named_timezones.rb +5 -0
  106. data/lib/sequel/extensions/thread_local_timezones.rb +1 -1
  107. data/lib/sequel/extensions/to_dot.rb +95 -83
  108. data/lib/sequel/model/associations.rb +997 -331
  109. data/lib/sequel/model/base.rb +386 -129
  110. data/lib/sequel/model/errors.rb +1 -1
  111. data/lib/sequel/model/exceptions.rb +5 -1
  112. data/lib/sequel/model.rb +15 -8
  113. data/lib/sequel/plugins/association_pks.rb +22 -4
  114. data/lib/sequel/plugins/class_table_inheritance.rb +4 -4
  115. data/lib/sequel/plugins/dataset_associations.rb +100 -0
  116. data/lib/sequel/plugins/defaults_setter.rb +58 -0
  117. data/lib/sequel/plugins/force_encoding.rb +6 -6
  118. data/lib/sequel/plugins/identity_map.rb +114 -7
  119. data/lib/sequel/plugins/many_through_many.rb +65 -7
  120. data/lib/sequel/plugins/prepared_statements.rb +151 -0
  121. data/lib/sequel/plugins/prepared_statements_associations.rb +84 -0
  122. data/lib/sequel/plugins/prepared_statements_safe.rb +82 -0
  123. data/lib/sequel/plugins/prepared_statements_with_pk.rb +59 -0
  124. data/lib/sequel/plugins/rcte_tree.rb +29 -15
  125. data/lib/sequel/plugins/serialization.rb +6 -1
  126. data/lib/sequel/plugins/serialization_modification_detection.rb +51 -0
  127. data/lib/sequel/plugins/sharding.rb +12 -25
  128. data/lib/sequel/plugins/single_table_inheritance.rb +3 -1
  129. data/lib/sequel/plugins/typecast_on_load.rb +9 -12
  130. data/lib/sequel/plugins/update_primary_key.rb +2 -2
  131. data/lib/sequel/plugins/xml_serializer.rb +3 -3
  132. data/lib/sequel/sql.rb +107 -51
  133. data/lib/sequel/timezones.rb +44 -35
  134. data/lib/sequel/version.rb +1 -1
  135. data/spec/adapters/db2_spec.rb +146 -0
  136. data/spec/adapters/mssql_spec.rb +65 -29
  137. data/spec/adapters/mysql_spec.rb +122 -123
  138. data/spec/adapters/oracle_spec.rb +48 -76
  139. data/spec/adapters/postgres_spec.rb +213 -61
  140. data/spec/adapters/spec_helper.rb +6 -5
  141. data/spec/adapters/sqlite_spec.rb +35 -21
  142. data/spec/core/connection_pool_spec.rb +71 -92
  143. data/spec/core/core_sql_spec.rb +20 -31
  144. data/spec/core/database_spec.rb +729 -508
  145. data/spec/core/dataset_spec.rb +980 -1044
  146. data/spec/core/expression_filters_spec.rb +159 -42
  147. data/spec/core/mock_adapter_spec.rb +378 -0
  148. data/spec/core/object_graph_spec.rb +48 -114
  149. data/spec/core/schema_generator_spec.rb +3 -3
  150. data/spec/core/schema_spec.rb +298 -38
  151. data/spec/core/spec_helper.rb +6 -48
  152. data/spec/extensions/association_pks_spec.rb +38 -0
  153. data/spec/extensions/class_table_inheritance_spec.rb +1 -1
  154. data/spec/extensions/columns_introspection_spec.rb +91 -0
  155. data/spec/extensions/dataset_associations_spec.rb +199 -0
  156. data/spec/extensions/defaults_setter_spec.rb +64 -0
  157. data/spec/extensions/identity_map_spec.rb +162 -0
  158. data/spec/extensions/instance_hooks_spec.rb +71 -0
  159. data/spec/extensions/many_through_many_spec.rb +195 -20
  160. data/spec/extensions/migration_spec.rb +17 -17
  161. data/spec/extensions/named_timezones_spec.rb +22 -2
  162. data/spec/extensions/nested_attributes_spec.rb +4 -0
  163. data/spec/extensions/prepared_statements_associations_spec.rb +126 -0
  164. data/spec/extensions/prepared_statements_safe_spec.rb +79 -0
  165. data/spec/extensions/prepared_statements_spec.rb +72 -0
  166. data/spec/extensions/prepared_statements_with_pk_spec.rb +38 -0
  167. data/spec/extensions/schema_dumper_spec.rb +2 -2
  168. data/spec/extensions/schema_spec.rb +13 -21
  169. data/spec/extensions/serialization_modification_detection_spec.rb +37 -0
  170. data/spec/extensions/serialization_spec.rb +5 -8
  171. data/spec/extensions/single_table_inheritance_spec.rb +11 -0
  172. data/spec/extensions/spec_helper.rb +7 -1
  173. data/spec/extensions/thread_local_timezones_spec.rb +22 -2
  174. data/spec/extensions/to_dot_spec.rb +3 -5
  175. data/spec/extensions/typecast_on_load_spec.rb +1 -6
  176. data/spec/extensions/xml_serializer_spec.rb +16 -4
  177. data/spec/integration/associations_test.rb +522 -21
  178. data/spec/integration/database_test.rb +4 -2
  179. data/spec/integration/dataset_test.rb +375 -62
  180. data/spec/integration/eager_loader_test.rb +19 -21
  181. data/spec/integration/model_test.rb +80 -1
  182. data/spec/integration/plugin_test.rb +304 -116
  183. data/spec/integration/prepared_statement_test.rb +200 -120
  184. data/spec/integration/schema_test.rb +161 -30
  185. data/spec/integration/spec_helper.rb +39 -30
  186. data/spec/integration/timezone_test.rb +38 -12
  187. data/spec/integration/transaction_test.rb +172 -5
  188. data/spec/integration/type_test.rb +17 -3
  189. data/spec/model/association_reflection_spec.rb +174 -7
  190. data/spec/model/associations_spec.rb +849 -661
  191. data/spec/model/base_spec.rb +255 -95
  192. data/spec/model/dataset_methods_spec.rb +7 -27
  193. data/spec/model/eager_loading_spec.rb +640 -676
  194. data/spec/model/hooks_spec.rb +309 -67
  195. data/spec/model/model_spec.rb +207 -167
  196. data/spec/model/plugins_spec.rb +24 -13
  197. data/spec/model/record_spec.rb +321 -218
  198. data/spec/model/spec_helper.rb +13 -71
  199. data/spec/model/validations_spec.rb +11 -0
  200. metadata +95 -38
data/doc/migration.rdoc CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  This guide is based on http://guides.rubyonrails.org/migrations.html
4
4
 
5
+
5
6
  == Overview
6
7
 
7
8
  Migrations make it easy to alter your database's schema in a systematic manner.
@@ -13,7 +14,7 @@ create the necessary database structure manually using Sequel's schema
13
14
  modification methods or another database tool. However, if you are dealing
14
15
  with other developers, you'll have to send them all of the changes you are
15
16
  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 then
17
+ to make the schema changes in 3 places (development, testing, and
17
18
  production), and it's probably easier to use the migrations system to apply
18
19
  the schema changes than it is to keep track of the changes manually and
19
20
  execute them manually at the appropriate time.
@@ -26,6 +27,8 @@ you generally need to use run Sequel's migrator with <tt>bin/sequel -m</tt>:
26
27
  Migrations in Sequel use a very simple DSL via the <tt>Sequel.migration</tt>
27
28
  method, and inside the DSL, use the <tt>Sequel::Database</tt> schema
28
29
  modification methods such as +create_table+ and +alter_table+.
30
+ See the {schema modification guide}[link:files/doc/schema_modification_rdoc.html]
31
+ for details on the schema modification methods you can use.
29
32
 
30
33
  == A Basic Migration
31
34
 
@@ -45,7 +48,7 @@ Here is a fairly basic Sequel migration:
45
48
  end
46
49
 
47
50
  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.
51
+ and a varchar or text column (depending on the database) named +name+ that doesn't accept +NULL+ values.
49
52
  Migrations should include both up and +down+ blocks, with the +down+ block reversing
50
53
  the change made by up. However, if you never need to be able to migrate down
51
54
  (i.e. you are one of the people that doesn't make mistakes), you can leave out
@@ -117,7 +120,8 @@ This looks a little weird, but you need to be aware that inside an up or +down+
117
120
  self always refers to the <tt>Sequel::Database</tt> object that the migration is being applied to.
118
121
  Since <tt>Database#[]</tt> creates datasets, using <tt>self[:artists]</tt> inside the +up+ block creates
119
122
  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>.
123
+ +location+ column to <tt>'Sacramento'</tt>. You should avoid referencing the <tt>Sequel::Database</tt>
124
+ object directly in your migration, and always use self to reference it, otherwise you may run into problems.
121
125
 
122
126
  It is possible to use model classes inside migrations, as long as they are loaded into the ruby interpreter,
123
127
  but it's a bad habit as changes to your model classes can then break old migrations, and this breakage is
@@ -141,7 +145,7 @@ Migrations themselves do not contain any schema modification methods, but they m
141
145
  any of the <tt>Sequel::Database</tt> modification methods, of which there are many. The main
142
146
  ones are +create_table+ and +alter_table+, but Sequel also comes with numerous other schema
143
147
  modification methods, most of which are shortcuts for +alter_table+ (all of these methods are
144
- described in more detail later):
148
+ described in more detail in the {schema modification guide}[link:files/doc/schema_modification_rdoc.html]):
145
149
 
146
150
  * add_column
147
151
  * add_index
@@ -343,539 +347,12 @@ reverses the changes made by the +up+ block:
343
347
  down{...}
344
348
  end
345
349
 
346
- == Schema modification methods
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
350
+ or they should use the reversible migrations feature with a +change+ block:
662
351
 
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+
761
-
762
- This modifies a column's type. Most databases will attempt to convert existing values in
763
- the columns to the new type:
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
352
+ Sequel.migration do
353
+ change{...}
851
354
  end
852
355
 
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
356
  == What to put in your migration's +down+ block
880
357
 
881
358
  It's usually easy to determine what you should put in your migration's +up+ block,
@@ -897,8 +374,8 @@ artist per album to multiple artists per album:
897
374
  # Insert one row in the albums_artists table
898
375
  # for each row in the albums table where there
899
376
  # is an associated artist
900
- DB[:albums_artists].insert([:album_id, :artist_id],
901
- DB[:albums].select(:id, :artist_id).exclude(:artist_id=>nil))
377
+ self[:albums_artists].insert([:album_id, :artist_id],
378
+ self[:albums].select(:id, :artist_id).exclude(:artist_id=>nil))
902
379
 
903
380
  # Drop the now unnecesssary column from the albums table
904
381
  drop_column :albums, :artist_id
@@ -910,12 +387,12 @@ artist per album to multiple artists per album:
910
387
  # If possible, associate each album with one of the artists
911
388
  # it was associated with. This loses information, but
912
389
  # there's no way around that.
913
- DB[:albums_artists].
390
+ self[:albums_artists].
914
391
  group(:album_id).
915
392
  select{[album_id, max(artist_id).as(artist_id)]}.
916
393
  having{artist_id > 0}.
917
394
  all do |r|
918
- DB[:artists].
395
+ self[:artists].
919
396
  filter(:id=>r[:album_id]).
920
397
  update(:artist_id=>r[:artist_id])
921
398
  end