data_migrate 9.4.0 → 11.3.1
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.
- checksums.yaml +4 -4
- data/Changelog.md +38 -0
- data/README.md +23 -13
- data/lib/data_migrate/config.rb +2 -2
- data/lib/data_migrate/data_migrator.rb +1 -2
- data/lib/data_migrate/data_schema.rb +6 -6
- data/lib/data_migrate/data_schema_migration.rb +2 -2
- data/lib/data_migrate/database_tasks.rb +123 -10
- data/lib/data_migrate/rails_helper.rb +15 -3
- data/lib/data_migrate/schema_dumper.rb +8 -1
- data/lib/data_migrate/tasks/data_migrate_tasks.rb +8 -6
- data/lib/data_migrate/version.rb +1 -1
- data/lib/generators/data_migration/data_migration_generator.rb +2 -1
- data/tasks/databases.rake +8 -28
- metadata +5 -50
- data/.github/workflows/build.yml +0 -35
- data/.github/workflows/gempush.yml +0 -28
- data/.gitignore +0 -12
- data/.hound.yml +0 -4
- data/.overcommit.yml +0 -21
- data/.rbenv-gemsets +0 -2
- data/.rspec +0 -3
- data/.rubocop.yml +0 -2
- data/.ruby-style.yml +0 -1061
- data/Appraisals +0 -11
- data/Gemfile +0 -3
- data/Gemfile.lock +0 -184
- data/Rakefile +0 -2
- data/data_migrate.gemspec +0 -41
- data/gemfiles/rails_6.1.gemfile +0 -7
- data/gemfiles/rails_6.1.gemfile.lock +0 -229
- data/gemfiles/rails_7.0.gemfile +0 -7
- data/gemfiles/rails_7.0.gemfile.lock +0 -230
- data/gemfiles/rails_7.1.gemfile +0 -7
- data/gemfiles/rails_7.1.gemfile.lock +0 -263
- data/lib/data_migrate/test.rb +0 -14
- data/screenshot.png +0 -0
- data/spec/data_migrate/config_spec.rb +0 -69
- data/spec/data_migrate/data_migrator_spec.rb +0 -83
- data/spec/data_migrate/data_schema_migration_spec.rb +0 -33
- data/spec/data_migrate/data_spec.rb +0 -74
- data/spec/data_migrate/database_tasks_spec.rb +0 -105
- data/spec/data_migrate/migration.rb +0 -17
- data/spec/data_migrate/migration_context_spec.rb +0 -108
- data/spec/data_migrate/schema_dumper_spec.rb +0 -36
- data/spec/data_migrate/schema_migration_spec.rb +0 -104
- data/spec/data_migrate/status_service_spec.rb +0 -76
- data/spec/data_migrate/tasks/data_migrate_tasks_spec.rb +0 -129
- data/spec/db/data/20091231235959_some_name.rb +0 -9
- data/spec/db/data/20171231235959_super_update.rb +0 -9
- data/spec/db/data/20181128000207_excluded_file.rb.other_ext +0 -1
- data/spec/db/data/partial_schema/data_schema.rb +0 -1
- data/spec/db/data/partial_schema/test_data_schema.rb +0 -1
- data/spec/db/data/schema/data_schema.rb +0 -1
- data/spec/db/data/schema/test_data_schema.rb +0 -1
- data/spec/db/migrate/20131111111111_late_migration.rb +0 -9
- data/spec/db/migrate/20202020202011_db_migration.rb +0 -9
- data/spec/generators/data_migration/data_migration_generator_spec.rb +0 -101
- data/spec/spec_helper.rb +0 -28
- data/tasks/.gitkeep +0 -0
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 7bef47d5608c410a0158dfd4847c065895db273e0809f8467f3cae2e4d428604
|
|
4
|
+
data.tar.gz: 5f1221a6c8fa70c5d095790892877e1aee369c3a06f2e145ec87534e06993065
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3df1a19a487efc065928f135dc8e372e282d300956b2172d654247d9f6bbc3edbfff279e309b1d557093e7ef44ba49217de9dbfd7d58f75c96d6e91be83fd3ec
|
|
7
|
+
data.tar.gz: 524ed4970e8f98a761d6b845b7d1d9be23015599be203e4aed2fb7fda68fe16b84a9dbcb29e68746de6d73559a2de584bad35307a1d60d134c8411b9f07fd75c
|
data/Changelog.md
CHANGED
|
@@ -1,5 +1,43 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
# 11.3.1
|
|
4
|
+
- Schema version -- do formatted version like activerecord https://github.com/ilyakatz/data-migrate/pull/363
|
|
5
|
+
- Resolve ActiveSupport::Configurable deprecation in Rails 8.2 https://github.com/ilyakatz/data-migrate/pull/365
|
|
6
|
+
- Add support for Rails 8.1 in Appraisal https://github.com/ilyakatz/data-migrate/pull/366
|
|
7
|
+
|
|
8
|
+
# 11.3.0
|
|
9
|
+
- Make table_name configurable https://github.com/ilyakatz/data-migrate/pull/361
|
|
10
|
+
- Use lease_connection over deprecated connection for rails 8 https://github.com/ilyakatz/data-migrate/pull/353
|
|
11
|
+
- Add Ruby 3.4 to CI matrix
|
|
12
|
+
|
|
13
|
+
# 11.2.0
|
|
14
|
+
- Remove committed Gemfile.lock, reduce bundled file list when running `gem install` https://github.com/ilyakatz/data-migrate/pull/351
|
|
15
|
+
- [Bump actionpack from 7.1.3.4 to 7.1.4.1](https://github.com/ilyakatz/data-migrate/pull/348)
|
|
16
|
+
- [Bump rexml from 3.3.6 to 3.3.9](https://github.com/ilyakatz/data-migrate/pull/349)
|
|
17
|
+
- Fix db_config_with_versions arity change and backport https://github.com/ilyakatz/data-migrate/pull/337
|
|
18
|
+
|
|
19
|
+
# 11.1.0
|
|
20
|
+
- Allow multiple data migration paths https://github.com/ilyakatz/data-migrate/pull/331
|
|
21
|
+
- Fix db:prepare:with_data task on [Rails 7.2](https://github.com/ilyakatz/data-migrate/pull/339)
|
|
22
|
+
- Fix ConnectionPool deprecation warnings on [Rails 7.2](https://github.com/ilyakatz/data-migrate/pull/341)
|
|
23
|
+
- Add Rails 8.0 to testing grounds https://github.com/ilyakatz/data-migrate/pull/341
|
|
24
|
+
- [Bump webrick from 1.8.1 to 1.8.2](https://github.com/ilyakatz/data-migrate/pull/345)
|
|
25
|
+
|
|
26
|
+
# 11.0.0
|
|
27
|
+
- [Update rexml to 3.3.6](https://github.com/ilyakatz/data-migrate/pull/329)
|
|
28
|
+
- Fixes a bug which ignored migrations on [Rails 7.1 and higher](https://github.com/ilyakatz/data-migrate/pull/326)
|
|
29
|
+
|
|
30
|
+
# 11.0.0rc
|
|
31
|
+
- Remove Ruby 3.0 from build matrix
|
|
32
|
+
- Support Rails 7.2.0 https://github.com/ilyakatz/data-migrate/pull/312
|
|
33
|
+
- Update gemfile.lock builds
|
|
34
|
+
|
|
35
|
+
## 9.4.2
|
|
36
|
+
- Fix db:prepare:with_data task
|
|
37
|
+
|
|
38
|
+
## 9.4.1
|
|
39
|
+
- Add db:prepare task
|
|
40
|
+
|
|
3
41
|
## 9.4.0
|
|
4
42
|
- Reset model schema cache before each data migration https://github.com/ilyakatz/data-migrate/pull/307
|
|
5
43
|
- Run load_config rake task before db:migrate:with_data https://github.com/ilyakatz/data-migrate/pull/308
|
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
|
|
39
|
+
Support Rails 6.1 through 8.0
|
|
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
|
|
@@ -109,6 +115,7 @@ You can override this setting in `config/initializers/data_migrate.rb`
|
|
|
109
115
|
|
|
110
116
|
```ruby
|
|
111
117
|
DataMigrate.configure do |config|
|
|
118
|
+
config.data_migrations_table_name = 'my_migrations_database_name'
|
|
112
119
|
config.data_migrations_path = 'db/awesomepath/'
|
|
113
120
|
config.data_template_path = Rails.root.join("lib", "awesomepath", "custom_data_migration.rb")
|
|
114
121
|
config.db_configuration = {
|
|
@@ -138,19 +145,12 @@ From now on capistrano will run `rake db:migrate:with_data` in every deploy.
|
|
|
138
145
|
## Rails Engines support
|
|
139
146
|
|
|
140
147
|
This gem also has a initial support for adding data migrations inside Rails engines.
|
|
141
|
-
|
|
142
|
-
|
|
148
|
+
Just add your engines directory pattern to data_migrations initializer, for example
|
|
149
|
+
in the case your engines are located in `engines` folder you can set it up like this:
|
|
143
150
|
|
|
144
151
|
```ruby
|
|
145
|
-
|
|
146
|
-
|
|
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
|
|
152
|
+
DataMigrate.configure do |config|
|
|
153
|
+
config.data_migrations_path = ['db/data'] + Dir['engines/**/db/data']
|
|
154
154
|
end
|
|
155
155
|
```
|
|
156
156
|
|
|
@@ -163,12 +163,22 @@ Then, in the Engine's `db/data` folder, you can add data migrations and run them
|
|
|
163
163
|
Run tests for a specific version of Rails
|
|
164
164
|
|
|
165
165
|
```
|
|
166
|
-
bundle exec appraisal install
|
|
167
166
|
bundle exec appraisal rails-6.1 rspec
|
|
168
167
|
bundle exec appraisal rails-7.0 rspec
|
|
169
168
|
bundle exec appraisal rails-7.1 rspec
|
|
169
|
+
bundle exec appraisal rails-7.2 rspec
|
|
170
|
+
bundle exec appraisal rails-8.0 rspec
|
|
170
171
|
```
|
|
171
172
|
|
|
173
|
+
## Releasing new version
|
|
174
|
+
|
|
175
|
+
1. Update version.rb file, run `bundle exec appraisal` to update the version in corresponding gemfile.lock
|
|
176
|
+
2. Create a new tag, eg `git tag 9.4.1`
|
|
177
|
+
3. Go to https://github.com/ilyakatz/data-migrate/tags
|
|
178
|
+
4. Click "Create release" under 9.4.1
|
|
179
|
+
5. CLick "Generate release notes"
|
|
180
|
+
6. Click "Publish release"
|
|
181
|
+
|
|
172
182
|
## Thanks
|
|
173
183
|
|
|
174
184
|
[Andrew J Vargo](http://github.com/ajvargo) Andrew was the original creator and maintainer of this project!
|
data/lib/data_migrate/config.rb
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
module DataMigrate
|
|
2
|
-
include ActiveSupport::Configurable
|
|
3
2
|
class << self
|
|
4
3
|
|
|
5
4
|
def configure
|
|
@@ -12,11 +11,12 @@ module DataMigrate
|
|
|
12
11
|
end
|
|
13
12
|
|
|
14
13
|
class Config
|
|
15
|
-
attr_accessor :data_migrations_path, :data_template_path, :db_configuration, :spec_name
|
|
14
|
+
attr_accessor :data_migrations_table_name, :data_migrations_path, :data_template_path, :db_configuration, :spec_name
|
|
16
15
|
|
|
17
16
|
DEFAULT_DATA_TEMPLATE_PATH = "data_migration.rb"
|
|
18
17
|
|
|
19
18
|
def initialize
|
|
19
|
+
@data_migrations_table_name = "data_migrations"
|
|
20
20
|
@data_migrations_path = "db/data/"
|
|
21
21
|
@data_template_path = DEFAULT_DATA_TEMPLATE_PATH
|
|
22
22
|
@db_configuration = nil
|
|
@@ -12,7 +12,7 @@ module DataMigrate
|
|
|
12
12
|
|
|
13
13
|
class << self
|
|
14
14
|
def migrations_paths
|
|
15
|
-
|
|
15
|
+
Array.wrap(DataMigrate.config.data_migrations_path)
|
|
16
16
|
end
|
|
17
17
|
|
|
18
18
|
def create_data_schema_table
|
|
@@ -48,7 +48,6 @@ module DataMigrate
|
|
|
48
48
|
|
|
49
49
|
# TODO: this was added to be backward compatible, need to re-evaluate
|
|
50
50
|
def migrations(_migrations_paths)
|
|
51
|
-
#DataMigrate::MigrationContext.new(migrations_paths).migrations
|
|
52
51
|
DataMigrate::MigrationContext.new(_migrations_paths).migrations
|
|
53
52
|
end
|
|
54
53
|
|
|
@@ -29,13 +29,13 @@ module DataMigrate
|
|
|
29
29
|
end
|
|
30
30
|
|
|
31
31
|
def versions
|
|
32
|
-
@versions ||=
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
|
|
@@ -4,7 +4,7 @@ module DataMigrate
|
|
|
4
4
|
# So we only load the appropriate methods depending on Rails version.
|
|
5
5
|
if DataMigrate::RailsHelper.rails_version_equal_to_or_higher_than_7_1
|
|
6
6
|
def table_name
|
|
7
|
-
ActiveRecord::Base.table_name_prefix +
|
|
7
|
+
ActiveRecord::Base.table_name_prefix + DataMigrate.config.data_migrations_table_name + ActiveRecord::Base.table_name_suffix
|
|
8
8
|
end
|
|
9
9
|
|
|
10
10
|
def primary_key
|
|
@@ -13,7 +13,7 @@ module DataMigrate
|
|
|
13
13
|
else
|
|
14
14
|
class << self
|
|
15
15
|
def table_name
|
|
16
|
-
ActiveRecord::Base.table_name_prefix +
|
|
16
|
+
ActiveRecord::Base.table_name_prefix + DataMigrate.config.data_migrations_table_name + ActiveRecord::Base.table_name_suffix
|
|
17
17
|
end
|
|
18
18
|
|
|
19
19
|
def primary_key
|
|
@@ -9,22 +9,29 @@ module DataMigrate
|
|
|
9
9
|
extend ActiveRecord::Tasks::DatabaseTasks
|
|
10
10
|
extend self
|
|
11
11
|
|
|
12
|
+
if respond_to?(:register_task)
|
|
13
|
+
register_task(/mysql/, "ActiveRecord::Tasks::MySQLDatabaseTasks")
|
|
14
|
+
register_task(/trilogy/, "ActiveRecord::Tasks::MySQLDatabaseTasks")
|
|
15
|
+
register_task(/postgresql/, "ActiveRecord::Tasks::PostgreSQLDatabaseTasks")
|
|
16
|
+
register_task(/sqlite/, "ActiveRecord::Tasks::SQLiteDatabaseTasks")
|
|
17
|
+
end
|
|
18
|
+
|
|
12
19
|
# These method are only introduced in Rails 7.1
|
|
13
|
-
unless respond_to?(:
|
|
14
|
-
def
|
|
20
|
+
unless respond_to?(:with_temporary_pool_for_each)
|
|
21
|
+
def with_temporary_pool_for_each(env: ActiveRecord::Tasks::DatabaseTasks.env, name: nil, &block) # :nodoc:
|
|
15
22
|
if name
|
|
16
23
|
db_config = ActiveRecord::Base.configurations.configs_for(env_name: env, name: name)
|
|
17
|
-
|
|
24
|
+
with_temporary_pool(db_config, &block)
|
|
18
25
|
else
|
|
19
26
|
ActiveRecord::Base.configurations.configs_for(env_name: env, name: name).each do |db_config|
|
|
20
|
-
|
|
27
|
+
with_temporary_pool(db_config, &block)
|
|
21
28
|
end
|
|
22
29
|
end
|
|
23
30
|
end
|
|
24
31
|
|
|
25
|
-
def with_temporary_connection(db_config) # :nodoc:
|
|
32
|
+
def with_temporary_connection(db_config, &block) # :nodoc:
|
|
26
33
|
with_temporary_pool(db_config) do |pool|
|
|
27
|
-
|
|
34
|
+
pool.with_connection(&block)
|
|
28
35
|
end
|
|
29
36
|
end
|
|
30
37
|
|
|
@@ -49,8 +56,8 @@ module DataMigrate
|
|
|
49
56
|
def db_configs_with_versions
|
|
50
57
|
db_configs_with_versions = Hash.new { |h, k| h[k] = [] }
|
|
51
58
|
|
|
52
|
-
|
|
53
|
-
db_config =
|
|
59
|
+
with_temporary_pool_for_each do |pool|
|
|
60
|
+
db_config = pool.db_config
|
|
54
61
|
if db_config.primary?
|
|
55
62
|
versions_to_run = DataMigrate::DatabaseTasks.pending_data_migrations.map { |m| m[:version] }
|
|
56
63
|
target_version = ActiveRecord::Tasks::DatabaseTasks.target_version
|
|
@@ -99,7 +106,7 @@ module DataMigrate
|
|
|
99
106
|
end
|
|
100
107
|
|
|
101
108
|
def sort_migrations(*migrations)
|
|
102
|
-
migrations.flatten.sort { |a, b|
|
|
109
|
+
migrations.flatten.sort { |a, b| sort_string(a) <=> sort_string(b) }
|
|
103
110
|
end
|
|
104
111
|
|
|
105
112
|
def sort_string migration
|
|
@@ -159,7 +166,7 @@ module DataMigrate
|
|
|
159
166
|
data_migrator = DataMigrate::RailsHelper.data_migrator(:up, data_migrations)
|
|
160
167
|
sort_migrations(
|
|
161
168
|
data_migrator.pending_migrations.map { |m| { version: m.version, name: m.name, kind: :data } }
|
|
162
|
-
|
|
169
|
+
)
|
|
163
170
|
end
|
|
164
171
|
|
|
165
172
|
def pending_schema_migrations
|
|
@@ -173,5 +180,111 @@ module DataMigrate
|
|
|
173
180
|
|
|
174
181
|
sort&.downcase == "asc" ? sort_migrations(migrations) : sort_migrations(migrations).reverse
|
|
175
182
|
end
|
|
183
|
+
|
|
184
|
+
def self.migrate_with_data
|
|
185
|
+
DataMigrate::DataMigrator.create_data_schema_table
|
|
186
|
+
|
|
187
|
+
ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
|
|
188
|
+
|
|
189
|
+
# 7.2 removes the param for db_configs_with_versions in https://github.com/rails/rails/commit/9572fcb4a0bd5396436689a6a42613886871cd81
|
|
190
|
+
# 7.1 stable backported the change in https://github.com/rails/rails/commit/c53ec4b60980036b43528829d4b0b7457f759224
|
|
191
|
+
schema_mapped_versions = if Gem::Dependency.new("railties", ">= 7.1.4").match?("railties", Gem.loaded_specs["railties"].version, true)
|
|
192
|
+
ActiveRecord::Tasks::DatabaseTasks.db_configs_with_versions
|
|
193
|
+
else
|
|
194
|
+
db_configs = ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env)
|
|
195
|
+
|
|
196
|
+
ActiveRecord::Tasks::DatabaseTasks.db_configs_with_versions(db_configs)
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
data_mapped_versions = DataMigrate::DatabaseTasks.db_configs_with_versions
|
|
200
|
+
|
|
201
|
+
mapped_versions = schema_mapped_versions.merge(data_mapped_versions) do |_key, schema_db_configs, data_db_configs|
|
|
202
|
+
schema_db_configs + data_db_configs
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
mapped_versions.sort.each do |version, db_configs|
|
|
206
|
+
db_configs.each do |db_config|
|
|
207
|
+
if is_data_migration = db_config.is_a?(DataMigrate::DatabaseConfigurationWrapper)
|
|
208
|
+
db_config = db_config.db_config
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
DataMigrate::DatabaseTasks.with_temporary_connection(db_config) do
|
|
212
|
+
if is_data_migration
|
|
213
|
+
DataMigrate::DataMigrator.run(:up, DataMigrate::DatabaseTasks.data_migrations_path, version)
|
|
214
|
+
else
|
|
215
|
+
ActiveRecord::Tasks::DatabaseTasks.migrate(version)
|
|
216
|
+
end
|
|
217
|
+
end
|
|
218
|
+
end
|
|
219
|
+
end
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
def self.prepare_all_with_data
|
|
223
|
+
seed = false
|
|
224
|
+
|
|
225
|
+
each_current_configuration(env) do |db_config|
|
|
226
|
+
next unless primary?(db_config)
|
|
227
|
+
|
|
228
|
+
with_temporary_pool(db_config) do |pool|
|
|
229
|
+
connection = pool.respond_to?(:lease_connection) ? pool.lease_connection : pool.connection
|
|
230
|
+
unless database_exists?(connection)
|
|
231
|
+
create(db_config)
|
|
232
|
+
if File.exist?(schema_dump_path(db_config))
|
|
233
|
+
load_schema(db_config, schema_format, nil)
|
|
234
|
+
load_schema_current(
|
|
235
|
+
:ruby,
|
|
236
|
+
ENV["DATA_SCHEMA"]
|
|
237
|
+
)
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
seed = true
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
migrate_with_data
|
|
244
|
+
if dump_schema_after_migration?
|
|
245
|
+
dump_schema(db_config)
|
|
246
|
+
DataMigrate::Tasks::DataMigrateTasks.dump
|
|
247
|
+
end
|
|
248
|
+
end
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
load_seed if seed
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
private
|
|
255
|
+
|
|
256
|
+
def database_exists?(connection)
|
|
257
|
+
if connection.respond_to?(:database_exists?) # Rails 7.1+
|
|
258
|
+
connection.database_exists?
|
|
259
|
+
else
|
|
260
|
+
connection.table_exists?(ActiveRecord::SchemaMigration.table_name)
|
|
261
|
+
end
|
|
262
|
+
rescue ActiveRecord::NoDatabaseError
|
|
263
|
+
false
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
def primary?(db_config)
|
|
267
|
+
if db_config.respond_to?(:primary?) # Rails 7.0+
|
|
268
|
+
db_config.primary?
|
|
269
|
+
else
|
|
270
|
+
db_config.name == "primary"
|
|
271
|
+
end
|
|
272
|
+
end
|
|
273
|
+
|
|
274
|
+
def dump_schema_after_migration?
|
|
275
|
+
if ActiveRecord.respond_to?(:dump_schema_after_migration)
|
|
276
|
+
ActiveRecord.dump_schema_after_migration
|
|
277
|
+
else
|
|
278
|
+
ActiveRecord::Base.dump_schema_after_migration
|
|
279
|
+
end
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
def schema_format
|
|
283
|
+
if ActiveRecord.respond_to?(:schema_format)
|
|
284
|
+
ActiveRecord.schema_format
|
|
285
|
+
else
|
|
286
|
+
ActiveRecord::Base.schema_format
|
|
287
|
+
end
|
|
288
|
+
end
|
|
176
289
|
end
|
|
177
290
|
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
|
|
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
|
|
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
|
|
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
|
|
@@ -15,7 +15,7 @@ module DataMigrate
|
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
def dump(stream)
|
|
18
|
-
define_params = @version ? "version: #{
|
|
18
|
+
define_params = @version ? "version: #{formatted_version}" : ""
|
|
19
19
|
|
|
20
20
|
stream.puts "DataMigrate::Data.define(#{define_params})"
|
|
21
21
|
|
|
@@ -34,5 +34,12 @@ module DataMigrate
|
|
|
34
34
|
0
|
|
35
35
|
end
|
|
36
36
|
end
|
|
37
|
+
|
|
38
|
+
# turns 20170404131909 into "2017_04_04_131909"
|
|
39
|
+
def formatted_version
|
|
40
|
+
stringified = @version.to_s
|
|
41
|
+
return stringified unless stringified.length == 14
|
|
42
|
+
stringified.insert(4, "_").insert(7, "_").insert(10, "_")
|
|
43
|
+
end
|
|
37
44
|
end
|
|
38
45
|
end
|
|
@@ -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
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
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
|
|
data/lib/data_migrate/version.rb
CHANGED
|
@@ -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
|
data/tasks/databases.rake
CHANGED
|
@@ -6,34 +6,7 @@ namespace :db do
|
|
|
6
6
|
namespace :migrate do
|
|
7
7
|
desc "Migrate the database data and schema (options: VERSION=x, VERBOSE=false)."
|
|
8
8
|
task :with_data => :load_config do
|
|
9
|
-
DataMigrate::
|
|
10
|
-
ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
|
|
11
|
-
|
|
12
|
-
db_configs = ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env)
|
|
13
|
-
|
|
14
|
-
schema_mapped_versions = ActiveRecord::Tasks::DatabaseTasks.db_configs_with_versions(db_configs)
|
|
15
|
-
data_mapped_versions = DataMigrate::DatabaseTasks.db_configs_with_versions
|
|
16
|
-
|
|
17
|
-
mapped_versions = schema_mapped_versions.merge(data_mapped_versions) do |_key, schema_db_configs, data_db_configs|
|
|
18
|
-
schema_db_configs + data_db_configs
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
mapped_versions.sort.each do |version, db_configs|
|
|
22
|
-
db_configs.each do |db_config|
|
|
23
|
-
if is_data_migration = db_config.is_a?(DataMigrate::DatabaseConfigurationWrapper)
|
|
24
|
-
db_config = db_config.db_config
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
DataMigrate::DatabaseTasks.with_temporary_connection(db_config) do
|
|
28
|
-
if is_data_migration
|
|
29
|
-
DataMigrate::DataMigrator.run(:up, DataMigrate::DatabaseTasks.data_migrations_path, version)
|
|
30
|
-
else
|
|
31
|
-
ActiveRecord::Tasks::DatabaseTasks.migrate(version)
|
|
32
|
-
end
|
|
33
|
-
end
|
|
34
|
-
end
|
|
35
|
-
end
|
|
36
|
-
|
|
9
|
+
DataMigrate::DatabaseTasks.migrate_with_data
|
|
37
10
|
Rake::Task["db:_dump"].invoke
|
|
38
11
|
Rake::Task["data:dump"].invoke
|
|
39
12
|
end
|
|
@@ -173,6 +146,13 @@ namespace :db do
|
|
|
173
146
|
end
|
|
174
147
|
end
|
|
175
148
|
end
|
|
149
|
+
|
|
150
|
+
namespace :prepare do
|
|
151
|
+
desc "Runs setup if database does not exist, or runs data and schema migrations if it does"
|
|
152
|
+
task with_data: :environment do
|
|
153
|
+
DataMigrate::DatabaseTasks.prepare_all_with_data
|
|
154
|
+
end
|
|
155
|
+
end
|
|
176
156
|
end
|
|
177
157
|
|
|
178
158
|
namespace :data do
|
metadata
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: data_migrate
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 11.3.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Andrew J Vargo
|
|
8
8
|
- Ilya Katz
|
|
9
9
|
- Deborah Enomah
|
|
10
|
-
autorequire:
|
|
10
|
+
autorequire:
|
|
11
11
|
bindir: bin
|
|
12
12
|
cert_chain: []
|
|
13
|
-
date:
|
|
13
|
+
date: 2025-10-27 00:00:00.000000000 Z
|
|
14
14
|
dependencies:
|
|
15
15
|
- !ruby/object:Gem::Dependency
|
|
16
16
|
name: activerecord
|
|
@@ -189,29 +189,9 @@ executables: []
|
|
|
189
189
|
extensions: []
|
|
190
190
|
extra_rdoc_files: []
|
|
191
191
|
files:
|
|
192
|
-
- ".github/workflows/build.yml"
|
|
193
|
-
- ".github/workflows/gempush.yml"
|
|
194
|
-
- ".gitignore"
|
|
195
|
-
- ".hound.yml"
|
|
196
|
-
- ".overcommit.yml"
|
|
197
|
-
- ".rbenv-gemsets"
|
|
198
|
-
- ".rspec"
|
|
199
|
-
- ".rubocop.yml"
|
|
200
|
-
- ".ruby-style.yml"
|
|
201
|
-
- Appraisals
|
|
202
192
|
- Changelog.md
|
|
203
|
-
- Gemfile
|
|
204
|
-
- Gemfile.lock
|
|
205
193
|
- LICENSE
|
|
206
194
|
- README.md
|
|
207
|
-
- Rakefile
|
|
208
|
-
- data_migrate.gemspec
|
|
209
|
-
- gemfiles/rails_6.1.gemfile
|
|
210
|
-
- gemfiles/rails_6.1.gemfile.lock
|
|
211
|
-
- gemfiles/rails_7.0.gemfile
|
|
212
|
-
- gemfiles/rails_7.0.gemfile.lock
|
|
213
|
-
- gemfiles/rails_7.1.gemfile
|
|
214
|
-
- gemfiles/rails_7.1.gemfile.lock
|
|
215
195
|
- lib/capistrano/data_migrate.rb
|
|
216
196
|
- lib/capistrano/data_migrate/migrate.rb
|
|
217
197
|
- lib/data_migrate.rb
|
|
@@ -228,42 +208,17 @@ files:
|
|
|
228
208
|
- lib/data_migrate/schema_migration.rb
|
|
229
209
|
- lib/data_migrate/status_service.rb
|
|
230
210
|
- lib/data_migrate/tasks/data_migrate_tasks.rb
|
|
231
|
-
- lib/data_migrate/test.rb
|
|
232
211
|
- lib/data_migrate/version.rb
|
|
233
212
|
- lib/generators/data_migrate.rb
|
|
234
213
|
- lib/generators/data_migration/data_migration_generator.rb
|
|
235
214
|
- lib/generators/data_migration/templates/data_migration.rb
|
|
236
215
|
- lib/generators/data_migration/templates/migration.rb
|
|
237
|
-
- screenshot.png
|
|
238
|
-
- spec/data_migrate/config_spec.rb
|
|
239
|
-
- spec/data_migrate/data_migrator_spec.rb
|
|
240
|
-
- spec/data_migrate/data_schema_migration_spec.rb
|
|
241
|
-
- spec/data_migrate/data_spec.rb
|
|
242
|
-
- spec/data_migrate/database_tasks_spec.rb
|
|
243
|
-
- spec/data_migrate/migration.rb
|
|
244
|
-
- spec/data_migrate/migration_context_spec.rb
|
|
245
|
-
- spec/data_migrate/schema_dumper_spec.rb
|
|
246
|
-
- spec/data_migrate/schema_migration_spec.rb
|
|
247
|
-
- spec/data_migrate/status_service_spec.rb
|
|
248
|
-
- spec/data_migrate/tasks/data_migrate_tasks_spec.rb
|
|
249
|
-
- spec/db/data/20091231235959_some_name.rb
|
|
250
|
-
- spec/db/data/20171231235959_super_update.rb
|
|
251
|
-
- spec/db/data/20181128000207_excluded_file.rb.other_ext
|
|
252
|
-
- spec/db/data/partial_schema/data_schema.rb
|
|
253
|
-
- spec/db/data/partial_schema/test_data_schema.rb
|
|
254
|
-
- spec/db/data/schema/data_schema.rb
|
|
255
|
-
- spec/db/data/schema/test_data_schema.rb
|
|
256
|
-
- spec/db/migrate/20131111111111_late_migration.rb
|
|
257
|
-
- spec/db/migrate/20202020202011_db_migration.rb
|
|
258
|
-
- spec/generators/data_migration/data_migration_generator_spec.rb
|
|
259
|
-
- spec/spec_helper.rb
|
|
260
|
-
- tasks/.gitkeep
|
|
261
216
|
- tasks/databases.rake
|
|
262
217
|
homepage: https://github.com/ilyakatz/data-migrate
|
|
263
218
|
licenses:
|
|
264
219
|
- MIT
|
|
265
220
|
metadata: {}
|
|
266
|
-
post_install_message:
|
|
221
|
+
post_install_message:
|
|
267
222
|
rdoc_options: []
|
|
268
223
|
require_paths:
|
|
269
224
|
- lib
|
|
@@ -279,7 +234,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
279
234
|
version: '0'
|
|
280
235
|
requirements: []
|
|
281
236
|
rubygems_version: 3.4.19
|
|
282
|
-
signing_key:
|
|
237
|
+
signing_key:
|
|
283
238
|
specification_version: 4
|
|
284
239
|
summary: Rake tasks to migrate data alongside schema changes.
|
|
285
240
|
test_files: []
|