@@ -407,10 +407,21 @@ production:
407
407
primary_shard_one:
408
408
database: my_primary_shard_one
409
409
adapter: mysql2
410
+ migrations_paths: db/migrate_shards
410
411
primary_shard_one_replica:
411
412
database: my_primary_shard_one
412
413
adapter: mysql2
413
414
replica: true
415
+ migrations_paths: db/migrate_shards
416
+ primary_shard_two:
417
+ database: my_primary_shard_two
418
+ adapter: mysql2
419
+ migrations_paths: db/migrate_shards
420
+ primary_shard_two_replica:
421
+ database: my_primary_shard_two
422
+ adapter: mysql2
423
+ replica: true
424
+ migrations_paths: db/migrate_shards
414
425
` ` `
415
426
416
427
Models are then connected with the `connects_to` API via the `shards` key :
@@ -419,18 +430,27 @@ Models are then connected with the `connects_to` API via the `shards` key:
419
430
class ApplicationRecord < ActiveRecord::Base
420
431
self.abstract_class = true
421
432
433
+ connects_to database: { writing: :primary, reading: :primary_replica }
434
+ end
435
+
436
+ class ShardRecord < ApplicationRecord
422
437
connects_to shards: {
423
- default: { writing: :primary, reading: :primary_replica },
424
438
shard_one: { writing: :primary_shard_one, reading: :primary_shard_one_replica }
439
+ shard_two: { writing: :primary_shard_two, reading: :primary_shard_two_replica }
425
440
}
426
441
end
427
442
` ` `
428
443
429
- You are not required to use `default` as the first shard name. Rails will assume the first
430
- shard name in the `connects_to` hash is the "default" connection. This connection is used
431
- internally to load type data and other information where the schema is the same across shards.
444
+ If you're using shards, make sure to set the `migrations_paths` to the same path for
445
+ all the shards. When generating a migration you can pass the `--database` option and
446
+ use one of the shard names. Since they all set the same path, it doesn't matter which
447
+ one you choose.
448
+
449
+ ```
450
+ $ bin/rails g scaffold Dog name: string --database primary_shard_one
451
+ ```
432
452
433
- Then models can swap connections manually via the `connected_to` API. If
453
+ Then models can swap shards manually via the `connected_to` API. If
434
454
using sharding, both a `role` and a `shard` must be passed:
435
455
436
456
```ruby
0 commit comments