sequel 3.32.0 → 3.33.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. data/CHANGELOG +42 -0
  2. data/Rakefile +3 -2
  3. data/doc/migration.rdoc +41 -14
  4. data/doc/opening_databases.rdoc +2 -0
  5. data/doc/release_notes/3.29.0.txt +1 -1
  6. data/doc/release_notes/3.33.0.txt +157 -0
  7. data/doc/sharding.rdoc +93 -7
  8. data/doc/testing.rdoc +1 -1
  9. data/lib/sequel/adapters/do.rb +1 -0
  10. data/lib/sequel/adapters/do/mysql.rb +6 -0
  11. data/lib/sequel/adapters/jdbc.rb +1 -0
  12. data/lib/sequel/adapters/jdbc/mysql.rb +0 -5
  13. data/lib/sequel/adapters/mock.rb +10 -5
  14. data/lib/sequel/adapters/mysql.rb +23 -1
  15. data/lib/sequel/adapters/mysql2.rb +16 -2
  16. data/lib/sequel/adapters/postgres.rb +22 -4
  17. data/lib/sequel/adapters/shared/mysql.rb +51 -10
  18. data/lib/sequel/adapters/shared/postgres.rb +101 -63
  19. data/lib/sequel/adapters/shared/sqlite.rb +19 -0
  20. data/lib/sequel/adapters/sqlite.rb +71 -16
  21. data/lib/sequel/adapters/swift.rb +1 -0
  22. data/lib/sequel/connection_pool.rb +1 -1
  23. data/lib/sequel/connection_pool/sharded_single.rb +6 -1
  24. data/lib/sequel/connection_pool/sharded_threaded.rb +6 -1
  25. data/lib/sequel/connection_pool/threaded.rb +12 -11
  26. data/lib/sequel/database/connecting.rb +2 -0
  27. data/lib/sequel/database/misc.rb +6 -0
  28. data/lib/sequel/database/query.rb +1 -1
  29. data/lib/sequel/extensions/arbitrary_servers.rb +108 -0
  30. data/lib/sequel/extensions/migration.rb +45 -7
  31. data/lib/sequel/extensions/server_block.rb +139 -0
  32. data/lib/sequel/model/associations.rb +9 -9
  33. data/lib/sequel/model/inflections.rb +1 -1
  34. data/lib/sequel/plugins/instance_hooks.rb +1 -1
  35. data/lib/sequel/plugins/json_serializer.rb +1 -1
  36. data/lib/sequel/plugins/list.rb +12 -2
  37. data/lib/sequel/version.rb +1 -1
  38. data/spec/adapters/mysql_spec.rb +64 -8
  39. data/spec/adapters/postgres_spec.rb +139 -78
  40. data/spec/adapters/sqlite_spec.rb +87 -0
  41. data/spec/core/connection_pool_spec.rb +14 -0
  42. data/spec/core/database_spec.rb +5 -0
  43. data/spec/core/mock_adapter_spec.rb +21 -9
  44. data/spec/extensions/arbitrary_servers_spec.rb +114 -0
  45. data/spec/extensions/instance_hooks_spec.rb +19 -0
  46. data/spec/extensions/list_spec.rb +31 -0
  47. data/spec/extensions/migration_spec.rb +61 -4
  48. data/spec/extensions/server_block_spec.rb +90 -0
  49. data/spec/extensions/spec_helper.rb +1 -1
  50. data/spec/files/transaction_migrations/001_create_alt_basic.rb +3 -0
  51. data/spec/files/transaction_migrations/002_create_basic.rb +3 -0
  52. data/spec/files/transactionless_migrations/001_create_alt_basic.rb +4 -0
  53. data/spec/files/transactionless_migrations/002_create_basic.rb +4 -0
  54. data/spec/integration/dataset_test.rb +2 -2
  55. data/spec/integration/plugin_test.rb +9 -9
  56. data/spec/integration/schema_test.rb +3 -1
  57. data/spec/integration/transaction_test.rb +2 -2
  58. metadata +12 -2
