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.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/build.yml +3 -3
  3. data/.github/workflows/gempush.yml +8 -6
  4. data/.gitignore +2 -3
  5. data/.ruby-version +1 -0
  6. data/.travis.yml +14 -0
  7. data/Appraisals +5 -5
  8. data/Changelog.md +2 -40
  9. data/Gemfile +1 -0
  10. data/Gemfile.rails6.1 +11 -0
  11. data/README.md +5 -7
  12. data/data_migrate.gemspec +2 -2
  13. data/gemfiles/{rails_7.1.gemfile → rails_6.0.gemfile} +2 -1
  14. data/gemfiles/rails_6.1.gemfile +1 -0
  15. data/gemfiles/rails_7.0.gemfile +2 -1
  16. data/lib/data_migrate/data_migrator.rb +23 -15
  17. data/lib/data_migrate/data_schema.rb +2 -2
  18. data/lib/data_migrate/data_schema_migration.rb +7 -24
  19. data/lib/data_migrate/database_tasks.rb +48 -126
  20. data/lib/data_migrate/legacy_migrator.rb +22 -0
  21. data/lib/data_migrate/migration_context.rb +8 -11
  22. data/lib/data_migrate/schema_dumper.rb +1 -1
  23. data/lib/data_migrate/schema_migration.rb +4 -5
  24. data/lib/data_migrate/status_service.rb +4 -4
  25. data/lib/data_migrate/tasks/data_migrate_tasks.rb +15 -14
  26. data/lib/data_migrate/version.rb +1 -1
  27. data/lib/data_migrate.rb +1 -2
  28. data/spec/data_migrate/data_migrator_spec.rb +14 -17
  29. data/spec/data_migrate/data_schema_migration_spec.rb +8 -25
  30. data/spec/data_migrate/data_spec.rb +1 -1
  31. data/spec/data_migrate/database_tasks_spec.rb +19 -34
  32. data/spec/data_migrate/legacy_migrator_spec.rb +38 -0
  33. data/spec/data_migrate/migration_context_spec.rb +8 -15
  34. data/spec/data_migrate/schema_dumper_spec.rb +3 -6
  35. data/spec/data_migrate/schema_migration_spec.rb +6 -13
  36. data/spec/data_migrate/status_service_spec.rb +4 -7
  37. data/spec/data_migrate/tasks/data_migrate_tasks_spec.rb +14 -13
  38. data/spec/db/data/20091231235959_some_name.rb +1 -1
  39. data/spec/db/data/20171231235959_super_update.rb +1 -1
  40. data/spec/db/migrate/20131111111111_late_migration.rb +1 -1
  41. data/spec/db/migrate/20202020202011_db_migration.rb +1 -1
  42. data/tasks/databases.rake +77 -39
  43. metadata +19 -21
  44. data/Gemfile.lock +0 -184
  45. data/gemfiles/rails_6.1.gemfile.lock +0 -229
  46. data/gemfiles/rails_7.0.gemfile.lock +0 -230
  47. data/gemfiles/rails_7.1.gemfile.lock +0 -263
  48. data/lib/data_migrate/database_configurations_wrapper.rb +0 -11
  49. data/lib/data_migrate/rails_helper.rb +0 -79
  50. data/lib/data_migrate/test.rb +0 -14
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8b1c79f9a3a6b30aeb19f85dd46e0c7d29885f3552787924f27eeb80f1738e95
4
- data.tar.gz: 56ff3a64d6cde029b8a6e9f57c82ebf15ad86aa0140520d5a08c67a8a30e9869
3
+ metadata.gz: d212f0e6fa0c9b767cbdf17854c7c52d640a9c8611d0b10ed6c0460de68985cd
4
+ data.tar.gz: ac338ef447f1942d64eb5cf6c0fe520f71089968c04fb03fb829a1318534b4eb
5
5
  SHA512:
6
- metadata.gz: 6b8d1bb7fd343e44f0cdf623fda3e52875e4efee1327bc4fbf2e3700b1071a96e3b50bb88a63ef4f94c98fcdb068c81f47a4a94d3ee80e91f4d9b47d84893234
7
- data.tar.gz: bbbcc92d53620c487cdce34a99dd5c41ddb156fa6a8a1a1b8304f1750af2c696df2c1f12079d9ec320b7f1a13497ab35ea20d19a0e1c5f33b3e7a7b2804a8ca0
6
+ metadata.gz: 01ae940433e4cd51a2f421297fb3ffb72372503971ee906164129c035c89aedb6c18d5003cd083930c24245f16728c38c93aed94fb8d9b2f7d0288895362a3d4
7
+ data.tar.gz: 9d94ff40ed274256247960c0dd1325645b638ac2198d29b3bce2c3c11f842b4b0a2b381bd73fcb17af308ab366cdca16c7f99c9e2fa778da7ab6103352e50ba1
@@ -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: Release Gem
1
+ name: Ruby Gem
2
2
 
3
3
  on:
4
- release:
5
- types: [released]
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 3.2
15
- uses: ruby/setup-ruby@v1
16
+ - name: Set up Ruby 2.6
17
+ uses: actions/setup-ruby@v1
16
18
  with:
17
- ruby-version: '3.2'
19
+ version: 2.6.x
18
20
 
19
21
  - name: Publish to RubyGems
20
22
  run: |
data/.gitignore CHANGED
@@ -1,12 +1,11 @@
1
1
  /.rvmrc
2
2
  *.gem
3
+ *.lock
3
4
  gemfiles/.bundle
4
5
  spec/db/test.db
