data_migrate 9.2.0 → 11.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/Changelog.md +36 -0
  3. data/README.md +20 -13
  4. data/lib/data_migrate/data_migrator.rb +5 -1
  5. data/lib/data_migrate/data_schema.rb +6 -6
  6. data/lib/data_migrate/database_configurations_wrapper.rb +11 -0
  7. data/lib/data_migrate/database_tasks.rb +218 -58
  8. data/lib/data_migrate/rails_helper.rb +15 -3
  9. data/lib/data_migrate/tasks/data_migrate_tasks.rb +8 -6
  10. data/lib/data_migrate/version.rb +1 -1
  11. data/lib/data_migrate.rb +1 -0
  12. data/lib/generators/data_migration/data_migration_generator.rb +2 -1
  13. data/tasks/databases.rake +9 -46
  14. metadata +8 -52
  15. data/.github/workflows/build.yml +0 -34
  16. data/.github/workflows/gempush.yml +0 -28
  17. data/.gitignore +0 -12
  18. data/.hound.yml +0 -4
  19. data/.overcommit.yml +0 -21
  20. data/.rbenv-gemsets +0 -2
  21. data/.rspec +0 -3
  22. data/.rubocop.yml +0 -2
  23. data/.ruby-style.yml +0 -1061
  24. data/Appraisals +0 -11
  25. data/Gemfile +0 -4
  26. data/Gemfile.lock +0 -148
  27. data/Gemfile.rails6.1 +0 -11
  28. data/Rakefile +0 -2
  29. data/data_migrate.gemspec +0 -41
  30. data/gemfiles/rails_6.1.gemfile +0 -8
  31. data/gemfiles/rails_6.1.gemfile.lock +0 -227
  32. data/gemfiles/rails_7.0.gemfile +0 -8
  33. data/gemfiles/rails_7.0.gemfile.lock +0 -229
  34. data/gemfiles/rails_7.1.gemfile +0 -8
  35. data/gemfiles/rails_7.1.gemfile.lock +0 -262
  36. data/screenshot.png +0 -0
  37. data/spec/data_migrate/config_spec.rb +0 -69
  38. data/spec/data_migrate/data_migrator_spec.rb +0 -83
  39. data/spec/data_migrate/data_schema_migration_spec.rb +0 -33
  40. data/spec/data_migrate/data_spec.rb +0 -74
  41. data/spec/data_migrate/database_tasks_spec.rb +0 -105
  42. data/spec/data_migrate/migration.rb +0 -17
  43. data/spec/data_migrate/migration_context_spec.rb +0 -108
  44. data/spec/data_migrate/schema_dumper_spec.rb +0 -36
  45. data/spec/data_migrate/schema_migration_spec.rb +0 -104
  46. data/spec/data_migrate/status_service_spec.rb +0 -76
  47. data/spec/data_migrate/tasks/data_migrate_tasks_spec.rb +0 -129
  48. data/spec/db/data/20091231235959_some_name.rb +0 -9
  49. data/spec/db/data/20171231235959_super_update.rb +0 -9
  50. data/spec/db/data/20181128000207_excluded_file.rb.other_ext +0 -1
  51. data/spec/db/data/partial_schema/data_schema.rb +0 -1
  52. data/spec/db/data/partial_schema/test_data_schema.rb +0 -1
  53. data/spec/db/data/schema/data_schema.rb +0 -1
  54. data/spec/db/data/schema/test_data_schema.rb +0 -1
  55. data/spec/db/migrate/20131111111111_late_migration.rb +0 -9
  56. data/spec/db/migrate/20202020202011_db_migration.rb +0 -9
  57. data/spec/generators/data_migration/data_migration_generator_spec.rb +0 -101
  58. data/spec/spec_helper.rb +0 -28
  59. data/tasks/.gitkeep +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 98382ecfcd0ad635d3e53bb746f4b2a7a402c2b2a78d312e864bbe6f288b65b1
