You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+24Lines changed: 24 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -142,6 +142,30 @@ Seed files can be run automatically using `rake db:seed_do`. There are two optio
142
142
143
143
You can also do a similar thing in your code by calling `SeedDo.seed(fixture_paths, filter)`.
144
144
145
+
## Bulk upsert
146
+
147
+
If you load a large amount of seed data, you can enable bulk upsert mode:
148
+
149
+
```ruby
150
+
SeedDo.seed(bulk:true)
151
+
```
152
+
153
+
In bulk mode, SeedDo buffers seed data for each file and writes it with `upsert_all`, which can significantly reduce the number of queries.
154
+
155
+
You can also control the batch size per `upsert_all` call. The default is `1000`.
156
+
157
+
```ruby
158
+
SeedDo.seed(bulk: { batch_size:100 })
159
+
```
160
+
161
+
### Requirements and caveats
162
+
163
+
- Bulk mode relies on Rails `upsert_all`
164
+
- The seed constraints are passed to `unique_by`, so the related columns must have a unique index
165
+
- Bulk mode is supported only on databases that support conflict targets, such as PostgreSQL and SQLite
166
+
- Bulk mode is not available on MySQL
167
+
-`SeedDo.seed` and `SeedDo.seed(bulk: true)` may not always produce the same record order, so verify the behavior carefully before enabling it in an existing production environment
168
+
145
169
## Disable output
146
170
147
171
To disable output from Seed Do, set `SeedDo.quiet = true`.
Copy file name to clipboardExpand all lines: lib/seed-do/runner.rb
+38-17Lines changed: 38 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -3,28 +3,39 @@
3
3
4
4
moduleSeedDo
5
5
# Runs seed files.
6
-
#
7
-
# It is not recommended to use this class directly. Instead, use {SeedDo.seed SeedDo.seed}, which creates
8
-
# an instead of {Runner} and calls {#run #run}.
6
+
# It is not recommended to use this class directly. Instead, use {SeedDo.seed SeedDo.seed}, which creates an instead of {Runner} and calls {#run #run}.
9
7
#
10
8
# @see SeedDo.seed SeedDo.seed
11
9
classRunner
12
10
# @param [Array<String>] fixture_paths The paths where fixtures are located. Will use
13
11
# `SeedDo.fixture_paths` if {nil}. If the argument is not an array, it will be wrapped by one.
14
12
# @param [Regexp] filter If given, only seed files with a file name matching this pattern will
15
13
# be used
16
-
definitialize(fixture_paths=nil,filter=nil)
14
+
# @param [Boolean, Hash] bulk If true, use upsert_all to insert/update records in bulk.
15
+
# If a hash, can include :batch_size (default: 1000) to control the number of records
skip'Bulk mode is not supported on databases without insert conflict target support'unlessActiveRecord::Base.connection.supports_insert_conflict_target?
0 commit comments