seed_dump 3.3.1 → 3.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b3ef68666d03715b3fb9f373025af97819527ce9f2cf2c68cca2237b9453901a
4
- data.tar.gz: 444459b826e138ab26977b25d0b94439f42a3debbfbab922a40bb88a64a6e1d3
3
+ metadata.gz: e51de5d37ae0d9f14a4c6126a53e589e20ab504e6fb79bff59e61e4950bb4633
4
+ data.tar.gz: edfad4053c8dafa40d45de0a6b01fbf6dd97645945015d68887be0ee38ebb640
5
5
  SHA512:
6
- metadata.gz: 526f868754d161d41ee9771502b35c1aa6658b695a7f7a8eca695b2515da5926f2fa02f6c1aff478cb5f5d07e0d1842ed1221d0900df4fe7f50267dace0de0a8
7
- data.tar.gz: 0b6629fb2b2baea086bb16d76b36b02d659f77f1330b351d7a311b8658e4ad993f14121fd04267ba4d3eadc7b301c04092b776276317c74068ddb015bc83380e
6
+ metadata.gz: 4fa4f77d53981389543d97e2295cc0945effdc5360fdfce1d10beedfda43ca6116763b7f05105ba928d45e903bb568f78ae71ace57a5dbab9fcf3d23b919d590
7
+ data.tar.gz: 3cec4ce1c103182cb46f1c53e126c1563911efb7253bd783eb9d623c07d7efc9323072a51de8bd006002ebc347a163ce78a464d4a0a8660dc20781c8b4e3dd40
data/README.md CHANGED
@@ -1,7 +1,6 @@
1
- Seed Dump
2
- ========
1
+ # Seed Dump
3
2
 
4
- Seed Dump is a Rails 4 and 5 plugin that adds a rake task named `db:seed:dump`.
3
+ Seed Dump is a Rails plugin (compatible with **Rails 4 through 8+**) that adds a rake task named `db:seed:dump`.
5
4
 
6
5
  It allows you to create seed data files from the existing data in your database.
7
6
 
@@ -9,8 +8,7 @@ You can also use Seed Dump from the Rails console. See below for usage examples.
9
8
 
