data_migrate 8.5.0 → 9.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/build.yml +34 -0
  3. data/.github/workflows/gempush.yml +6 -7
  4. data/.gitignore +3 -1
  5. data/Appraisals +0 -4
  6. data/Changelog.md +40 -0
  7. data/README.md +9 -7
  8. data/data_migrate.gemspec +1 -10
  9. data/lib/data_migrate/config.rb +1 -1
  10. data/lib/data_migrate/{data_migrator_five.rb → data_migrator.rb} +1 -12
  11. data/lib/data_migrate/data_schema.rb +1 -1
  12. data/lib/data_migrate/database_tasks.rb +33 -72
  13. data/lib/data_migrate/{schema_migration_six.rb → schema_migration.rb} +4 -2
  14. data/lib/data_migrate/{status_service_five.rb → status_service.rb} +12 -6
  15. data/lib/data_migrate/tasks/data_migrate_tasks.rb +15 -36
  16. data/lib/data_migrate/version.rb +1 -1
  17. data/lib/data_migrate.rb +3 -8
  18. data/spec/data_migrate/config_spec.rb +13 -10
  19. data/spec/data_migrate/data_migrator_spec.rb +13 -34
  20. data/spec/data_migrate/data_spec.rb +0 -11
  21. data/spec/data_migrate/database_tasks_spec.rb +10 -59
  22. data/spec/data_migrate/legacy_migrator_spec.rb +6 -18
  23. data/spec/data_migrate/migration.rb +11 -13
  24. data/spec/data_migrate/migration_context_spec.rb +11 -37
  25. data/spec/data_migrate/schema_dumper_spec.rb +10 -23
  26. data/spec/data_migrate/schema_migration_spec.rb +40 -42
  27. data/spec/data_migrate/status_service_spec.rb +26 -55
  28. data/spec/data_migrate/tasks/data_migrate_tasks_spec.rb +31 -66
  29. data/spec/db/data/20091231235959_some_name.rb +1 -1
  30. data/spec/db/data/20171231235959_super_update.rb +1 -1
  31. data/spec/spec_helper.rb +2 -8
  32. data/tasks/databases.rake +15 -13
  33. metadata +14 -31
  34. data/.ruby-version +0 -1
  35. data/.travis.yml +0 -14
  36. data/Gemfile.rails5.2 +0 -10
  37. data/gemfiles/rails_5.2.gemfile +0 -8
  38. data/lib/data_migrate/schema_migration_five.rb +0 -31
  39. data/spec/db/6.0/20091231235959_some_name.rb +0 -9
  40. data/spec/db/6.0/20171231235959_super_update.rb +0 -9
  41. data/spec/db/data-6.0/20091231235959_some_name.rb +0 -9
  42. data/spec/db/data-6.0/20171231235959_super_update.rb +0 -9
  43. data/spec/db/data-6.0/20181128000207_excluded_file.rb.other_ext +0 -1
  44. data/spec/db/migrate/5.2/20131111111111_late_migration.rb +0 -9
  45. data/spec/db/migrate/5.2/20202020202011_db_migration.rb +0 -9
  46. /data/spec/db/migrate/{6.0/20131111111111_late_migration.rb → 20131111111111_late_migration.rb} +0 -0
  47. /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: c0ed214d2b242e9802276616e52abc79dc40081b413f6c38cdcbc5414d5897e9
4
- data.tar.gz: f4045cba836825fa34a0da775d38999ea472f2fb8624537e8ffc216f01204e85
3
+ metadata.gz: e439cb5180eca3cf1195560f8942178ac4ca64304489f1ec3ce3b1efa3462be4
4
+ data.tar.gz: efe9f8166f6fd7080d68b6dc050ac63124528d66ccc0465a505cd1002bbd7dce
5
5
  SHA512:
6
- metadata.gz: c98becc1b27f1afc98a8e45c4c81ea35195a992f2ff7433d8891b542cc05063dde7ffd1655ba521e14e700e477acc23345b15a893e2a900c508fc0b72c69302c
7
- data.tar.gz: 04560cc96cf6696b032b7a9d6892d70b772801b62910899ea2c360d7a17b98e8ab58bcd535e9a1fbdd40aa07c8633e92f5f52db8f9a6340c00788919eb5acf5b
6
+ metadata.gz: 64ca4079825785ca93b6e636a62b9117d15e5b6e10c667467657b229833014286e3339c644360f5ffa1ddab83bafca3dbff0397e5b1ecaa75eb646fe87b637d0
7
+ data.tar.gz: c9dced4b2efe056e2519ad8493c2d25a165d122f6350569a191b182d8d6f6e189f40fbff744f1b31039810636cd603b18363618080b4eb502570e1943828631d
@@ -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: Ruby Gem
1
+ name: Release Gem
2
2
 
3
3
  on:
4
- push:
5
- branches:
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.6
16
- uses: actions/setup-ruby@v1
14
+ - name: Set up Ruby 3.2
15
+ uses: ruby/setup-ruby@v1
17
16
  with:
18
- version: 2.6.x
17
+ ruby-version: '3.2'
19
18
 
20
19
  - name: Publish to RubyGems
21
20
  run: |
data/.gitignore CHANGED
@@ -7,5 +7,7 @@ spec/db/other_test.db
7
7
  spec/db/data_schema.rb
8
8
  .vscode/
9
9
  .DS_Store
10
+ .ruby-gemset
11
+ .ruby-version
10
12
  .idea/
11
- vendor/
13
+ vendor/
data/Appraisals CHANGED
@@ -1,7 +1,3 @@
1
- appraise 'rails-5.2' do
2
- gem 'rails', '~> 5.2.3'
3
- end
4
-
5
1
  appraise 'rails-6.0' do
6
2
  gem 'rails', '~> 6.0.0'
