data_migrate 9.4.0 → 10.0.0.rc1
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 +3 -3
- data/.github/workflows/gempush.yml +8 -6
- data/.gitignore +2 -3
- data/.ruby-version +1 -0
- data/.travis.yml +14 -0
- data/Appraisals +5 -5
- data/Changelog.md +2 -40
- data/Gemfile +1 -0
- data/Gemfile.rails6.1 +11 -0
- data/README.md +5 -7
- data/data_migrate.gemspec +2 -2
- data/gemfiles/{rails_7.1.gemfile → rails_6.0.gemfile} +2 -1
- data/gemfiles/rails_6.1.gemfile +1 -0
- data/gemfiles/rails_7.0.gemfile +2 -1
- data/lib/data_migrate/data_migrator.rb +23 -15
- data/lib/data_migrate/data_schema.rb +2 -2
- data/lib/data_migrate/data_schema_migration.rb +7 -24
- data/lib/data_migrate/database_tasks.rb +48 -126
- data/lib/data_migrate/legacy_migrator.rb +22 -0
- data/lib/data_migrate/migration_context.rb +8 -11
- data/lib/data_migrate/schema_dumper.rb +1 -1
- data/lib/data_migrate/schema_migration.rb +4 -5
- data/lib/data_migrate/status_service.rb +4 -4
- data/lib/data_migrate/tasks/data_migrate_tasks.rb +15 -14
- data/lib/data_migrate/version.rb +1 -1
- data/lib/data_migrate.rb +1 -2
- data/spec/data_migrate/data_migrator_spec.rb +14 -17
- data/spec/data_migrate/data_schema_migration_spec.rb +8 -25
- data/spec/data_migrate/data_spec.rb +1 -1
- data/spec/data_migrate/database_tasks_spec.rb +19 -34
- data/spec/data_migrate/legacy_migrator_spec.rb +38 -0
- data/spec/data_migrate/migration_context_spec.rb +8 -15
- data/spec/data_migrate/schema_dumper_spec.rb +3 -6
- data/spec/data_migrate/schema_migration_spec.rb +6 -13
- data/spec/data_migrate/status_service_spec.rb +4 -7
- data/spec/data_migrate/tasks/data_migrate_tasks_spec.rb +14 -13
- data/spec/db/data/20091231235959_some_name.rb +1 -1
- data/spec/db/data/20171231235959_super_update.rb +1 -1
- data/spec/db/migrate/20131111111111_late_migration.rb +1 -1
- data/spec/db/migrate/20202020202011_db_migration.rb +1 -1
- data/tasks/databases.rake +77 -39
- metadata +19 -21
- data/Gemfile.lock +0 -184
- data/gemfiles/rails_6.1.gemfile.lock +0 -229
- data/gemfiles/rails_7.0.gemfile.lock +0 -230
- data/gemfiles/rails_7.1.gemfile.lock +0 -263
- data/lib/data_migrate/database_configurations_wrapper.rb +0 -11
- data/lib/data_migrate/rails_helper.rb +0 -79
- data/lib/data_migrate/test.rb +0 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d212f0e6fa0c9b767cbdf17854c7c52d640a9c8611d0b10ed6c0460de68985cd
|
4
|
+
data.tar.gz: ac338ef447f1942d64eb5cf6c0fe520f71089968c04fb03fb829a1318534b4eb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 01ae940433e4cd51a2f421297fb3ffb72372503971ee906164129c035c89aedb6c18d5003cd083930c24245f16728c38c93aed94fb8d9b2f7d0288895362a3d4
|
7
|
+
data.tar.gz: 9d94ff40ed274256247960c0dd1325645b638ac2198d29b3bce2c3c11f842b4b0a2b381bd73fcb17af308ab366cdca16c7f99c9e2fa778da7ab6103352e50ba1
|
data/.github/workflows/build.yml
CHANGED
@@ -11,14 +11,14 @@ jobs:
|
|
11
11
|
matrix:
|
12
12
|
os: [ "ubuntu-20.04" ]
|
13
13
|
ruby:
|
14
|
+
- '2.7'
|
14
15
|
- '3.0'
|
15
16
|
- '3.1'
|
16
17
|
- '3.2'
|
17
|
-
- '3.3'
|
18
18
|
gemfile:
|
19
|
+
- gemfiles/rails_6.0.gemfile
|
19
20
|
- gemfiles/rails_6.1.gemfile
|
20
21
|
- gemfiles/rails_7.0.gemfile
|
21
|
-
- gemfiles/rails_7.1.gemfile
|
22
22
|
runs-on: ubuntu-latest
|
23
23
|
env:
|
24
24
|
BUNDLE_GEMFILE: ${{ matrix.gemfile }}
|
@@ -32,4 +32,4 @@ jobs:
|
|
32
32
|
ruby-version: ${{ matrix.ruby }}
|
33
33
|
bundler-cache: true
|
34
34
|
- name: Run tests
|
35
|
-
run: bundle exec rspec
|
35
|
+
run: bundle exec rspec
|
@@ -1,8 +1,10 @@
|
|
1
|
-
name:
|
1
|
+
name: Ruby Gem
|
2
2
|
|
3
3
|
on:
|
4
|
-
|
5
|
-
|
4
|
+
push:
|
5
|
+
branches:
|
6
|
+
- main
|
7
|
+
- chaunce-multiple_connection_support
|
6
8
|
|
7
9
|
jobs:
|
8
10
|
build:
|
@@ -11,10 +13,10 @@ jobs:
|
|
11
13
|
|
12
14
|
steps:
|
13
15
|
- uses: actions/checkout@master
|
14
|
-
- name: Set up Ruby
|
15
|
-
uses:
|
16
|
+
- name: Set up Ruby 2.6
|
17
|
+
uses: actions/setup-ruby@v1
|
16
18
|
with:
|
17
|
-
|
19
|
+
version: 2.6.x
|
18
20
|
|
19
21
|
- name: Publish to RubyGems
|
20
22
|
run: |
|
data/.gitignore
CHANGED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.7.5
|
data/.travis.yml
ADDED
data/Appraisals
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
+
appraise 'rails-6.0' do
|
2
|
+
gem 'rails', '~> 6.0.0'
|
3
|
+
end
|
4
|
+
|
1
5
|
appraise 'rails-6.1' do
|
2
6
|
gem 'rails', '~> 6.1.0'
|
3
7
|
end
|
4
8
|
|
5
9
|
appraise 'rails-7.0' do
|
6
|
-
gem 'rails', '~> 7.0
|
7
|
-
end
|
8
|
-
|
9
|
-
appraise 'rails-7.1' do
|
10
|
-
gem 'rails', '7.1.0'
|
10
|
+
gem 'rails', '~> 7.0'
|
11
11
|
end
|
data/Changelog.md
CHANGED
@@ -1,51 +1,13 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
-
##
|
4
|
-
- Reset model schema cache before each data migration https://github.com/ilyakatz/data-migrate/pull/307
|
5
|
-
- Run load_config rake task before db:migrate:with_data https://github.com/ilyakatz/data-migrate/pull/308
|
6
|
-
|
7
|
-
## 9.3.0
|
8
|
-
- Improve with_data Rake task for multiple database https://github.com/ilyakatz/data-migrate/pull/296
|
9
|
-
|
10
|
-
## 9.2.0
|
11
|
-
- Support Rails 7.1 https://github.com/ilyakatz/data-migrate/pull/278
|
12
|
-
- Build and test against 7.1.0.rc1 https://github.com/ilyakatz/data-migrate/pull/286
|
13
|
-
|
14
|
-
## 9.1.0
|
15
|
-
|
16
|
-
- Fix a bug that caused `schema_sha1` in `ar_internal_metadata` to be reset to the `data_schema.rb` file. (#272)
|
17
|
-
- Remove the need for empty data_schema files for non-primary databases. (#273)
|
18
|
-
|
19
|
-
## [YANKED] 10.0.3.rc
|
20
|
-
|
21
|
-
- Remove all travis references [leoarnold](https//:github.com/leoarnold)
|
22
|
-
- Changing to rc because of ongoing discussion how to properly handle multiple database environments
|
23
|
-
|
24
|
-
## [YANKED] 10.0.2
|
25
|
-
|
26
|
-
Change "rails" dependencies to "railties"
|
27
|
-
|
28
|
-
## [YANKED] 10.0.1
|
29
|
-
|
30
|
-
- Bug fix for Rails 6 config [chaunce](https//:github.com/chaunce)
|
31
|
-
- Railties bug fix by [opti](https://github.com/opti)
|
32
|
-
|
33
|
-
## [YANKED] 10.0.0
|
34
|
-
|
35
|
-
Releasing 10.0.0
|
36
|
-
|
37
|
-
!!! Breaking changes !!!
|
38
|
-
|
39
|
-
- This version introduces a breaking change which may lead to undesired
|
40
|
-
behavior in multi-database environments. See https://github.com/ilyakatz/data-migrate/issues/181
|
41
|
-
|
42
|
-
## [YANKED] 10.0.0.rc1
|
3
|
+
## 10.0.0.rc1
|
43
4
|
|
44
5
|
- Changes by [chaunce](https//:github.com/chaunce)
|
45
6
|
- Multiple databases support
|
46
7
|
- Refactor to clean things up
|
47
8
|
- Deprecate rails 5.2 support for real
|
48
9
|
|
10
|
+
|
49
11
|
## 9.0.0
|
50
12
|
|
51
13
|
Ruby 3.2 support [mehanoid](https://github.com/mehanoid)
|
data/Gemfile
CHANGED
data/Gemfile.rails6.1
ADDED
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
|
+
- [![Travis](https://img.shields.io/travis/ilyakatz/data-migrate.svg)](https://travis-ci.org/ilyakatz/data-migrate)
|
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
|
+
![Travis](./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 6.
|
39
|
+
Support Rails 6.0 through 7.0
|
40
40
|
|
41
41
|
|
42
42
|
#### v1
|
@@ -96,9 +96,7 @@ Tasks work as they would with the 'vanilla' db version. The 'with_data' addition
|
|
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
|
-
|
100
|
-
|
101
|
-
`rake db:migrate:status:with_data` provides an additional column to indicate which type of migration.
|
99
|
+
`rake db:migrate:status:with_data` provides and additional column to indicate which type of migration.
|
102
100
|
|
103
101
|
### Configuration
|
104
102
|
|
@@ -164,9 +162,9 @@ Run tests for a specific version of Rails
|
|
164
162
|
|
165
163
|
```
|
166
164
|
bundle exec appraisal install
|
165
|
+
bundle exec appraisal rails-6.0 rspec
|
167
166
|
bundle exec appraisal rails-6.1 rspec
|
168
167
|
bundle exec appraisal rails-7.0 rspec
|
169
|
-
bundle exec appraisal rails-7.1 rspec
|
170
168
|
```
|
171
169
|
|
172
170
|
## 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, '>= 6.
|
22
|
+
s.add_dependency(rails_gem, '>= 6.0')
|
23
23
|
end
|
24
24
|
|
25
25
|
s.add_development_dependency "appraisal"
|
@@ -28,7 +28,7 @@ Gem::Specification.new do |s|
|
|
28
28
|
s.add_development_dependency "rspec-core"
|
29
29
|
s.add_development_dependency "pry"
|
30
30
|
s.add_development_dependency "rb-readline"
|
31
|
-
s.add_development_dependency "sqlite3"
|
31
|
+
s.add_development_dependency "sqlite3", "~> 1.3.6"
|
32
32
|
s.add_development_dependency "timecop"
|
33
33
|
s.add_development_dependency "rubocop"
|
34
34
|
s.add_development_dependency "overcommit"
|
data/gemfiles/rails_6.1.gemfile
CHANGED
data/gemfiles/rails_7.0.gemfile
CHANGED
@@ -5,20 +5,32 @@ require "data_migrate/config"
|
|
5
5
|
|
6
6
|
module DataMigrate
|
7
7
|
class DataMigrator < ActiveRecord::Migrator
|
8
|
+
def self.migrations_paths
|
9
|
+
[DataMigrate.config.data_migrations_path]
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.assure_data_schema_table
|
13
|
+
DataMigrate::DataSchemaMigration.create_table
|
14
|
+
end
|
15
|
+
|
16
|
+
def initialize(direction, migrations, target_version = nil)
|
17
|
+
@direction = direction
|
18
|
+
@target_version = target_version
|
19
|
+
@migrated_versions = nil
|
20
|
+
@migrations = migrations
|
21
|
+
|
22
|
+
validate(@migrations)
|
23
|
+
|
24
|
+
DataMigrate::DataSchemaMigration.create_table
|
25
|
+
ActiveRecord::InternalMetadata.create_table
|
26
|
+
end
|
27
|
+
|
8
28
|
def load_migrated
|
9
29
|
@migrated_versions =
|
10
|
-
DataMigrate::
|
30
|
+
DataMigrate::DataSchemaMigration.normalized_versions.map(&:to_i).sort
|
11
31
|
end
|
12
32
|
|
13
33
|
class << self
|
14
|
-
def migrations_paths
|
15
|
-
[DataMigrate.config.data_migrations_path]
|
16
|
-
end
|
17
|
-
|
18
|
-
def create_data_schema_table
|
19
|
-
DataMigrate::RailsHelper.data_schema_migration.create_table
|
20
|
-
end
|
21
|
-
|
22
34
|
def current_version
|
23
35
|
DataMigrate::MigrationContext.new(migrations_paths).current_version
|
24
36
|
end
|
@@ -54,10 +66,6 @@ module DataMigrate
|
|
54
66
|
|
55
67
|
#TODO: this was added to be backward compatible, need to re-evaluate
|
56
68
|
def run(direction, migration_paths, version)
|
57
|
-
# Ensure all Active Record model cache is reset for each data migration
|
58
|
-
# As recommended in: https://github.com/rails/rails/blob/da21c2e9812e5eb0698fba4a9aa38632fc004432/activerecord/lib/active_record/migration.rb#L467-L470
|
59
|
-
ActiveRecord::Base.descendants.each(&:reset_column_information)
|
60
|
-
|
61
69
|
DataMigrate::MigrationContext.new(migration_paths).run(direction, version)
|
62
70
|
end
|
63
71
|
|
@@ -71,10 +79,10 @@ module DataMigrate
|
|
71
79
|
def record_version_state_after_migrating(version)
|
72
80
|
if down?
|
73
81
|
migrated.delete(version)
|
74
|
-
DataMigrate::
|
82
|
+
DataMigrate::DataSchemaMigration.where(version: version.to_s).delete_all
|
75
83
|
else
|
76
84
|
migrated << version
|
77
|
-
DataMigrate::
|
85
|
+
DataMigrate::DataSchemaMigration.create!(version: version.to_s)
|
78
86
|
end
|
79
87
|
end
|
80
88
|
end
|
@@ -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.assure_data_schema_table
|
13
13
|
|
14
14
|
return if info[:version].blank?
|
15
15
|
|
@@ -57,7 +57,7 @@ module DataMigrate
|
|
57
57
|
end
|
58
58
|
|
59
59
|
def table_name
|
60
|
-
DataMigrate::
|
60
|
+
DataMigrate::DataSchemaMigration.table_name
|
61
61
|
end
|
62
62
|
end
|
63
63
|
end
|
@@ -1,29 +1,12 @@
|
|
1
1
|
module DataMigrate
|
2
|
-
class DataSchemaMigration
|
3
|
-
|
4
|
-
|
5
|
-
if DataMigrate::RailsHelper.rails_version_equal_to_or_higher_than_7_1
|
6
|
-
def table_name
|
7
|
-
ActiveRecord::Base.table_name_prefix + 'data_migrations' + ActiveRecord::Base.table_name_suffix
|
8
|
-
end
|
9
|
-
|
10
|
-
def primary_key
|
11
|
-
"version"
|
12
|
-
end
|
13
|
-
else
|
14
|
-
class << self
|
15
|
-
def table_name
|
16
|
-
ActiveRecord::Base.table_name_prefix + 'data_migrations' + ActiveRecord::Base.table_name_suffix
|
17
|
-
end
|
18
|
-
|
19
|
-
def primary_key
|
20
|
-
"version"
|
21
|
-
end
|
2
|
+
class DataSchemaMigration
|
3
|
+
class << self
|
4
|
+
delegate :table_name, :primary_key, :create_table, :normalized_versions, :create, :create!, :table_exists?, :exists?, :where, to: :instance
|
22
5
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
6
|
+
def instance
|
7
|
+
@instance ||= Class.new(::ActiveRecord::SchemaMigration) do
|
8
|
+
define_singleton_method(:table_name) { ActiveRecord::Base.table_name_prefix + 'data_migrations' + ActiveRecord::Base.table_name_suffix }
|
9
|
+
define_singleton_method(:primary_key) { "version" }
|
27
10
|
end
|
28
11
|
end
|
29
12
|
end
|
@@ -5,143 +5,67 @@ require "data_migrate/config"
|
|
5
5
|
module DataMigrate
|
6
6
|
##
|
7
7
|
# This class extends DatabaseTasks to add a schema_file method.
|
8
|
-
|
8
|
+
class DatabaseTasks
|
9
9
|
extend ActiveRecord::Tasks::DatabaseTasks
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
def with_temporary_connection_for_each(env: ActiveRecord::Tasks::DatabaseTasks.env, name: nil, &block) # :nodoc:
|
15
|
-
if name
|
16
|
-
db_config = ActiveRecord::Base.configurations.configs_for(env_name: env, name: name)
|
17
|
-
with_temporary_connection(db_config, &block)
|
18
|
-
else
|
19
|
-
ActiveRecord::Base.configurations.configs_for(env_name: env, name: name).each do |db_config|
|
20
|
-
with_temporary_connection(db_config, &block)
|
21
|
-
end
|
22
|
-
end
|
10
|
+
|
11
|
+
class << self
|
12
|
+
def schema_file_type(_format = nil)
|
13
|
+
"data_schema.rb"
|
23
14
|
end
|
24
15
|
|
25
|
-
def
|
26
|
-
|
27
|
-
|
16
|
+
def dump_filename(spec_name, format = ActiveRecord::Base.schema_format)
|
17
|
+
filename = if spec_name == "primary"
|
18
|
+
schema_file_type(format)
|
19
|
+
else
|
20
|
+
"#{spec_name}_#{schema_file_type(format)}"
|
28
21
|
end
|
29
|
-
end
|
30
22
|
|
31
|
-
|
32
|
-
ActiveRecord::Base
|
23
|
+
ENV["DATA_SCHEMA"] || File.join(db_dir, filename)
|
33
24
|
end
|
34
25
|
|
35
|
-
def
|
36
|
-
|
26
|
+
def check_schema_file(filename)
|
27
|
+
unless File.exist?(filename)
|
28
|
+
message = +%{#{filename} doesn't exist yet. Run `rake data:migrate` to create it, then try again.}
|
29
|
+
Kernel.abort message
|
30
|
+
end
|
37
31
|
end
|
38
32
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
ensure
|
45
|
-
migration_class.connection_handler.establish_connection(original_db_config)
|
33
|
+
def pending_migrations
|
34
|
+
sort_migrations(
|
35
|
+
pending_schema_migrations,
|
36
|
+
pending_data_migrations
|
37
|
+
)
|
46
38
|
end
|
47
|
-
end
|
48
|
-
|
49
|
-
def db_configs_with_versions
|
50
|
-
db_configs_with_versions = Hash.new { |h, k| h[k] = [] }
|
51
39
|
|
52
|
-
|
53
|
-
|
54
|
-
if db_config.primary?
|
55
|
-
versions_to_run = DataMigrate::DatabaseTasks.pending_data_migrations.map { |m| m[:version] }
|
56
|
-
target_version = ActiveRecord::Tasks::DatabaseTasks.target_version
|
57
|
-
|
58
|
-
versions_to_run.each do |version|
|
59
|
-
next if target_version && target_version != version
|
60
|
-
db_configs_with_versions[version] << DatabaseConfigurationWrapper.new(db_config)
|
61
|
-
end
|
62
|
-
end
|
40
|
+
def sort_migrations(*migrations)
|
41
|
+
migrations.flatten.sort { |a, b| sort_string(a) <=> sort_string(b) }
|
63
42
|
end
|
64
43
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
def schema_file(_format = nil)
|
69
|
-
File.join(db_dir, "data_schema.rb")
|
70
|
-
end
|
71
|
-
|
72
|
-
def schema_file_type(_format = nil)
|
73
|
-
"data_schema.rb"
|
74
|
-
end
|
75
|
-
|
76
|
-
# This method is removed in Rails 7.0
|
77
|
-
def dump_filename(spec_name, format = ActiveRecord::Base.schema_format)
|
78
|
-
filename = if spec_name == "primary"
|
79
|
-
schema_file_type(format)
|
80
|
-
else
|
81
|
-
"#{spec_name}_#{schema_file_type(format)}"
|
44
|
+
def sort_string migration
|
45
|
+
"#{migration[:version]}_#{migration[:kind] == :data ? 1 : 0}"
|
82
46
|
end
|
83
47
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
def check_schema_file(filename)
|
88
|
-
unless File.exist?(filename)
|
89
|
-
message = +%{#{filename} doesn't exist yet. Run `rake data:migrate` to create it, then try again.}
|
90
|
-
Kernel.abort message
|
48
|
+
def data_migrations_path
|
49
|
+
::DataMigrate.config.data_migrations_path
|
91
50
|
end
|
92
|
-
end
|
93
|
-
|
94
|
-
def pending_migrations
|
95
|
-
sort_migrations(
|
96
|
-
pending_schema_migrations,
|
97
|
-
pending_data_migrations
|
98
|
-
)
|
99
|
-
end
|
100
|
-
|
101
|
-
def sort_migrations(*migrations)
|
102
|
-
migrations.flatten.sort { |a, b| sort_string(a) <=> sort_string(b) }
|
103
|
-
end
|
104
51
|
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
else
|
118
|
-
::ActiveRecord::Migration.write("== %s %s" % ['Schema', "=" * 69])
|
119
|
-
::DataMigrate::SchemaMigration.run(
|
120
|
-
direction,
|
121
|
-
::DataMigrate::SchemaMigration.migrations_paths,
|
122
|
-
migration[:version]
|
123
|
-
)
|
52
|
+
def run_migration(migration, direction)
|
53
|
+
if migration[:kind] == :data
|
54
|
+
::ActiveRecord::Migration.write("== %s %s" % ['Data', "=" * 71])
|
55
|
+
::DataMigrate::DataMigrator.run(direction, data_migrations_path, migration[:version])
|
56
|
+
else
|
57
|
+
::ActiveRecord::Migration.write("== %s %s" % ['Schema', "=" * 69])
|
58
|
+
::DataMigrate::SchemaMigration.run(
|
59
|
+
direction,
|
60
|
+
::DataMigrate::SchemaMigration.migrations_paths,
|
61
|
+
migration[:version]
|
62
|
+
)
|
63
|
+
end
|
124
64
|
end
|
125
65
|
end
|
126
66
|
|
127
|
-
def
|
128
|
-
|
129
|
-
|
130
|
-
# We only require a schema.rb file for the primary database
|
131
|
-
return unless db_config.primary?
|
132
|
-
|
133
|
-
File.join(File.dirname(ActiveRecord::Tasks::DatabaseTasks.schema_dump_path(db_config, format)), schema_file_type)
|
134
|
-
end
|
135
|
-
|
136
|
-
# Override this method from `ActiveRecord::Tasks::DatabaseTasks`
|
137
|
-
# to ensure that the sha saved in ar_internal_metadata table
|
138
|
-
# is from the original schema.rb file
|
139
|
-
def schema_sha1(file)
|
140
|
-
ActiveRecord::Tasks::DatabaseTasks.schema_dump_path(ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env, name: "primary"))
|
141
|
-
end
|
142
|
-
|
143
|
-
def forward(step = 1)
|
144
|
-
DataMigrate::DataMigrator.create_data_schema_table
|
67
|
+
def self.forward(step = 1)
|
68
|
+
DataMigrate::DataMigrator.assure_data_schema_table
|
145
69
|
migrations = pending_migrations.reverse.pop(step).reverse
|
146
70
|
migrations.each do | pending_migration |
|
147
71
|
if pending_migration[:kind] == :data
|
@@ -154,21 +78,19 @@ module DataMigrate
|
|
154
78
|
end
|
155
79
|
end
|
156
80
|
|
157
|
-
def pending_data_migrations
|
81
|
+
def self.pending_data_migrations
|
158
82
|
data_migrations = DataMigrate::DataMigrator.migrations(data_migrations_path)
|
159
|
-
|
160
|
-
|
161
|
-
data_migrator.pending_migrations.map { |m| { version: m.version, name: m.name, kind: :data } }
|
162
|
-
)
|
83
|
+
sort_migrations(DataMigrate::DataMigrator.new(:up, data_migrations ).
|
84
|
+
pending_migrations.map {|m| { version: m.version, name: m.name, kind: :data }})
|
163
85
|
end
|
164
86
|
|
165
|
-
def pending_schema_migrations
|
87
|
+
def self.pending_schema_migrations
|
166
88
|
::DataMigrate::SchemaMigration.pending_schema_migrations
|
167
89
|
end
|
168
90
|
|
169
|
-
def past_migrations(sort = nil)
|
170
|
-
data_versions = DataMigrate::
|
171
|
-
schema_versions =
|
91
|
+
def self.past_migrations(sort = nil)
|
92
|
+
data_versions = DataMigrate::DataSchemaMigration.table_exists? ? DataMigrate::DataSchemaMigration.normalized_versions : []
|
93
|
+
schema_versions = ActiveRecord::SchemaMigration.normalized_versions
|
172
94
|
migrations = data_versions.map { |v| { version: v.to_i, kind: :data } } + schema_versions.map { |v| { version: v.to_i, kind: :schema } }
|
173
95
|
|
174
96
|
sort&.downcase == "asc" ? sort_migrations(migrations) : sort_migrations(migrations).reverse
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module DataMigrate
|
2
|
+
class LegacyMigrator
|
3
|
+
def initialize(migrations_paths = "db/data")
|
4
|
+
@migrations_paths = migrations_paths || "db/data"
|
5
|
+
end
|
6
|
+
|
7
|
+
def migrate
|
8
|
+
dates =
|
9
|
+
DataMigrate::DataMigrator.migrations(@migrations_paths).collect(&:version)
|
10
|
+
legacy = ActiveRecord::SchemaMigration.where(version: dates)
|
11
|
+
legacy.each do |v|
|
12
|
+
begin
|
13
|
+
version = v.version
|
14
|
+
puts "Creating #{version} in data schema"
|
15
|
+
DataMigrate::DataSchemaMigration.create(version: version)
|
16
|
+
rescue ActiveRecord::RecordNotUnique
|
17
|
+
nil
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|