sequel 3.21.0 → 3.28.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 (158) hide show
  1. data/CHANGELOG +294 -0
  2. data/README.rdoc +20 -6
  3. data/Rakefile +20 -15
  4. data/doc/association_basics.rdoc +210 -43
  5. data/doc/dataset_basics.rdoc +4 -4
  6. data/doc/mass_assignment.rdoc +54 -0
  7. data/doc/migration.rdoc +15 -538
  8. data/doc/model_hooks.rdoc +64 -27
  9. data/doc/opening_databases.rdoc +37 -10
  10. data/doc/prepared_statements.rdoc +16 -10
  11. data/doc/reflection.rdoc +8 -2
  12. data/doc/release_notes/3.22.0.txt +39 -0
  13. data/doc/release_notes/3.23.0.txt +172 -0
  14. data/doc/release_notes/3.24.0.txt +420 -0
  15. data/doc/release_notes/3.25.0.txt +88 -0
  16. data/doc/release_notes/3.26.0.txt +88 -0
  17. data/doc/release_notes/3.27.0.txt +82 -0
  18. data/doc/release_notes/3.28.0.txt +304 -0
  19. data/doc/schema_modification.rdoc +547 -0
  20. data/doc/testing.rdoc +106 -0
  21. data/doc/transactions.rdoc +97 -0
  22. data/doc/virtual_rows.rdoc +2 -2
  23. data/lib/sequel/adapters/ado.rb +12 -1
  24. data/lib/sequel/adapters/amalgalite.rb +4 -0
  25. data/lib/sequel/adapters/db2.rb +95 -58
  26. data/lib/sequel/adapters/do.rb +12 -0
  27. data/lib/sequel/adapters/firebird.rb +25 -203
  28. data/lib/sequel/adapters/ibmdb.rb +440 -0
  29. data/lib/sequel/adapters/informix.rb +4 -19
  30. data/lib/sequel/adapters/jdbc/as400.rb +0 -7
  31. data/lib/sequel/adapters/jdbc/db2.rb +49 -0
  32. data/lib/sequel/adapters/jdbc/firebird.rb +34 -0
  33. data/lib/sequel/adapters/jdbc/h2.rb +16 -5
  34. data/lib/sequel/adapters/jdbc/informix.rb +31 -0
  35. data/lib/sequel/adapters/jdbc/jtds.rb +34 -0
  36. data/lib/sequel/adapters/jdbc/mssql.rb +0 -32
  37. data/lib/sequel/adapters/jdbc/mysql.rb +9 -0
  38. data/lib/sequel/adapters/jdbc/oracle.rb +2 -27
  39. data/lib/sequel/adapters/jdbc/postgresql.rb +6 -0
  40. data/lib/sequel/adapters/jdbc/sqlserver.rb +46 -0
  41. data/lib/sequel/adapters/jdbc/transactions.rb +34 -0
  42. data/lib/sequel/adapters/jdbc.rb +62 -29
  43. data/lib/sequel/adapters/mysql.rb +22 -139
  44. data/lib/sequel/adapters/mysql2.rb +9 -14
  45. data/lib/sequel/adapters/odbc/db2.rb +21 -0
  46. data/lib/sequel/adapters/odbc.rb +15 -3
  47. data/lib/sequel/adapters/oracle.rb +17 -1
  48. data/lib/sequel/adapters/postgres.rb +111 -16
  49. data/lib/sequel/adapters/shared/access.rb +21 -0
  50. data/lib/sequel/adapters/shared/db2.rb +290 -0
  51. data/lib/sequel/adapters/shared/firebird.rb +214 -0
  52. data/lib/sequel/adapters/shared/informix.rb +45 -0
  53. data/lib/sequel/adapters/shared/mssql.rb +85 -47
  54. data/lib/sequel/adapters/shared/mysql.rb +50 -7
  55. data/lib/sequel/adapters/shared/mysql_prepared_statements.rb +143 -0
  56. data/lib/sequel/adapters/shared/oracle.rb +0 -4
  57. data/lib/sequel/adapters/shared/postgres.rb +75 -43
  58. data/lib/sequel/adapters/shared/sqlite.rb +56 -8
  59. data/lib/sequel/adapters/sqlite.rb +12 -11
  60. data/lib/sequel/adapters/swift/mysql.rb +9 -0
  61. data/lib/sequel/adapters/tinytds.rb +139 -7
  62. data/lib/sequel/adapters/utils/emulate_offset_with_row_number.rb +55 -0
  63. data/lib/sequel/ast_transformer.rb +190 -0
  64. data/lib/sequel/connection_pool/threaded.rb +3 -2
  65. data/lib/sequel/connection_pool.rb +1 -1
  66. data/lib/sequel/core.rb +6 -5
  67. data/lib/sequel/database/connecting.rb +5 -5
  68. data/lib/sequel/database/dataset.rb +1 -1
  69. data/lib/sequel/database/dataset_defaults.rb +1 -1
  70. data/lib/sequel/database/logging.rb +1 -1
  71. data/lib/sequel/database/misc.rb +38 -17
  72. data/lib/sequel/database/query.rb +50 -19
  73. data/lib/sequel/database/schema_generator.rb +8 -5
  74. data/lib/sequel/database/schema_methods.rb +52 -27
  75. data/lib/sequel/dataset/actions.rb +167 -48
  76. data/lib/sequel/dataset/features.rb +57 -8
  77. data/lib/sequel/dataset/graph.rb +1 -1
  78. data/lib/sequel/dataset/misc.rb +39 -20
  79. data/lib/sequel/dataset/mutation.rb +3 -3
  80. data/lib/sequel/dataset/prepared_statements.rb +29 -14
  81. data/lib/sequel/dataset/query.rb +182 -32
  82. data/lib/sequel/dataset/sql.rb +31 -58
  83. data/lib/sequel/dataset.rb +8 -0
  84. data/lib/sequel/exceptions.rb +4 -0
  85. data/lib/sequel/extensions/columns_introspection.rb +61 -0
  86. data/lib/sequel/extensions/migration.rb +6 -4
  87. data/lib/sequel/extensions/to_dot.rb +95 -83
  88. data/lib/sequel/model/associations.rb +893 -309
  89. data/lib/sequel/model/base.rb +302 -105
  90. data/lib/sequel/model/errors.rb +1 -1
  91. data/lib/sequel/model/exceptions.rb +5 -1
  92. data/lib/sequel/model.rb +13 -7
  93. data/lib/sequel/plugins/association_pks.rb +22 -4
  94. data/lib/sequel/plugins/defaults_setter.rb +58 -0
  95. data/lib/sequel/plugins/identity_map.rb +113 -6
  96. data/lib/sequel/plugins/many_through_many.rb +67 -5
  97. data/lib/sequel/plugins/prepared_statements.rb +140 -0
  98. data/lib/sequel/plugins/prepared_statements_associations.rb +84 -0
  99. data/lib/sequel/plugins/prepared_statements_safe.rb +82 -0
  100. data/lib/sequel/plugins/prepared_statements_with_pk.rb +59 -0
  101. data/lib/sequel/plugins/serialization_modification_detection.rb +51 -0
  102. data/lib/sequel/plugins/sharding.rb +12 -20
  103. data/lib/sequel/plugins/single_table_inheritance.rb +2 -0
  104. data/lib/sequel/plugins/update_primary_key.rb +1 -1
  105. data/lib/sequel/plugins/xml_serializer.rb +3 -3
  106. data/lib/sequel/sql.rb +107 -51
  107. data/lib/sequel/timezones.rb +12 -3
  108. data/lib/sequel/version.rb +1 -1
  109. data/spec/adapters/db2_spec.rb +146 -0
  110. data/spec/adapters/mssql_spec.rb +36 -0
  111. data/spec/adapters/mysql_spec.rb +36 -19
  112. data/spec/adapters/postgres_spec.rb +115 -28
  113. data/spec/adapters/spec_helper.rb +6 -0
  114. data/spec/adapters/sqlite_spec.rb +11 -0
  115. data/spec/core/connection_pool_spec.rb +62 -77
  116. data/spec/core/database_spec.rb +244 -287
  117. data/spec/core/dataset_spec.rb +383 -34
  118. data/spec/core/expression_filters_spec.rb +159 -41
  119. data/spec/core/schema_spec.rb +326 -3
  120. data/spec/core/spec_helper.rb +45 -0
  121. data/spec/extensions/association_pks_spec.rb +38 -0
  122. data/spec/extensions/columns_introspection_spec.rb +91 -0
  123. data/spec/extensions/defaults_setter_spec.rb +64 -0
  124. data/spec/extensions/identity_map_spec.rb +162 -0
  125. data/spec/extensions/many_through_many_spec.rb +195 -20
  126. data/spec/extensions/migration_spec.rb +17 -17
  127. data/spec/extensions/nested_attributes_spec.rb +1 -0
  128. data/spec/extensions/prepared_statements_associations_spec.rb +126 -0
  129. data/spec/extensions/prepared_statements_safe_spec.rb +79 -0
  130. data/spec/extensions/prepared_statements_spec.rb +72 -0
  131. data/spec/extensions/prepared_statements_with_pk_spec.rb +38 -0
  132. data/spec/extensions/schema_dumper_spec.rb +2 -2
  133. data/spec/extensions/schema_spec.rb +12 -20
  134. data/spec/extensions/serialization_modification_detection_spec.rb +36 -0
  135. data/spec/extensions/single_table_inheritance_spec.rb +11 -0
  136. data/spec/extensions/spec_helper.rb +3 -1
  137. data/spec/extensions/to_dot_spec.rb +3 -5
  138. data/spec/extensions/xml_serializer_spec.rb +16 -4
  139. data/spec/integration/associations_test.rb +405 -15
  140. data/spec/integration/database_test.rb +4 -2
  141. data/spec/integration/dataset_test.rb +240 -20
  142. data/spec/integration/plugin_test.rb +142 -5
  143. data/spec/integration/prepared_statement_test.rb +174 -95
  144. data/spec/integration/schema_test.rb +128 -16
  145. data/spec/integration/spec_helper.rb +15 -0
  146. data/spec/integration/transaction_test.rb +40 -0
  147. data/spec/integration/type_test.rb +16 -2
  148. data/spec/model/association_reflection_spec.rb +91 -0
  149. data/spec/model/associations_spec.rb +476 -5
  150. data/spec/model/base_spec.rb +91 -1
  151. data/spec/model/eager_loading_spec.rb +519 -31
  152. data/spec/model/hooks_spec.rb +161 -0
  153. data/spec/model/model_spec.rb +89 -2
  154. data/spec/model/plugins_spec.rb +17 -0
  155. data/spec/model/record_spec.rb +184 -12
  156. data/spec/model/spec_helper.rb +5 -0
  157. data/spec/model/validations_spec.rb +11 -0
  158. metadata +85 -34
