data-migration 1.1.0 → 1.3.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/CHANGELOG.md +13 -2
- data/Gemfile.lock +1 -1
- data/README.md +11 -5
- data/lib/data-migration.rb +2 -2
- data/lib/data_migration/config.rb +2 -2
- data/lib/data_migration/job.rb +1 -1
- data/lib/data_migration/task.rb +12 -6
- data/lib/generators/data_migration/install_generator.rb +28 -0
- data/lib/generators/{templates → data_migration/templates}/install_data_migration_tasks.rb.tt +4 -0
- data/spec/fixtures/data_migrations/20241206200114_create_batch_users.rb +1 -1
- data/spec/generators/install_generator_spec.rb +2 -2
- metadata +4 -4
- data/lib/generators/install_generator.rb +0 -17
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c5f01e486573054202526bf5fa21c9acb04b7c1093928cc5c1f1fa42a5d04a67
|
|
4
|
+
data.tar.gz: dbe195a0ee725c658de65fd75190f38a7fe86cc74564945c75388e04de906f1c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 92812fe8790d454531b66618f6f142f69dec5a3e46e75bdb7e22bf914c10c9e23739709a5e300a750882b7c806edb604da5eb023332a6133e8de332beb6963b5
|
|
7
|
+
data.tar.gz: f2109fed76e5508ff160f87109d5c4fefa011233257248b8dcd4ca678b5594e27ef85d8883c9a08c0583485748a769299c02bc09e8cd86ca47250530cbc95fee
|
data/CHANGELOG.md
CHANGED
|
@@ -1,7 +1,18 @@
|
|
|
1
|
-
#
|
|
1
|
+
# CHANGELOG
|
|
2
|
+
|
|
3
|
+
## 1.3.0
|
|
4
|
+
|
|
5
|
+
- Fix data migration tasks table generator
|
|
6
|
+
- Update README with better examples
|
|
7
|
+
|
|
8
|
+
## 1.2.0
|
|
9
|
+
|
|
10
|
+
- Wrap Task.status enum with ActiveRecord version check
|
|
11
|
+
|
|
12
|
+
## 1.1.0
|
|
2
13
|
|
|
3
14
|
- Fix gem main file name to be `data-migration.rb` instead of `data_migration.rb`
|
|
4
15
|
|
|
5
|
-
|
|
16
|
+
## 1.0.0
|
|
6
17
|
|
|
7
18
|
- Initial version
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
|
@@ -39,26 +39,32 @@ Using RubyGems:
|
|
|
39
39
|
gem install data-migration
|
|
40
40
|
```
|
|
41
41
|
|
|
42
|
-
|
|
42
|
+
### Gemfile
|
|
43
43
|
|
|
44
44
|
```ruby
|
|
45
45
|
gem "data-migration"
|
|
46
46
|
```
|
|
47
47
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
### Run data migrations
|
|
48
|
+
### Data migration tasks table
|
|
51
49
|
|
|
52
50
|
```sh
|
|
53
|
-
bin/rails
|
|
51
|
+
bin/rails g data_migration:install data_migration_tasks
|
|
54
52
|
```
|
|
55
53
|
|
|
54
|
+
## Usage
|
|
55
|
+
|
|
56
56
|
### Generate data migration job
|
|
57
57
|
|
|
58
58
|
```sh
|
|
59
59
|
bin/rails g data_migration create_users
|
|
60
60
|
```
|
|
61
61
|
|
|
62
|
+
### Run data migrations
|
|
63
|
+
|
|
64
|
+
```sh
|
|
65
|
+
bin/rails db:migrate:data 20241207120000_create_users
|
|
66
|
+
```
|
|
67
|
+
|
|
62
68
|
## Configuration
|
|
63
69
|
|
|
64
70
|
### Set data migrations directory
|
data/lib/data-migration.rb
CHANGED
|
@@ -3,7 +3,7 @@ require "data_migration/job"
|
|
|
3
3
|
require "data_migration/task"
|
|
4
4
|
|
|
5
5
|
module DataMigration
|
|
6
|
-
VERSION = "1.
|
|
6
|
+
VERSION = "1.3.0".freeze
|
|
7
7
|
|
|
8
8
|
module_function
|
|
9
9
|
|
|
@@ -17,7 +17,7 @@ module DataMigration
|
|
|
17
17
|
|
|
18
18
|
def notify(message, context: {})
|
|
19
19
|
if Object.const_defined?(:ActionReporter)
|
|
20
|
-
ActionReporter.notify(message, context:)
|
|
20
|
+
ActionReporter.notify(message, context: context.presence || {})
|
|
21
21
|
elsif Object.const_defined?(:Rails)
|
|
22
22
|
Rails.logger.info("#{message} #{context.inspect}")
|
|
23
23
|
end
|
|
@@ -35,7 +35,7 @@ module DataMigration
|
|
|
35
35
|
|
|
36
36
|
attr_writer :operator_resolver
|
|
37
37
|
def operator_resolver(&block)
|
|
38
|
-
if
|
|
38
|
+
if block.present?
|
|
39
39
|
@operator_resolver = block
|
|
40
40
|
else
|
|
41
41
|
@operator_resolver ||= -> do
|
|
@@ -50,7 +50,7 @@ module DataMigration
|
|
|
50
50
|
|
|
51
51
|
attr_writer :monitoring_context
|
|
52
52
|
def monitoring_context(&block)
|
|
53
|
-
if
|
|
53
|
+
if block.present?
|
|
54
54
|
@monitoring_context = block
|
|
55
55
|
else
|
|
56
56
|
@monitoring_context ||= ->(migration) do
|
data/lib/data_migration/job.rb
CHANGED
data/lib/data_migration/task.rb
CHANGED
|
@@ -2,6 +2,7 @@ require "active_record"
|
|
|
2
2
|
|
|
3
3
|
module DataMigration
|
|
4
4
|
class JobConcurrencyLimitError < StandardError; end
|
|
5
|
+
|
|
5
6
|
class JobConflictError < StandardError; end
|
|
6
7
|
|
|
7
8
|
def self.tasks_table_name
|
|
@@ -18,16 +19,21 @@ module DataMigration
|
|
|
18
19
|
self.jobs_limit ||= DataMigration.config.default_jobs_limit
|
|
19
20
|
end
|
|
20
21
|
|
|
21
|
-
|
|
22
|
+
STATUS_OPTIONS = {
|
|
22
23
|
started: "started",
|
|
23
24
|
performing: "performing",
|
|
24
25
|
paused: "paused",
|
|
25
26
|
completed: "completed"
|
|
26
27
|
}
|
|
28
|
+
if ActiveRecord::VERSION::MAJOR >= 7
|
|
29
|
+
enum :status, STATUS_OPTIONS
|
|
30
|
+
else
|
|
31
|
+
enum status: STATUS_OPTIONS
|
|
32
|
+
end
|
|
27
33
|
|
|
28
34
|
validates :name, presence: true
|
|
29
|
-
validates :pause_minutes, numericality: {
|
|
30
|
-
validates :jobs_limit, numericality: {
|
|
35
|
+
validates :pause_minutes, numericality: {greater_than_or_equal_to: 0, only_integer: true}, if: -> { pause_minutes.present? }
|
|
36
|
+
validates :jobs_limit, numericality: {greater_than_or_equal_to: 0, only_integer: true}, if: -> { jobs_limit.present? }
|
|
31
37
|
validate :file_should_exist
|
|
32
38
|
|
|
33
39
|
after_save do
|
|
@@ -50,7 +56,7 @@ module DataMigration
|
|
|
50
56
|
end
|
|
51
57
|
|
|
52
58
|
def self.perform_now(name, **kwargs)
|
|
53
|
-
create!(name:).perform_now(**kwargs)
|
|
59
|
+
create!(name: name).perform_now(**kwargs)
|
|
54
60
|
end
|
|
55
61
|
|
|
56
62
|
def perform_now(**perform_args)
|
|
@@ -59,7 +65,7 @@ module DataMigration
|
|
|
59
65
|
end
|
|
60
66
|
|
|
61
67
|
def self.perform_later(name, **kwargs)
|
|
62
|
-
create!(name:).perform_later(**kwargs)
|
|
68
|
+
create!(name: name).perform_later(**kwargs)
|
|
63
69
|
end
|
|
64
70
|
|
|
65
71
|
def perform_later(**perform_args)
|
|
@@ -68,7 +74,7 @@ module DataMigration
|
|
|
68
74
|
end
|
|
69
75
|
|
|
70
76
|
def self.prepare(name, pause_minutes: nil, jobs_limit: nil)
|
|
71
|
-
create!(name
|
|
77
|
+
create!(name: name, pause_minutes: pause_minutes, jobs_limit: jobs_limit)
|
|
72
78
|
end
|
|
73
79
|
|
|
74
80
|
def self.root_path
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
require "rails/generators"
|
|
2
|
+
require "rails/generators/active_record"
|
|
3
|
+
require "rails/generators/active_record/migration/migration_generator"
|
|
4
|
+
|
|
5
|
+
module DataMigration
|
|
6
|
+
module Generators
|
|
7
|
+
class InstallGenerator < ActiveRecord::Generators::MigrationGenerator
|
|
8
|
+
source_root File.expand_path("../templates", __FILE__)
|
|
9
|
+
|
|
10
|
+
attr_reader :table_exists, :table_columns
|
|
11
|
+
def create_migration_file
|
|
12
|
+
if ActiveRecord::Base.connection.table_exists?(name)
|
|
13
|
+
puts "\e[31mWARNING: Table `#{name}` already exists\e[0m"
|
|
14
|
+
@table_exists = true
|
|
15
|
+
@table_columns = ActiveRecord::Base.connection.columns(name)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
set_local_assigns!
|
|
19
|
+
validate_file_name!
|
|
20
|
+
migration_template "install_#{name}.rb", "#{DataMigration.config.schema_migrations_path}/install_#{file_name}.rb"
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def migration_parent
|
|
24
|
+
"ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]"
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
data/lib/generators/{templates → data_migration/templates}/install_data_migration_tasks.rb.tt
RENAMED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
class <%= migration_class_name %> < <%= migration_parent %>
|
|
2
|
+
<% if table_exists %>
|
|
3
|
+
# Current columns: "<%= table_columns.map(&:name).join("\", \"") %>"
|
|
4
|
+
<% end %>
|
|
5
|
+
|
|
2
6
|
def self.up
|
|
3
7
|
create_table :<%= DataMigration.tasks_table_name %>, force: true do |t|
|
|
4
8
|
t.string "name", null: false
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
require "spec_helper"
|
|
2
2
|
|
|
3
|
-
require "generators/install_generator"
|
|
3
|
+
require "generators/data_migration/install_generator"
|
|
4
4
|
|
|
5
|
-
describe InstallGenerator, type: :generator do
|
|
5
|
+
describe DataMigration::Generators::InstallGenerator, type: :generator do
|
|
6
6
|
include FileUtils
|
|
7
7
|
|
|
8
8
|
subject(:generator) { described_class.start params }
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: data-migration
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Andrei Makarov
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2024-12-
|
|
11
|
+
date: 2024-12-11 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rails
|
|
@@ -180,10 +180,10 @@ files:
|
|
|
180
180
|
- lib/data_migration/config.rb
|
|
181
181
|
- lib/data_migration/job.rb
|
|
182
182
|
- lib/data_migration/task.rb
|
|
183
|
+
- lib/generators/data_migration/install_generator.rb
|
|
184
|
+
- lib/generators/data_migration/templates/install_data_migration_tasks.rb.tt
|
|
183
185
|
- lib/generators/data_migration_generator.rb
|
|
184
|
-
- lib/generators/install_generator.rb
|
|
185
186
|
- lib/generators/templates/data_migration.rb.tt
|
|
186
|
-
- lib/generators/templates/install_data_migration_tasks.rb.tt
|
|
187
187
|
- spec/data_migration/config_spec.rb
|
|
188
188
|
- spec/data_migration/job_spec.rb
|
|
189
189
|
- spec/data_migration/task_spec.rb
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
require "rails/generators"
|
|
2
|
-
require "rails/generators/active_record"
|
|
3
|
-
require "rails/generators/active_record/migration/migration_generator"
|
|
4
|
-
|
|
5
|
-
class InstallGenerator < ActiveRecord::Generators::MigrationGenerator
|
|
6
|
-
source_root File.expand_path("../templates", __FILE__)
|
|
7
|
-
|
|
8
|
-
def create_migration_file
|
|
9
|
-
set_local_assigns!
|
|
10
|
-
validate_file_name!
|
|
11
|
-
migration_template "install_#{name}.rb", "#{DataMigration.config.schema_migrations_path}/#{file_name}.rb"
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
def migration_parent
|
|
15
|
-
"ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]"
|
|
16
|
-
end
|
|
17
|
-
end
|