7
3
  end
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
- - [![Travis](https://img.shields.io/travis/ilyakatz/data-migrate.svg)](https://travis-ci.org/ilyakatz/data-migrate)
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
- ![Travis](./screenshot.png)
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 5.2 through 7.0
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:status:with_data` provides and additional column to indicate which type of migration.
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, '>= 5.0')
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
@@ -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.exists?(path)
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.assure_data_schema_table
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.assure_data_schema_table
12
+ DataMigrate::DataMigrator.create_data_schema_table
13
13
 
14
14
  return if info[:version].blank?
15
15
 
@@ -9,36 +9,23 @@ module DataMigrate
9
9
  extend ActiveRecord::Tasks::DatabaseTasks
10
10
 
11
11
  class << self
12
- def schema_file_type(_format = nil)
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 dump_filename(namespace, format = ActiveRecord::Base.schema_format)
17
- filename = if namespace == "primary"
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 schema_dump_path(db_config, format = ActiveRecord.schema_format)
27
- return ENV["DATA_SCHEMA"] if ENV["DATA_SCHEMA"]
28
-
29
- filename = if db_config.primary?
20
+ # This method is removed in Rails 7.0
21
+ def dump_filename(spec_name, format = ActiveRecord::Base.schema_format)
22
+ filename = if spec_name == "primary"
30
23
  schema_file_type(format)
31
24
  else
32
- [db_config.name, schema_file_type(format)].join("_")
25
+ "#{spec_name}_#{schema_file_type(format)}"
33
26
  end
34
27
 
35
- return unless filename
36
-
37
- File.dirname(filename) == schema_location ? filename : File.join(schema_location, filename)
38
- end
39
-
40
- def schema_location
41
- db_dir
28
+ ENV["DATA_SCHEMA"] || File.join(db_dir, filename)
42
29
  end
43
30
 
44
31
  def check_schema_file(filename)
@@ -55,9 +42,8 @@ module DataMigrate
55
42
  )
56
43
  end
57
44
 
58
- def sort_migrations set_1, set_2=nil
59
- migrations = set_1 + (set_2 || [])
60
- migrations.sort{|a,b| sort_string(a) <=> sort_string(b)}
45
+ def sort_migrations(*migrations)
46
+ migrations.flatten.sort { |a, b| sort_string(a) <=> sort_string(b) }
61
47
  end
62
48
 
63
49
  def sort_string migration
@@ -81,15 +67,26 @@ module DataMigrate
81
67
  )
82
68
  end
83
69
  end
84
- end
85
70
 
86
- # This overrides ActiveRecord::Tasks::DatabaseTasks
87
- def self.schema_file(_format = nil)
88
- File.join(db_dir, "data_schema.rb")
71
+ def schema_dump_path(db_config, format = ActiveRecord.schema_format)
72
+ return ENV["DATA_SCHEMA"] if ENV["DATA_SCHEMA"]
73
+
74
+ # We only require a schema.rb file for the primary database
75
+ return unless db_config.primary?
76
+
77
+ File.join(File.dirname(ActiveRecord::Tasks::DatabaseTasks.schema_dump_path(db_config, format)), schema_file_type)
78
+ end
79
+
80
+ # Override this method from `ActiveRecord::Tasks::DatabaseTasks`
81
+ # to ensure that the sha saved in ar_internal_metadata table
82
+ # is from the original schema.rb file
83
+ def schema_sha1(file)
84
+ ActiveRecord::Tasks::DatabaseTasks.schema_dump_path(ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env, name: "primary"))
85
+ end
89
86
  end
90
87
 
91
88
  def self.forward(step = 1)
92
- DataMigrate::DataMigrator.assure_data_schema_table
89
+ DataMigrate::DataMigrator.create_data_schema_table
93
90
  migrations = pending_migrations.reverse.pop(step).reverse
94
91
  migrations.each do | pending_migration |
95
92
  if pending_migration[:kind] == :data
@@ -97,23 +94,11 @@ module DataMigrate
97
94
  DataMigrate::DataMigrator.run(:up, data_migrations_path, pending_migration[:version])
98
95
  elsif pending_migration[:kind] == :schema
99
96
  ActiveRecord::Migration.write("== %s %s" % ["Schema", "=" * 69])
100
- DataMigrate::SchemaMigration.run(:up, schema_migrations_path, pending_migration[:version])
97
+ DataMigrate::SchemaMigration.run(:up, DataMigrate::SchemaMigration.migrations_paths, pending_migration[:version])
101
98
  end
102
99
  end
103
100
  end
104
101
 
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
102
  def self.pending_data_migrations
118
103
  data_migrations = DataMigrate::DataMigrator.migrations(data_migrations_path)
119
104
  sort_migrations(DataMigrate::DataMigrator.new(:up, data_migrations ).
@@ -124,36 +109,12 @@ module DataMigrate
124
109
  ::DataMigrate::SchemaMigration.pending_schema_migrations
125
110
  end
126
111
 
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
112
  def self.past_migrations(sort = nil)
137
- sort = sort.downcase if sort
138
- db_list_data =
139
- if DataMigrate::DataSchemaMigration.table_exists?
140
- DataMigrate::DataSchemaMigration.normalized_versions.sort
141
- else
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
113
+ data_versions = DataMigrate::DataSchemaMigration.table_exists? ? DataMigrate::DataSchemaMigration.normalized_versions : []
114
+ schema_versions = ActiveRecord::SchemaMigration.normalized_versions
115
+ migrations = data_versions.map { |v| { version: v.to_i, kind: :data } } + schema_versions.map { |v| { version: v.to_i, kind: :schema } }
116
+
117
+ sort&.downcase == "asc" ? sort_migrations(migrations) : sort_migrations(migrations).reverse
157
118
  end
158
119
  end
159
120
  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 && Rails.version > '6.1'
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
- if ActiveRecord.version >= Gem::Version.new('7.0')
36
- stream.puts "\ndatabase: #{ActiveRecord::Base.connection_db_config.configuration_hash[:database]}\n\n"
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.assure_data_schema_table
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 > 1 ? 'migrations:' : 'migration:'}"
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
- config = connect_to_database
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::Base.connection.select_values(
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
- Dir.foreach(File.join(Rails.root, schema_migrations_path)) do |file|
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: #{config['database']}\n\n"
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 connect_to_database
107
- config = if ActiveRecord.version < Gem::Version.new('6.1')
108
- ActiveRecord::Base.configurations[Rails.env || 'development']
109
- else
110
- ActiveRecord::Base.configurations.find_db_config(Rails.env || 'development').configuration_hash
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
@@ -1,3 +1,3 @@
1
1
  module DataMigrate
2
- VERSION = "8.5.0".freeze
2
+ VERSION = "9.1.1".freeze
3
3
  end
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", "data_migrator_five")
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", "status_service_five")
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