Enable bulk upsert in SeedDo.seed#26
Merged
Merged
Conversation
Owner
Author
|
Points to consider
|
b98cc32 to
35ef885
Compare
Owner
Author
For the first one, I kept it as it is because I couldn’t think of a better way. For the second one, I assumed users define a large number of seeds, so I chose a method that sets the batch size. |
8c42956 to
49974df
Compare
396ab43 to
37459d9
Compare
Add a `bulk: true` option to the `SeedDo.seed` method. Normally, `SeedDo.seed` runs queries for each model, but `SeedDo.seed(bulk: true)` combines multiple queries into one. This should give a large performance improvement when a project defines a lot of seed data.
Allow the number of records in each query to be set with the `batch_size` option. The default is 1000.
```ruby
SeedDo.seed(bulk: { batch_size: 100 })
```
## Requirements
Use Rails [upsert_all](https://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-upsert_all) internally to implement this feature. To keep the behavior close to the existing seed-do behavior, use the `:unique_by` option, so the related constraints must have unique indexes. Also, `:unique_by` is supported only by PostgreSQL and SQLite, so this feature is not available on MySQL.
## Notes
`SeedDo.seed` and `SeedDo.seed(bulk: true)` do not always produce the same results.
For example, with the following seed definition:
```ruby
User.seed(:name) do |u|
u.name = 'first'
end
User.create!(name: 'second')
User.seed(:name) do |u|
u.name = 'third'
end
```
With `SeedDo.seed`, records are created in the order `first`, `second`, `third`. However, with `SeedDo.seed(bulk: true)`, the order becomes `second`, `first`, `third`.
Because of this, verify the behavior carefully before using `SeedDo.seed(bulk: true)` in an existing production environment.
## Breaking Change
This change also updates the interface of `SeedDo::Seeder#new`. In particular, the `quiet` and `insert_only` options can no longer be passed to `SeedDo::Seeder.new`. Since `SeedDo::Seeder` is an internal class, normal usage through `SeedDo.seed` is expected to keep working, but any code that calls `SeedDo::Seeder.new` directly will need to be updated.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add a
bulk: trueoption to theSeedDo.seedmethod. Normally,SeedDo.seedruns queries for each model, butSeedDo.seed(bulk: true)combines multiple queries into one. This should give a large performance improvement when a project defines a lot of seed data.Allow the number of records in each query to be set with the
batch_sizeoption. The default is 1000.Requirements
Use Rails upsert_all internally to implement this feature. To keep the behavior close to the existing seed-do behavior, use the
:unique_byoption, so the related constraints must have unique indexes. Also,:unique_byis supported only by PostgreSQL and SQLite, so this feature is not available on MySQL.Notes
SeedDo.seedandSeedDo.seed(bulk: true)do not always produce the same results.For example, with the following seed definition:
With
SeedDo.seed, records are created in the orderfirst,second,third. However, withSeedDo.seed(bulk: true), the order becomessecond,first,third.Because of this, verify the behavior carefully before using
SeedDo.seed(bulk: true)in an existing production environment.Breaking Change
This change also updates the interface of
SeedDo::Seeder#new. In particular, thequietandinsert_onlyoptions can no longer be passed toSeedDo::Seeder.new. SinceSeedDo::Seederis an internal class, normal usage throughSeedDo.seedis expected to keep working, but any code that callsSeedDo::Seeder.newdirectly will need to be updated.