data_migrate 8.5.0 → 9.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/build.yml +34 -0
- data/.github/workflows/gempush.yml +6 -7
- data/.gitignore +3 -1
- data/Appraisals +0 -4
- data/Changelog.md +40 -0
- data/README.md +9 -7
- data/data_migrate.gemspec +1 -10
- data/lib/data_migrate/config.rb +1 -1
- data/lib/data_migrate/{data_migrator_five.rb → data_migrator.rb} +1 -12
- data/lib/data_migrate/data_schema.rb +1 -1
- data/lib/data_migrate/database_tasks.rb +32 -72
- data/lib/data_migrate/{schema_migration_six.rb → schema_migration.rb} +4 -2
- data/lib/data_migrate/{status_service_five.rb → status_service.rb} +12 -6
- data/lib/data_migrate/tasks/data_migrate_tasks.rb +15 -36
- data/lib/data_migrate/version.rb +1 -1
- data/lib/data_migrate.rb +3 -8
- data/spec/data_migrate/config_spec.rb +13 -10
- data/spec/data_migrate/data_migrator_spec.rb +13 -34
- data/spec/data_migrate/data_spec.rb +0 -11
- data/spec/data_migrate/database_tasks_spec.rb +10 -58
- data/spec/data_migrate/legacy_migrator_spec.rb +6 -18
- data/spec/data_migrate/migration.rb +11 -13
- data/spec/data_migrate/migration_context_spec.rb +11 -37
- data/spec/data_migrate/schema_dumper_spec.rb +10 -23
- data/spec/data_migrate/schema_migration_spec.rb +40 -42
- data/spec/data_migrate/status_service_spec.rb +26 -55
- data/spec/data_migrate/tasks/data_migrate_tasks_spec.rb +31 -66
- data/spec/db/data/20091231235959_some_name.rb +1 -1
- data/spec/db/data/20171231235959_super_update.rb +1 -1
- data/spec/spec_helper.rb +2 -8
- data/tasks/databases.rake +15 -13
- metadata +14 -31
- data/.ruby-version +0 -1
- data/.travis.yml +0 -14
- data/Gemfile.rails5.2 +0 -10
- data/gemfiles/rails_5.2.gemfile +0 -8
- data/lib/data_migrate/schema_migration_five.rb +0 -31
- data/spec/db/6.0/20091231235959_some_name.rb +0 -9
- data/spec/db/6.0/20171231235959_super_update.rb +0 -9
- data/spec/db/data-6.0/20091231235959_some_name.rb +0 -9
- data/spec/db/data-6.0/20171231235959_super_update.rb +0 -9
- data/spec/db/data-6.0/20181128000207_excluded_file.rb.other_ext +0 -1
- data/spec/db/migrate/5.2/20131111111111_late_migration.rb +0 -9
- data/spec/db/migrate/5.2/20202020202011_db_migration.rb +0 -9
- /data/spec/db/migrate/{6.0/20131111111111_late_migration.rb → 20131111111111_late_migration.rb} +0 -0
- /data/spec/db/migrate/{6.0/20202020202011_db_migration.rb → 20202020202011_db_migration.rb} +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a19a5d3223862fe4ee25980432501b8e054bad3cc530e036f8d3f6206f4146c2
|
4
|
+
data.tar.gz: ae1da5e5f75edc2dead2fcab270aaa70fe36d5d52011a2dbd687a3feee8868d4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 61b4d3052eecd2512e9d9b1bf0f3b6d82502308d9031a71c53bc54d10a167415633475afe2683f8fa65de96a84ae2f1fc69a4132833775640af86550f6f3e161
|
7
|
+
data.tar.gz: 86c53c8a4567a256368c280faf90f4acfa9c3153b1b56398b3cd36b28727db7f88de3133788303dc8210c632cf113d0f2017c0125728a19d0fd504f625d293b6
|
@@ -0,0 +1,34 @@
|
|
1
|
+
name: CI
|
2
|
+
|
3
|
+
on:
|
4
|
+
- push
|
5
|
+
- pull_request
|
6
|
+
|
7
|
+
jobs:
|
8
|
+
test:
|
9
|
+
strategy:
|
10
|
+
fail-fast: false
|
11
|
+
matrix:
|
12
|
+
os: [ "ubuntu-20.04" ]
|
13
|
+
ruby:
|
14
|
+
- '3.0'
|
15
|
+
- '3.1'
|
16
|
+
- '3.2'
|
17
|
+
gemfile:
|
18
|
+
- gemfiles/rails_6.0.gemfile
|
19
|
+
- gemfiles/rails_6.1.gemfile
|
20
|
+
- gemfiles/rails_7.0.gemfile
|
21
|
+
runs-on: ubuntu-latest
|
22
|
+
env:
|
23
|
+
BUNDLE_GEMFILE: ${{ matrix.gemfile }}
|
24
|
+
RAILS_ENV: test
|
25
|
+
steps:
|
26
|
+
- name: Checkout code
|
27
|
+
uses: actions/checkout@v3
|
28
|
+
- name: Set up Ruby
|
29
|
+
uses: ruby/setup-ruby@v1
|
30
|
+
with:
|
31
|
+
ruby-version: ${{ matrix.ruby }}
|
32
|
+
bundler-cache: true
|
33
|
+
- name: Run tests
|
34
|
+
run: bundle exec rspec
|
@@ -1,9 +1,8 @@
|
|
1
|
-
name:
|
1
|
+
name: Release Gem
|
2
2
|
|
3
3
|
on:
|
4
|
-
|
5
|
-
|
6
|
-
- main
|
4
|
+
release:
|
5
|
+
types: [released]
|
7
6
|
|
8
7
|
jobs:
|
9
8
|
build:
|
@@ -12,10 +11,10 @@ jobs:
|
|
12
11
|
|
13
12
|
steps:
|
14
13
|
- uses: actions/checkout@master
|
15
|
-
- name: Set up Ruby 2
|
16
|
-
uses:
|
14
|
+
- name: Set up Ruby 3.2
|
15
|
+
uses: ruby/setup-ruby@v1
|
17
16
|
with:
|
18
|
-
version: 2
|
17
|
+
ruby-version: '3.2'
|
19
18
|
|
20
19
|
- name: Publish to RubyGems
|
21
20
|
run: |
|
data/.gitignore
CHANGED
data/Appraisals
CHANGED
data/Changelog.md
CHANGED
@@ -1,5 +1,45 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## 9.1.0
|
4
|
+
|
5
|
+
- Fix a bug that caused `schema_sha1` in `ar_internal_metadata` to be reset to the `data_schema.rb` file. (#272)
|
6
|
+
- Remove the need for empty data_schema files for non-primary databases. (#273)
|
7
|
+
|
8
|
+
## [YANKED] 10.0.3.rc
|
9
|
+
|
10
|
+
- Remove all travis references [leoarnold](https//:github.com/leoarnold)
|
11
|
+
- Changing to rc because of ongoing discussion how to properly handle multiple database environments
|
12
|
+
|
13
|
+
## [YANKED] 10.0.2
|
14
|
+
|
15
|
+
Change "rails" dependencies to "railties"
|
16
|
+
|
17
|
+
## [YANKED] 10.0.1
|
18
|
+
|
19
|
+
- Bug fix for Rails 6 config [chaunce](https//:github.com/chaunce)
|
20
|
+
- Railties bug fix by [opti](https://github.com/opti)
|
21
|
+
|
22
|
+
## [YANKED] 10.0.0
|
23
|
+
|
24
|
+
Releasing 10.0.0
|
25
|
+
|
26
|
+
!!! Breaking changes !!!
|
27
|
+
|
28
|
+
- This version introduces a breaking change which may lead to undesired
|
29
|
+
behavior in multi-database environments. See https://github.com/ilyakatz/data-migrate/issues/181
|
30
|
+
|
31
|
+
## [YANKED] 10.0.0.rc1
|
32
|
+
|
33
|
+
- Changes by [chaunce](https//:github.com/chaunce)
|
34
|
+
- Multiple databases support
|
35
|
+
- Refactor to clean things up
|
36
|
+
- Deprecate rails 5.2 support for real
|
37
|
+
|
38
|
+
## 9.0.0
|
39
|
+
|
40
|
+
Ruby 3.2 support [mehanoid](https://github.com/mehanoid)
|
41
|
+
Rails 5.2 is no longer supported
|
42
|
+
|
3
43
|
## 8.5.0
|
4
44
|
|
5
45
|
Allow custom templates [bazay](https://github.com/bazay)
|
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
- [![Version](http://img.shields.io/gem/v/data_migrate.svg?style=flat-square)](https://rubygems.org/gems/data_migrate)
|
4
4
|
- [![License](http://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](http://opensource.org/licenses/MIT)
|
5
|
-
-
|
5
|
+
- ![.github/workflows/build.yml](https://github.com/ilyakatz/data-migrate/actions/workflows/.github/workflows/build.yml/badge.svg)
|
6
6
|
- [![Reviewed by Hound](https://img.shields.io/badge/Reviewed_by-Hound-8E64B0.svg)](https://houndci.com)
|
7
7
|
|
8
8
|
Run data migrations alongside schema migrations.
|
@@ -12,7 +12,7 @@ migrations, except they should be reserved for data migrations. For
|
|
12
12
|
instance, if you realize you need to titleize all your titles, this
|
13
13
|
is the place to do it.
|
14
14
|
|
15
|
-
![
|
15
|
+
![directory tree example](./screenshot.png)
|
16
16
|
|
17
17
|
## Why should I use this?
|
18
18
|
|
@@ -36,7 +36,7 @@ table to track all migrations.
|
|
36
36
|
|
37
37
|
## Rails Support
|
38
38
|
|
39
|
-
Support Rails
|
39
|
+
Support Rails 6.0 through 7.0
|
40
40
|
|
41
41
|
|
42
42
|
#### v1
|
@@ -92,11 +92,13 @@ You can generate a data migration as you would a schema migration:
|
|
92
92
|
rake db:version:with_data # Retrieves the current schema version numbers for data and schema migrations
|
93
93
|
|
94
94
|
|
95
|
-
Tasks work as they would with the 'vanilla' db version. The 'with_data' addition to the 'db' tasks will run the task in the context of both the data and schema migrations. That is, rake db:rollback:with_data will check to see if it was a schema or data migration invoked last, and do that. Tasks invoked in that space also have an additional line of output, indicating if the action is performed on data or schema.
|
95
|
+
Tasks work as they would with the 'vanilla' db version. The 'with_data' addition to the 'db' tasks will run the task in the context of both the data and schema migrations. That is, rake db:rollback:with_data will check to see if it was a schema or data migration invoked last, and do that. Tasks invoked in that space also have an additional line of output, indicating if the action is performed on data or schema.
|
96
96
|
|
97
97
|
With 'up' and 'down', you can specify the option 'BOTH', which defaults to false. Using true, will migrate both the data and schema (in the desired direction) if they both match the version provided. Again, going up, schema is given precedence. Down its data.
|
98
98
|
|
99
|
-
`rake db:migrate:
|
99
|
+
When using `rake db:migrate:with_data` migrations will be run in ascending order by their version. For example, if you have a data migration with version 20230410000000 and a schema migration with version 20230415000000, expect the data migration to run first.
|
100
|
+
|
101
|
+
`rake db:migrate:status:with_data` provides an additional column to indicate which type of migration.
|
100
102
|
|
101
103
|
### Configuration
|
102
104
|
|
@@ -162,9 +164,9 @@ Run tests for a specific version of Rails
|
|
162
164
|
|
163
165
|
```
|
164
166
|
bundle exec appraisal install
|
165
|
-
bundle exec appraisal rails-5.1 rspec
|
166
|
-
bundle exec appraisal rails-5.2 rspec
|
167
167
|
bundle exec appraisal rails-6.0 rspec
|
168
|
+
bundle exec appraisal rails-6.1 rspec
|
169
|
+
bundle exec appraisal rails-7.0 rspec
|
168
170
|
```
|
169
171
|
|
170
172
|
## Thanks
|
data/data_migrate.gemspec
CHANGED
@@ -19,7 +19,7 @@ Gem::Specification.new do |s|
|
|
19
19
|
activerecord
|
20
20
|
railties
|
21
21
|
].each do |rails_gem|
|
22
|
-
s.add_dependency(rails_gem, '>=
|
22
|
+
s.add_dependency(rails_gem, '>= 6.0')
|
23
23
|
end
|
24
24
|
|
25
25
|
s.add_development_dependency "appraisal"
|
@@ -38,13 +38,4 @@ Gem::Specification.new do |s|
|
|
38
38
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
39
39
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
40
40
|
s.require_paths = ["lib"]
|
41
|
-
|
42
|
-
s.post_install_message = <<-POST_INSTALL_MESSAGE
|
43
|
-
#{"*" * 80}
|
44
|
-
data-migrate: IMPORTANT: Breaking change introduced for migrations from v2.
|
45
|
-
|
46
|
-
Failure to run the migration can have serious consequences.
|
47
|
-
See Readme for more info.
|
48
|
-
#{"*" * 80}
|
49
|
-
POST_INSTALL_MESSAGE
|
50
41
|
end
|
data/lib/data_migrate/config.rb
CHANGED
@@ -25,7 +25,7 @@ module DataMigrate
|
|
25
25
|
|
26
26
|
def data_template_path=(value)
|
27
27
|
@data_template_path = value.tap do |path|
|
28
|
-
raise ArgumentError, "File not found: '#{path}'" unless path == DEFAULT_DATA_TEMPLATE_PATH || File.
|
28
|
+
raise ArgumentError, "File not found: '#{path}'" unless path == DEFAULT_DATA_TEMPLATE_PATH || File.exist?(path)
|
29
29
|
end
|
30
30
|
end
|
31
31
|
end
|
@@ -9,8 +9,7 @@ module DataMigrate
|
|
9
9
|
[DataMigrate.config.data_migrations_path]
|
10
10
|
end
|
11
11
|
|
12
|
-
def self.
|
13
|
-
ActiveRecord::Base.establish_connection(db_config)
|
12
|
+
def self.create_data_schema_table
|
14
13
|
DataMigrate::DataSchemaMigration.create_table
|
15
14
|
end
|
16
15
|
|
@@ -73,16 +72,6 @@ module DataMigrate
|
|
73
72
|
def rollback(migrations_path, steps)
|
74
73
|
DataMigrate::MigrationContext.new(migrations_path).rollback(steps)
|
75
74
|
end
|
76
|
-
|
77
|
-
def db_config
|
78
|
-
env = Rails.env || "development"
|
79
|
-
ar_config = if (Rails::VERSION::MAJOR == 6 && Rails::VERSION::MINOR >= 1) || Rails::VERSION::MAJOR > 6
|
80
|
-
ActiveRecord::Base.configurations.configs_for(env_name: env).first
|
81
|
-
else
|
82
|
-
ActiveRecord::Base.configurations[env]
|
83
|
-
end
|
84
|
-
ar_config || ENV["DATABASE_URL"]
|
85
|
-
end
|
86
75
|
end
|
87
76
|
|
88
77
|
private
|
@@ -9,7 +9,7 @@ module DataMigrate
|
|
9
9
|
# ActiveRecord::ConnectionAdapters::SchemaStatements
|
10
10
|
# #assume_migrated_upto_version
|
11
11
|
def define(info)
|
12
|
-
DataMigrate::DataMigrator.
|
12
|
+
DataMigrate::DataMigrator.create_data_schema_table
|
13
13
|
|
14
14
|
return if info[:version].blank?
|
15
15
|
|
@@ -9,36 +9,22 @@ module DataMigrate
|
|
9
9
|
extend ActiveRecord::Tasks::DatabaseTasks
|
10
10
|
|
11
11
|
class << self
|
12
|
-
def
|
13
|
-
"data_schema.rb"
|
12
|
+
def schema_file(_format = nil)
|
13
|
+
File.join(db_dir, "data_schema.rb")
|
14
14
|
end
|
15
15
|
|
16
|
-
def
|
17
|
-
|
18
|
-
schema_file_type(format)
|
19
|
-
else
|
20
|
-
"#{namespace}_#{schema_file_type(format)}"
|
21
|
-
end
|
22
|
-
|
23
|
-
ENV["DATA_SCHEMA"] || File.join(schema_location, filename)
|
16
|
+
def schema_file_type(_format = nil)
|
17
|
+
"data_schema.rb"
|
24
18
|
end
|
25
19
|
|
26
|
-
def
|
27
|
-
|
28
|
-
|
29
|
-
filename = if db_config.primary?
|
20
|
+
def dump_filename(spec_name, format = ActiveRecord::Base.schema_format)
|
21
|
+
filename = if spec_name == "primary"
|
30
22
|
schema_file_type(format)
|
31
23
|
else
|
32
|
-
|
24
|
+
"#{spec_name}_#{schema_file_type(format)}"
|
33
25
|
end
|
34
26
|
|
35
|
-
|
36
|
-
|
37
|
-
File.dirname(filename) == schema_location ? filename : File.join(schema_location, filename)
|
38
|
-
end
|
39
|
-
|
40
|
-
def schema_location
|
41
|
-
db_dir
|
27
|
+
ENV["DATA_SCHEMA"] || File.join(db_dir, filename)
|
42
28
|
end
|
43
29
|
|
44
30
|
def check_schema_file(filename)
|
@@ -55,9 +41,8 @@ module DataMigrate
|
|
55
41
|
)
|
56
42
|
end
|
57
43
|
|
58
|
-
def sort_migrations
|
59
|
-
migrations
|
60
|
-
migrations.sort{|a,b| sort_string(a) <=> sort_string(b)}
|
44
|
+
def sort_migrations(*migrations)
|
45
|
+
migrations.flatten.sort { |a, b| sort_string(a) <=> sort_string(b) }
|
61
46
|
end
|
62
47
|
|
63
48
|
def sort_string migration
|
@@ -81,15 +66,26 @@ module DataMigrate
|
|
81
66
|
)
|
82
67
|
end
|
83
68
|
end
|
84
|
-
end
|
85
69
|
|
86
|
-
|
87
|
-
|
88
|
-
|
70
|
+
def schema_dump_path(db_config, format = ActiveRecord.schema_format)
|
71
|
+
return ENV["DATA_SCHEMA"] if ENV["DATA_SCHEMA"]
|
72
|
+
|
73
|
+
# We only require a schema.rb file for the primary database
|
74
|
+
return unless db_config.primary?
|
75
|
+
|
76
|
+
super.gsub(/(_)?schema\.rb\z/, '\1data_schema.rb')
|
77
|
+
end
|
78
|
+
|
79
|
+
# Override this method from `ActiveRecord::Tasks::DatabaseTasks`
|
80
|
+
# to ensure that the sha saved in ar_internal_metadata table
|
81
|
+
# is from the original schema.rb file
|
82
|
+
def schema_sha1(file)
|
83
|
+
super(file.gsub(/data_schema.rb\z/, 'schema.rb'))
|
84
|
+
end
|
89
85
|
end
|
90
86
|
|
91
87
|
def self.forward(step = 1)
|
92
|
-
DataMigrate::DataMigrator.
|
88
|
+
DataMigrate::DataMigrator.create_data_schema_table
|
93
89
|
migrations = pending_migrations.reverse.pop(step).reverse
|
94
90
|
migrations.each do | pending_migration |
|
95
91
|
if pending_migration[:kind] == :data
|
@@ -97,23 +93,11 @@ module DataMigrate
|
|
97
93
|
DataMigrate::DataMigrator.run(:up, data_migrations_path, pending_migration[:version])
|
98
94
|
elsif pending_migration[:kind] == :schema
|
99
95
|
ActiveRecord::Migration.write("== %s %s" % ["Schema", "=" * 69])
|
100
|
-
DataMigrate::SchemaMigration.run(:up,
|
96
|
+
DataMigrate::SchemaMigration.run(:up, DataMigrate::SchemaMigration.migrations_paths, pending_migration[:version])
|
101
97
|
end
|
102
98
|
end
|
103
99
|
end
|
104
100
|
|
105
|
-
def self.data_migrations_path
|
106
|
-
DataMigrate.config.data_migrations_path
|
107
|
-
end
|
108
|
-
|
109
|
-
def self.schema_migrations_path
|
110
|
-
"db/migrate/"
|
111
|
-
end
|
112
|
-
|
113
|
-
def self.pending_migrations
|
114
|
-
sort_migrations(pending_schema_migrations, pending_data_migrations)
|
115
|
-
end
|
116
|
-
|
117
101
|
def self.pending_data_migrations
|
118
102
|
data_migrations = DataMigrate::DataMigrator.migrations(data_migrations_path)
|
119
103
|
sort_migrations(DataMigrate::DataMigrator.new(:up, data_migrations ).
|
@@ -124,36 +108,12 @@ module DataMigrate
|
|
124
108
|
::DataMigrate::SchemaMigration.pending_schema_migrations
|
125
109
|
end
|
126
110
|
|
127
|
-
def self.sort_migrations(set1, set2 = nil)
|
128
|
-
migrations = set1 + (set2 || [])
|
129
|
-
migrations.sort {|a, b| sort_string(a) <=> sort_string(b)}
|
130
|
-
end
|
131
|
-
|
132
|
-
def self.sort_string(migration)
|
133
|
-
"#{migration[:version]}_#{migration[:kind] == :data ? 1 : 0}"
|
134
|
-
end
|
135
|
-
|
136
111
|
def self.past_migrations(sort = nil)
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
[]
|
143
|
-
end
|
144
|
-
db_list_schema = ActiveRecord::SchemaMigration.normalized_versions.sort.sort
|
145
|
-
migrations = db_list_data.map do |d|
|
146
|
-
{
|
147
|
-
version: d.to_i, kind: :data
|
148
|
-
}
|
149
|
-
end +
|
150
|
-
db_list_schema.map do |d|
|
151
|
-
{
|
152
|
-
version: d.to_i, kind: :schema
|
153
|
-
}
|
154
|
-
end
|
155
|
-
|
156
|
-
sort == "asc" ? sort_migrations(migrations) : sort_migrations(migrations).reverse
|
112
|
+
data_versions = DataMigrate::DataSchemaMigration.table_exists? ? DataMigrate::DataSchemaMigration.normalized_versions : []
|
113
|
+
schema_versions = ActiveRecord::SchemaMigration.normalized_versions
|
114
|
+
migrations = data_versions.map { |v| { version: v.to_i, kind: :data } } + schema_versions.map { |v| { version: v.to_i, kind: :schema } }
|
115
|
+
|
116
|
+
sort&.downcase == "asc" ? sort_migrations(migrations) : sort_migrations(migrations).reverse
|
157
117
|
end
|
158
118
|
end
|
159
119
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module DataMigrate
|
2
4
|
# Helper class to getting access to db schema
|
3
5
|
# to allow data/schema combiation tasks
|
@@ -22,9 +24,9 @@ module DataMigrate
|
|
22
24
|
|
23
25
|
def self.migrations_paths
|
24
26
|
spec_name = DataMigrate.config.spec_name
|
25
|
-
if spec_name &&
|
27
|
+
if spec_name && Gem::Dependency.new("railties", "~> 7.0").match?("railties", Gem.loaded_specs["railties"].version)
|
26
28
|
ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, name: spec_name).migrations_paths
|
27
|
-
elsif spec_name
|
29
|
+
elsif spec_name && Gem::Dependency.new("railties", "~> 6.0").match?("railties", Gem.loaded_specs["railties"].version)
|
28
30
|
ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, spec_name: spec_name).migrations_paths
|
29
31
|
else
|
30
32
|
Rails.application.config.paths["db/migrate"].to_a
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module DataMigrate
|
2
4
|
class StatusService
|
3
5
|
class << self
|
@@ -32,12 +34,8 @@ module DataMigrate
|
|
32
34
|
end
|
33
35
|
|
34
36
|
# output
|
35
|
-
|
36
|
-
|
37
|
-
else
|
38
|
-
stream.puts "\ndatabase: #{ActiveRecord::Base.connection_config[:database]}\n\n"
|
39
|
-
end
|
40
|
-
stream.puts "#{'Status'.center(8)} #{'Migration ID'.ljust(14)} Migration Name"
|
37
|
+
stream.puts "\ndatabase: #{database_name}\n\n"
|
38
|
+
stream.puts "#{"Status".center(8)} #{"Migration ID".ljust(14)} Migration Name"
|
41
39
|
stream.puts "-" * 50
|
42
40
|
db_list.each do |status, version, name|
|
43
41
|
stream.puts "#{status.center(8)} #{version.ljust(14)} #{name}"
|
@@ -45,6 +43,14 @@ module DataMigrate
|
|
45
43
|
stream.puts
|
46
44
|
end
|
47
45
|
|
46
|
+
def database_name
|
47
|
+
if Gem::Dependency.new("railties", "~> 7.0").match?("railties", Gem.loaded_specs["railties"].version)
|
48
|
+
ActiveRecord::Base.connection_db_config.configuration_hash[:database]
|
49
|
+
elsif Gem::Dependency.new("railties", "~> 6.0").match?("railties", Gem.loaded_specs["railties"].version)
|
50
|
+
ActiveRecord::Base.connection_config[:database]
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
48
54
|
def db_list
|
49
55
|
DataMigrate::DataMigrator.migrations_status
|
50
56
|
end
|
@@ -1,12 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module DataMigrate
|
2
4
|
module Tasks
|
3
5
|
module DataMigrateTasks
|
4
6
|
extend self
|
5
7
|
|
6
|
-
def schema_migrations_path
|
7
|
-
File.join('db', 'migrate')
|
8
|
-
end
|
9
|
-
|
10
8
|
def migrations_paths
|
11
9
|
@migrations_paths ||= DataMigrate.config.data_migrations_path
|
12
10
|
end
|
@@ -24,13 +22,13 @@ module DataMigrate
|
|
24
22
|
def migrate
|
25
23
|
target_version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
|
26
24
|
|
27
|
-
DataMigrate::DataMigrator.
|
25
|
+
DataMigrate::DataMigrator.create_data_schema_table
|
28
26
|
DataMigrate::MigrationContext.new(migrations_paths).migrate(target_version)
|
29
27
|
end
|
30
28
|
|
31
29
|
def abort_if_pending_migrations(migrations, message)
|
32
30
|
if migrations.any?
|
33
|
-
puts "You have #{migrations.size} pending #{migrations.size
|
31
|
+
puts "You have #{migrations.size} pending #{'migration'.pluralize(migrations.size)}:"
|
34
32
|
migrations.each do |pending_migration|
|
35
33
|
puts " %4d %s" % [pending_migration[:version], pending_migration[:name]]
|
36
34
|
end
|
@@ -47,24 +45,14 @@ module DataMigrate
|
|
47
45
|
end
|
48
46
|
|
49
47
|
def status
|
50
|
-
|
51
|
-
return unless config
|
52
|
-
|
53
|
-
connection = ActiveRecord::Base.connection
|
54
|
-
puts "\ndatabase: #{config['database']}\n\n"
|
55
|
-
DataMigrate::StatusService.dump(connection)
|
48
|
+
DataMigrate::StatusService.dump
|
56
49
|
end
|
57
50
|
|
58
51
|
def status_with_schema
|
59
|
-
config = connect_to_database
|
60
|
-
return unless config
|
61
|
-
|
62
52
|
db_list_data = ActiveRecord::Base.connection.select_values(
|
63
53
|
"SELECT version FROM #{DataMigrate::DataSchemaMigration.table_name}"
|
64
54
|
)
|
65
|
-
db_list_schema = ActiveRecord::
|
66
|
-
"SELECT version FROM #{ActiveRecord::SchemaMigration.schema_migrations_table_name}"
|
67
|
-
)
|
55
|
+
db_list_schema = ActiveRecord::SchemaMigration.all.pluck(:version)
|
68
56
|
file_list = []
|
69
57
|
|
70
58
|
Dir.foreach(File.join(Rails.root, migrations_paths)) do |file|
|
@@ -75,7 +63,9 @@ module DataMigrate
|
|
75
63
|
end
|
76
64
|
end
|
77
65
|
|
78
|
-
|
66
|
+
DataMigrate::SchemaMigration.migrations_paths.map do |path|
|
67
|
+
Dir.children(path) if Dir.exist?(path)
|
68
|
+
end.flatten.compact.each do |file|
|
79
69
|
# only files matching "20091231235959_some_name.rb" pattern
|
80
70
|
if match_data = /(\d{14})_(.+)\.rb/.match(file)
|
81
71
|
status = db_list_schema.delete(match_data[1]) ? 'up' : 'down'
|
@@ -86,7 +76,7 @@ module DataMigrate
|
|
86
76
|
file_list.sort!{|a,b| "#{a[1]}_#{a[3] == 'data' ? 1 : 0}" <=> "#{b[1]}_#{b[3] == 'data' ? 1 : 0}" }
|
87
77
|
|
88
78
|
# output
|
89
|
-
puts "\ndatabase: #{
|
79
|
+
puts "\ndatabase: #{database_name}\n\n"
|
90
80
|
puts "#{"Status".center(8)} #{"Type".center(8)} #{"Migration ID".ljust(14)} Migration Name"
|
91
81
|
puts "-" * 60
|
92
82
|
file_list.each do |file|
|
@@ -103,23 +93,12 @@ module DataMigrate
|
|
103
93
|
|
104
94
|
private
|
105
95
|
|
106
|
-
def
|
107
|
-
|
108
|
-
ActiveRecord::Base.
|
109
|
-
|
110
|
-
ActiveRecord::Base.
|
111
|
-
end
|
112
|
-
ActiveRecord::Base.establish_connection(config)
|
113
|
-
|
114
|
-
unless DataMigrate::DataSchemaMigration.table_exists?
|
115
|
-
puts 'Data migrations table does not exist yet.'
|
116
|
-
config = nil
|
117
|
-
end
|
118
|
-
unless ActiveRecord::SchemaMigration.table_exists?
|
119
|
-
puts 'Schema migrations table does not exist yet.'
|
120
|
-
config = nil
|
96
|
+
def database_name
|
97
|
+
if Gem::Dependency.new("railties", "~> 7.0").match?("railties", Gem.loaded_specs["railties"].version)
|
98
|
+
ActiveRecord::Base.connection_db_config.database
|
99
|
+
elsif Gem::Dependency.new("railties", "~> 6.0").match?("railties", Gem.loaded_specs["railties"].version)
|
100
|
+
ActiveRecord::Base.connection_config[:database]
|
121
101
|
end
|
122
|
-
config
|
123
102
|
end
|
124
103
|
end
|
125
104
|
end
|
data/lib/data_migrate/version.rb
CHANGED
data/lib/data_migrate.rb
CHANGED
@@ -1,22 +1,17 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require File.join(File.dirname(__FILE__), "data_migrate", "
|
3
|
+
require File.join(File.dirname(__FILE__), "data_migrate", "data_migrator")
|
4
4
|
require File.join(File.dirname(__FILE__), "data_migrate", "data_schema_migration")
|
5
5
|
require File.join(File.dirname(__FILE__), "data_migrate", "data_schema")
|
6
6
|
require File.join(File.dirname(__FILE__), "data_migrate", "database_tasks")
|
7
7
|
require File.join(File.dirname(__FILE__), "data_migrate", "schema_dumper")
|
8
|
-
require File.join(File.dirname(__FILE__), "data_migrate", "
|
8
|
+
require File.join(File.dirname(__FILE__), "data_migrate", "status_service")
|
9
9
|
require File.join(File.dirname(__FILE__), "data_migrate", "migration_context")
|
10
10
|
require File.join(File.dirname(__FILE__), "data_migrate", "railtie")
|
11
11
|
require File.join(File.dirname(__FILE__), "data_migrate", "tasks/data_migrate_tasks")
|
12
12
|
require File.join(File.dirname(__FILE__), "data_migrate", "legacy_migrator")
|
13
13
|
require File.join(File.dirname(__FILE__), "data_migrate", "config")
|
14
|
-
|
15
|
-
if Rails::VERSION::MAJOR == 5
|
16
|
-
require File.join(File.dirname(__FILE__), "data_migrate", "schema_migration_five")
|
17
|
-
else
|
18
|
-
require File.join(File.dirname(__FILE__), "data_migrate", "schema_migration_six")
|
19
|
-
end
|
14
|
+
require File.join(File.dirname(__FILE__), "data_migrate", "schema_migration")
|
20
15
|
|
21
16
|
module DataMigrate
|
22
17
|
def self.root
|