5
6
  spec/db/other_test.db
6
7
  spec/db/data_schema.rb
7
8
  .vscode/
8
9
  .DS_Store
9
- .ruby-gemset
10
- .ruby-version
11
10
  .idea/
12
- vendor/
11
+ vendor/
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.7.5
data/.travis.yml ADDED
@@ -0,0 +1,14 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.7
4
+ - 3.0
5
+ - 3.2
6
+ script: bundle exec rspec
7
+ gemfile:
8
+ - gemfiles/rails_6.0.gemfile
9
+ - gemfiles/rails_6.1.gemfile
10
+ - gemfiles/rails_7.0.gemfile
11
+ jobs:
12
+ exclude:
13
+ - rvm: 3.0
14
+ - rvm: 3.2
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.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
- ## 9.4.0
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
@@ -1,3 +1,4 @@
1
1
  source "http://rubygems.org"
2
2
 
3
+ gem 'sqlite3', "~> 1.4"
3
4
  gemspec
data/Gemfile.rails6.1 ADDED
@@ -0,0 +1,11 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in data_migrate.gemspec
4
+ %w[
5
+ activerecord
6
+ railties
7
+ ].each do |rails_gem|
8
+ gem rails_gem, '~> 6.1.0'
9
+ end
10
+ gem 'sqlite3', "~> 1.4"
11
+ gemspec
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
- - ![.github/workflows/build.yml](https://github.com/ilyakatz/data-migrate/actions/workflows/.github/workflows/build.yml/badge.svg)
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
- ![directory tree example](./screenshot.png)
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.1 through 7.1
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
- 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.
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.1')
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"
@@ -2,6 +2,7 @@
2
2
 
3
3
  source "http://rubygems.org"
4
4
 
5
- gem "rails", "7.1.0"
5
+ gem "rails", "~> 6.0.0"
6
+ gem "sqlite3", "~> 1.4"
6
7
 
7
8
  gemspec path: "../"
@@ -3,5 +3,6 @@
3
3
  source "http://rubygems.org"
4
4
 
5
5
  gem "rails", "~> 6.1.0"
6
+ gem "sqlite3", "~> 1.4"
6
7
 
7
8
  gemspec path: "../"
@@ -2,6 +2,7 @@
2
2
 
3
3
  source "http://rubygems.org"
4
4
 
5
- gem "rails", "~> 7.0.0"
5
+ gem "sqlite3", "~> 1.4"
6
+ gem "rails", "~> 7.0"
6
7
 
7
8
  gemspec path: "../"
@@ -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::RailsHelper.data_schema_migration.normalized_versions.map(&:to_i).sort
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::RailsHelper.data_schema_delete_version(version.to_s)
82
+ DataMigrate::DataSchemaMigration.where(version: version.to_s).delete_all
75
83
  else
76
84
  migrated << version
77
- DataMigrate::RailsHelper.data_schema_migration.create_version(version.to_s)
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.create_data_schema_table
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::RailsHelper.data_schema_migration.table_name
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 < ActiveRecord::SchemaMigration
3
- # In Rails 7.1+, ActiveRecord::SchemaMigration methods are instance methods
4
- # So we only load the appropriate methods depending on Rails version.
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
- def create_version(version)
24
- # Note that SchemaMigration.create_version in Rails 7.1 does not
25
- # raise an error if validations fail but we retain this behaviour for now.
26
- create!(version: version)
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
- module DatabaseTasks
8
+ class DatabaseTasks
9
9
  extend ActiveRecord::Tasks::DatabaseTasks
10
- extend self
11
-
12
- # These method are only introduced in Rails 7.1
13
- unless respond_to?(:with_temporary_connection_for_each)
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 with_temporary_connection(db_config) # :nodoc:
26
- with_temporary_pool(db_config) do |pool|
27
- yield pool.connection
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
- def migration_class # :nodoc:
32
- ActiveRecord::Base
23
+ ENV["DATA_SCHEMA"] || File.join(db_dir, filename)
33
24
  end
34
25
 
35
- def migration_connection # :nodoc:
36
- migration_class.connection
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
- private def with_temporary_pool(db_config)
40
- original_db_config = migration_class.connection_db_config
41
- pool = migration_class.connection_handler.establish_connection(db_config)
42
-
43
- yield pool
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
- with_temporary_connection_for_each do |conn|
53
- db_config = conn.pool.db_config
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
- db_configs_with_versions
66
- end
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
- ENV["DATA_SCHEMA"] || File.join(db_dir, filename)
85
- end
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
- def sort_string migration
106
- "#{migration[:version]}_#{migration[:kind] == :data ? 1 : 0}"
107
- end
108
-
109
- def data_migrations_path
110
- ::DataMigrate.config.data_migrations_path
111
- end
112
-
113
- def run_migration(migration, direction)
114
- if migration[:kind] == :data
115
- ::ActiveRecord::Migration.write("== %s %s" % ['Data', "=" * 71])
116
- ::DataMigrate::DataMigrator.run(direction, data_migrations_path, migration[:version])
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 schema_dump_path(db_config, format = ActiveRecord.schema_format)
128
- return ENV["DATA_SCHEMA"] if ENV["DATA_SCHEMA"]
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
- data_migrator = DataMigrate::RailsHelper.data_migrator(:up, data_migrations)
160
- sort_migrations(
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::RailsHelper.data_schema_migration.table_exists? ? DataMigrate::RailsHelper.data_schema_migration.normalized_versions : []
171
- schema_versions = DataMigrate::RailsHelper.schema_migration.normalized_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