sequel 3.26.0 → 3.27.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +26 -0
- data/Rakefile +2 -3
- data/doc/mass_assignment.rdoc +54 -0
- data/doc/migration.rdoc +9 -533
- data/doc/prepared_statements.rdoc +8 -7
- data/doc/release_notes/3.27.0.txt +82 -0
- data/doc/schema_modification.rdoc +547 -0
- data/doc/testing.rdoc +64 -0
- data/lib/sequel/adapters/amalgalite.rb +4 -0
- data/lib/sequel/adapters/jdbc.rb +3 -1
- data/lib/sequel/adapters/jdbc/h2.rb +11 -5
- data/lib/sequel/adapters/mysql.rb +4 -122
- data/lib/sequel/adapters/mysql2.rb +4 -13
- data/lib/sequel/adapters/odbc.rb +4 -1
- data/lib/sequel/adapters/odbc/db2.rb +21 -0
- data/lib/sequel/adapters/shared/mysql.rb +12 -0
- data/lib/sequel/adapters/shared/mysql_prepared_statements.rb +143 -0
- data/lib/sequel/adapters/tinytds.rb +122 -3
- data/lib/sequel/core.rb +4 -3
- data/lib/sequel/database/misc.rb +7 -10
- data/lib/sequel/dataset/misc.rb +1 -1
- data/lib/sequel/dataset/sql.rb +7 -0
- data/lib/sequel/model/associations.rb +2 -2
- data/lib/sequel/model/base.rb +60 -10
- data/lib/sequel/plugins/prepared_statements_safe.rb +17 -7
- data/lib/sequel/sql.rb +5 -0
- data/lib/sequel/timezones.rb +12 -3
- data/lib/sequel/version.rb +1 -1
- data/spec/adapters/mysql_spec.rb +25 -21
- data/spec/core/database_spec.rb +200 -0
- data/spec/core/dataset_spec.rb +6 -0
- data/spec/extensions/prepared_statements_safe_spec.rb +10 -0
- data/spec/extensions/schema_dumper_spec.rb +2 -2
- data/spec/integration/schema_test.rb +30 -1
- data/spec/integration/type_test.rb +10 -3
- data/spec/model/base_spec.rb +44 -0
- data/spec/model/model_spec.rb +14 -0
- data/spec/model/record_spec.rb +131 -12
- metadata +14 -4
data/CHANGELOG
CHANGED
@@ -1,3 +1,29 @@
|
|
1
|
+
=== 3.27.0 (2011-09-01)
|
2
|
+
|
3
|
+
* Add support for native prepared statements to the tinytds adapter (jeremyevans)
|
4
|
+
|
5
|
+
* Add support for native prepared statements and stored procedures to the mysql2 adapter (jeremyevans)
|
6
|
+
|
7
|
+
* Support dropping primary key, foreign key, and unique constraints on MySQL via the drop_constraint :type option (jeremyevans)
|
8
|
+
|
9
|
+
* Add Sequel::SQLTime class for handling SQL time columns (jeremyevans)
|
10
|
+
|
11
|
+
* Typecast DateTime objects to Date for date columns (jeremyevans)
|
12
|
+
|
13
|
+
* When typecasting Date objects to timestamps, make the resulting objects always have no fractional date components (jeremyevans)
|
14
|
+
|
15
|
+
* Add Model.dataset_module for simplifying many def_dataset_method calls (jeremyevans)
|
16
|
+
|
17
|
+
* Make prepared_statements_safe plugin work on classes without datasets (jeremyevans)
|
18
|
+
|
19
|
+
* Make Dataset#hash work correctly when referencing SQL::Expression instances (jeremyevans)
|
20
|
+
|
21
|
+
* Handle allowed mass assignment methods correctly when including modules in classes or extending instances with modules (jeremyevans)
|
22
|
+
|
23
|
+
* Fix Model#hash to work correctly with composite primary keys and with no primary key (jeremyevans)
|
24
|
+
|
25
|
+
* Model#exists? now returns false without issuing a query for new model objects (jeremyevans)
|
26
|
+
|
1
27
|
=== 3.26.0 (2011-08-01)
|
2
28
|
|
3
29
|
* Fix bug in default connection pool if a disconnect error is raised and the disconnection_proc also raises an error (jeremyevans)
|
data/Rakefile
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
require "rake"
|
2
2
|
require "rake/clean"
|
3
|
-
require "rake/gempackagetask"
|
4
3
|
|
5
4
|
NAME = 'sequel'
|
6
5
|
VERS = lambda do
|
@@ -56,7 +55,7 @@ end
|
|
56
55
|
|
57
56
|
desc "Make local version of website"
|
58
57
|
task :website do
|
59
|
-
sh %{www/make_www.rb}
|
58
|
+
sh %{#{RUBY} www/make_www.rb}
|
60
59
|
end
|
61
60
|
|
62
61
|
desc "Make rdoc for website"
|
@@ -160,5 +159,5 @@ end
|
|
160
159
|
|
161
160
|
desc "Check syntax of all .rb files"
|
162
161
|
task :check_syntax do
|
163
|
-
Dir['**/*.rb'].each{|file| print `#{
|
162
|
+
Dir['**/*.rb'].each{|file| print `#{RUBY} -c #{file} | fgrep -v "Syntax OK"`}
|
164
163
|
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
= Sequel::Model Mass Assignment
|
2
|
+
|
3
|
+
Most Model methods that take a hash of attribute keys and values, including <tt>Model.new</tt>, <tt>Model.create</tt>, <tt>Model#set</tt> and <tt>Model#update</tt> are subject to Sequel's mass assignment rules.
|
4
|
+
When you pass a hash to these methods, each key has an <tt>=</tt> appended to it (the setter method), and if the setter method exists and access to it is not restricted, Sequel will call the setter method with the hash value.
|
5
|
+
By default, there are two types of setter methods that are restricted.
|
6
|
+
The first is methods like <tt>typecast_on_assignment=</tt> and <tt>==</tt>, which don't affect columns.
|
7
|
+
These methods cannot be enabled for mass assignment.
|
8
|
+
The second is primary key setters.
|
9
|
+
To enable use of primary key setters, you need to call +unrestrict_primary_key+ for that model:
|
10
|
+
|
11
|
+
Post.unrestrict_primary_key
|
12
|
+
|
13
|
+
Since mass assignment by default allows modification of all column values except for primary key columns, it can be a security risk in some cases.
|
14
|
+
Sequel has multiple ways of securing mass assignment.
|
15
|
+
The first way is using +set_allowed_columns+:
|
16
|
+
|
17
|
+
Post.set_allowed_columns :title, :body, :category
|
18
|
+
|
19
|
+
This explicitly sets which methods are allowed (<tt>title=</tt>, <tt>body=</tt>, and <tt>category=</tt>), all other methods will not be allowed.
|
20
|
+
This method is useful in simple applications where the same columns are allowed in all cases, but not appropriate when different columns are allowed in different scenarios (e.g. admin access vs. user access).
|
21
|
+
To handle cases where different columns are allowed in different cases, you can use +set_only+ or +update_only+:
|
22
|
+
|
23
|
+
# user case
|
24
|
+
post.set_only(params[:post], :title, :body)
|
25
|
+
# admin case
|
26
|
+
post.set_only(params[:post], :title, :body, :deleted)
|
27
|
+
|
28
|
+
In this case, only the <tt>title=</tt> and <tt>body=</tt> methods will be allowed in the mass assignment.
|
29
|
+
|
30
|
+
By default, if an invalid setter method call is attempted, Sequel raises a <tt>Sequel::Error</tt> exception. You can have Sequel silently ignore invalid calls by doing:
|
31
|
+
|
32
|
+
# Global default
|
33
|
+
Sequel::Model.strict_param_setting = false
|
34
|
+
# Class level
|
35
|
+
Post.strict_param_setting = false
|
36
|
+
# Instance level
|
37
|
+
post.strict_param_setting = false
|
38
|
+
|
39
|
+
These mass assignment methods have been around a long time, but starting in Sequel 3.12.0, the +set_fields+ or +update_fields+ methods were added, and these may be a better mass assignment choice for most users.
|
40
|
+
These methods take two arguments, the attributes hash as the first argument, and a single array of valid field names as the second argument:
|
41
|
+
|
42
|
+
post.set_fields(params[:post], [:title, :body])
|
43
|
+
|
44
|
+
+set_fields+ and +update_fields+ differ in implementation from +set_only+ and +update_only+.
|
45
|
+
With +set_only+ and +update_only+, the hash is iterated over and it checks each method call attempt to see if it is valid.
|
46
|
+
With +set_fields+ and +update_fields+, the array is iterated over, and it just looks up the value in the hash and calls the appropriate setter method.
|
47
|
+
|
48
|
+
+set_fields+ and +update_fields+ are designed for the case where you are expecting specific fields in the input, and want to ignore the other fields.
|
49
|
+
They work great for things like HTML forms where the form fields are static.
|
50
|
+
+set_only+ and +update_only+ are designed for cases where you are not sure what fields are going to be present in the input, but still want to make sure only certain setter methods can be called.
|
51
|
+
They work great for flexible APIs.
|
52
|
+
|
53
|
+
In all of the mass assignment cases, methods starting with +set+ will set the attributes without saving the object, while methods starting with +update+ will set the attributes and then save the changes to the object.
|
54
|
+
|
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
|
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
|
@@ -142,7 +145,7 @@ Migrations themselves do not contain any schema modification methods, but they m
|
|
142
145
|
any of the <tt>Sequel::Database</tt> modification methods, of which there are many. The main
|
143
146
|
ones are +create_table+ and +alter_table+, but Sequel also comes with numerous other schema
|
144
147
|
modification methods, most of which are shortcuts for +alter_table+ (all of these methods are
|
145
|
-
described in more detail
|
148
|
+
described in more detail in the {schema modification guide}[link:files/doc/schema_modification_rdoc.html]):
|
146
149
|
|
147
150
|
* add_column
|
148
151
|
* add_index
|
@@ -344,539 +347,12 @@ reverses the changes made by the +up+ block:
|
|
344
347
|
down{...}
|
345
348
|
end
|
346
349
|
|
347
|
-
|
348
|
-
|
349
|
-
Inside your migration's down and +up+ blocks is where you will call the +Database+ schema modification methods.
|
350
|
-
Here's a brief description of the most common schema modification methods:
|
351
|
-
|
352
|
-
=== +create_table+
|
353
|
-
|
354
|
-
+create_table+ is the most common schema modification method, and it's used for adding new tables
|
355
|
-
to the schema. You provide it with the name of the table as a symbol, as well a block:
|
356
|
-
|
357
|
-
create_table(:artists) do
|
358
|
-
primary_key :id
|
359
|
-
String :name
|
360
|
-
end
|
361
|
-
|
362
|
-
Not that if you want a primary key for the table, you need to specify it, Sequel does not create one
|
363
|
-
by default.
|
364
|
-
|
365
|
-
==== Column types
|
366
|
-
|
367
|
-
Most method calls inside the create_table block will create columns, since +method_missing+ calls +column+
|
368
|
-
Columns are generally created by specifying the column type as the method
|
369
|
-
name, followed by the column name symbol to use, and after that any options that should be used.
|
370
|
-
If the method is a ruby class name that Sequel recognizes, Sequel will transform it into the appropriate
|
371
|
-
type for the given database. So while you specified +String+, Sequel will actually use +varchar+ or
|
372
|
-
+text+ depending on the underlying database. Here's a list of all of ruby classes that Sequel will
|
373
|
-
convert to database types:
|
374
|
-
|
375
|
-
create_table(:columns_types) do # common database type used
|
376
|
-
Integer :a0 # integer
|
377
|
-
String :a1 # varchar(255)
|
378
|
-
String :a2, :size=>50 # varchar(50)
|
379
|
-
String :a3, :fixed=>true # char(255)
|
380
|
-
String :a4, :fixed=>true, :size=>50 # char(50)
|
381
|
-
String :a5, :text=>true # text
|
382
|
-
File :b, # blob
|
383
|
-
Fixnum :c # integer
|
384
|
-
Bignum :d # bigint
|
385
|
-
Float :e # double precision
|
386
|
-
BigDecimal :f # numeric
|
387
|
-
BigDecimal :f2, :size=>10 # numeric(10)
|
388
|
-
BigDecimal :f3, :size=>[10, 2] # numeric(10, 2)
|
389
|
-
Date :g # date
|
390
|
-
DateTime :h # timestamp
|
391
|
-
Time :i # timestamp
|
392
|
-
Time :i2, :only_time=>true # time
|
393
|
-
Numeric :j # numeric
|
394
|
-
TrueClass :k # boolean
|
395
|
-
FalseClass :l # boolean
|
396
|
-
end
|
397
|
-
|
398
|
-
Note that in addition to the ruby class name, Sequel also pays attention to the column options when
|
399
|
-
determining which database type to use.
|
400
|
-
|
401
|
-
Also note that this conversion is only done if you use a supported ruby class name. In all other
|
402
|
-
cases, Sequel uses the type specified verbatim:
|
403
|
-
|
404
|
-
create_table(:columns_types) do # database type used
|
405
|
-
string :a1 # string
|
406
|
-
datetime :a2 # datetime
|
407
|
-
blob :a3 # blob
|
408
|
-
inet :a4 # inet
|
409
|
-
end
|
410
|
-
|
411
|
-
In addition to specifying the types as methods, you can use the +column+ method and specify the types
|
412
|
-
as the second argument, either as ruby classes, symbols, or strings:
|
413
|
-
|
414
|
-
create_table(:columns_types) do # database type used
|
415
|
-
column :a1, :string # string
|
416
|
-
column :a2, String # varchar(255)
|
417
|
-
column :a3, 'string' # string
|
418
|
-
column :a4, :datetime # datetime
|
419
|
-
column :a5, DateTime # timestamp
|
420
|
-
column :a6, 'timestamp(6)' # timestamp(6)
|
421
|
-
end
|
422
|
-
|
423
|
-
==== Column options
|
424
|
-
|
425
|
-
When using the type name as method, the third argument is an options hash, and when using the +column+
|
426
|
-
method, the fourth argument is the options hash. The following options are supported:
|
427
|
-
|
428
|
-
:default :: The default value for the column.
|
429
|
-
:index :: Create an index on this column.
|
430
|
-
:null :: Mark the column as allowing NULL values (if true),
|
431
|
-
or not allowing NULL values (if false). If unspecified, will default
|
432
|
-
to whatever the database default is.
|
433
|
-
:size :: The size of the column, generally used with string
|
434
|
-
columns to specify the maximum number of characters the column will hold.
|
435
|
-
An array of two integers can be provided to set the size and the
|
436
|
-
precision, respectively, of decimal columns.
|
437
|
-
:unique :: Mark the column as unique, generally has the same effect as
|
438
|
-
creating a unique index on the column.
|
439
|
-
:unsigned :: Make the column type unsigned, only useful for integer
|
440
|
-
columns.
|
441
|
-
|
442
|
-
==== Other methods
|
443
|
-
|
444
|
-
In addition to the +column+ method and other methods that create columns, there are a other methods that can be used:
|
445
|
-
|
446
|
-
==== +primary_key+
|
447
|
-
|
448
|
-
You've seen this one used already. It's used to create an autoincrementing integer primary key column.
|
449
|
-
|
450
|
-
create_table(:a0){primary_key :id}
|
451
|
-
|
452
|
-
If you want to create a primary key column that doesn't use an autoincrementing integer, you should
|
453
|
-
not use this method. Instead, you should use the :primary_key option to the +column+ method or type
|
454
|
-
method:
|
455
|
-
|
456
|
-
create_table(:a1){Integer :id, :primary_key=>true} # Non autoincrementing integer primary key
|
457
|
-
create_table(:a2){String :name, :primary_key=>true} # varchar(255) primary key
|
458
|
-
|
459
|
-
If you want to create a composite primary key, you should call the +primary_key+ method with an
|
460
|
-
array of column symbols:
|
461
|
-
|
462
|
-
create_table(:items) do
|
463
|
-
Integer :group_id
|
464
|
-
Integer :position
|
465
|
-
primary_key [:group_id, :position]
|
466
|
-
end
|
467
|
-
|
468
|
-
If provided with an array, +primary_key+ does not create a column, it just sets up the primary key constraint.
|
469
|
-
|
470
|
-
==== +foreign_key+
|
471
|
-
|
472
|
-
+foreign_key+ is used to create a foreign key column that references a column in another table (or the same table).
|
473
|
-
It takes the column name as the first argument, the table it references as the second argument, and an options hash
|
474
|
-
as it's third argument. A simple example is:
|
475
|
-
|
476
|
-
create_table(:albums) do
|
477
|
-
primary_key :id
|
478
|
-
foreign_key :artist_id, :artists
|
479
|
-
String :name
|
480
|
-
end
|
481
|
-
|
482
|
-
+foreign_key+ accepts some specific options:
|
483
|
-
|
484
|
-
:deferrable :: Makes the foreign key constraint checks deferrable, so they aren't checked
|
485
|
-
until the end of the transaction.
|
486
|
-
:key :: For foreign key columns, the column in the associated table
|
487
|
-
that this column references. Unnecessary if this column
|
488
|
-
references the primary key of the associated table, at least
|
489
|
-
on most databases.
|
490
|
-
:on_delete :: Specify the behavior of this foreign key column when the row with the primary key
|
491
|
-
it references is deleted , can be :restrict, :cascade, :set_null, or :set_default.
|
492
|
-
:on_update :: Specify the behavior of this foreign key column when the row with the primary key
|
493
|
-
it references modifies the value of the primary key, can be
|
494
|
-
:restrict, :cascade, :set_null, or :set_default.
|
495
|
-
|
496
|
-
Like +primary_key+, if you provide +foreign_key+ with an array of symbols, it will not create a
|
497
|
-
column, but create a foreign key constraint:
|
498
|
-
|
499
|
-
create_table(:artists) do
|
500
|
-
String :name
|
501
|
-
String :location
|
502
|
-
primary_key [:name, :location]
|
503
|
-
end
|
504
|
-
create_table(:albums) do
|
505
|
-
String :artist_name
|
506
|
-
String :artist_location
|
507
|
-
String :name
|
508
|
-
foreign_key [:artist_name, :artist_location], :artists
|
509
|
-
end
|
510
|
-
|
511
|
-
==== +index+
|
512
|
-
|
513
|
-
+index+ creates indexes on the table. For single columns, calling index is the same as using the
|
514
|
-
<tt>:index</tt> option when creating the column:
|
515
|
-
|
516
|
-
create_table(:a){Integer :id, :index=>true}
|
517
|
-
# Same as:
|
518
|
-
create_table(:a) do
|
519
|
-
Integer :id
|
520
|
-
index :id
|
521
|
-
end
|
522
|
-
|
523
|
-
Similar to the +primary_key+ and +foreign_key+ methods, calling +index+ with an array of symbols
|
524
|
-
will create a multiple column index:
|
525
|
-
|
526
|
-
create_table(:albums) do
|
527
|
-
primary_key :id
|
528
|
-
foreign_key :artist_id, :artists
|
529
|
-
Integer :position
|
530
|
-
index [:artist_id, :position]
|
531
|
-
end
|
532
|
-
|
533
|
-
The +index+ method also accepts some options:
|
534
|
-
|
535
|
-
:name :: The name of the index (generated based on the table and column names if not provided).
|
536
|
-
:type :: The type of index to use (only supported by some databases)
|
537
|
-
:unique :: Make the index unique, so duplicate values are not allowed.
|
538
|
-
:where :: Create a partial index (only supported by some databases)
|
539
|
-
|
540
|
-
==== +unique+
|
541
|
-
|
542
|
-
The +unique+ method creates a unique constraint on the table. A unique constraint generally
|
543
|
-
operates identically to a unique index, so the following three +create_table+ blocks are
|
544
|
-
pretty much identical:
|
545
|
-
|
546
|
-
create_table(:a){Integer :a, :unique=>true}
|
547
|
-
|
548
|
-
create_table(:a) do
|
549
|
-
Integer :a
|
550
|
-
index :a, :unique=>true
|
551
|
-
end
|
552
|
-
|
553
|
-
create_table(:a) do
|
554
|
-
Integer :a
|
555
|
-
unique :a
|
556
|
-
end
|
557
|
-
|
558
|
-
Just like +index+, +unique+ can set up a multiple column unique constraint, where the
|
559
|
-
combination of the columns must be unique:
|
560
|
-
|
561
|
-
create_table(:a) do
|
562
|
-
Integer :a
|
563
|
-
Integer :b
|
564
|
-
unique [:a, :b]
|
565
|
-
end
|
566
|
-
|
567
|
-
==== +full_text_index+ and +spatial_index+
|
568
|
-
|
569
|
-
Both of these create specialized index types supported by some databases. They
|
570
|
-
both take the same options as +index+.
|
571
|
-
|
572
|
-
==== +constraint+
|
573
|
-
|
574
|
-
+constraint+ creates a named table constraint:
|
575
|
-
|
576
|
-
create_table(:artists) do
|
577
|
-
primary_key :id
|
578
|
-
String :name
|
579
|
-
constraint(:name_min_length){char_length(name) > 2}
|
580
|
-
end
|
581
|
-
|
582
|
-
Instead of using a block, you can use arguments that will be handled similarly
|
583
|
-
to <tt>Dataset#filter</tt>:
|
584
|
-
|
585
|
-
create_table(:artists) do
|
586
|
-
primary_key :id
|
587
|
-
String :name
|
588
|
-
constraint(:name_length_range, :char_length.sql_function(:name)=>3..50)
|
589
|
-
end
|
590
|
-
|
591
|
-
==== +check+
|
592
|
-
|
593
|
-
+check+ operates just like +constraint+, except that it doesn't take a name
|
594
|
-
and it creates an unnamed constraint
|
595
|
-
|
596
|
-
create_table(:artists) do
|
597
|
-
primary_key :id
|
598
|
-
String :name
|
599
|
-
check{char_length(name) > 2}
|
600
|
-
end
|
601
|
-
|
602
|
-
=== +alter_table+
|
603
|
-
|
604
|
-
+alter_table+ is used to alter existing tables, changing their columns, indexes,
|
605
|
-
or constraints. It it used just like +create_table+, accepting a block which
|
606
|
-
is instance_evaled, and providing its own methods:
|
607
|
-
|
608
|
-
==== +add_column+
|
609
|
-
|
610
|
-
One of the most common methods, +add_column+ is used to add a column to the table.
|
611
|
-
Its API is similar to that of +create_table+'s +column+ method, where the first
|
612
|
-
argument is the column name, the second is the type, and the third is an options
|
613
|
-
hash:
|
614
|
-
|
615
|
-
alter_table(:albums) do
|
616
|
-
add_column :copies_sold, Integer, :default=>0
|
617
|
-
end
|
618
|
-
|
619
|
-
When adding a column, it's a good idea to provide a default value, unless you
|
620
|
-
want the value for all rows to be set to NULL.
|
621
|
-
|
622
|
-
==== +drop_column+
|
623
|
-
|
624
|
-
As you may expect, +drop_column+ takes a column name and drops the column. It's
|
625
|
-
often used in the +down+ block of a migration to drop a column added in an +up+ block:
|
626
|
-
|
627
|
-
alter_table(:albums) do
|
628
|
-
drop_column :copies_sold
|
629
|
-
end
|
630
|
-
|
631
|
-
==== +rename_column+
|
632
|
-
|
633
|
-
+rename_column+ is used to rename a column. It takes the old column name as the first
|
634
|
-
argument, and the new column name as the second argument:
|
635
|
-
|
636
|
-
alter_table(:albums) do
|
637
|
-
rename_column :copies_sold, :total_sales
|
638
|
-
end
|
639
|
-
|
640
|
-
==== +add_primary_key+
|
641
|
-
|
642
|
-
If you forgot to include a primary key on the table, and want to add one later, you
|
643
|
-
can use +add_primary_key+. A common use of this is to make many_to_many association
|
644
|
-
join tables into real models:
|
645
|
-
|
646
|
-
alter_table(:albums_artists) do
|
647
|
-
add_primary_key :id
|
648
|
-
end
|
649
|
-
|
650
|
-
Just like +create_table+'s +primary_key+ method, if you provide an array of symbols,
|
651
|
-
Sequel will not add a column, but will add a composite primary key constraint:
|
652
|
-
|
653
|
-
alter_table(:albums_artists) do
|
654
|
-
add_primary_key [:album_id, :artist_id]
|
655
|
-
end
|
656
|
-
|
657
|
-
If you just want to take an existing single column and make it a primary key, call
|
658
|
-
+add_primary_key+ with an array with a single symbol:
|
659
|
-
|
660
|
-
alter_table(:artists) do
|
661
|
-
add_primary_key [:id]
|
662
|
-
end
|
350
|
+
or they should use the reversible migrations feature with a +change+ block:
|
663
351
|
|
664
|
-
|
665
|
-
|
666
|
-
+add_foreign_key+ can be used to add a new foreign key column or constraint to a table.
|
667
|
-
Like +add_primary_key+, if you provide it with a symbol as the first argument, it
|
668
|
-
creates a new column:
|
669
|
-
|
670
|
-
alter_table(:albums) do
|
671
|
-
add_foreign_key :artist_id, :artists
|
672
|
-
end
|
673
|
-
|
674
|
-
If you want to add a new foreign key constraint to an existing column, you provide an
|
675
|
-
array with a single element:
|
676
|
-
|
677
|
-
alter_table(:albums) do
|
678
|
-
add_foreign_key [:artist_id], :artists
|
679
|
-
end
|
680
|
-
|
681
|
-
To set up a multiple column foreign key constraint, use an array with multiple column
|
682
|
-
symbols:
|
683
|
-
|
684
|
-
alter_table(:albums) do
|
685
|
-
add_foreign_key [:artist_name, :artist_location], :artists
|
686
|
-
end
|
687
|
-
|
688
|
-
==== +add_index+
|
689
|
-
|
690
|
-
+add_index+ works just like +create_table+'s +index+ method, creating a new index on
|
691
|
-
the table:
|
692
|
-
|
693
|
-
alter_table(:albums) do
|
694
|
-
add_index :artist_id
|
695
|
-
end
|
696
|
-
|
697
|
-
It accepts the same options as +create_table+'s +index+ method, and you can set up
|
698
|
-
a multiple column index using an array:
|
699
|
-
|
700
|
-
alter_table(:albums_artists) do
|
701
|
-
add_index [:album_id, :artist_id], :unique=>true
|
702
|
-
end
|
703
|
-
|
704
|
-
==== +drop_index+
|
705
|
-
|
706
|
-
As you may expect, +drop_index+ drops an existing index:
|
707
|
-
|
708
|
-
alter_table(:albums) do
|
709
|
-
drop_index :artist_id
|
710
|
-
end
|
711
|
-
|
712
|
-
Just like +drop_column+, it is often used in the +down+ block of a migration.
|
713
|
-
|
714
|
-
==== +add_full_text_index+, +add_spatial_index+
|
715
|
-
|
716
|
-
Corresponding to +create_table+'s +full_text_index+ and +spatial_index+ methods,
|
717
|
-
these two methods create new indexes on the table.
|
718
|
-
|
719
|
-
==== +add_constraint+
|
720
|
-
|
721
|
-
This adds a named constraint to the table, similar to +create_table+'s +constraint+
|
722
|
-
method:
|
723
|
-
|
724
|
-
alter_table(:albums) do
|
725
|
-
add_constraint(:name_min_length){char_length(name) > 2}
|
726
|
-
end
|
727
|
-
|
728
|
-
There is no method to add an unnamed constraint, but you can pass nil as the first
|
729
|
-
argument of +add_constraint+ to do so. However, it's not recommend to do that
|
730
|
-
as it is difficult to drop such a constraint.
|
731
|
-
|
732
|
-
==== +add_unique_constraint+
|
733
|
-
|
734
|
-
This adds a unique constraint to the table, similar to +create_table+'s +unique+
|
735
|
-
method. This usually has the same effect as adding a unique index.
|
736
|
-
|
737
|
-
alter_table(:albums) do
|
738
|
-
add_unique_constraint [:artist_id, :name]
|
739
|
-
end
|
740
|
-
|
741
|
-
==== +drop_constraint+
|
742
|
-
|
743
|
-
This method drops an existing named constraint:
|
744
|
-
|
745
|
-
alter_table(:albums) do
|
746
|
-
drop_constraint(:name_min_length)
|
747
|
-
end
|
748
|
-
|
749
|
-
There is no database independent method to drop an unnamed constraint. Generally, the
|
750
|
-
database will give it a name automatically, and you will have to figure out what it is.
|
751
|
-
For that reason, you should not add unnamed constraints that you ever might need to remove.
|
752
|
-
|
753
|
-
==== +set_column_default+
|
754
|
-
|
755
|
-
This modifies the default value of a column:
|
756
|
-
|
757
|
-
alter_table(:albums) do
|
758
|
-
set_column_default :copies_sold, 0
|
759
|
-
end
|
760
|
-
|
761
|
-
==== +set_column_type+
|
762
|
-
|
763
|
-
This modifies a column's type. Most databases will attempt to convert existing values in
|
764
|
-
the columns to the new type:
|
765
|
-
|
766
|
-
alter_table(:albums) do
|
767
|
-
set_column_type :copies_sold, Bignum
|
768
|
-
end
|
769
|
-
|
770
|
-
You can specify the type as a string or symbol, in which case it is used verbatim, or as a supported
|
771
|
-
ruby class, in which case it gets converted to an appropriate database type.
|
772
|
-
|
773
|
-
==== +set_column_allow_null+
|
774
|
-
|
775
|
-
This changes the NULL or NOT NULL setting of a column:
|
776
|
-
|
777
|
-
alter_table(:albums) do
|
778
|
-
set_column_allow_null :artist_id, true # NULL
|
779
|
-
set_column_allow_null :copies_sold, false # NOT NULL
|
780
|
-
end
|
781
|
-
|
782
|
-
=== Other +Database+ schema modification methods
|
783
|
-
|
784
|
-
<tt>Sequel::Database</tt> has many schema modification instance methods,
|
785
|
-
most of which are shortcuts to the same methods in +alter_table+. The
|
786
|
-
following +Database+ instance methods just call +alter_table+ with a
|
787
|
-
block that calls the method with the same name inside the +alter_table+
|
788
|
-
block with all arguments after the first argument (which is used as
|
789
|
-
the table name):
|
790
|
-
|
791
|
-
* +add_column+
|
792
|
-
* +drop_column+
|
793
|
-
* +rename_column+
|
794
|
-
* +add_index+
|
795
|
-
* +drop_index+
|
796
|
-
* +set_column_default+
|
797
|
-
* +set_column_type+
|
798
|
-
|
799
|
-
For example, the following two method calls do the same thing:
|
800
|
-
|
801
|
-
alter_table(:artists){add_column :copies_sold, Integer}
|
802
|
-
add_column :artists, :copies_sold, Integer
|
803
|
-
|
804
|
-
There are some other schema modification methods that have no +alter_table+
|
805
|
-
counterpart:
|
806
|
-
|
807
|
-
==== +drop_table+
|
808
|
-
|
809
|
-
+drop_table+ takes multiple arguments and treats all arguments as a
|
810
|
-
table name to drop:
|
811
|
-
|
812
|
-
drop_table(:albums_artists, :albums, :artists)
|
813
|
-
|
814
|
-
Note that when dropping tables, you may need to drop them in a specific order
|
815
|
-
if you are using foreign keys and the database is enforcing referential
|
816
|
-
integrity. In general, you need to drop the tables containing the foreign
|
817
|
-
keys before the tables containing the primary keys they reference.
|
818
|
-
|
819
|
-
==== +rename_table+
|
820
|
-
|
821
|
-
You can rename an existing table using +rename_table+. Like +rename_column+,
|
822
|
-
the first argument is the current name, and the second is the new name:
|
823
|
-
|
824
|
-
rename_table(:artist, :artists)
|
825
|
-
|
826
|
-
==== <tt>create_table!</tt>
|
827
|
-
|
828
|
-
<tt>create_table!</tt> with the bang drops the table unconditionally (swallowing
|
829
|
-
any errors) before attempting to create it, so:
|
830
|
-
|
831
|
-
create_table!(:artists)
|
832
|
-
primary_key :id
|
833
|
-
end
|
834
|
-
|
835
|
-
is the same as:
|
836
|
-
|
837
|
-
drop_table(:artists) rescue nil
|
838
|
-
create_table(:artists)
|
839
|
-
primary_key :id
|
840
|
-
end
|
841
|
-
|
842
|
-
It should not be used inside migrations, as if the table does not exist, it may
|
843
|
-
mess up the migration.
|
844
|
-
|
845
|
-
==== <tt>create_table?</tt>
|
846
|
-
|
847
|
-
<tt>create_table?</tt> with a question mark only creates the table if it does
|
848
|
-
not already exist, so:
|
849
|
-
|
850
|
-
create_table!(:artists)
|
851
|
-
primary_key :id
|
352
|
+
Sequel.migration do
|
353
|
+
change{...}
|
852
354
|
end
|
853
355
|
|
854
|
-
is the same as:
|
855
|
-
|
856
|
-
create_table(:artists)
|
857
|
-
primary_key :id
|
858
|
-
end unless table_exists?(:artists)
|
859
|
-
|
860
|
-
Like <tt>create_table!</tt>, it should not be used inside migrations.
|
861
|
-
|
862
|
-
==== +create_view+ and +create_or_replace_view+
|
863
|
-
|
864
|
-
These can be used to create views. The difference between them is that
|
865
|
-
+create_or_replace_view+ will unconditionally replace an existing view of
|
866
|
-
the same name, while +create_view+ will probably raise an error. Both methods
|
867
|
-
take the name as the first argument, and either an string or a dataset as the
|
868
|
-
second argument:
|
869
|
-
|
870
|
-
create_view(:gold_albums, DB[:albums].filter{copies_sold > 500000})
|
871
|
-
create_or_replace_view(:gold_albums, "SELECT * FROM albums WHERE copies_sold > 500000")
|
872
|
-
|
873
|
-
==== +drop_view+
|
874
|
-
|
875
|
-
+drop_view+ drops existing views. Just like +drop_table+, it can accept multiple
|
876
|
-
arguments:
|
877
|
-
|
878
|
-
drop_view(:gold_albums, :platinum_albums)
|
879
|
-
|
880
356
|
== What to put in your migration's +down+ block
|
881
357
|
|
882
358
|
It's usually easy to determine what you should put in your migration's +up+ block,
|