4
- data.tar.gz: 315d89e1465e7f5a804d4e70d67963949608632b91b7276ea23319f2183789ba
3
+ metadata.gz: 907383933e031d0a1da7bad6dd3a43807794c0abeb27313af27e59ed8d802f3a
4
+ data.tar.gz: 1ddf826962f7078972ca9a234a723646d7744a41990dbe38550155fc0276fca3
5
5
  SHA512:
6
- metadata.gz: '04098142838de3bb59be85adc46a9a4aed734668a154378488c74ea2a1982793a45450364f089edb479add87cab1b47e801916553c01ad003bbe71c0ba06b855'
7
- data.tar.gz: 4654fdfb138a53c5cc4ff59df1d63bc66ccd0f66d292f79c0d37ccbdb43c184144d5122718e9027ea643f1e0786d79844aee44d70feb5a1c455a59670286e5ef
6
+ metadata.gz: e9e79dd56bda8e5f2afecb79f940eb17d7b5439c1d07b38d67943d58fc0ebaf159283d4478ac13925a2ee399097f6825b519052d3fea9f7b04c659a98647e75f
7
+ data.tar.gz: '0959a1bcf969cbc7834c839eca029d1416ff287ea2927a8ae0493a7880fb6910adea4baf5a2d162bd4cce35f80b610dc2f9d671cf77cf6583a699a96196871de'
data/Changelog.md CHANGED
@@ -1,5 +1,41 @@
1
1
  # Changelog
2
2
 
