dekiru-data_migration 1.3.1 → 1.4.0
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 +4 -4
- data/.rubocop.yml +5 -0
- data/README.md +11 -56
- data/lib/dekiru/data_migration/migration.rb +1 -1
- data/lib/dekiru/data_migration/operator.rb +6 -0
- data/lib/dekiru/data_migration/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2be3a7af7bab104c9eb7c8461e408d82f0d1605bd5dc882bcd8b011a2b7fb646
|
|
4
|
+
data.tar.gz: 9467fcfce4288679751724dae8968b586b04beb3383026cb55f4c48139d4102a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9020fda17cf13c7fc29d4d9bb4f03c543ef5b6516cd80012b864539295e0f7cfc418ded1915e4eaf926ddbdc47f9d65f8376f8c790077a6d174669ae60cf952e
|
|
7
|
+
data.tar.gz: 4932cce5481c646c3f48f8879a8062efc7c990fa04280e47a5fe65db8515e7921f1bdadbfbc01c6869451a40d8b866859bd4ef4bcb0bf04d62ee93db7735b66e
|
data/.rubocop.yml
CHANGED
data/README.md
CHANGED
|
@@ -32,27 +32,9 @@ Or install it yourself as:
|
|
|
32
32
|
$ gem install dekiru-data_migration
|
|
33
33
|
```
|
|
34
34
|
|
|
35
|
-
## Data Migration
|
|
35
|
+
## Data Migration Class
|
|
36
36
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
```ruby
|
|
40
|
-
# scripts/demo.rb
|
|
41
|
-
Dekiru::DataMigration::Operator.execute('Grant admin privileges to users') do
|
|
42
|
-
targets = User.where("email LIKE '%sonicgarden%'")
|
|
43
|
-
|
|
44
|
-
log "Target user count: #{targets.count}"
|
|
45
|
-
find_each_with_progress(targets) do |user|
|
|
46
|
-
user.update!(admin: true)
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
log "Updated user count: #{User.where("email LIKE '%sonicgarden%'").where(admin: true).count}"
|
|
50
|
-
end
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
## Data Migration Class (Recommended)
|
|
54
|
-
|
|
55
|
-
You can also define migration logic as a class, which makes testing easier:
|
|
37
|
+
Define migration logic as a class:
|
|
56
38
|
|
|
57
39
|
```ruby
|
|
58
40
|
# scripts/20230118_demo_migration.rb
|
|
@@ -95,20 +77,6 @@ end
|
|
|
95
77
|
DeactivateStaleUsersMigration.run
|
|
96
78
|
```
|
|
97
79
|
|
|
98
|
-
If you use the block-based `Operator` directly, pass `in_batches` to `each_with_progress`:
|
|
99
|
-
|
|
100
|
-
```ruby
|
|
101
|
-
# scripts/deactivate_stale_users.rb
|
|
102
|
-
Dekiru::DataMigration::Operator.execute('Deactivate stale users') do
|
|
103
|
-
targets = User.where(active: true).where("last_login_at < ?", 1.year.ago)
|
|
104
|
-
|
|
105
|
-
log "Target user count: #{targets.count}"
|
|
106
|
-
each_with_progress(targets.in_batches) do |batch|
|
|
107
|
-
batch.update_all(active: false)
|
|
108
|
-
end
|
|
109
|
-
end
|
|
110
|
-
```
|
|
111
|
-
|
|
112
80
|
#### Specifying the batch size
|
|
113
81
|
|
|
114
82
|
Pass `batch_size` to `run` to control how many records are fetched per batch. It applies to both paths — `in_batches(of:)` for `migrate_batch` and `find_each(batch_size:)` for `migrate_record`. When omitted, ActiveRecord's default of 1000 is used.
|
|
@@ -164,12 +132,10 @@ Total time: 6.35 sec
|
|
|
164
132
|
|
|
165
133
|
## Side Effect Monitoring
|
|
166
134
|
|
|
167
|
-
By
|
|
135
|
+
By passing `warning_side_effects: true` to `run`, side effects that occur during data migration tasks (database writes, job enqueuing, email sending, etc.) will be displayed.
|
|
168
136
|
|
|
169
137
|
```ruby
|
|
170
|
-
|
|
171
|
-
# Processing content...
|
|
172
|
-
end
|
|
138
|
+
DemoMigration.run(warning_side_effects: true)
|
|
173
139
|
```
|
|
174
140
|
|
|
175
141
|
Execution result:
|
|
@@ -208,14 +174,14 @@ Generated file example:
|
|
|
208
174
|
|
|
209
175
|
class DemoMigration < Dekiru::DataMigration::Migration
|
|
210
176
|
def migration_targets
|
|
211
|
-
#
|
|
212
|
-
#
|
|
177
|
+
# Return an ActiveRecord::Relation of records to migrate
|
|
178
|
+
# e.g. User.where(some_condition: true)
|
|
213
179
|
raise NotImplementedError, 'migration_targets method must be implemented'
|
|
214
180
|
end
|
|
215
181
|
|
|
216
182
|
def migrate_record(record)
|
|
217
|
-
#
|
|
218
|
-
#
|
|
183
|
+
# Define the update logic for each record
|
|
184
|
+
# e.g. record.update!(some_attribute: new_value)
|
|
219
185
|
raise NotImplementedError, 'migrate_record method must be implemented'
|
|
220
186
|
end
|
|
221
187
|
end
|
|
@@ -223,17 +189,6 @@ end
|
|
|
223
189
|
DemoMigration.run
|
|
224
190
|
```
|
|
225
191
|
|
|
226
|
-
### Legacy Block-based Approach
|
|
227
|
-
|
|
228
|
-
For backward compatibility, you can still use the block-based approach:
|
|
229
|
-
|
|
230
|
-
```ruby
|
|
231
|
-
# scripts/legacy_demo.rb
|
|
232
|
-
Dekiru::DataMigration::Operator.execute('demo_migration') do
|
|
233
|
-
# write here
|
|
234
|
-
end
|
|
235
|
-
```
|
|
236
|
-
|
|
237
192
|
### Output Directory Configuration
|
|
238
193
|
|
|
239
194
|
The output directory for files is by default the `scripts` directory directly under the application root. You can change the output directory through configuration.
|
|
@@ -311,10 +266,10 @@ end
|
|
|
311
266
|
|
|
312
267
|
### Runtime Options
|
|
313
268
|
|
|
269
|
+
Pass options to `Migration.run`:
|
|
270
|
+
|
|
314
271
|
```ruby
|
|
315
|
-
|
|
316
|
-
# Processing content
|
|
317
|
-
end
|
|
272
|
+
DemoMigration.run(warning_side_effects: true, without_transaction: false)
|
|
318
273
|
```
|
|
319
274
|
|
|
320
275
|
Available options:
|
|
@@ -16,7 +16,7 @@ module Dekiru
|
|
|
16
16
|
options = options.dup
|
|
17
17
|
migration.batch_size = options.delete(:batch_size)
|
|
18
18
|
|
|
19
|
-
Operator.
|
|
19
|
+
Operator.new(title, options).execute do
|
|
20
20
|
migration.instance_variable_set(:@operator_context, self)
|
|
21
21
|
migration.migrate
|
|
22
22
|
end
|
|
@@ -12,7 +12,13 @@ module Dekiru
|
|
|
12
12
|
|
|
13
13
|
attr_reader :title, :stream, :logger, :result, :canceled, :started_at, :ended_at, :error
|
|
14
14
|
|
|
15
|
+
DEPRECATOR = ActiveSupport::Deprecation.new("2.0", "Dekiru::DataMigration")
|
|
16
|
+
|
|
15
17
|
def self.execute(title, options = {}, &block)
|
|
18
|
+
DEPRECATOR.warn(
|
|
19
|
+
"Dekiru::DataMigration::Operator.execute is deprecated and will be removed in 2.0. " \
|
|
20
|
+
"Use Dekiru::DataMigration::Migration subclass instead."
|
|
21
|
+
)
|
|
16
22
|
new(title, options).execute(&block)
|
|
17
23
|
end
|
|
18
24
|
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: dekiru-data_migration
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- SonicGarden
|
|
@@ -88,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
88
88
|
- !ruby/object:Gem::Version
|
|
89
89
|
version: '0'
|
|
90
90
|
requirements: []
|
|
91
|
-
rubygems_version: 4.0.
|
|
91
|
+
rubygems_version: 4.0.15
|
|
92
92
|
specification_version: 4
|
|
93
93
|
summary: A Ruby on Rails library for executing data migration tasks safely and efficiently.
|
|
94
94
|
test_files: []
|