Skip to content

Commit 35e0ae4

Browse files
committed
[ci skip] Fix shard docs
We never explained how migrations paths work for shards. This fixes that and also adds the appropriate class setup. You no longer need to set a `default` shard as of rails#48353. In addition, `ApplicationRecord` should be used for the non-sharded db that also serves as the tenant/shard router. Then shards should get their own connection class since the schema differs.
1 parent 2872d55 commit 35e0ae4

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

guides/source/active_record_multiple_databases.md

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -407,10 +407,21 @@ production:
407407
primary_shard_one:
408408
database: my_primary_shard_one
409409
adapter: mysql2
410+
migrations_paths: db/migrate_shards
410411
primary_shard_one_replica:
411412
database: my_primary_shard_one
412413
adapter: mysql2
413414
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
414425
```
415426

416427
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:
419430
class ApplicationRecord < ActiveRecord::Base
420431
self.abstract_class = true
421432
433+
connects_to database: { writing: :primary, reading: :primary_replica }
434+
end
435+
436+
class ShardRecord < ApplicationRecord
422437
connects_to shards: {
423-
default: { writing: :primary, reading: :primary_replica },
424438
shard_one: { writing: :primary_shard_one, reading: :primary_shard_one_replica }
439+
shard_two: { writing: :primary_shard_two, reading: :primary_shard_two_replica }
425440
}
426441
end
427442
```
428443

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+
```
432452
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
434454
using sharding, both a `role` and a `shard` must be passed:
435455
436456
```ruby

0 commit comments

Comments
 (0)