@@ -0,0 +1,547 @@
1
+ = Schema modification methods
2
+
3
+ Here's a brief description of the most common schema modification methods:
4
+
5
+ == +create_table+
6
+
7
+ +create_table+ is the most common schema modification method, and it's used for adding new tables
8
+ to the schema. You provide it with the name of the table as a symbol, as well a block:
9
+
10
+ create_table(:artists) do
11
+ primary_key :id
12
+ String :name
13
+ end
14
+
15
+ Not that if you want a primary key for the table, you need to specify it, Sequel does not create one
16
+ by default.
17
+
18
+ === Column types
19
+
20
+ Most method calls inside the create_table block will create columns, since +method_missing+ calls +column+.
21
+ Columns are generally created by specifying the column type as the method
22
+ name, followed by the column name symbol to use, and after that any options that should be used.
23
+ If the method is a ruby class name that Sequel recognizes, Sequel will transform it into the appropriate
24
+ type for the given database. So while you specified +String+, Sequel will actually use +varchar+ or
25
+ +text+ depending on the underlying database. Here's a list of all of ruby classes that Sequel will
26
+ convert to database types:
27
+
28
+ create_table(:columns_types) do # common database type used
29
+ Integer :a0 # integer
30
+ String :a1 # varchar(255)
31
+ String :a2, :size=>50 # varchar(50)
32
+ String :a3, :fixed=>true # char(255)
33
+ String :a4, :fixed=>true, :size=>50 # char(50)
34
+ String :a5, :text=>true # text
35
+ File :b, # blob
36
+ Fixnum :c # integer
37
+ Bignum :d # bigint
38
+ Float :e # double precision
39
+ BigDecimal :f # numeric
40
+ BigDecimal :f2, :size=>10 # numeric(10)
41
+ BigDecimal :f3, :size=>[10, 2] # numeric(10, 2)
42
+ Date :g # date
43
+ DateTime :h # timestamp
44
+ Time :i # timestamp
45
+ Time :i2, :only_time=>true # time
46
+ Numeric :j # numeric
47
+ TrueClass :k # boolean
48
+ FalseClass :l # boolean
49
+ end
50
+
51
+ Note that in addition to the ruby class name, Sequel also pays attention to the column options when
52
+ determining which database type to use. Also note that for boolean columns, you can use either
53
+ TrueClass or FalseClass, they are treated the same way (ruby doesn't have a Boolean class).
54
+
55
+ Also note that this conversion is only done if you use a supported ruby class name. In all other
56
+ cases, Sequel uses the type specified verbatim:
57
+
58
+ create_table(:columns_types) do # database type used
59
+ string :a1 # string
60
+ datetime :a2 # datetime
61
+ blob :a3 # blob
62
+ inet :a4 # inet
63
+ end
64
+
65
+ In addition to specifying the types as methods, you can use the +column+ method and specify the types
66
+ as the second argument, either as ruby classes, symbols, or strings:
67
+
68
+ create_table(:columns_types) do # database type used
69
+ column :a1, :string # string
70
+ column :a2, String # varchar(255)
71
+ column :a3, 'string' # string
72
+ column :a4, :datetime # datetime
73
+ column :a5, DateTime # timestamp
74
+ column :a6, 'timestamp(6)' # timestamp(6)
75
+ end
76
+
77
+ === Column options
78
+
79
+ When using the type name as method, the third argument is an options hash, and when using the +column+
80
+ method, the fourth argument is the options hash. The following options are supported:
81
+
82
+ :default :: The default value for the column.
83
+ :index :: Create an index on this column.
84
+ :null :: Mark the column as allowing NULL values (if true),
85
+ or not allowing NULL values (if false). If unspecified, will default
86
+ to whatever the database default is.
87
+ :size :: The size of the column, generally used with string
88
+ columns to specify the maximum number of characters the column will hold.
89
+ An array of two integers can be provided to set the size and the
90
+ precision, respectively, of decimal columns.
91
+ :unique :: Mark the column as unique, generally has the same effect as
92
+ creating a unique index on the column.
93
+ :unsigned :: Make the column type unsigned, only useful for integer
94
+ columns.
95
+
96
+ === Other methods
97
+
98
+ In addition to the +column+ method and other methods that create columns, there are a other methods that can be used:
99
+
100
+ ==== +primary_key+
101
+
102
+ You've seen this one used already. It's used to create an autoincrementing integer primary key column.
103
+
104
+ create_table(:a0){primary_key :id}
105
+
106
+ If you want to create a primary key column that doesn't use an autoincrementing integer, you should
107
+ not use this method. Instead, you should use the :primary_key option to the +column+ method or type
108
+ method:
109
+
110
+ create_table(:a1){Integer :id, :primary_key=>true} # Non autoincrementing integer primary key
111
+ create_table(:a2){String :name, :primary_key=>true} # varchar(255) primary key
112
+
113
+ If you want to create a composite primary key, you should call the +primary_key+ method with an
114
+ array of column symbols:
115
+
116
+ create_table(:items) do
117
+ Integer :group_id
118
+ Integer :position
119
+ primary_key [:group_id, :position]
120
+ end
121
+
122
+ If provided with an array, +primary_key+ does not create a column, it just sets up the primary key constraint.
123
+
124
+ ==== +foreign_key+
125
+
126
+ +foreign_key+ is used to create a foreign key column that references a column in another table (or the same table).
127
+ It takes the column name as the first argument, the table it references as the second argument, and an options hash
128
+ as it's third argument. A simple example is:
129
+
130
+ create_table(:albums) do
131
+ primary_key :id
132
+ foreign_key :artist_id, :artists
133
+ String :name
134
+ end
135
+
136
+ +foreign_key+ accepts some specific options:
137
+
138
+ :deferrable :: Makes the foreign key constraint checks deferrable, so they aren't checked
139
+ until the end of the transaction.
140
+ :key :: For foreign key columns, the column in the associated table
141
+ that this column references. Unnecessary if this column
142
+ references the primary key of the associated table, at least
143
+ on most databases.
144
+ :on_delete :: Specify the behavior of this foreign key column when the row with the primary key
145
+ it references is deleted , can be :restrict, :cascade, :set_null, or :set_default.
146
+ :on_update :: Specify the behavior of this foreign key column when the row with the primary key
147
+ it references modifies the value of the primary key, can be
148
+ :restrict, :cascade, :set_null, or :set_default.
149
+
150
+ Like +primary_key+, if you provide +foreign_key+ with an array of symbols, it will not create a
151
+ column, but create a foreign key constraint:
152
+
153
+ create_table(:artists) do
154
+ String :name
155
+ String :location
156
+ primary_key [:name, :location]
157
+ end
158
+ create_table(:albums) do
159
+ String :artist_name
160
+ String :artist_location
161
+ String :name
162
+ foreign_key [:artist_name, :artist_location], :artists
163
+ end
164
+
165
+ ==== +index+
166
+
167
+ +index+ creates indexes on the table. For single columns, calling index is the same as using the
168
+ <tt>:index</tt> option when creating the column:
169
+
170
+ create_table(:a){Integer :id, :index=>true}
171
+ # Same as:
172
+ create_table(:a) do
173
+ Integer :id
174
+ index :id
175
+ end
176
+
177
+ Similar to the +primary_key+ and +foreign_key+ methods, calling +index+ with an array of symbols
178
+ will create a multiple column index:
179
+
180
+ create_table(:albums) do
181
+ primary_key :id
182
+ foreign_key :artist_id, :artists
183
+ Integer :position
184
+ index [:artist_id, :position]
185
+ end
186
+
187
+ The +index+ method also accepts some options:
188
+
189
+ :name :: The name of the index (generated based on the table and column names if not provided).
190
+ :type :: The type of index to use (only supported by some databases)
191
+ :unique :: Make the index unique, so duplicate values are not allowed.
192
+ :where :: Create a partial index (only supported by some databases)
193
+
194
+ ==== +unique+
195
+
196
+ The +unique+ method creates a unique constraint on the table. A unique constraint generally
197
+ operates identically to a unique index, so the following three +create_table+ blocks are
198
+ pretty much identical:
199
+
200
+ create_table(:a){Integer :a, :unique=>true}
201
+
202
+ create_table(:a) do
203
+ Integer :a
204
+ index :a, :unique=>true
205
+ end
206
+
207
+ create_table(:a) do
208
+ Integer :a
209
+ unique :a
210
+ end
211
+
212
+ Just like +index+, +unique+ can set up a multiple column unique constraint, where the
213
+ combination of the columns must be unique:
214
+
215
+ create_table(:a) do
216
+ Integer :a
217
+ Integer :b
218
+ unique [:a, :b]
219
+ end
220
+
221
+ ==== +full_text_index+ and +spatial_index+
222
+
223
+ Both of these create specialized index types supported by some databases. They
224
+ both take the same options as +index+.
225
+
226
+ ==== +constraint+
227
+
228
+ +constraint+ creates a named table constraint:
229
+
230
+ create_table(:artists) do
231
+ primary_key :id
232
+ String :name
233
+ constraint(:name_min_length){char_length(name) > 2}
234
+ end
235
+
236
+ Instead of using a block, you can use arguments that will be handled similarly
237
+ to <tt>Dataset#filter</tt>:
238
+
239
+ create_table(:artists) do
240
+ primary_key :id
241
+ String :name
242
+ constraint(:name_length_range, :char_length.sql_function(:name)=>3..50)
243
+ end
244
+
245
+ ==== +check+
246
+
247
+ +check+ operates just like +constraint+, except that it doesn't take a name
248
+ and it creates an unnamed constraint
249
+
250
+ create_table(:artists) do
251
+ primary_key :id
252
+ String :name
253
+ check{char_length(name) > 2}
254
+ end
255
+
256
+ == +alter_table+
257
+
258
+ +alter_table+ is used to alter existing tables, changing their columns, indexes,
259
+ or constraints. It it used just like +create_table+, accepting a block which
260
+ is instance_evaled, and providing its own methods:
261
+
262
+ === +add_column+
263
+
264
+ One of the most common methods, +add_column+ is used to add a column to the table.
265
+ Its API is similar to that of +create_table+'s +column+ method, where the first
266
+ argument is the column name, the second is the type, and the third is an options
267
+ hash:
268
+
269
+ alter_table(:albums) do
270
+ add_column :copies_sold, Integer, :default=>0
271
+ end
272
+
273
+ When adding a column, it's a good idea to provide a default value, unless you
274
+ want the value for all rows to be set to NULL.
275
+
276
+ === +drop_column+
277
+
278
+ As you may expect, +drop_column+ takes a column name and drops the column. It's
279
+ often used in the +down+ block of a migration to drop a column added in an +up+ block:
280
+
281
+ alter_table(:albums) do
282
+ drop_column :copies_sold
283
+ end
284
+
285
+ === +rename_column+
286
+
287
+ +rename_column+ is used to rename a column. It takes the old column name as the first
288
+ argument, and the new column name as the second argument:
289
+
290
+ alter_table(:albums) do
291
+ rename_column :copies_sold, :total_sales
292
+ end
293
+
294
+ === +add_primary_key+
295
+
296
+ If you forgot to include a primary key on the table, and want to add one later, you
297
+ can use +add_primary_key+. A common use of this is to make many_to_many association
298
+ join tables into real models:
299
+
300
+ alter_table(:albums_artists) do
301
+ add_primary_key :id
302
+ end
303
+
304
+ Just like +create_table+'s +primary_key+ method, if you provide an array of symbols,
305
+ Sequel will not add a column, but will add a composite primary key constraint:
306
+
307
+ alter_table(:albums_artists) do
308
+ add_primary_key [:album_id, :artist_id]
309
+ end
310
+
311
+ If you just want to take an existing single column and make it a primary key, call
312
+ +add_primary_key+ with an array with a single symbol:
313
+
314
+ alter_table(:artists) do
315
+ add_primary_key [:id]
316
+ end
317
+
318
+ === +add_foreign_key+
319
+
320
+ +add_foreign_key+ can be used to add a new foreign key column or constraint to a table.
321
+ Like +add_primary_key+, if you provide it with a symbol as the first argument, it
322
+ creates a new column:
323
+
324
+ alter_table(:albums) do
325
+ add_foreign_key :artist_id, :artists
326
+ end
327
+
328
+ If you want to add a new foreign key constraint to an existing column, you provide an
329
+ array with a single element:
330
+
331
+ alter_table(:albums) do
332
+ add_foreign_key [:artist_id], :artists
333
+ end
334
+
335
+ To set up a multiple column foreign key constraint, use an array with multiple column
336
+ symbols:
337
+
338
+ alter_table(:albums) do
339
+ add_foreign_key [:artist_name, :artist_location], :artists
340
+ end
341
+
342
+ === +add_index+
343
+
344
+ +add_index+ works just like +create_table+'s +index+ method, creating a new index on
345
+ the table:
346
+
347
+ alter_table(:albums) do
348
+ add_index :artist_id
349
+ end
350
+
351
+ It accepts the same options as +create_table+'s +index+ method, and you can set up
352
+ a multiple column index using an array:
353
+
354
+ alter_table(:albums_artists) do
355
+ add_index [:album_id, :artist_id], :unique=>true
356
+ end
357
+
358
+ === +drop_index+
359
+
360
+ As you may expect, +drop_index+ drops an existing index:
361
+
362
+ alter_table(:albums) do
363
+ drop_index :artist_id
364
+ end
365
+
366
+ Just like +drop_column+, it is often used in the +down+ block of a migration.
367
+
368
+ To drop an index with a specific name, use the <tt>:name</tt> option:
369
+
370
+ alter_table(:albums) do
371
+ drop_index :artist_id, :name=>:artists_id_index
372
+ end
373
+
374
+ === +add_full_text_index+, +add_spatial_index+
375
+
376
+ Corresponding to +create_table+'s +full_text_index+ and +spatial_index+ methods,
377
+ these two methods create new indexes on the table.
378
+
379
+ === +add_constraint+
380
+
381
+ This adds a named constraint to the table, similar to +create_table+'s +constraint+
382
+ method:
383
+
384
+ alter_table(:albums) do
385
+ add_constraint(:name_min_length){char_length(name) > 2}
386
+ end
387
+
388
+ There is no method to add an unnamed constraint, but you can pass nil as the first
389
+ argument of +add_constraint+ to do so. However, it's not recommend to do that
390
+ as it is difficult to drop such a constraint.
391
+
392
+ === +add_unique_constraint+
393
+
394
+ This adds a unique constraint to the table, similar to +create_table+'s +unique+
395
+ method. This usually has the same effect as adding a unique index.
396
+
397
+ alter_table(:albums) do
398
+ add_unique_constraint [:artist_id, :name]
399
+ end
400
+
401
+ === +drop_constraint+
402
+
403
+ This method drops an existing named constraint:
404
+
405
+ alter_table(:albums) do
406
+ drop_constraint(:name_min_length)
407
+ end
408
+
409
+ There is no database independent method to drop an unnamed constraint. Generally, the
410
+ database will give it a name automatically, and you will have to figure out what it is.
411
+ For that reason, you should not add unnamed constraints that you ever might need to remove.
412
+
413
+ On MySQL, you must specify the type of constraint via a <tt>:type</tt> option:
414
+
415
+ alter_table(:albums) do
416
+ drop_constraint(:albums_pk, :type=>:primary_key)
417
+ drop_constraint(:albums_fk, :type=>:foreign_key)
418
+ drop_constraint(:albums_uk, :type=>:unique)
419
+ end
420
+
421
+ === +set_column_default+
422
+
423
+ This modifies the default value of a column:
424
+
425
+ alter_table(:albums) do
426
+ set_column_default :copies_sold, 0
427
+ end
428
+
429
+ === +set_column_type+
430
+
431
+ This modifies a column's type. Most databases will attempt to convert existing values in
432
+ the columns to the new type:
433
+
434
+ alter_table(:albums) do
435
+ set_column_type :copies_sold, Bignum
436
+ end
437
+
438
+ You can specify the type as a string or symbol, in which case it is used verbatim, or as a supported
439
+ ruby class, in which case it gets converted to an appropriate database type.
440
+
441
+ === +set_column_allow_null+
442
+
443
+ This changes the NULL or NOT NULL setting of a column:
444
+
445
+ alter_table(:albums) do
446
+ set_column_allow_null :artist_id, true # NULL
447
+ set_column_allow_null :copies_sold, false # NOT NULL
448
+ end
449
+
450
+ == Other +Database+ schema modification methods
451
+
452
+ <tt>Sequel::Database</tt> has many schema modification instance methods,
453
+ most of which are shortcuts to the same methods in +alter_table+. The
454
+ following +Database+ instance methods just call +alter_table+ with a
455
+ block that calls the method with the same name inside the +alter_table+
456
+ block with all arguments after the first argument (which is used as
457
+ the table name):
458
+
459
+ * +add_column+
460
+ * +drop_column+
461
+ * +rename_column+
462
+ * +add_index+
463
+ * +drop_index+
464
+ * +set_column_default+
465
+ * +set_column_type+
466
+
467
+ For example, the following two method calls do the same thing:
468
+
469
+ alter_table(:artists){add_column :copies_sold, Integer}
470
+ add_column :artists, :copies_sold, Integer
471
+
472
+ There are some other schema modification methods that have no +alter_table+
473
+ counterpart:
474
+
475
+ === +drop_table+
476
+
477
+ +drop_table+ takes multiple arguments and treats all arguments as a
478
+ table name to drop:
479
+
480
+ drop_table(:albums_artists, :albums, :artists)
481
+
482
+ Note that when dropping tables, you may need to drop them in a specific order
483
+ if you are using foreign keys and the database is enforcing referential
484
+ integrity. In general, you need to drop the tables containing the foreign
485
+ keys before the tables containing the primary keys they reference.
486
+
487
+ === +rename_table+
488
+
489
+ You can rename an existing table using +rename_table+. Like +rename_column+,
490
+ the first argument is the current name, and the second is the new name:
491
+
492
+ rename_table(:artist, :artists)
493
+
494
+ === <tt>create_table!</tt>
495
+
496
+ <tt>create_table!</tt> with the bang drops the table if it exists
497
+ before attempting to create it, so:
498
+
499
+ create_table!(:artists)
500
+ primary_key :id
501
+ end
502
+
503
+ is the same as:
504
+
505
+ drop_table(:artists) if table_exists?(:artists)
506
+ create_table(:artists)
507
+ primary_key :id
508
+ end
509
+
510
+ It should not be used inside migrations, as if the table does not exist, it may
511
+ mess up the migration.
512
+
513
+ === <tt>create_table?</tt>
514
+
515
+ <tt>create_table?</tt> with a question mark only creates the table if it does
516
+ not already exist, so:
517
+
518
+ create_table!(:artists)
519
+ primary_key :id
520
+ end
521
+
522
+ is the same as:
523
+
524
+ create_table(:artists)
525
+ primary_key :id
526
+ end unless table_exists?(:artists)
527
+
528
+ Like <tt>create_table!</tt>, it should not be used inside migrations.
529
+
530
+ === +create_view+ and +create_or_replace_view+
531
+
532
+ These can be used to create views. The difference between them is that
533
+ +create_or_replace_view+ will unconditionally replace an existing view of
534
+ the same name, while +create_view+ will probably raise an error. Both methods
535
+ take the name as the first argument, and either an string or a dataset as the
536
+ second argument:
537
+
538
+ create_view(:gold_albums, DB[:albums].filter{copies_sold > 500000})
539
+ create_or_replace_view(:gold_albums, "SELECT * FROM albums WHERE copies_sold > 500000")
540
+
541
+ === +drop_view+
542
+
543
+ +drop_view+ drops existing views. Just like +drop_table+, it can accept multiple
544
+ arguments:
545
+
546
+ drop_view(:gold_albums, :platinum_albums)
547
+
data/doc/testing.rdoc ADDED
@@ -0,0 +1,106 @@
1
+ = Testing with Sequel
2
+
3
+ Whether or not you use Sequel in your application, you are usually going to want to have tests that ensure that your code works. When you are using Sequel, it's helpful to integrate it into your testing framework, and it's generally best to run each test in its own transaction if possible. That keeps all tests isolated from each other, and it's simple as it handles all of the cleanup for you. Sequel doesn't ship with helpers for common libraries, as the exact code you need is often application-specific, but this page offers some examples that you can either use directly or build on.
4
+
5
+ == Transactional tests
6
+
7
+ These run each test in its own transaction, the recommended way to test.
8
+
9
+ === RSpec 1
10
+
11
+ class Spec::Example::ExampleGroup
12
+ def execute(*args, &block)
13
+ x = nil
14
+ Sequel::Model.db.transaction{x = super(*args, &block); raise Sequel::Rollback}
15
+ x
16
+ end
17
+ end
18
+
19
+ === RSpec 2
20
+
21
+ class Spec::Example::ExampleGroup
22
+ around do |example|
23
+ Sequel::Model.db.transaction{example.call; raise Sequel::Rollback}
24
+ end
25
+ end
26
+
27
+ === Test::Unit
28
+
29
+ # Must use this class as the base class for your tests
30
+ class SequelTestCase < Test::Unit::TestCase
31
+ def run(*args, &block)
32
+ Sequel::Model.db.transaction do
33
+ super
34
+ raise Sequel::Rollback
35
+ end
36
+ end
37
+ end
38
+
39
+ == Nontransactional tests
40
+
41
+ In some cases, it is not possible to use transactions. For example, if you are testing a web application that is running in a separate process, you don't have access to that process's database connections, so you can't run your examples in transactions. In that case, the best way to handle things is to cleanup after each test by deleting or truncating the database tables used in the test.
42
+
43
+ The order in which you delete/truncate the tables is important if you are using referential integrity in your database (which you probably should be doing). If you are using referential integrity, you need to make sure to delete in tables referencing other tables before the tables that are being referenced. For example, if you have an +albums+ table with an +artist_id+ field referencing the +artists+ table, you want to delete/truncate the +albums+ table before the +artists+ table. Note that if you have cyclic references in your database, you will probably need to write your own custom cleaning code.
44
+
45
+ === RSpec
46
+
47
+ class Spec::Example::ExampleGroup
48
+ after do
49
+ [:table1, :table2].each{|x| Sequel::Model.db.from(x).truncate}
50
+ # or
51
+ [:table1, :table2].each{|x| Sequel::Model.db.from(x).delete}
52
+ end
53
+ end
54
+
55
+ === Test::Unit
56
+
57
+ # Must use this class as the base class for your tests
58
+ class SequelTestCase < Test::Unit::TestCase
59
+ def teardown
60
+ [:table1, :table2].each{|x| Sequel::Model.db.from(x).truncate}
61
+ # or
62
+ [:table1, :table2].each{|x| Sequel::Model.db.from(x).delete}
63
+ end
64
+ end
65
+
66
+ = Testing Sequel Itself
67
+
68
+ Sequel has multiple separate test suites. All test suites run under either RSpec 1 or RSpec 2.
69
+
70
+ == rake spec
71
+
72
+ The +spec+ rake task (which is also the default rake task) runs Sequel's core and model specs. These specs use a mocked database connection, and test for specific SQL used and for generally correct behavior.
73
+
74
+ == rake spec_plugin
75
+
76
+ The +spec_plugin+ rake task runs the specs for the plugins and extensions that ship with Sequel. These also use a mocked database connection, and operate very similarly to the general Sequel core and model specs.
77
+
78
+ == rake spec_<i>adapter</i> (e.g. rake spec_postgres)
79
+
80
+ The <tt>spec_<i>adapter</i></tt> specs run against a real database connection with nothing mocked, and test for correct results. They are much slower than the standard specs, but they will catch errors that are mocked out by the default specs, as well show issues that only occur on a certain database, adapter, interpreter, or some combination of those.
81
+
82
+ These specs are broken down into two parts. For each database, there are specific specs that only apply to that database, and these are called the adapter specs. There are also shared specs that apply to all (or almost all) databases, these are called the integration specs.
83
+
84
+ == Environment variables
85
+
86
+ Sequel often uses environment variables when testing to specify either the database to be tested or specify how testing should be done.
87
+
88
+ === Connection Strings
89
+
90
+ The following environment variables specify Database connection URL strings:
91
+
92
+ * SEQUEL_INTEGRATION_URL: integration specs
93
+ * SEQUEL_FB_SPEC_DB: firebird adapter specs
94
+ * SEQUEL_MSSQL_SPEC_DB: mssql adapter specs
95
+ * SEQUEL_PG_SPEC_DB: postgres adapter specs
96
+ * SEQUEL_SQLITE_SPEC_DB: sqlite adapter specs
97
+ * SEQUEL_MY_SPEC_DB: mysql adapter specs
98
+ * SEQUEL_DB2_SPEC_DB: db2 adapter specs
99
+
100
+ === Other
101
+
102
+ * SEQUEL_MSSQL_SPEC_REQUIRE: Separate file to require when running mssql adapter specs
103
+ * SEQUEL_DB2_SPEC_REQUIRE: Separate file to require when running db2 adapter specs
104
+ * SEQUEL_COLUMNS_INTROSPECTION: Whehter to run the specs with the columns_introspection extension loaded by default
105
+ * SEQUEL_NO_PENDING: Don't mark any specs as pending, try running all specs
106
+ * SKIPPED_TEST_WARN: Warn when skipping any tests because libraries aren't available