10
9
  Note: if you want to use Seed Dump with Rails 3 or earlier, use [version 0.5.3](http://rubygems.org/gems/seed_dump/versions/0.5.3).
11
10
 
12
- Installation
13
- ------------
11
+ ## Installation
14
12
 
15
13
  Add it to your Gemfile with:
16
14
  ```ruby
@@ -20,8 +18,7 @@ Or install it by hand:
20
18
  ```sh
21
19
  $ gem install seed_dump
22
20
  ```
23
- Examples
24
- --------
21
+ ## Examples
25
22
 
26
23
  ### Rake task
27
24
 
@@ -98,34 +95,73 @@ irb(main):004:0> SeedDump.dump(User, exclude: [:name, :age])
98
95
 
99
96
  Options are specified as a Hash for the second argument.
100
97
 
101
- In the console, any relation of ActiveRecord rows can be dumped (not individual objects though)
98
+ In the console, any relation of ActiveRecord rows can be dumped (not individual objects though):
102
99
  ```ruby
103
- irb(main):001:0> puts SeedDump.dump(User.where(is_admin: false))
100
+ irb(main):005:0> puts SeedDump.dump(User.where(is_admin: false))
104
101
  User.create!([
105
102
  { password: "123456", username: "test_1", is_admin: false },
106
103
  { password: "234567", username: "test_2", is_admin: false }
107
104
  ])
108
105
  ```
109
106
 
110
- Options
111
- -------
107
+ ## Options
112
108
 
113
109
  Options are common to both the Rake task and the console, except where noted.
114
110
 
115
111
  `append`: If set to `true`, append the data to the file instead of overwriting it. Default: `false`.
116
112
 
117
- `batch_size`: Controls the number of records that are written to file at a given time. Default: 1000. If you're running out of memory when dumping, try decreasing this. If things are dumping too slow, trying increasing this.
113
+ `batch_size`: Controls the number of records that are processed and written at a given time. Default: 1000. If you're running out of memory when dumping, try decreasing this. If things are dumping too slow, trying increasing this.
118
114
 
119
- `exclude`: Attributes to be excluded from the dump. Pass a comma-separated list to the Rake task (i.e. `name,age`) and an array on the console (i.e. `[:name, :age]`). Default: `[:id, :created_at, :updated_at]`.
115
+ `exclude`: Attributes to be excluded from the dump. Pass a comma-separated list to the Rake task (e.g., `EXCLUDE=name,age`) and an array of symbols on the console (e.g., `exclude: [:name, :age]`). Default: `[:id, :created_at, :updated_at, :created_on, :updated_on]`.
120
116
 
121
- `file`: Write to the specified output file. The Rake task default is `db/seeds.rb`. The console returns the dump as a string by default.
117
+ `file`: Write to the specified output file. The Rake task default is `db/seeds.rb`. The console returns the dump as a string by default if this option is omitted.
122
118
 
123
- `import`: If `true`, output will be in the format needed by the [activerecord-import](https://github.com/zdennis/activerecord-import) gem, rather than the default format. Default: `false`.
119
+ `group_sti_by_class`: If `true`, Single Table Inheritance (STI) records are grouped by their actual class (e.g., `Dog`, `Cat`) instead of the base class (e.g., `Animal`). This is necessary when STI subclasses have different enum definitions or other class-specific attributes that would be lost if dumped via the base class. Default: `false`. Example: `rake db:seed:dump GROUP_STI_BY_CLASS=true` or `SeedDump.dump(Animal, group_sti_by_class: true)`. See the STI Handling section below for more details.
124
120
 
125
- `limit`: Dump no more than this amount of data. Default: no limit. Rake task only. In the console just pass in an ActiveRecord::Relation with the appropriate limit (e.g. `SeedDump.dump(User.limit(5))`).
121
+ `header`: If `true`, adds a comment header to the output file showing the seed_dump command and options used. If a string, uses that string as the header comment. Default: `false`. **Rake task only.** Example: `rake db:seed:dump HEADER=true` or `HEADER="Generated by seed_dump"`.
126
122
 
127
- `conditions`: Dump only specific records. In the console just pass in an ActiveRecord::Relation with the appropriate conditions (e.g. `SeedDump.dump(User.where(state: :active))`).
123
+ `import`: If `true`, output will be in the format needed by the [activerecord-import](https://github.com/zdennis/activerecord-import) gem, rather than the default format. You can also pass a Hash of options which will be passed through to the `import` call (e.g., `IMPORT='{ "validate": false }'` for Rake, or `import: { validate: false }` for console). Default: `false`.
128
124
 
129
- `model[s]`: Restrict the dump to the specified comma-separated list of models. Default: all models. If you are using a Rails engine you can dump a specific model by passing "EngineName::ModelName". Rake task only. Example: `rake db:seed:dump MODELS="User, Position, Function"`
125
+ `include_all`: If set to `true`, include all columns in the dump (including `id`, `created_at`, and `updated_at`). Equivalent to `EXCLUDE=""`. Default: `false`. **Rake task only.** Example: `rake db:seed:dump INCLUDE_ALL=true`
130
126
 
131
- `models_exclude`: Exclude the specified comma-separated list of models from the dump. Default: no models excluded. Rake task only. Example: `rake db:seed:dump MODELS_EXCLUDE="User"`
127
+ `insert_all`: If `true`, output will use Rails 6+ [`insert_all`](https://api.rubyonrails.org/classes/ActiveRecord/Persistence/ClassMethods.html#method-i-insert_all) for faster bulk inserts that bypass validations and callbacks. Default: `false`. Example: `rake db:seed:dump INSERT_ALL=true` or `SeedDump.dump(User, insert_all: true)`.
128
+
129
+ `limit`: Dump no more than this amount of data *per model*. Default: no limit. **Rake task only.** In the console, just pass in an ActiveRecord::Relation with the appropriate limit (e.g., `SeedDump.dump(User.limit(5))`).
130
+
131
+ `model_limits`: Set different limits for specific models. Format: `Model1:limit1,Model2:limit2`. Use `0` to mean "no limit" for a specific model. This is useful when `LIMIT` would break foreign key relationships. **Rake task only.** Example: `rake db:seed:dump LIMIT=10 MODEL_LIMITS="Teacher:0,Student:50"` dumps all Teachers, 50 Students, and 10 of everything else.
132
+
133
+ `model[s]`: Restrict the dump to the specified comma-separated list of models. Default: all models that have data. If you are using a Rails engine you can dump a specific model by passing "EngineName::ModelName". **Rake task only.** Example: `rake db:seed:dump MODELS="User, Position, Function"`
134
+
135
+ `models_exclude`: Exclude the specified comma-separated list of models from the dump. Default: no models excluded. **Rake task only.** Example: `rake db:seed:dump MODELS_EXCLUDE="User"`
136
+
137
+ `upsert_all`: If `true`, output will use Rails 6+ [`upsert_all`](https://api.rubyonrails.org/classes/ActiveRecord/Persistence/ClassMethods.html#method-i-upsert_all) which preserves record IDs and handles conflicts by updating existing records. This is useful when you need to maintain foreign key relationships or want idempotent seed files. Automatically includes `id` in the dump. Default: `false`. Example: `rake db:seed:dump UPSERT_ALL=true` or `SeedDump.dump(User, upsert_all: true)`.
138
+
139
+ ## Automatic Behaviors
140
+
141
+ **Foreign Key Ordering**: Models are automatically dumped in dependency order based on foreign key relationships. This ensures that parent records are created before child records that reference them.
142
+
143
+ **STI Handling**: Single Table Inheritance (STI) models are automatically deduplicated. By default, only the base class is dumped to avoid duplicate records (e.g., `Animal.create!` for both `Dog` and `Cat` records). However, if your STI subclasses have different enum definitions or other class-specific attributes, use the `group_sti_by_class: true` option to dump each subclass separately (e.g., `Dog.create!` and `Cat.create!`). This ensures that subclass-specific type casting and validations are properly applied when the seed data is loaded.
144
+
145
+ **HABTM Handling**: Has-and-belongs-to-many join tables are automatically detected and dumped without duplication.
146
+
147
+ ## Usage Outside of Rails
148
+
149
+ If you're using ActiveRecord outside of Rails (e.g., with [standalone-migrations](https://github.com/thuss/standalone-migrations)), you can set up a custom Rake task:
150
+
151
+ ```ruby
152
+ # In your Rakefile
153
+ $LOAD_PATH.unshift(File.expand_path('app/models', __dir__))
154
+ Dir.glob(File.expand_path('app/models/*.rb', __dir__)).sort.each(&method(:require))
155
+ require 'seed_dump'
156
+
157
+ namespace :db do
158
+ namespace :seed do
159
+ desc "Dump records from the database into db/seeds.rb"
160
+ task :dump => :environment do
161
+ SeedDump.dump_using_environment(ENV)
162
+ end
163
+ end
164
+ end
165
+ ```
166
+
167
+ This loads your models and creates a `db:seed:dump` task that works like the Rails version.
@@ -3,9 +3,11 @@ class SeedDump
3
3
  module Enumeration
4
4
  def active_record_enumeration(records, io, options)
5
5
  # If the records don't already have an order,
6
- # order them by primary key ascending.
6
+ # order them by primary key ascending (if the table has a primary key).
7
7
  if !records.respond_to?(:arel) || records.arel.orders.blank?
8
- records.order("#{records.quoted_table_name}.#{records.quoted_primary_key} ASC")
8
+ if records.primary_key.present?
9
+ records = records.order(records.primary_key => :asc)
10
+ end
9
11
  end
10
12
 
11
13
  num_of_batches, batch_size, last_batch_size = batch_params_from(records, options)
@@ -56,7 +58,13 @@ class SeedDump
56
58
  def batch_params_from(records, options)
57
59
  batch_size = batch_size_from(records, options)
58
60
 
59
- count = records.count
61
+ # Use unscope(:select) to avoid issues with default_scope that selects
62
+ # specific columns, which would cause COUNT(col1, col2, ...) errors
63
+ count = if records.respond_to?(:unscope)
64
+ records.unscope(:select).count
65
+ else
66
+ records.count
67
+ end
60
68
 
61
69
  remainder = count % batch_size
62
70