3
+ # 11.2.0
4
+
5
+ - Remove committed Gemfile.lock, reduce bundled file list when running `gem install` https://github.com/ilyakatz/data-migrate/pull/351
6
+ - [Bump actionpack from 7.1.3.4 to 7.1.4.1](https://github.com/ilyakatz/data-migrate/pull/348)
7
+ - [Bump rexml from 3.3.6 to 3.3.9](https://github.com/ilyakatz/data-migrate/pull/349)
8
+ - Fix db_config_with_versions arity change and backport https://github.com/ilyakatz/data-migrate/pull/337
9
+
10
+ # 11.1.0
11
+ - Allow multiple data migration paths https://github.com/ilyakatz/data-migrate/pull/331
12
+ - Fix db:prepare:with_data task on [Rails 7.2](https://github.com/ilyakatz/data-migrate/pull/339)
13
+ - Fix ConnectionPool deprecation warnings on [Rails 7.2](https://github.com/ilyakatz/data-migrate/pull/341)
14
+ - Add Rails 8.0 to testing grounds https://github.com/ilyakatz/data-migrate/pull/341
15
+ - [Bump webrick from 1.8.1 to 1.8.2](https://github.com/ilyakatz/data-migrate/pull/345)
16
+
17
+ # 11.0.0
18
+ - [Update rexml to 3.3.6](https://github.com/ilyakatz/data-migrate/pull/329)
19
+ - Fixes a bug which ignored migrations on [Rails 7.1 and higher](https://github.com/ilyakatz/data-migrate/pull/326)
20
+
21
+ # 11.0.0rc
22
+ - Remove Ruby 3.0 from build matrix
23
+ - Support Rails 7.2.0 https://github.com/ilyakatz/data-migrate/pull/312
24
+ - Update gemfile.lock builds
25
+
26
+ ## 9.4.2
27
+ - Fix db:prepare:with_data task
28
+
29
+ ## 9.4.1
30
+ - Add db:prepare task
31
+
32
+ ## 9.4.0
33
+ - Reset model schema cache before each data migration https://github.com/ilyakatz/data-migrate/pull/307
34
+ - Run load_config rake task before db:migrate:with_data https://github.com/ilyakatz/data-migrate/pull/308
35
+
36
+ ## 9.3.0
37
+ - Improve with_data Rake task for multiple database https://github.com/ilyakatz/data-migrate/pull/296
38
+
3
39
  ## 9.2.0
4
40
  - Support Rails 7.1 https://github.com/ilyakatz/data-migrate/pull/278
5
41
  - Build and test against 7.1.0.rc1 https://github.com/ilyakatz/data-migrate/pull/286
data/README.md CHANGED
@@ -36,8 +36,13 @@ 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.1 through 7.2
40
40
 
41
+ For **Rails 6.0** support, please use gem version `9.1.x`:
42
+
43
+ ```ruby
44
+ gem 'data_migrate', '~> 9.1.0'
45
+ ```
41
46
 
42
47
  #### v1
43
48
 
@@ -86,6 +91,7 @@ You can generate a data migration as you would a schema migration:
86
91
  rake db:migrate:status:with_data # Display status of data and schema migrations
87
92
  rake db:migrate:up:with_data # Runs the "up" for a given migration VERSION
88
93
  rake db:migrate:with_data # Migrate the database data and schema (options: VERSION=x, VERBOSE=false)
94
+ rake db:prepare:with_data # Runs setup if database does not exist, or runs data and schema migrations if it does
89
95
  rake db:rollback:with_data # Rolls the schema back to the previous version (specify steps w/ STEP=n)
90
96
  rake db:schema:load:with_data # Load both schema.rb and data_schema.rb file into the database
91
97
  rake db:structure:load:with_data # Load both structure.sql and data_schema.rb file into the database
@@ -138,19 +144,12 @@ From now on capistrano will run `rake db:migrate:with_data` in every deploy.
138
144
  ## Rails Engines support
139
145
 
140
146
  This gem also has a initial support for adding data migrations inside Rails engines.
141
- Inside the Engine's class initializer (the one that inherits from `Rails::Engine`, usually inside `engines/ENGINE_NAME/lib/engine.rb`) you need to add something like this:
142
-
147
+ Just add your engines directory pattern to data_migrations initializer, for example
148
+ in the case your engines are located in `engines` folder you can set it up like this:
143
149
 
144
150
  ```ruby
145
- module EngineName
146
- class Engine < ::Rails::Engine
147
- initializer :engine_name do |app|
148
- ::DataMigrate.configure do |data_migrate|
149
- default_path = ::DataMigrate::Config.new.data_migrations_path
150
- data_migrate.data_migrations_path = [default_path, root.join('db', 'data')]
151
- end
152
- end
153
- end
151
+ DataMigrate.configure do |config|
152
+ config.data_migrations_path = ['db/data'] + Dir['engines/**/db/data']
154
153
  end
155
154
  ```
156
155
 
@@ -163,12 +162,20 @@ Then, in the Engine's `db/data` folder, you can add data migrations and run them
163
162
  Run tests for a specific version of Rails
164
163
 
165
164
  ```
166
- bundle exec appraisal install
167
165
  bundle exec appraisal rails-6.1 rspec
168
166
  bundle exec appraisal rails-7.0 rspec
169
167
  bundle exec appraisal rails-7.1 rspec
168
+ bundle exec appraisal rails-7.2 rspec
170
169
  ```
171
170
 
171
+ ## Releasing new version
172
+
173
+ 1. Create a new tag, eg `git tag 9.4.1`
174
+ 1. Go to https://github.com/ilyakatz/data-migrate/tags
175
+ 1. Click "Create release" under 9.4.1
176
+ 1. CLick "Generate release notes"
177
+ 1. Click "Publish release"
178
+
172
179
  ## Thanks
173
180
 
174
181
  [Andrew J Vargo](http://github.com/ajvargo) Andrew was the original creator and maintainer of this project!
@@ -12,7 +12,7 @@ module DataMigrate
12
12
 
13
13
  class << self
14
14
  def migrations_paths
15
- [DataMigrate.config.data_migrations_path]
15
+ Array.wrap(DataMigrate.config.data_migrations_path)
16
16
  end
17
17
 
18
18
  def create_data_schema_table
@@ -54,6 +54,10 @@ module DataMigrate
54
54
 
55
55
  #TODO: this was added to be backward compatible, need to re-evaluate
56
56
  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
+
57
61
  DataMigrate::MigrationContext.new(migration_paths).run(direction, version)
58
62
  end
59
63
 
@@ -29,13 +29,13 @@ module DataMigrate
29
29
  end
30
30
 
31
31
  def versions
32
- @versions ||= begin
33
- versions = []
34
- Dir.foreach(DataMigrate::DataMigrator.full_migrations_path) do |file|
35
- match_data = DataMigrate::DataMigrator.match(file)
36
- versions << match_data[1].to_i if match_data
32
+ @versions ||= Set.new.tap do |versions|
33
+ DataMigrate::DataMigrator.migrations_paths.each do |path|
34
+ Dir.foreach(path) do |file|
35
+ match_data = DataMigrate::DataMigrator.match(file)
36
+ versions << match_data[1].to_i if match_data
37
+ end
37
38
  end
38
- versions
39
39
  end
40
40
  end
41
41
 
@@ -0,0 +1,11 @@
1
+ module DataMigrate
2
+ # This wrapper is used to differentiate between
3
+ # a data and schema db config when running migrations
4
+ class DatabaseConfigurationWrapper
5
+ attr_reader :db_config
6
+
7
+ def initialize(db_config)
8
+ @db_config = db_config
9
+ end
10
+ end
11
+ end
@@ -5,87 +5,142 @@ require "data_migrate/config"
5
5
  module DataMigrate
6
6
  ##
7
7
  # This class extends DatabaseTasks to add a schema_file method.
8
- class DatabaseTasks
8
+ module DatabaseTasks
9
9
  extend ActiveRecord::Tasks::DatabaseTasks
10
+ extend self
10
11
 
11
- class << self
12
- def schema_file(_format = nil)
13
- File.join(db_dir, "data_schema.rb")
14
- end
15
-
16
- def schema_file_type(_format = nil)
17
- "data_schema.rb"
18
- end
19
-
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"
23
- schema_file_type(format)
12
+ # These method are only introduced in Rails 7.1
13
+ unless respond_to?(:with_temporary_pool_for_each)
14
+ def with_temporary_pool_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_pool(db_config, &block)
24
18
  else
25
- "#{spec_name}_#{schema_file_type(format)}"
19
+ ActiveRecord::Base.configurations.configs_for(env_name: env, name: name).each do |db_config|
20
+ with_temporary_pool(db_config, &block)
21
+ end
26
22
  end
27
-
28
- ENV["DATA_SCHEMA"] || File.join(db_dir, filename)
29
23
  end
30
24
 
31
- def check_schema_file(filename)
32
- unless File.exist?(filename)
33
- message = +%{#{filename} doesn't exist yet. Run `rake data:migrate` to create it, then try again.}
34
- Kernel.abort message
25
+ def with_temporary_connection(db_config, &block) # :nodoc:
26
+ with_temporary_pool(db_config) do |pool|
27
+ pool.with_connection(&block)
35
28
  end
36
29
  end
37
30
 
38
- def pending_migrations
39
- sort_migrations(
40
- pending_schema_migrations,
41
- pending_data_migrations
42
- )
31
+ def migration_class # :nodoc:
32
+ ActiveRecord::Base
43
33
  end
44
34
 
45
- def sort_migrations(*migrations)
46
- migrations.flatten.sort { |a, b| sort_string(a) <=> sort_string(b) }
35
+ def migration_connection # :nodoc:
36
+ migration_class.connection
47
37
  end
48
38
 
49
- def sort_string migration
50
- "#{migration[:version]}_#{migration[:kind] == :data ? 1 : 0}"
51
- end
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)
52
42
 
53
- def data_migrations_path
54
- ::DataMigrate.config.data_migrations_path
43
+ yield pool
44
+ ensure
45
+ migration_class.connection_handler.establish_connection(original_db_config)
55
46
  end
47
+ end
56
48
 
57
- def run_migration(migration, direction)
58
- if migration[:kind] == :data
59
- ::ActiveRecord::Migration.write("== %s %s" % ['Data', "=" * 71])
60
- ::DataMigrate::DataMigrator.run(direction, data_migrations_path, migration[:version])
61
- else
62
- ::ActiveRecord::Migration.write("== %s %s" % ['Schema', "=" * 69])
63
- ::DataMigrate::SchemaMigration.run(
64
- direction,
65
- ::DataMigrate::SchemaMigration.migrations_paths,
66
- migration[:version]
67
- )
49
+ def db_configs_with_versions
50
+ db_configs_with_versions = Hash.new { |h, k| h[k] = [] }
51
+
52
+ with_temporary_pool_for_each do |pool|
53
+ db_config = 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
68
62
  end
69
63
  end
70
64
 
71
- def schema_dump_path(db_config, format = ActiveRecord.schema_format)
72
- return ENV["DATA_SCHEMA"] if ENV["DATA_SCHEMA"]
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)}"
82
+ end
73
83
 
74
- # We only require a schema.rb file for the primary database
75
- return unless db_config.primary?
84
+ ENV["DATA_SCHEMA"] || File.join(db_dir, filename)
85
+ end
76
86
 
77
- File.join(File.dirname(ActiveRecord::Tasks::DatabaseTasks.schema_dump_path(db_config, format)), schema_file_type)
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
78
91
  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
+
105
+ def sort_string migration
106
+ "#{migration[:version]}_#{migration[:kind] == :data ? 1 : 0}"
107
+ end
79
108
 
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"))
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
+ )
85
124
  end
86
125
  end
87
126
 
88
- def self.forward(step = 1)
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)
89
144
  DataMigrate::DataMigrator.create_data_schema_table
90
145
  migrations = pending_migrations.reverse.pop(step).reverse
91
146
  migrations.each do | pending_migration |
@@ -99,7 +154,7 @@ module DataMigrate
99
154
  end
100
155
  end
101
156
 
102
- def self.pending_data_migrations
157
+ def pending_data_migrations
103
158
  data_migrations = DataMigrate::DataMigrator.migrations(data_migrations_path)
104
159
  data_migrator = DataMigrate::RailsHelper.data_migrator(:up, data_migrations)
105
160
  sort_migrations(
@@ -107,16 +162,121 @@ module DataMigrate
107
162
  )
108
163
  end
109
164
 
110
- def self.pending_schema_migrations
165
+ def pending_schema_migrations
111
166
  ::DataMigrate::SchemaMigration.pending_schema_migrations
112
167
  end
113
168
 
114
- def self.past_migrations(sort = nil)
169
+ def past_migrations(sort = nil)
115
170
  data_versions = DataMigrate::RailsHelper.data_schema_migration.table_exists? ? DataMigrate::RailsHelper.data_schema_migration.normalized_versions : []
116
171
  schema_versions = DataMigrate::RailsHelper.schema_migration.normalized_versions
117
172
  migrations = data_versions.map { |v| { version: v.to_i, kind: :data } } + schema_versions.map { |v| { version: v.to_i, kind: :schema } }
118
173
 
119
174
  sort&.downcase == "asc" ? sort_migrations(migrations) : sort_migrations(migrations).reverse
120
175
  end
176
+
177
+ def self.migrate_with_data
178
+ DataMigrate::DataMigrator.create_data_schema_table
179
+
180
+ ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
181
+
182
+ # 7.2 removes the param for db_configs_with_versions in https://github.com/rails/rails/commit/9572fcb4a0bd5396436689a6a42613886871cd81
183
+ # 7.1 stable backported the change in https://github.com/rails/rails/commit/c53ec4b60980036b43528829d4b0b7457f759224
184
+ schema_mapped_versions = if Gem::Dependency.new("railties", ">= 7.1.4").match?("railties", Gem.loaded_specs["railties"].version, true)
185
+ ActiveRecord::Tasks::DatabaseTasks.db_configs_with_versions
186
+ else
187
+ db_configs = ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env)
188
+
189
+ ActiveRecord::Tasks::DatabaseTasks.db_configs_with_versions(db_configs)
190
+ end
191
+
192
+ data_mapped_versions = DataMigrate::DatabaseTasks.db_configs_with_versions
193
+
194
+ mapped_versions = schema_mapped_versions.merge(data_mapped_versions) do |_key, schema_db_configs, data_db_configs|
195
+ schema_db_configs + data_db_configs
196
+ end
197
+
198
+ mapped_versions.sort.each do |version, db_configs|
199
+ db_configs.each do |db_config|
200
+ if is_data_migration = db_config.is_a?(DataMigrate::DatabaseConfigurationWrapper)
201
+ db_config = db_config.db_config
202
+ end
203
+
204
+ DataMigrate::DatabaseTasks.with_temporary_connection(db_config) do
205
+ if is_data_migration
206
+ DataMigrate::DataMigrator.run(:up, DataMigrate::DatabaseTasks.data_migrations_path, version)
207
+ else
208
+ ActiveRecord::Tasks::DatabaseTasks.migrate(version)
209
+ end
210
+ end
211
+ end
212
+ end
213
+ end
214
+
215
+ def self.prepare_all_with_data
216
+ seed = false
217
+
218
+ each_current_configuration(env) do |db_config|
219
+ next unless primary?(db_config)
220
+
221
+ with_temporary_pool(db_config) do |pool|
222
+ unless database_exists?(pool.connection)
223
+ create(db_config)
224
+ if File.exist?(schema_dump_path(db_config))
225
+ load_schema(db_config, schema_format, nil)
226
+ load_schema_current(
227
+ :ruby,
228
+ ENV["DATA_SCHEMA"]
229
+ )
230
+ end
231
+
232
+ seed = true
233
+ end
234
+
235
+ migrate_with_data
236
+ if dump_schema_after_migration?
237
+ dump_schema(db_config)
238
+ DataMigrate::Tasks::DataMigrateTasks.dump
239
+ end
240
+ end
241
+ end
242
+
243
+ load_seed if seed
244
+ end
245
+
246
+ private
247
+
248
+ def database_exists?(connection)
249
+ if connection.respond_to?(:database_exists?) # Rails 7.1+
250
+ connection.database_exists?
251
+ else
252
+ connection.table_exists?(ActiveRecord::SchemaMigration.table_name)
253
+ end
254
+ rescue ActiveRecord::NoDatabaseError
255
+ false
256
+ end
257
+
258
+ def primary?(db_config)
259
+ if db_config.respond_to?(:primary?) # Rails 7.0+
260
+ db_config.primary?
261
+ else
262
+ db_config.name == "primary"
263
+ end
264
+ end
265
+
266
+ def dump_schema_after_migration?
267
+ if ActiveRecord.respond_to?(:dump_schema_after_migration)
268
+ ActiveRecord.dump_schema_after_migration
269
+ else
270
+ ActiveRecord::Base.dump_schema_after_migration
271
+ end
272
+ end
273
+
274
+ def schema_format
275
+ if ActiveRecord.respond_to?(:schema_format)
276
+ ActiveRecord.schema_format
277
+ else
278
+ ActiveRecord::Base.schema_format
279
+ end
280
+ end
121
281
  end
122
282
  end
@@ -1,6 +1,12 @@
1
1
  module DataMigrate
2
2
  class RailsHelper
3
3
  class << self
4
+ def rails_version_equal_to_or_higher_than_7_2
5
+ return @equal_to_or_higher_than_7_2 if defined?(@equal_to_or_higher_than_7_2)
6
+
7
+ @equal_to_or_higher_than_7_2 = Gem::Dependency.new("railties", ">= 7.2.0.alpha").match?("railties", Gem.loaded_specs["railties"].version, true)
8
+ end
9
+
4
10
  def rails_version_equal_to_or_higher_than_7_1
5
11
  return @equal_to_or_higher_than_7_1 if defined?(@equal_to_or_higher_than_7_1)
6
12
 
@@ -14,7 +20,9 @@ module DataMigrate
14
20
  end
15
21
 
16
22
  def internal_metadata
17
- if rails_version_equal_to_or_higher_than_7_1
23
+ if rails_version_equal_to_or_higher_than_7_2
24
+ ActiveRecord::Base.connection_pool.internal_metadata
25
+ elsif rails_version_equal_to_or_higher_than_7_1
18
26
  ActiveRecord::Base.connection.internal_metadata
19
27
  else
20
28
  ActiveRecord::InternalMetadata
@@ -22,7 +30,9 @@ module DataMigrate
22
30
  end
23
31
 
24
32
  def schema_migration
25
- if rails_version_equal_to_or_higher_than_7_1
33
+ if rails_version_equal_to_or_higher_than_7_2
34
+ ActiveRecord::Base.connection_pool.schema_migration
35
+ elsif rails_version_equal_to_or_higher_than_7_1
26
36
  ActiveRecord::Base.connection.schema_migration
27
37
  else
28
38
  ActiveRecord::SchemaMigration
@@ -54,7 +64,9 @@ module DataMigrate
54
64
  end
55
65
 
56
66
  def data_schema_migration
57
- if rails_version_equal_to_or_higher_than_7_1
67
+ if rails_version_equal_to_or_higher_than_7_2
68
+ DataMigrate::DataSchemaMigration.new(ActiveRecord::Tasks::DatabaseTasks.migration_connection_pool)
69
+ elsif rails_version_equal_to_or_higher_than_7_1
58
70
  DataMigrate::DataSchemaMigration.new(ActiveRecord::Tasks::DatabaseTasks.migration_connection)
59
71
  else
60
72
  DataMigrate::DataSchemaMigration
@@ -6,7 +6,7 @@ module DataMigrate
6
6
  extend self
7
7
 
8
8
  def migrations_paths
9
- @migrations_paths ||= DataMigrate.config.data_migrations_path
9
+ @migrations_paths ||= Array.wrap(DataMigrate.config.data_migrations_path)
10
10
  end
11
11
 
12
12
  def dump
@@ -55,11 +55,13 @@ module DataMigrate
55
55
  db_list_schema = DataMigrate::RailsHelper.schema_migration_versions
56
56
  file_list = []
57
57
 
58
- Dir.foreach(File.join(Rails.root, migrations_paths)) do |file|
59
- # only files matching "20091231235959_some_name.rb" pattern
60
- if match_data = /(\d{14})_(.+)\.rb/.match(file)
61
- status = db_list_data.delete(match_data[1]) ? 'up' : 'down'
62
- file_list << [status, match_data[1], match_data[2], 'data']
58
+ migrations_paths.each do |path|
59
+ Dir.foreach(File.join(Rails.root, path)) do |file|
60
+ # only files matching "20091231235959_some_name.rb" pattern
61
+ if match_data = /(\d{14})_(.+)\.rb/.match(file)
62
+ status = db_list_data.delete(match_data[1]) ? 'up' : 'down'
63
+ file_list << [status, match_data[1], match_data[2], 'data']
64
+ end
63
65
  end
64
66
  end
65
67
 
@@ -1,3 +1,3 @@
1
1
  module DataMigrate
2
- VERSION = "9.2.0".freeze
2
+ VERSION = "11.2.0".freeze
3
3
  end
data/lib/data_migrate.rb CHANGED
@@ -12,6 +12,7 @@ require File.join(File.dirname(__FILE__), "data_migrate", "railtie")
12
12
  require File.join(File.dirname(__FILE__), "data_migrate", "tasks/data_migrate_tasks")
13
13
  require File.join(File.dirname(__FILE__), "data_migrate", "config")
14
14
  require File.join(File.dirname(__FILE__), "data_migrate", "schema_migration")
15
+ require File.join(File.dirname(__FILE__), "data_migrate", "database_configurations_wrapper")
15
16
 
16
17
  module DataMigrate
17
18
  def self.root
@@ -38,8 +38,9 @@ module DataMigrate
38
38
  File.join(data_migrations_path, "#{file_name}.rb")
39
39
  end
40
40
 
41
+ # Use the first path in the data_migrations_path as the target directory
41
42
  def data_migrations_path
42
- DataMigrate.config.data_migrations_path
43
+ Array.wrap(DataMigrate.config.data_migrations_path).first
43
44
  end
44
45
  end
45
46
  end