sequel 3.21.0 → 3.36.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +855 -3
- data/MIT-LICENSE +1 -1
- data/README.rdoc +23 -7
- data/Rakefile +42 -24
- data/bin/sequel +26 -8
- data/doc/active_record.rdoc +1 -0
- data/doc/advanced_associations.rdoc +171 -11
- data/doc/association_basics.rdoc +293 -43
- data/doc/dataset_basics.rdoc +5 -5
- data/doc/dataset_filtering.rdoc +8 -0
- data/doc/mass_assignment.rdoc +56 -0
- data/doc/migration.rdoc +85 -552
- data/doc/model_hooks.rdoc +78 -27
- data/doc/object_model.rdoc +541 -0
- data/doc/opening_databases.rdoc +97 -50
- data/doc/prepared_statements.rdoc +40 -14
- data/doc/querying.rdoc +28 -4
- data/doc/reflection.rdoc +30 -4
- data/doc/release_notes/3.22.0.txt +39 -0
- data/doc/release_notes/3.23.0.txt +172 -0
- data/doc/release_notes/3.24.0.txt +420 -0
- data/doc/release_notes/3.25.0.txt +88 -0
- data/doc/release_notes/3.26.0.txt +88 -0
- data/doc/release_notes/3.27.0.txt +82 -0
- data/doc/release_notes/3.28.0.txt +304 -0
- data/doc/release_notes/3.29.0.txt +459 -0
- data/doc/release_notes/3.30.0.txt +135 -0
- data/doc/release_notes/3.31.0.txt +146 -0
- data/doc/release_notes/3.32.0.txt +202 -0
- data/doc/release_notes/3.33.0.txt +157 -0
- data/doc/release_notes/3.34.0.txt +671 -0
- data/doc/release_notes/3.35.0.txt +144 -0
- data/doc/release_notes/3.36.0.txt +245 -0
- data/doc/schema_modification.rdoc +572 -0
- data/doc/sharding.rdoc +95 -3
- data/doc/testing.rdoc +129 -0
- data/doc/thread_safety.rdoc +17 -0
- data/doc/transactions.rdoc +137 -0
- data/doc/virtual_rows.rdoc +51 -2
- data/lib/sequel/adapters/ado/mssql.rb +18 -6
- data/lib/sequel/adapters/ado.rb +40 -18
- data/lib/sequel/adapters/amalgalite.rb +17 -9
- data/lib/sequel/adapters/db2.rb +175 -86
- data/lib/sequel/adapters/dbi.rb +15 -15
- data/lib/sequel/adapters/do/mysql.rb +17 -11
- data/lib/sequel/adapters/do/postgres.rb +1 -57
- data/lib/sequel/adapters/do/sqlite.rb +0 -10
- data/lib/sequel/adapters/do.rb +18 -36
- data/lib/sequel/adapters/firebird.rb +27 -208
- data/lib/sequel/adapters/ibmdb.rb +453 -0
- data/lib/sequel/adapters/informix.rb +6 -23
- data/lib/sequel/adapters/jdbc/as400.rb +16 -34
- data/lib/sequel/adapters/jdbc/db2.rb +47 -0
- data/lib/sequel/adapters/jdbc/derby.rb +287 -0
- data/lib/sequel/adapters/jdbc/firebird.rb +24 -0
- data/lib/sequel/adapters/jdbc/h2.rb +73 -31
- data/lib/sequel/adapters/jdbc/hsqldb.rb +184 -0
- data/lib/sequel/adapters/jdbc/informix.rb +21 -0
- data/lib/sequel/adapters/jdbc/jtds.rb +29 -0
- data/lib/sequel/adapters/jdbc/mssql.rb +3 -33
- data/lib/sequel/adapters/jdbc/mysql.rb +7 -24
- data/lib/sequel/adapters/jdbc/oracle.rb +88 -25
- data/lib/sequel/adapters/jdbc/postgresql.rb +58 -48
- data/lib/sequel/adapters/jdbc/progress.rb +21 -0
- data/lib/sequel/adapters/jdbc/sqlite.rb +20 -10
- data/lib/sequel/adapters/jdbc/sqlserver.rb +66 -0
- data/lib/sequel/adapters/jdbc/transactions.rb +83 -0
- data/lib/sequel/adapters/jdbc.rb +265 -94
- data/lib/sequel/adapters/mock.rb +361 -0
- data/lib/sequel/adapters/mysql.rb +118 -222
- data/lib/sequel/adapters/mysql2.rb +58 -57
- data/lib/sequel/adapters/odbc/mssql.rb +15 -7
- data/lib/sequel/adapters/odbc.rb +28 -9
- data/lib/sequel/adapters/openbase.rb +3 -5
- data/lib/sequel/adapters/oracle.rb +349 -53
- data/lib/sequel/adapters/postgres.rb +356 -107
- data/lib/sequel/adapters/shared/access.rb +32 -9
- data/lib/sequel/adapters/shared/db2.rb +342 -0
- data/lib/sequel/adapters/shared/firebird.rb +221 -0
- data/lib/sequel/adapters/shared/informix.rb +53 -0
- data/lib/sequel/adapters/shared/mssql.rb +335 -118
- data/lib/sequel/adapters/shared/mysql.rb +340 -76
- data/lib/sequel/adapters/shared/mysql_prepared_statements.rb +155 -0
- data/lib/sequel/adapters/shared/oracle.rb +243 -45
- data/lib/sequel/adapters/shared/postgres.rb +524 -279
- data/lib/sequel/adapters/shared/progress.rb +5 -8
- data/lib/sequel/adapters/shared/sqlite.rb +265 -89
- data/lib/sequel/adapters/sqlite.rb +101 -39
- data/lib/sequel/adapters/swift/mysql.rb +9 -12
- data/lib/sequel/adapters/swift/postgres.rb +9 -74
- data/lib/sequel/adapters/swift/sqlite.rb +8 -6
- data/lib/sequel/adapters/swift.rb +8 -7
- data/lib/sequel/adapters/tinytds.rb +161 -21
- data/lib/sequel/adapters/utils/emulate_offset_with_row_number.rb +66 -0
- data/lib/sequel/adapters/utils/stored_procedures.rb +1 -11
- data/lib/sequel/ast_transformer.rb +194 -0
- data/lib/sequel/connection_pool/sharded_single.rb +11 -1
- data/lib/sequel/connection_pool/sharded_threaded.rb +28 -2
- data/lib/sequel/connection_pool/single.rb +5 -0
- data/lib/sequel/connection_pool/threaded.rb +38 -14
- data/lib/sequel/connection_pool.rb +10 -2
- data/lib/sequel/core.rb +118 -11
- data/lib/sequel/database/connecting.rb +38 -28
- data/lib/sequel/database/dataset.rb +4 -4
- data/lib/sequel/database/dataset_defaults.rb +63 -2
- data/lib/sequel/database/logging.rb +7 -2
- data/lib/sequel/database/misc.rb +150 -46
- data/lib/sequel/database/query.rb +215 -65
- data/lib/sequel/database/schema_generator.rb +64 -20
- data/lib/sequel/database/schema_methods.rb +212 -54
- data/lib/sequel/dataset/actions.rb +322 -85
- data/lib/sequel/dataset/features.rb +96 -8
- data/lib/sequel/dataset/graph.rb +38 -11
- data/lib/sequel/dataset/misc.rb +50 -23
- data/lib/sequel/dataset/mutation.rb +12 -7
- data/lib/sequel/dataset/prepared_statements.rb +68 -30
- data/lib/sequel/dataset/query.rb +237 -44
- data/lib/sequel/dataset/sql.rb +698 -278
- data/lib/sequel/dataset.rb +8 -0
- data/lib/sequel/exceptions.rb +4 -0
- data/lib/sequel/extensions/_pretty_table.rb +79 -0
- data/lib/sequel/extensions/arbitrary_servers.rb +108 -0
- data/lib/sequel/extensions/columns_introspection.rb +61 -0
- data/lib/sequel/{core_sql.rb → extensions/core_extensions.rb} +16 -34
- data/lib/sequel/extensions/migration.rb +97 -11
- data/lib/sequel/extensions/named_timezones.rb +5 -0
- data/lib/sequel/extensions/null_dataset.rb +90 -0
- data/lib/sequel/extensions/pg_array.rb +460 -0
- data/lib/sequel/extensions/pg_array_ops.rb +220 -0
- data/lib/sequel/extensions/pg_auto_parameterize.rb +169 -0
- data/lib/sequel/extensions/pg_hstore.rb +296 -0
- data/lib/sequel/extensions/pg_hstore_ops.rb +259 -0
- data/lib/sequel/extensions/pg_inet.rb +89 -0
- data/lib/sequel/extensions/pg_json.rb +178 -0
- data/lib/sequel/extensions/pg_statement_cache.rb +316 -0
- data/lib/sequel/extensions/pretty_table.rb +5 -71
- data/lib/sequel/extensions/query.rb +1 -1
- data/lib/sequel/extensions/query_literals.rb +79 -0
- data/lib/sequel/extensions/schema_caching.rb +76 -0
- data/lib/sequel/extensions/schema_dumper.rb +251 -37
- data/lib/sequel/extensions/select_remove.rb +35 -0
- data/lib/sequel/extensions/server_block.rb +139 -0
- data/lib/sequel/extensions/sql_expr.rb +4 -110
- data/lib/sequel/extensions/thread_local_timezones.rb +1 -1
- data/lib/sequel/extensions/to_dot.rb +95 -83
- data/lib/sequel/model/associations.rb +1187 -393
- data/lib/sequel/model/base.rb +578 -170
- data/lib/sequel/model/errors.rb +1 -1
- data/lib/sequel/model/exceptions.rb +5 -1
- data/lib/sequel/model/inflections.rb +1 -1
- data/lib/sequel/model.rb +30 -11
- data/lib/sequel/no_core_ext.rb +2 -0
- data/lib/sequel/plugins/active_model.rb +13 -1
- data/lib/sequel/plugins/association_pks.rb +22 -4
- data/lib/sequel/plugins/caching.rb +25 -18
- data/lib/sequel/plugins/class_table_inheritance.rb +4 -4
- data/lib/sequel/plugins/composition.rb +44 -12
- data/lib/sequel/plugins/dataset_associations.rb +100 -0
- data/lib/sequel/plugins/defaults_setter.rb +58 -0
- data/lib/sequel/plugins/dirty.rb +214 -0
- data/lib/sequel/plugins/eager_each.rb +59 -0
- data/lib/sequel/plugins/force_encoding.rb +6 -6
- data/lib/sequel/plugins/hook_class_methods.rb +1 -1
- data/lib/sequel/plugins/identity_map.rb +123 -8
- data/lib/sequel/plugins/instance_filters.rb +10 -0
- data/lib/sequel/plugins/instance_hooks.rb +1 -1
- data/lib/sequel/plugins/json_serializer.rb +58 -6
- data/lib/sequel/plugins/list.rb +12 -2
- data/lib/sequel/plugins/many_through_many.rb +79 -28
- data/lib/sequel/plugins/many_to_one_pk_lookup.rb +71 -0
- data/lib/sequel/plugins/nested_attributes.rb +139 -63
- data/lib/sequel/plugins/optimistic_locking.rb +8 -0
- data/lib/sequel/plugins/prepared_statements.rb +167 -0
- data/lib/sequel/plugins/prepared_statements_associations.rb +87 -0
- data/lib/sequel/plugins/prepared_statements_safe.rb +82 -0
- data/lib/sequel/plugins/prepared_statements_with_pk.rb +59 -0
- data/lib/sequel/plugins/rcte_tree.rb +29 -15
- data/lib/sequel/plugins/schema.rb +7 -2
- data/lib/sequel/plugins/serialization.rb +98 -49
- data/lib/sequel/plugins/serialization_modification_detection.rb +63 -0
- data/lib/sequel/plugins/sharding.rb +21 -54
- data/lib/sequel/plugins/single_table_inheritance.rb +5 -3
- data/lib/sequel/plugins/static_cache.rb +99 -0
- data/lib/sequel/plugins/subclasses.rb +10 -2
- data/lib/sequel/plugins/tactical_eager_loading.rb +7 -7
- data/lib/sequel/plugins/timestamps.rb +1 -1
- data/lib/sequel/plugins/typecast_on_load.rb +9 -12
- data/lib/sequel/plugins/update_primary_key.rb +2 -2
- data/lib/sequel/plugins/validation_class_methods.rb +1 -1
- data/lib/sequel/plugins/xml_serializer.rb +15 -4
- data/lib/sequel/sql.rb +526 -59
- data/lib/sequel/timezones.rb +57 -38
- data/lib/sequel/version.rb +1 -1
- data/spec/adapters/db2_spec.rb +146 -0
- data/spec/adapters/firebird_spec.rb +1 -1
- data/spec/adapters/mssql_spec.rb +142 -40
- data/spec/adapters/mysql_spec.rb +380 -234
- data/spec/adapters/oracle_spec.rb +67 -77
- data/spec/adapters/postgres_spec.rb +1050 -212
- data/spec/adapters/spec_helper.rb +8 -5
- data/spec/adapters/sqlite_spec.rb +139 -27
- data/spec/core/connection_pool_spec.rb +196 -93
- data/spec/core/database_spec.rb +879 -510
- data/spec/core/dataset_spec.rb +1415 -1086
- data/spec/core/expression_filters_spec.rb +554 -109
- data/spec/core/mock_adapter_spec.rb +449 -0
- data/spec/core/object_graph_spec.rb +86 -114
- data/spec/core/schema_generator_spec.rb +4 -4
- data/spec/core/schema_spec.rb +482 -38
- data/spec/core/spec_helper.rb +6 -48
- data/spec/extensions/active_model_spec.rb +13 -0
- data/spec/extensions/arbitrary_servers_spec.rb +110 -0
- data/spec/extensions/association_autoreloading_spec.rb +18 -10
- data/spec/extensions/association_dependencies_spec.rb +15 -25
- data/spec/extensions/association_pks_spec.rb +66 -32
- data/spec/extensions/association_proxies_spec.rb +4 -4
- data/spec/extensions/caching_spec.rb +47 -51
- data/spec/extensions/class_table_inheritance_spec.rb +31 -83
- data/spec/extensions/columns_introspection_spec.rb +91 -0
- data/spec/extensions/composition_spec.rb +18 -13
- data/spec/{core/core_sql_spec.rb → extensions/core_extensions_spec.rb} +119 -139
- data/spec/extensions/dataset_associations_spec.rb +199 -0
- data/spec/extensions/defaults_setter_spec.rb +64 -0
- data/spec/extensions/dirty_spec.rb +155 -0
- data/spec/extensions/eager_each_spec.rb +34 -0
- data/spec/extensions/force_encoding_spec.rb +4 -2
- data/spec/extensions/hook_class_methods_spec.rb +69 -92
- data/spec/extensions/identity_map_spec.rb +131 -22
- data/spec/extensions/inflector_spec.rb +0 -4
- data/spec/extensions/instance_filters_spec.rb +11 -21
- data/spec/extensions/instance_hooks_spec.rb +72 -0
- data/spec/extensions/json_serializer_spec.rb +49 -6
- data/spec/extensions/lazy_attributes_spec.rb +16 -20
- data/spec/extensions/list_spec.rb +48 -39
- data/spec/extensions/many_through_many_spec.rb +283 -250
- data/spec/extensions/many_to_one_pk_lookup_spec.rb +140 -0
- data/spec/extensions/migration_spec.rb +140 -35
- data/spec/extensions/named_timezones_spec.rb +29 -11
- data/spec/extensions/nested_attributes_spec.rb +251 -89
- data/spec/extensions/null_dataset_spec.rb +85 -0
- data/spec/extensions/optimistic_locking_spec.rb +24 -21
- data/spec/extensions/pg_array_ops_spec.rb +105 -0
- data/spec/extensions/pg_array_spec.rb +196 -0
- data/spec/extensions/pg_auto_parameterize_spec.rb +65 -0
- data/spec/extensions/pg_hstore_ops_spec.rb +136 -0
- data/spec/extensions/pg_hstore_spec.rb +195 -0
- data/spec/extensions/pg_inet_spec.rb +44 -0
- data/spec/extensions/pg_json_spec.rb +101 -0
- data/spec/extensions/pg_statement_cache_spec.rb +209 -0
- data/spec/extensions/prepared_statements_associations_spec.rb +111 -0
- data/spec/extensions/prepared_statements_safe_spec.rb +61 -0
- data/spec/extensions/prepared_statements_spec.rb +87 -0
- data/spec/extensions/prepared_statements_with_pk_spec.rb +31 -0
- data/spec/extensions/pretty_table_spec.rb +7 -6
- data/spec/extensions/query_literals_spec.rb +168 -0
- data/spec/extensions/query_spec.rb +6 -0
- data/spec/extensions/rcte_tree_spec.rb +50 -43
- data/spec/extensions/schema_caching_spec.rb +41 -0
- data/spec/extensions/schema_dumper_spec.rb +429 -22
- data/spec/extensions/schema_spec.rb +25 -21
- data/spec/extensions/select_remove_spec.rb +38 -0
- data/spec/extensions/serialization_modification_detection_spec.rb +72 -0
- data/spec/extensions/serialization_spec.rb +85 -43
- data/spec/extensions/server_block_spec.rb +90 -0
- data/spec/extensions/sharding_spec.rb +69 -143
- data/spec/extensions/single_table_inheritance_spec.rb +23 -29
- data/spec/extensions/skip_create_refresh_spec.rb +1 -1
- data/spec/extensions/spec_helper.rb +22 -61
- data/spec/extensions/sql_expr_spec.rb +29 -60
- data/spec/extensions/static_cache_spec.rb +145 -0
- data/spec/extensions/tactical_eager_loading_spec.rb +17 -19
- data/spec/extensions/thread_local_timezones_spec.rb +22 -2
- data/spec/extensions/timestamps_spec.rb +6 -6
- data/spec/extensions/to_dot_spec.rb +4 -7
- data/spec/extensions/touch_spec.rb +13 -14
- data/spec/extensions/tree_spec.rb +11 -26
- data/spec/extensions/typecast_on_load_spec.rb +9 -6
- data/spec/extensions/update_primary_key_spec.rb +30 -24
- data/spec/extensions/validation_class_methods_spec.rb +30 -51
- data/spec/extensions/validation_helpers_spec.rb +16 -35
- data/spec/extensions/xml_serializer_spec.rb +29 -4
- data/spec/files/transaction_specified_migrations/001_create_alt_basic.rb +4 -0
- data/spec/files/transaction_specified_migrations/002_create_basic.rb +4 -0
- data/spec/files/transaction_unspecified_migrations/001_create_alt_basic.rb +3 -0
- data/spec/files/transaction_unspecified_migrations/002_create_basic.rb +3 -0
- data/spec/integration/associations_test.rb +659 -21
- data/spec/integration/database_test.rb +9 -3
- data/spec/integration/dataset_test.rb +605 -91
- data/spec/integration/eager_loader_test.rb +19 -21
- data/spec/integration/migrator_test.rb +2 -3
- data/spec/integration/model_test.rb +81 -2
- data/spec/integration/plugin_test.rb +454 -143
- data/spec/integration/prepared_statement_test.rb +263 -128
- data/spec/integration/schema_test.rb +309 -43
- data/spec/integration/spec_helper.rb +40 -30
- data/spec/integration/timezone_test.rb +38 -12
- data/spec/integration/transaction_test.rb +183 -8
- data/spec/integration/type_test.rb +18 -4
- data/spec/model/association_reflection_spec.rb +201 -7
- data/spec/model/associations_spec.rb +1067 -661
- data/spec/model/base_spec.rb +329 -95
- data/spec/model/dataset_methods_spec.rb +7 -27
- data/spec/model/eager_loading_spec.rb +677 -676
- data/spec/model/hooks_spec.rb +349 -67
- data/spec/model/model_spec.rb +240 -168
- data/spec/model/plugins_spec.rb +24 -13
- data/spec/model/record_spec.rb +490 -232
- data/spec/model/spec_helper.rb +14 -71
- data/spec/model/validations_spec.rb +11 -0
- data/spec/rcov.opts +2 -0
- metadata +178 -80
|
@@ -0,0 +1,572 @@
|
|
|
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. If given a hash, use the hash as the
|
|
84
|
+
options for the index.
|
|
85
|
+
:null :: Mark the column as allowing NULL values (if true),
|
|
86
|
+
or not allowing NULL values (if false). If unspecified, will default
|
|
87
|
+
to whatever the database default is.
|
|
88
|
+
:unique :: Mark the column as unique, generally has the same effect as
|
|
89
|
+
creating a unique index on the column.
|
|
90
|
+
|
|
91
|
+
=== Other methods
|
|
92
|
+
|
|
93
|
+
In addition to the +column+ method and other methods that create columns, there are a other methods that can be used:
|
|
94
|
+
|
|
95
|
+
==== +primary_key+
|
|
96
|
+
|
|
97
|
+
You've seen this one used already. It's used to create an autoincrementing integer primary key column.
|
|
98
|
+
|
|
99
|
+
create_table(:a0){primary_key :id}
|
|
100
|
+
|
|
101
|
+
If you want to create a primary key column that doesn't use an autoincrementing integer, you should
|
|
102
|
+
not use this method. Instead, you should use the :primary_key option to the +column+ method or type
|
|
103
|
+
method:
|
|
104
|
+
|
|
105
|
+
create_table(:a1){Integer :id, :primary_key=>true} # Non autoincrementing integer primary key
|
|
106
|
+
create_table(:a2){String :name, :primary_key=>true} # varchar(255) primary key
|
|
107
|
+
|
|
108
|
+
If you want to create a composite primary key, you should call the +primary_key+ method with an
|
|
109
|
+
array of column symbols:
|
|
110
|
+
|
|
111
|
+
create_table(:items) do
|
|
112
|
+
Integer :group_id
|
|
113
|
+
Integer :position
|
|
114
|
+
primary_key [:group_id, :position]
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
If provided with an array, +primary_key+ does not create a column, it just sets up the primary key constraint.
|
|
118
|
+
|
|
119
|
+
==== +foreign_key+
|
|
120
|
+
|
|
121
|
+
+foreign_key+ is used to create a foreign key column that references a column in another table (or the same table).
|
|
122
|
+
It takes the column name as the first argument, the table it references as the second argument, and an options hash
|
|
123
|
+
as it's third argument. A simple example is:
|
|
124
|
+
|
|
125
|
+
create_table(:albums) do
|
|
126
|
+
primary_key :id
|
|
127
|
+
foreign_key :artist_id, :artists
|
|
128
|
+
String :name
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
+foreign_key+ accepts some specific options:
|
|
132
|
+
|
|
133
|
+
:deferrable :: Makes the foreign key constraint checks deferrable, so they aren't checked
|
|
134
|
+
until the end of the transaction.
|
|
135
|
+
:key :: For foreign key columns, the column in the associated table
|
|
136
|
+
that this column references. Unnecessary if this column
|
|
137
|
+
references the primary key of the associated table, at least
|
|
138
|
+
on most databases.
|
|
139
|
+
:on_delete :: Specify the behavior of this foreign key column when the row with the primary key
|
|
140
|
+
it references is deleted , can be :restrict, :cascade, :set_null, or :set_default.
|
|
141
|
+
You can also use a string, which is used literally.
|
|
142
|
+
:on_update :: Specify the behavior of this foreign key column when the row with the primary key
|
|
143
|
+
it references modifies the value of the primary key. Takes the same options as
|
|
144
|
+
:on_delete.
|
|
145
|
+
|
|
146
|
+
Like +primary_key+, if you provide +foreign_key+ with an array of symbols, it will not create a
|
|
147
|
+
column, but create a foreign key constraint:
|
|
148
|
+
|
|
149
|
+
create_table(:artists) do
|
|
150
|
+
String :name
|
|
151
|
+
String :location
|
|
152
|
+
primary_key [:name, :location]
|
|
153
|
+
end
|
|
154
|
+
create_table(:albums) do
|
|
155
|
+
String :artist_name
|
|
156
|
+
String :artist_location
|
|
157
|
+
String :name
|
|
158
|
+
foreign_key [:artist_name, :artist_location], :artists
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
==== +index+
|
|
162
|
+
|
|
163
|
+
+index+ creates indexes on the table. For single columns, calling index is the same as using the
|
|
164
|
+
<tt>:index</tt> option when creating the column:
|
|
165
|
+
|
|
166
|
+
create_table(:a){Integer :id, :index=>true}
|
|
167
|
+
# Same as:
|
|
168
|
+
create_table(:a) do
|
|
169
|
+
Integer :id
|
|
170
|
+
index :id
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
create_table(:a){Integer :id, :index=>{:unique=>true}}
|
|
174
|
+
# Same as:
|
|
175
|
+
create_table(:a) do
|
|
176
|
+
Integer :id
|
|
177
|
+
index :id, :unique=>true
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
Similar to the +primary_key+ and +foreign_key+ methods, calling +index+ with an array of symbols
|
|
181
|
+
will create a multiple column index:
|
|
182
|
+
|
|
183
|
+
create_table(:albums) do
|
|
184
|
+
primary_key :id
|
|
185
|
+
foreign_key :artist_id, :artists
|
|
186
|
+
Integer :position
|
|
187
|
+
index [:artist_id, :position]
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
The +index+ method also accepts some options:
|
|
191
|
+
|
|
192
|
+
:name :: The name of the index (generated based on the table and column names if not provided).
|
|
193
|
+
:type :: The type of index to use (only supported by some databases)
|
|
194
|
+
:unique :: Make the index unique, so duplicate values are not allowed.
|
|
195
|
+
:where :: Create a partial index (only supported by some databases)
|
|
196
|
+
|
|
197
|
+
==== +unique+
|
|
198
|
+
|
|
199
|
+
The +unique+ method creates a unique constraint on the table. A unique constraint generally
|
|
200
|
+
operates identically to a unique index, so the following three +create_table+ blocks are
|
|
201
|
+
pretty much identical:
|
|
202
|
+
|
|
203
|
+
create_table(:a){Integer :a, :unique=>true}
|
|
204
|
+
|
|
205
|
+
create_table(:a) do
|
|
206
|
+
Integer :a
|
|
207
|
+
index :a, :unique=>true
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
create_table(:a) do
|
|
211
|
+
Integer :a
|
|
212
|
+
unique :a
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
Just like +index+, +unique+ can set up a multiple column unique constraint, where the
|
|
216
|
+
combination of the columns must be unique:
|
|
217
|
+
|
|
218
|
+
create_table(:a) do
|
|
219
|
+
Integer :a
|
|
220
|
+
Integer :b
|
|
221
|
+
unique [:a, :b]
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
==== +full_text_index+ and +spatial_index+
|
|
225
|
+
|
|
226
|
+
Both of these create specialized index types supported by some databases. They
|
|
227
|
+
both take the same options as +index+.
|
|
228
|
+
|
|
229
|
+
==== +constraint+
|
|
230
|
+
|
|
231
|
+
+constraint+ creates a named table constraint:
|
|
232
|
+
|
|
233
|
+
create_table(:artists) do
|
|
234
|
+
primary_key :id
|
|
235
|
+
String :name
|
|
236
|
+
constraint(:name_min_length){char_length(name) > 2}
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
Instead of using a block, you can use arguments that will be handled similarly
|
|
240
|
+
to <tt>Dataset#filter</tt>:
|
|
241
|
+
|
|
242
|
+
create_table(:artists) do
|
|
243
|
+
primary_key :id
|
|
244
|
+
String :name
|
|
245
|
+
constraint(:name_length_range, :char_length.sql_function(:name)=>3..50)
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
==== +check+
|
|
249
|
+
|
|
250
|
+
+check+ operates just like +constraint+, except that it doesn't take a name
|
|
251
|
+
and it creates an unnamed constraint
|
|
252
|
+
|
|
253
|
+
create_table(:artists) do
|
|
254
|
+
primary_key :id
|
|
255
|
+
String :name
|
|
256
|
+
check{char_length(name) > 2}
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
== +create_join_table+
|
|
260
|
+
|
|
261
|
+
+create_join_table+ is a shortcut that you can use to create simple many-to-many join tables:
|
|
262
|
+
|
|
263
|
+
create_join_table(:artist_id=>:artists, :album_id=>:albums)
|
|
264
|
+
|
|
265
|
+
which expands to:
|
|
266
|
+
|
|
267
|
+
create_table(:albums_artists) do
|
|
268
|
+
foreign_key :album_id, :albums, :null=>false
|
|
269
|
+
foreign_key :artist_id, :artists, :null=>false
|
|
270
|
+
primary_key [:album_id, :artist_id]
|
|
271
|
+
index [:artist_id, :album_id]
|
|
272
|
+
end
|
|
273
|
+
|
|
274
|
+
== <tt>create_table :as=></tt>
|
|
275
|
+
|
|
276
|
+
To create a table from the result of a SELECT query, instead of passing a block
|
|
277
|
+
to +create_table+, provide a dataset to the :as option:
|
|
278
|
+
|
|
279
|
+
create_table(:older_items, :as=>DB[:items].where{updated_at < Date.today << 6})
|
|
280
|
+
|
|
281
|
+
== +alter_table+
|
|
282
|
+
|
|
283
|
+
+alter_table+ is used to alter existing tables, changing their columns, indexes,
|
|
284
|
+
or constraints. It it used just like +create_table+, accepting a block which
|
|
285
|
+
is instance_evaled, and providing its own methods:
|
|
286
|
+
|
|
287
|
+
=== +add_column+
|
|
288
|
+
|
|
289
|
+
One of the most common methods, +add_column+ is used to add a column to the table.
|
|
290
|
+
Its API is similar to that of +create_table+'s +column+ method, where the first
|
|
291
|
+
argument is the column name, the second is the type, and the third is an options
|
|
292
|
+
hash:
|
|
293
|
+
|
|
294
|
+
alter_table(:albums) do
|
|
295
|
+
add_column :copies_sold, Integer, :default=>0
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
When adding a column, it's a good idea to provide a default value, unless you
|
|
299
|
+
want the value for all rows to be set to NULL.
|
|
300
|
+
|
|
301
|
+
=== +drop_column+
|
|
302
|
+
|
|
303
|
+
As you may expect, +drop_column+ takes a column name and drops the column. It's
|
|
304
|
+
often used in the +down+ block of a migration to drop a column added in an +up+ block:
|
|
305
|
+
|
|
306
|
+
alter_table(:albums) do
|
|
307
|
+
drop_column :copies_sold
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
=== +rename_column+
|
|
311
|
+
|
|
312
|
+
+rename_column+ is used to rename a column. It takes the old column name as the first
|
|
313
|
+
argument, and the new column name as the second argument:
|
|
314
|
+
|
|
315
|
+
alter_table(:albums) do
|
|
316
|
+
rename_column :copies_sold, :total_sales
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
=== +add_primary_key+
|
|
320
|
+
|
|
321
|
+
If you forgot to include a primary key on the table, and want to add one later, you
|
|
322
|
+
can use +add_primary_key+. A common use of this is to make many_to_many association
|
|
323
|
+
join tables into real models:
|
|
324
|
+
|
|
325
|
+
alter_table(:albums_artists) do
|
|
326
|
+
add_primary_key :id
|
|
327
|
+
end
|
|
328
|
+
|
|
329
|
+
Just like +create_table+'s +primary_key+ method, if you provide an array of symbols,
|
|
330
|
+
Sequel will not add a column, but will add a composite primary key constraint:
|
|
331
|
+
|
|
332
|
+
alter_table(:albums_artists) do
|
|
333
|
+
add_primary_key [:album_id, :artist_id]
|
|
334
|
+
end
|
|
335
|
+
|
|
336
|
+
If you just want to take an existing single column and make it a primary key, call
|
|
337
|
+
+add_primary_key+ with an array with a single symbol:
|
|
338
|
+
|
|
339
|
+
alter_table(:artists) do
|
|
340
|
+
add_primary_key [:id]
|
|
341
|
+
end
|
|
342
|
+
|
|
343
|
+
=== +add_foreign_key+
|
|
344
|
+
|
|
345
|
+
+add_foreign_key+ can be used to add a new foreign key column or constraint to a table.
|
|
346
|
+
Like +add_primary_key+, if you provide it with a symbol as the first argument, it
|
|
347
|
+
creates a new column:
|
|
348
|
+
|
|
349
|
+
alter_table(:albums) do
|
|
350
|
+
add_foreign_key :artist_id, :artists
|
|
351
|
+
end
|
|
352
|
+
|
|
353
|
+
If you want to add a new foreign key constraint to an existing column, you provide an
|
|
354
|
+
array with a single element:
|
|
355
|
+
|
|
356
|
+
alter_table(:albums) do
|
|
357
|
+
add_foreign_key [:artist_id], :artists
|
|
358
|
+
end
|
|
359
|
+
|
|
360
|
+
To set up a multiple column foreign key constraint, use an array with multiple column
|
|
361
|
+
symbols:
|
|
362
|
+
|
|
363
|
+
alter_table(:albums) do
|
|
364
|
+
add_foreign_key [:artist_name, :artist_location], :artists
|
|
365
|
+
end
|
|
366
|
+
|
|
367
|
+
=== +add_index+
|
|
368
|
+
|
|
369
|
+
+add_index+ works just like +create_table+'s +index+ method, creating a new index on
|
|
370
|
+
the table:
|
|
371
|
+
|
|
372
|
+
alter_table(:albums) do
|
|
373
|
+
add_index :artist_id
|
|
374
|
+
end
|
|
375
|
+
|
|
376
|
+
It accepts the same options as +create_table+'s +index+ method, and you can set up
|
|
377
|
+
a multiple column index using an array:
|
|
378
|
+
|
|
379
|
+
alter_table(:albums_artists) do
|
|
380
|
+
add_index [:album_id, :artist_id], :unique=>true
|
|
381
|
+
end
|
|
382
|
+
|
|
383
|
+
=== +drop_index+
|
|
384
|
+
|
|
385
|
+
As you may expect, +drop_index+ drops an existing index:
|
|
386
|
+
|
|
387
|
+
alter_table(:albums) do
|
|
388
|
+
drop_index :artist_id
|
|
389
|
+
end
|
|
390
|
+
|
|
391
|
+
Just like +drop_column+, it is often used in the +down+ block of a migration.
|
|
392
|
+
|
|
393
|
+
To drop an index with a specific name, use the <tt>:name</tt> option:
|
|
394
|
+
|
|
395
|
+
alter_table(:albums) do
|
|
396
|
+
drop_index :artist_id, :name=>:artists_id_index
|
|
397
|
+
end
|
|
398
|
+
|
|
399
|
+
=== +add_full_text_index+, +add_spatial_index+
|
|
400
|
+
|
|
401
|
+
Corresponding to +create_table+'s +full_text_index+ and +spatial_index+ methods,
|
|
402
|
+
these two methods create new indexes on the table.
|
|
403
|
+
|
|
404
|
+
=== +add_constraint+
|
|
405
|
+
|
|
406
|
+
This adds a named constraint to the table, similar to +create_table+'s +constraint+
|
|
407
|
+
method:
|
|
408
|
+
|
|
409
|
+
alter_table(:albums) do
|
|
410
|
+
add_constraint(:name_min_length){char_length(name) > 2}
|
|
411
|
+
end
|
|
412
|
+
|
|
413
|
+
There is no method to add an unnamed constraint, but you can pass nil as the first
|
|
414
|
+
argument of +add_constraint+ to do so. However, it's not recommend to do that
|
|
415
|
+
as it is difficult to drop such a constraint.
|
|
416
|
+
|
|
417
|
+
=== +add_unique_constraint+
|
|
418
|
+
|
|
419
|
+
This adds a unique constraint to the table, similar to +create_table+'s +unique+
|
|
420
|
+
method. This usually has the same effect as adding a unique index.
|
|
421
|
+
|
|
422
|
+
alter_table(:albums) do
|
|
423
|
+
add_unique_constraint [:artist_id, :name]
|
|
424
|
+
end
|
|
425
|
+
|
|
426
|
+
=== +drop_constraint+
|
|
427
|
+
|
|
428
|
+
This method drops an existing named constraint:
|
|
429
|
+
|
|
430
|
+
alter_table(:albums) do
|
|
431
|
+
drop_constraint(:name_min_length)
|
|
432
|
+
end
|
|
433
|
+
|
|
434
|
+
There is no database independent method to drop an unnamed constraint. Generally, the
|
|
435
|
+
database will give it a name automatically, and you will have to figure out what it is.
|
|
436
|
+
For that reason, you should not add unnamed constraints that you ever might need to remove.
|
|
437
|
+
|
|
438
|
+
On MySQL, you must specify the type of constraint via a <tt>:type</tt> option:
|
|
439
|
+
|
|
440
|
+
alter_table(:albums) do
|
|
441
|
+
drop_constraint(:albums_pk, :type=>:primary_key)
|
|
442
|
+
drop_constraint(:albums_fk, :type=>:foreign_key)
|
|
443
|
+
drop_constraint(:albums_uk, :type=>:unique)
|
|
444
|
+
end
|
|
445
|
+
|
|
446
|
+
=== +set_column_default+
|
|
447
|
+
|
|
448
|
+
This modifies the default value of a column:
|
|
449
|
+
|
|
450
|
+
alter_table(:albums) do
|
|
451
|
+
set_column_default :copies_sold, 0
|
|
452
|
+
end
|
|
453
|
+
|
|
454
|
+
=== +set_column_type+
|
|
455
|
+
|
|
456
|
+
This modifies a column's type. Most databases will attempt to convert existing values in
|
|
457
|
+
the columns to the new type:
|
|
458
|
+
|
|
459
|
+
alter_table(:albums) do
|
|
460
|
+
set_column_type :copies_sold, Bignum
|
|
461
|
+
end
|
|
462
|
+
|
|
463
|
+
You can specify the type as a string or symbol, in which case it is used verbatim, or as a supported
|
|
464
|
+
ruby class, in which case it gets converted to an appropriate database type.
|
|
465
|
+
|
|
466
|
+
=== +set_column_allow_null+
|
|
467
|
+
|
|
468
|
+
This changes the NULL or NOT NULL setting of a column:
|
|
469
|
+
|
|
470
|
+
alter_table(:albums) do
|
|
471
|
+
set_column_allow_null :artist_id, true # NULL
|
|
472
|
+
set_column_allow_null :copies_sold, false # NOT NULL
|
|
473
|
+
end
|
|
474
|
+
|
|
475
|
+
== Other +Database+ schema modification methods
|
|
476
|
+
|
|
477
|
+
<tt>Sequel::Database</tt> has many schema modification instance methods,
|
|
478
|
+
most of which are shortcuts to the same methods in +alter_table+. The
|
|
479
|
+
following +Database+ instance methods just call +alter_table+ with a
|
|
480
|
+
block that calls the method with the same name inside the +alter_table+
|
|
481
|
+
block with all arguments after the first argument (which is used as
|
|
482
|
+
the table name):
|
|
483
|
+
|
|
484
|
+
* +add_column+
|
|
485
|
+
* +drop_column+
|
|
486
|
+
* +rename_column+
|
|
487
|
+
* +add_index+
|
|
488
|
+
* +drop_index+
|
|
489
|
+
* +set_column_default+
|
|
490
|
+
* +set_column_type+
|
|
491
|
+
|
|
492
|
+
For example, the following two method calls do the same thing:
|
|
493
|
+
|
|
494
|
+
alter_table(:artists){add_column :copies_sold, Integer}
|
|
495
|
+
add_column :artists, :copies_sold, Integer
|
|
496
|
+
|
|
497
|
+
There are some other schema modification methods that have no +alter_table+
|
|
498
|
+
counterpart:
|
|
499
|
+
|
|
500
|
+
=== +drop_table+
|
|
501
|
+
|
|
502
|
+
+drop_table+ takes multiple arguments and treats all arguments as a
|
|
503
|
+
table name to drop:
|
|
504
|
+
|
|
505
|
+
drop_table(:albums_artists, :albums, :artists)
|
|
506
|
+
|
|
507
|
+
Note that when dropping tables, you may need to drop them in a specific order
|
|
508
|
+
if you are using foreign keys and the database is enforcing referential
|
|
509
|
+
integrity. In general, you need to drop the tables containing the foreign
|
|
510
|
+
keys before the tables containing the primary keys they reference.
|
|
511
|
+
|
|
512
|
+
=== +rename_table+
|
|
513
|
+
|
|
514
|
+
You can rename an existing table using +rename_table+. Like +rename_column+,
|
|
515
|
+
the first argument is the current name, and the second is the new name:
|
|
516
|
+
|
|
517
|
+
rename_table(:artist, :artists)
|
|
518
|
+
|
|
519
|
+
=== <tt>create_table!</tt>
|
|
520
|
+
|
|
521
|
+
<tt>create_table!</tt> with the bang drops the table if it exists
|
|
522
|
+
before attempting to create it, so:
|
|
523
|
+
|
|
524
|
+
create_table!(:artists)
|
|
525
|
+
primary_key :id
|
|
526
|
+
end
|
|
527
|
+
|
|
528
|
+
is the same as:
|
|
529
|
+
|
|
530
|
+
drop_table(:artists) if table_exists?(:artists)
|
|
531
|
+
create_table(:artists)
|
|
532
|
+
primary_key :id
|
|
533
|
+
end
|
|
534
|
+
|
|
535
|
+
It should not be used inside migrations, as if the table does not exist, it may
|
|
536
|
+
mess up the migration.
|
|
537
|
+
|
|
538
|
+
=== <tt>create_table?</tt>
|
|
539
|
+
|
|
540
|
+
<tt>create_table?</tt> with a question mark only creates the table if it does
|
|
541
|
+
not already exist, so:
|
|
542
|
+
|
|
543
|
+
create_table?(:artists)
|
|
544
|
+
primary_key :id
|
|
545
|
+
end
|
|
546
|
+
|
|
547
|
+
is the same as:
|
|
548
|
+
|
|
549
|
+
create_table(:artists)
|
|
550
|
+
primary_key :id
|
|
551
|
+
end unless table_exists?(:artists)
|
|
552
|
+
|
|
553
|
+
Like <tt>create_table!</tt>, it should not be used inside migrations.
|
|
554
|
+
|
|
555
|
+
=== +create_view+ and +create_or_replace_view+
|
|
556
|
+
|
|
557
|
+
These can be used to create views. The difference between them is that
|
|
558
|
+
+create_or_replace_view+ will unconditionally replace an existing view of
|
|
559
|
+
the same name, while +create_view+ will probably raise an error. Both methods
|
|
560
|
+
take the name as the first argument, and either an string or a dataset as the
|
|
561
|
+
second argument:
|
|
562
|
+
|
|
563
|
+
create_view(:gold_albums, DB[:albums].filter{copies_sold > 500000})
|
|
564
|
+
create_or_replace_view(:gold_albums, "SELECT * FROM albums WHERE copies_sold > 500000")
|
|
565
|
+
|
|
566
|
+
=== +drop_view+
|
|
567
|
+
|
|
568
|
+
+drop_view+ drops existing views. Just like +drop_table+, it can accept multiple
|
|
569
|
+
arguments:
|
|
570
|
+
|
|
571
|
+
drop_view(:gold_albums, :platinum_albums)
|
|
572
|
+
|
data/doc/sharding.rdoc
CHANGED
|
@@ -8,9 +8,25 @@ that ship with Sequel.
|
|
|
8
8
|
|
|
9
9
|
== The :servers Database option
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
Sharding and read_only support are both enabled via the :servers database
|
|
12
|
+
option. Using the :servers database option makes Sequel use a connection pool
|
|
13
|
+
class that supports sharding, and the minimum required to enable sharding
|
|
14
|
+
support is to use the empty hash:
|
|
15
|
+
|
|
16
|
+
DB=Sequel.connect('postgres://master_server/database', :servers=>{})
|
|
17
|
+
|
|
18
|
+
In most cases, you are probably not going to want to use an empty hash,
|
|
19
|
+
so you'll want to have entries in the hash. Keys in the server hash are
|
|
20
|
+
not restricted to type, but the general recommendation is to use a symbol
|
|
21
|
+
unless you have special requirements. Values in the server hash should be
|
|
22
|
+
either hashes or procs that return hashes. These hashes are merged into
|
|
23
|
+
the Database object's default options hash to get the connection options
|
|
24
|
+
for the shard, so you don't need to override all options, just the ones
|
|
25
|
+
that need to be modified. For example, if you are using the same user,
|
|
26
|
+
password, and database name and just the host is changing, you only need
|
|
27
|
+
a :host entry in each shard's hash.
|
|
28
|
+
|
|
29
|
+
Note that all servers should have the same schema for all
|
|
14
30
|
tables you are accessing, unless you really know what you are doing.
|
|
15
31
|
|
|
16
32
|
== Master and Slave Database Configurations
|
|
@@ -153,3 +169,79 @@ work well with shards. You just need to remember to set to model to use the plu
|
|
|
153
169
|
If all of your models are sharded, you can set all models to use the plugin via:
|
|
154
170
|
|
|
155
171
|
Sequel::Model.plugin :sharding
|
|
172
|
+
|
|
173
|
+
=== server_block Extension
|
|
174
|
+
|
|
175
|
+
By default, you must specify the server/shard you want to use for every dataset/action,
|
|
176
|
+
or Sequel will use the default shard. If you have a group of queries that should use the
|
|
177
|
+
same shard, it can get a bit redundent to specify the same shard for all of them.
|
|
178
|
+
|
|
179
|
+
The server_block extension adds a Database#with_server method that scopes all database
|
|
180
|
+
access inside the block to the given shard by default:
|
|
181
|
+
|
|
182
|
+
Sequel.extension :server_block
|
|
183
|
+
DB.extend Sequel::ServerBlock
|
|
184
|
+
DB.with_server(:a) do
|
|
185
|
+
# this SELECT query uses the "a" shard
|
|
186
|
+
if r = Rainbow.first(:hash=>/31337/)
|
|
187
|
+
r.count += 1
|
|
188
|
+
# this UPDATE query also uses the "a" shard
|
|
189
|
+
r.save
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
The server_block extension doesn't currently integrate with the sharding plugin, as it
|
|
194
|
+
ties into the Dataset#server method. This shouldn't present a problem in practice as
|
|
195
|
+
long as you just access the models inside the with_server block, since they will use
|
|
196
|
+
the shard set by with_server by default. However, you will probably have issues if
|
|
197
|
+
you retrieve the models inside the block and save them outside of the block. If you
|
|
198
|
+
need to do that, call the server method explicitly on the dataset used to retrieve the
|
|
199
|
+
model objects.
|
|
200
|
+
|
|
201
|
+
=== arbitrary_servers Extension
|
|
202
|
+
|
|
203
|
+
By default, Sequel's sharding support is designed to work with predefined shards. It ships
|
|
204
|
+
with Database#add_servers and Database#remove_servers methods to modify these predefined
|
|
205
|
+
shards on the fly, but it is a bit cumbersome to work with truly arbitrary servers
|
|
206
|
+
(requiring you to call add_servers before use, then remove_servers after use).
|
|
207
|
+
|
|
208
|
+
The arbitrary_servers extension allows you to pass a server/shard options hash as the
|
|
209
|
+
server to use, and those options will be merged directly into the database's default options:
|
|
210
|
+
|
|
211
|
+
Sequel.extension :arbitrary_servers
|
|
212
|
+
DB.pool.extend Sequel::ArbitraryServers
|
|
213
|
+
DB[:rainbows].server(:host=>'hash_host_a').all
|
|
214
|
+
# or
|
|
215
|
+
DB[:rainbows].server(:host=>'hash_host_b', :database=>'backup').all
|
|
216
|
+
|
|
217
|
+
arbitrary_servers is designed to work well in conjunction with the server_block extension:
|
|
218
|
+
|
|
219
|
+
DB.with_server(:host=>'hash_host_b', :database=>'backup') do
|
|
220
|
+
DB.synchronize do
|
|
221
|
+
# All queries here default to the backup database on hash_host_b
|
|
222
|
+
end
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
If you are using arbitrary_servers with server_block, you may want to
|
|
226
|
+
define the following method (or something similar) so that you don't
|
|
227
|
+
need to call synchronize separately:
|
|
228
|
+
|
|
229
|
+
def DB.with_server(*)
|
|
230
|
+
super{synchronize{yield}}
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
The reason for the synchronize method is that it checks out a connection
|
|
234
|
+
and makes the same connection available for the duration of the block.
|
|
235
|
+
If you don't do that, Sequel will probably disconnect from the database
|
|
236
|
+
and reconnect to the database on each request, since connections to
|
|
237
|
+
arbitrary servers are not cached.
|
|
238
|
+
|
|
239
|
+
Note that this extension only works with the sharded threaded connection
|
|
240
|
+
pool. If you are using the sharded single connection pool, you need
|
|
241
|
+
to switch to the sharded threaded connection pool before using this
|
|
242
|
+
extension. If you are passing the :single_threaded option to
|
|
243
|
+
the Database, just remove that option. If you are setting:
|
|
244
|
+
|
|
245
|
+
Sequel.single_threaded = true
|
|
246
|
+
|
|
247
|
+
just remove or comment out that code.
|