data/CHANGELOG CHANGED
@@ -1,3 +1,45 @@
1
+ === 3.33.0 (2012-03-01)
2
+
3
+ * Add ability to force or disable transactions completely in the migrators using the :use_transactions option (jeremyevans)
4
+
5
+ * Add ability to turn off transactions for migrations by calling no_transaction inside the Sequel.migration block (jeremyevans)
6
+
7
+ * Allow specifically choosing which migrator to use via TimestampMigrator.apply or IntegerMigrator.apply (jeremyevans)
8
+
9
+ * Add arbitrary_servers extension to allow the use of arbitrary servers/shards by providing a hash of options as the server (jeremyevans)
10
+
11
+ * Add server_block extension to scope database access inside the block to a specific default server/shard (jeremyevans)
12
+
13
+ * Respect :collate column option on MySQL (jeremyevans) (#445)
14
+
15
+ * Use Mysql2::Client::FOUND_ROWS to get accurate number of rows matched in the mysql2 adapter (jeremyevans)
16
+
17
+ * Use Mysql#info to get accurate number of rows matched in the mysql adapter (jeremyevans)
18
+
19
+ * Make mock adapter with specific SQL dialect use appropriate defaults for quoting identifiers (jeremyevans)
20
+
21
+ * Make list plugin automatically set position field value on creation if not already set (jeremyevans)
22
+
23
+ * Add Database#integer_booleans setting on SQLite to store booleans as integers (jeremyevans)
24
+
25
+ * Typecast columns stored as integers/floats in the SQLite adapter (jeremyevans)
26
+
27
+ * In the instance_hooks plugin, (before|after)_*_hook instance methods now return self (jeremyevans)
28
+
29
+ * Handle NaN, Infinity, and -Infinity floats on PostgreSQL (kf8a, jeremyevans) (#444)
30
+
31
+ * Support an :sslmode option when using the postgres adapter with the pg driver (jeremyevans)
32
+
33
+ * Add Database#create_schema and #drop_schema to the shared postgres adapter (tkellen, jeremyevans) (#440)
34
+
35
+ * Add Database#supports_savepoints_in_prepared_transactions?, false on MySQL >=5.5.12 (jeremyevans) (#437)
36
+
37
+ * Support an identifier output method in the mysql2 adapter (jeremyevans)
38
+
39
+ * Make foreign key creation work on MySQL with InnoDB engine without specifying :key option (jeremyevans)
40
+
41
+ * Allow disabling use of sudo with SUDO='' when running the rake install/uninstall tasks (jeremyevans) (#433)
42
+
1
43
  === 3.32.0 (2012-02-01)
2
44
 
3
45
  * Make serialization_modification_detection plugin work correctly with new objects and after saving existing objects (jeremyevans) (#432)
data/Rakefile CHANGED
@@ -7,6 +7,7 @@ VERS = lambda do
7
7
  Sequel.version
8
8
  end
9
9
  CLEAN.include ["**/.*.sw?", "sequel-*.gem", ".config", "rdoc", "coverage", "www/public/*.html", "www/public/rdoc*", '**/*.rbc']
10
+ SUDO = ENV['SUDO'] || 'sudo'
10
11
 
11
12
  # Gem Packaging and Release
12
13
 
@@ -18,12 +19,12 @@ end
18
19
 
19
20
  desc "Install sequel gem"
20
21
  task :install=>[:package] do
21
- sh %{sudo gem install ./#{NAME}-#{VERS.call} --local}
22
+ sh %{#{SUDO} gem install ./#{NAME}-#{VERS.call} --local}
22
23
  end
23
24
 
24
25
  desc "Uninstall sequel gem"
25
26
  task :uninstall=>[:clean] do
26
- sh %{sudo gem uninstall #{NAME}}
27
+ sh %{#{SUDO} gem uninstall #{NAME}}
27
28
  end
28
29
 
29
30
  desc "Upload sequel gem to gemcutter"
data/doc/migration.rdoc CHANGED
@@ -2,7 +2,6 @@
2
2
 
3
3
  This guide is based on http://guides.rubyonrails.org/migrations.html
4
4
 
5
-
6
5
  == Overview
7
6
 
8
7
  Migrations make it easy to alter your database's schema in a systematic manner.
@@ -14,20 +13,20 @@ create the necessary database structure manually using Sequel's schema
14
13
  modification methods or another database tool. However, if you are dealing
15
14
  with other developers, you'll have to send them all of the changes you are
16
15
  making. Even if you aren't dealing with other developers, you generally have
17
- to make the schema changes in 3 places (development, testing, and
16
+ to make the schema changes in 3 places (development, testing, and
18
17
  production), and it's probably easier to use the migrations system to apply
19
18
  the schema changes than it is to keep track of the changes manually and
20
19
  execute them manually at the appropriate time.
21
20
 
22
21
  Sequel tracks which migrations you have already run, so to apply migrations
23
- you generally need to use run Sequel's migrator with <tt>bin/sequel -m</tt>:
22
+ you generally need to run Sequel's migrator with <tt>bin/sequel -m</tt>:
24
23
 
25
24
  sequel -m path/to/migrations postgres://host/database
26
25
 
27
26
  Migrations in Sequel use a very simple DSL via the <tt>Sequel.migration</tt>
28
27
  method, and inside the DSL, use the <tt>Sequel::Database</tt> schema
29
28
  modification methods such as +create_table+ and +alter_table+.
30
- See the {schema modification guide}[link:files/doc/schema_modification_rdoc.html]
29
+ See the {schema modification guide}[link:files/doc/schema_modification_rdoc.html]
31
30
  for details on the schema modification methods you can use.
32
31
 
33
32
  == A Basic Migration
@@ -182,6 +181,31 @@ need to update the database by hand.
182
181
  It's recommended to always run migrations on a test database and ensure they work
183
182
  before running them on any production database.
184
183
 
184
+ == Transactions
185
+
186
+ As mentioned above, Sequel attempts to run migrations inside of a transaction by default.
187
+ However, you can disable this on a per migration basis by calling +no_transaction+ inside
188
+ of the Sequel.migration block:
189
+
190
+ Sequel.migration do
191
+ no_transaction
192
+ change do
193
+ # ...
194
+ end
195
+ end
196
+
197
+ This is necessary in some cases, such as when attempting to use CREATE INDEX CONCURRENTLY
198
+ on PostgreSQL.
199
+
200
+ You can also override the transactions setting at the migrator level, either by forcing
201
+ transactions even if no_transaction is set, or by disabling transactions all together:
202
+
203
+ # Force transaction use
204
+ Sequel::Migrator.run(DB, '/path/to/migrations/dir', :use_transactions=>true)
205
+
206
+ # Disable use of transactions
207
+ Sequel::Migrator.run(DB, '/path/to/migrations/dir', :use_transactions=>false)
208
+
185
209
  == Migration files
186
210
 
187
211
  While you can create migration objects yourself and apply them manually, most of the
@@ -267,7 +291,7 @@ is important though, as it is used to mean that all migrations should be unappli
267
291
  === +TimestampMigrator+ Filenames
268
292
 
269
293
  With the +TimestampMigrator+, the version integer should represent a timestamp, though this isn't strictly
270
- required.
294
+ required.
271
295
 
272
296
  For example, for <tt>5/10/2010 12:00:00pm</tt>, you could use any of the following formats:
273
297
 
@@ -287,6 +311,9 @@ be difficult to unapply some the affected migrations correctly.
287
311
  The +TimestampMigrator+ will be used if any filename in the migrations directory has a version
288
312
  greater than 20000101. Otherwise, the +IntegerMigrator+ will be used.
289
313
 
314
+ You can force the use of the +TimestampMigrator+ in the API by calling TimestampMigrator.apply
315
+ instead of Migrator.apply.
316
+
290
317
  === How to choose
291
318
 
292
319
  Basically, unless you need the features provided by the +TimestampMigrator+, stick with the
@@ -322,8 +349,8 @@ you should give it some thought before using it.
322
349
 
323
350
  Just don't do it.
324
351
 
325
- In general, you should not modify any migration that has been run on the database and been committed
326
- the source control repository, unless the migration contains a error that causes data loss. As long
352
+ In general, you should not modify any migration that has been run on the database and been committed to
353
+ the source control repository, unless the migration contains an error that causes data loss. As long
327
354
  as it is possible to undo the migration without losing data, you should just add another migration
328
355
  that undoes the actions of the previous bad migration, and maybe does the correct action afterward.
329
356
 
@@ -358,7 +385,7 @@ or they should use the reversible migrations feature with a +change+ block:
358
385
  It's usually easy to determine what you should put in your migration's +up+ block,
359
386
  as it's whatever change you want to make to the database. The +down+ block is
360
387
  less obvious. In general, it should reverse the changes made by the +up+ block, which means
361
- it should execute the opposite of what the +up+ block does in the reverse order in which
388
+ it should execute the opposite of what the +up+ block does in the reverse order in which
362
389
  the +up+ block does it. Here's an example where you are switching from having a single
363
390
  artist per album to multiple artists per album:
364
391
 
@@ -402,12 +429,12 @@ artist per album to multiple artists per album:
402
429
  end
403
430
  end
404
431
 
405
- Note that the order in which things were done in the +down+ block is in
406
- reverse order to how they were done in the +up+ block. Also note how it
407
- isn't always possible to reverse exactly what was done in the +up+ block.
408
- You should try to do so as much as possible, but if you can't, you may
409
- want to have your +down+ block raise a <tt>Sequel::Error</tt> exception
410
- saying why the migration cannot be reverted.
432
+ Note that the operations performed in the +down+ block are performed in the
433
+ reverse order of how they are performed in the +up+ block. Also note how it
434
+ isn't always possible to reverse exactly what was done in the +up+ block. You
435
+ should try to do so as much as possible, but if you can't, you may want to have
436
+ your +down+ block raise a <tt>Sequel::Error</tt> exception saying why the
437
+ migration cannot be reverted.
411
438
 
412
439
  == Running migrations
413
440
 
@@ -345,6 +345,8 @@ The following additional options are supported:
345
345
  :encoding :: Set the client_encoding to the given string
346
346
  :connect_timeout :: Set the number of seconds to wait for a connection (default 20, only respected
347
347
  if using the pg library).
348
+ :sslmode :: Set to 'disable', 'allow', 'prefer', 'require' to choose how to treat SSL (only
349
+ respected if using the pg library)
348
350
 
349
351
  === sqlite
350
352
 
@@ -130,7 +130,7 @@
130
130
  * Database#<< and Dataset#<< now return self, which allow them
131
131
  to be used in chaining:
132
132
 
133
- DB << "UPADTE foo SET bar_id = NULL" << "DROP TABLE bars"
133
+ DB << "UPDATE foo SET bar_id = NULL" << "DROP TABLE bars"
134
134
  DB[:foo] << {:bar_id=>0} << DB[:bars].select(:id)
135
135
 
136
136
  * A Database#timezone accessor has been added, allowing you to
@@ -0,0 +1,157 @@
1
+ = New Features
2
+
3
+ * A server_block extension has been added that makes Sequel's
4
+ sharding support easier to use by scoping database access inside
5
+ the block to a given server/shard:
6
+
7
+ Sequel.extension :server_block
8
+ DB.extend Sequel::ServerBlock
9
+ DB.with_server(:shard_1) do
10
+ # All of these will execute against shard_1
11
+ DB.tables
12
+ DB[:table].all
13
+ DB.run 'SOME SQL'
14
+ end
15
+
16
+ * An arbitrary_servers extension has been added that extends
17
+ Sequel's sharding support so that you can use arbitrary
18
+ connection options instead of referencing an existing, predefined
19
+ server/shard:
20
+
21
+ Sequel.extension :arbitrary_servers
22
+ DB.pool.extend Sequel::ArbitraryServers
23
+ DB[:table].server(:host=>'foo', :database=>'bar').all
24
+
25
+ You can use this extension in conjunction with the server_block
26
+ extension:
27
+
28
+ DB.with_server(:host=>'foo', :database=>'bar') do
29
+ DB.synchronize do
30
+ # All of these will execute on host foo, database bar
31
+ DB.tables
32
+ DB[:table].all
33
+ DB.run 'SOME SQL'
34
+ end
35
+ end
36
+
37
+ The combination of these two extensions makes it pretty easy to
38
+ write a thread-safe Rack middleware that scopes each request
39
+ to an arbitrary database.
40
+
41
+ * The sqlite adapter now supports an integer_booleans setting
42
+ for using 1/0 for true/false values, instead of the the 't'/'f'
43
+ values used by default. As SQLite recommends using integers to
44
+ store booleans, converting your existing database and enabling
45
+ this setting is recommended, but for backwards compatibility it
46
+ is set to false. You can convert you existing database by doing
47
+ the following for each table/column that has booleans:
48
+
49
+ DB[:table].update(:boolean_column=>{'t'=>1}.
50
+ case(0, :boolean_column))
51
+
52
+ The integer_booleans default setting may change in a future
53
+ version of Sequel, so you should set it manually to false if you
54
+ prefer the current default.
55
+
56
+ * You can now disable transaction use in migrations, in one of two
57
+ ways. You generally only need to do this if you are using an
58
+ SQL query inside a migration that is specifically not supported
59
+ inside a transaction, such as CREATE INDEX CONCURRENTLY on
60
+ PostgreSQL.
61
+
62
+ The first way to disable transactions is on a per-migration basis
63
+ by calling the no_transaction method inside the Sequel.migration
64
+ block:
65
+
66
+ Sequel.migration do
67
+ no_transaction
68
+ change do
69
+ # ...
70
+ end
71
+ end
72
+
73
+ That will make it so that a transaction is not used for that
74
+ particular migration. The second way is passing the
75
+ :use_tranctions=>false option when calling Migrator.run (using
76
+ the API), which will completely disable transactions for all
77
+ migrations during the migrator run.
78
+
79
+ * The postgres adapter now respects an :sslmode option when using
80
+ pg as the underlying driver, you can set the value of this option to
81
+ disable, allow, prefer, or require.
82
+
83
+ * Database#create_schema and #drop_schema are now defined when
84
+ connecting to PostgreSQL.
85
+
86
+ * Database#supports_savepoints_in_prepared_transactions? has been
87
+ added for checking if savepoints are supported inside prepared
88
+ transactions. This is true if both savepoints and prepared
89
+ transactions are both supported, except on MySQL > 5.5.12 (due to
90
+ MySQL bug 64374).
91
+
92
+ = Other Improvements
93
+
94
+ * The mysql and mysql2 adapters now both provide an accurate number
95
+ of rows matched, so Sequel::Model usage on those adapters will now
96
+ raise a NoExistingObject exception by default if you attempt to
97
+ delete or update an instance that no longer exists in the database.
98
+
99
+ * Foreign key creation now works correctly without specifying the
100
+ :key option when using MySQL with the InnoDB table engine. InnoDB
101
+ requires that you list the column explicitly, even if you are
102
+ referencing the primary key of the table, so if the :key option is
103
+ not given, the database schema is introspected to find the primary
104
+ key for the table. If you are attempting to create a table with
105
+ a self-referential foreign key, it introspects the generator to
106
+ get the primary key for the table.
107
+
108
+ * The sqlite adapter will now return 1/0 stored in boolean columns as
109
+ true/false. It will convert dates stored as Integers/Floats to
110
+ Date objects by assuming they represent the julian date. It will
111
+ convert times stored as Integers/Floats to Sequel::SQLTime objects
112
+ by assuming they represent a number of seconds. It will convert
113
+ datetimes stored as Integers by assuming they represent a unix
114
+ epoch time integer, and datetimes stored as Floats by assuming the
115
+ represent the julian date (with fractional part representing the
116
+ time of day). These changes make Sequel handle SQLite's
117
+ recommendations for boolean/date/time storage.
118
+
119
+ * The instance_hooks plugin's (before|after)_*_hook methods now return
120
+ self so they can be used in a method chain.
121
+
122
+ * The list plugin now automatically adds new entries to the end of the
123
+ list when creating the entries, if the position field is not
124
+ specifically set.
125
+
126
+ * An identifier_output_method is now respected in the mysql2 adapter.
127
+
128
+ * NaN/Infinity Float values are now quoted correctly for input on
129
+ PostgreSQL, and the postgres adapter correctly handles them on
130
+ retrieval from the database.
131
+
132
+ * The :collate column option is now respected when creating tables or
133
+ altering columns on MySQL.
134
+
135
+ * You can now force use of the TimestampMigrator when the
136
+ IntegerMigrator would be used by default by calling
137
+ TimestampMigrator.apply or .run.
138
+
139
+ * Mock adapter usage with a specific SQL dialect now uses the
140
+ appropriate defaults for quoting identifiers.
141
+
142
+ * You can now disable the use of sudo in the rake install/uninstall
143
+ tasks using the SUDO='' environment variable.
144
+
145
+ * A very misleading error message has been fixed when attempting
146
+ to constantize an invalid string in the model inflector.
147
+
148
+ = Backwards Compatibility
149
+
150
+ * The sqlite adapter now typecasts columns that SQLite stores as
151
+ INTEGER/REAL. Previously, it only typecasted columns that
152
+ SQLite stored as TEXT/BLOB. For details about SQLite storage, see
153
+ http://www.sqlite.org/datatype3.html.
154
+
155
+ Any custom type conversion procs used with the sqlite adapter should
156
+ be modified to work with Integer/Float objects in addition to String
157
+ objects.
data/doc/sharding.rdoc CHANGED
@@ -8,13 +8,23 @@ that ship with Sequel.
8
8
 
9
9
  == The :servers Database option
10
10
 
11
- Both features use the :servers Database option. The :servers option should
12
- be a hash with symbol keys and values that are either hashes or procs that
13
- return hashes. These hashes are merged into the Database object's default
14
- options hash to get the connection options for the shard, so you don't need
15
- to override all options, just the ones that need to be modified. For example,
16
- if you are using the same user, password, and database name and just the
17
- host is changing, you only need a :host entry in each shard's hash.
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.
18
28
 
19
29
  Note that all servers should have the same schema for all
20
30
  tables you are accessing, unless you really know what you are doing.
@@ -159,3 +169,79 @@ work well with shards. You just need to remember to set to model to use the plu
159
169
  If all of your models are sharded, you can set all models to use the plugin via:
160
170
 
161
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.