data_migrate 8.1.1 → 11.3.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.
- checksums.yaml +4 -4
- data/Changelog.md +100 -0
- data/README.md +42 -8
- data/lib/data_migrate/config.rb +11 -1
- data/lib/data_migrate/{data_migrator_five.rb → data_migrator.rb} +15 -35
- data/lib/data_migrate/data_schema.rb +8 -8
- data/lib/data_migrate/data_schema_migration.rb +21 -5
- data/lib/data_migrate/database_configurations_wrapper.rb +11 -0
- data/lib/data_migrate/database_tasks.rb +233 -69
- data/lib/data_migrate/migration_context.rb +11 -8
- data/lib/data_migrate/rails_helper.rb +91 -0
- data/lib/data_migrate/schema_dumper.rb +1 -1
- data/lib/data_migrate/{schema_migration_six.rb → schema_migration.rb} +7 -4
- data/lib/data_migrate/{status_service_five.rb → status_service.rb} +14 -8
- data/lib/data_migrate/tasks/data_migrate_tasks.rb +24 -43
- data/lib/data_migrate/version.rb +1 -1
- data/lib/data_migrate.rb +8 -9
- data/lib/generators/data_migrate.rb +15 -2
- data/lib/generators/data_migration/data_migration_generator.rb +7 -2
- data/tasks/databases.rake +36 -121
- metadata +19 -77
- data/.github/workflows/gempush.yml +0 -29
- data/.gitignore +0 -11
- 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/.ruby-version +0 -1
- data/.travis.yml +0 -14
- data/Appraisals +0 -15
- data/Gemfile +0 -4
- data/Gemfile.rails5.2 +0 -10
- data/Gemfile.rails6.1 +0 -11
- data/Rakefile +0 -2
- data/data_migrate.gemspec +0 -50
- data/gemfiles/rails_5.2.gemfile +0 -8
- data/gemfiles/rails_6.0.gemfile +0 -8
- data/gemfiles/rails_6.1.gemfile +0 -8
- data/gemfiles/rails_7.0.gemfile +0 -8
- data/lib/data_migrate/legacy_migrator.rb +0 -22
- data/lib/data_migrate/schema_migration_five.rb +0 -31
- data/screenshot.png +0 -0
- data/spec/data_migrate/config_spec.rb +0 -27
- data/spec/data_migrate/data_migrator_spec.rb +0 -101
- data/spec/data_migrate/data_schema_migration_spec.rb +0 -16
- data/spec/data_migrate/data_spec.rb +0 -85
- data/spec/data_migrate/database_tasks_spec.rb +0 -138
- data/spec/data_migrate/legacy_migrator_spec.rb +0 -50
- data/spec/data_migrate/migration.rb +0 -19
- data/spec/data_migrate/migration_context_spec.rb +0 -127
- data/spec/data_migrate/schema_dumper_spec.rb +0 -46
- data/spec/data_migrate/schema_migration_spec.rb +0 -99
- data/spec/data_migrate/status_service_spec.rb +0 -102
- data/spec/data_migrate/tasks/data_migrate_tasks_spec.rb +0 -164
- data/spec/db/6.0/20091231235959_some_name.rb +0 -9
- data/spec/db/6.0/20171231235959_super_update.rb +0 -9
- 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/data-6.0/20091231235959_some_name.rb +0 -9
- data/spec/db/data-6.0/20171231235959_super_update.rb +0 -9
- data/spec/db/data-6.0/20181128000207_excluded_file.rb.other_ext +0 -1
- data/spec/db/migrate/5.2/20131111111111_late_migration.rb +0 -9
- data/spec/db/migrate/5.2/20202020202011_db_migration.rb +0 -9
- data/spec/db/migrate/6.0/20131111111111_late_migration.rb +0 -9
- data/spec/db/migrate/6.0/20202020202011_db_migration.rb +0 -9
- data/spec/generators/data_migration/data_migration_generator_spec.rb +0 -60
- data/spec/spec_helper.rb +0 -34
- data/tasks/.gitkeep +0 -0
@@ -5,57 +5,150 @@ require "data_migrate/config"
|
|
5
5
|
module DataMigrate
|
6
6
|
##
|
7
7
|
# This class extends DatabaseTasks to add a schema_file method.
|
8
|
-
|
8
|
+
module DatabaseTasks
|
9
9
|
extend ActiveRecord::Tasks::DatabaseTasks
|
10
|
+
extend self
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
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
|
15
18
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
+
# These method are only introduced in Rails 7.1
|
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:
|
22
|
+
if name
|
23
|
+
db_config = ActiveRecord::Base.configurations.configs_for(env_name: env, name: name)
|
24
|
+
with_temporary_pool(db_config, &block)
|
19
25
|
else
|
20
|
-
|
26
|
+
ActiveRecord::Base.configurations.configs_for(env_name: env, name: name).each do |db_config|
|
27
|
+
with_temporary_pool(db_config, &block)
|
28
|
+
end
|
21
29
|
end
|
22
|
-
|
23
|
-
ENV["DATA_SCHEMA"] || File.join(schema_location, filename)
|
24
30
|
end
|
25
31
|
|
26
|
-
def
|
27
|
-
|
28
|
-
|
29
|
-
filename = if db_config.primary?
|
30
|
-
schema_file_type(format)
|
31
|
-
else
|
32
|
-
[db_config.name, schema_file_type(format)].join("_")
|
32
|
+
def with_temporary_connection(db_config, &block) # :nodoc:
|
33
|
+
with_temporary_pool(db_config) do |pool|
|
34
|
+
pool.with_connection(&block)
|
33
35
|
end
|
36
|
+
end
|
34
37
|
|
35
|
-
|
38
|
+
def migration_class # :nodoc:
|
39
|
+
ActiveRecord::Base
|
40
|
+
end
|
36
41
|
|
37
|
-
|
42
|
+
def migration_connection # :nodoc:
|
43
|
+
migration_class.connection
|
38
44
|
end
|
39
45
|
|
40
|
-
def
|
41
|
-
|
46
|
+
private def with_temporary_pool(db_config)
|
47
|
+
original_db_config = migration_class.connection_db_config
|
48
|
+
pool = migration_class.connection_handler.establish_connection(db_config)
|
49
|
+
|
50
|
+
yield pool
|
51
|
+
ensure
|
52
|
+
migration_class.connection_handler.establish_connection(original_db_config)
|
42
53
|
end
|
54
|
+
end
|
43
55
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
56
|
+
def db_configs_with_versions
|
57
|
+
db_configs_with_versions = Hash.new { |h, k| h[k] = [] }
|
58
|
+
|
59
|
+
with_temporary_pool_for_each do |pool|
|
60
|
+
db_config = pool.db_config
|
61
|
+
if db_config.primary?
|
62
|
+
versions_to_run = DataMigrate::DatabaseTasks.pending_data_migrations.map { |m| m[:version] }
|
63
|
+
target_version = ActiveRecord::Tasks::DatabaseTasks.target_version
|
64
|
+
|
65
|
+
versions_to_run.each do |version|
|
66
|
+
next if target_version && target_version != version
|
67
|
+
db_configs_with_versions[version] << DatabaseConfigurationWrapper.new(db_config)
|
68
|
+
end
|
48
69
|
end
|
49
70
|
end
|
71
|
+
|
72
|
+
db_configs_with_versions
|
50
73
|
end
|
51
74
|
|
52
|
-
|
53
|
-
def self.schema_file(_format = nil)
|
75
|
+
def schema_file(_format = nil)
|
54
76
|
File.join(db_dir, "data_schema.rb")
|
55
77
|
end
|
56
78
|
|
57
|
-
def
|
58
|
-
|
79
|
+
def schema_file_type(_format = nil)
|
80
|
+
"data_schema.rb"
|
81
|
+
end
|
82
|
+
|
83
|
+
# This method is removed in Rails 7.0
|
84
|
+
def dump_filename(spec_name, format = ActiveRecord::Base.schema_format)
|
85
|
+
filename = if spec_name == "primary"
|
86
|
+
schema_file_type(format)
|
87
|
+
else
|
88
|
+
"#{spec_name}_#{schema_file_type(format)}"
|
89
|
+
end
|
90
|
+
|
91
|
+
ENV["DATA_SCHEMA"] || File.join(db_dir, filename)
|
92
|
+
end
|
93
|
+
|
94
|
+
def check_schema_file(filename)
|
95
|
+
unless File.exist?(filename)
|
96
|
+
message = +%{#{filename} doesn't exist yet. Run `rake data:migrate` to create it, then try again.}
|
97
|
+
Kernel.abort message
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def pending_migrations
|
102
|
+
sort_migrations(
|
103
|
+
pending_schema_migrations,
|
104
|
+
pending_data_migrations
|
105
|
+
)
|
106
|
+
end
|
107
|
+
|
108
|
+
def sort_migrations(*migrations)
|
109
|
+
migrations.flatten.sort { |a, b| sort_string(a) <=> sort_string(b) }
|
110
|
+
end
|
111
|
+
|
112
|
+
def sort_string migration
|
113
|
+
"#{migration[:version]}_#{migration[:kind] == :data ? 1 : 0}"
|
114
|
+
end
|
115
|
+
|
116
|
+
def data_migrations_path
|
117
|
+
::DataMigrate.config.data_migrations_path
|
118
|
+
end
|
119
|
+
|
120
|
+
def run_migration(migration, direction)
|
121
|
+
if migration[:kind] == :data
|
122
|
+
::ActiveRecord::Migration.write("== %s %s" % ['Data', "=" * 71])
|
123
|
+
::DataMigrate::DataMigrator.run(direction, data_migrations_path, migration[:version])
|
124
|
+
else
|
125
|
+
::ActiveRecord::Migration.write("== %s %s" % ['Schema', "=" * 69])
|
126
|
+
::DataMigrate::SchemaMigration.run(
|
127
|
+
direction,
|
128
|
+
::DataMigrate::SchemaMigration.migrations_paths,
|
129
|
+
migration[:version]
|
130
|
+
)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
def schema_dump_path(db_config, format = ActiveRecord.schema_format)
|
135
|
+
return ENV["DATA_SCHEMA"] if ENV["DATA_SCHEMA"]
|
136
|
+
|
137
|
+
# We only require a schema.rb file for the primary database
|
138
|
+
return unless db_config.primary?
|
139
|
+
|
140
|
+
File.join(File.dirname(ActiveRecord::Tasks::DatabaseTasks.schema_dump_path(db_config, format)), schema_file_type)
|
141
|
+
end
|
142
|
+
|
143
|
+
# Override this method from `ActiveRecord::Tasks::DatabaseTasks`
|
144
|
+
# to ensure that the sha saved in ar_internal_metadata table
|
145
|
+
# is from the original schema.rb file
|
146
|
+
def schema_sha1(file)
|
147
|
+
ActiveRecord::Tasks::DatabaseTasks.schema_dump_path(ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env, name: "primary"))
|
148
|
+
end
|
149
|
+
|
150
|
+
def forward(step = 1)
|
151
|
+
DataMigrate::DataMigrator.create_data_schema_table
|
59
152
|
migrations = pending_migrations.reverse.pop(step).reverse
|
60
153
|
migrations.each do | pending_migration |
|
61
154
|
if pending_migration[:kind] == :data
|
@@ -63,64 +156,135 @@ module DataMigrate
|
|
63
156
|
DataMigrate::DataMigrator.run(:up, data_migrations_path, pending_migration[:version])
|
64
157
|
elsif pending_migration[:kind] == :schema
|
65
158
|
ActiveRecord::Migration.write("== %s %s" % ["Schema", "=" * 69])
|
66
|
-
DataMigrate::SchemaMigration.run(:up,
|
159
|
+
DataMigrate::SchemaMigration.run(:up, DataMigrate::SchemaMigration.migrations_paths, pending_migration[:version])
|
67
160
|
end
|
68
161
|
end
|
69
162
|
end
|
70
163
|
|
71
|
-
def
|
72
|
-
DataMigrate.
|
164
|
+
def pending_data_migrations
|
165
|
+
data_migrations = DataMigrate::DataMigrator.migrations(data_migrations_path)
|
166
|
+
data_migrator = DataMigrate::RailsHelper.data_migrator(:up, data_migrations)
|
167
|
+
sort_migrations(
|
168
|
+
data_migrator.pending_migrations.map { |m| { version: m.version, name: m.name, kind: :data } }
|
169
|
+
)
|
73
170
|
end
|
74
171
|
|
75
|
-
def
|
76
|
-
|
172
|
+
def pending_schema_migrations
|
173
|
+
::DataMigrate::SchemaMigration.pending_schema_migrations
|
77
174
|
end
|
78
175
|
|
79
|
-
def
|
80
|
-
|
81
|
-
|
176
|
+
def past_migrations(sort = nil)
|
177
|
+
data_versions = DataMigrate::RailsHelper.data_schema_migration.table_exists? ? DataMigrate::RailsHelper.data_schema_migration.normalized_versions : []
|
178
|
+
schema_versions = DataMigrate::RailsHelper.schema_migration.normalized_versions
|
179
|
+
migrations = data_versions.map { |v| { version: v.to_i, kind: :data } } + schema_versions.map { |v| { version: v.to_i, kind: :schema } }
|
82
180
|
|
83
|
-
|
84
|
-
data_migrations = DataMigrate::DataMigrator.migrations(data_migrations_path)
|
85
|
-
sort_migrations(DataMigrate::DataMigrator.new(:up, data_migrations ).
|
86
|
-
pending_migrations.map {|m| { version: m.version, name: m.name, kind: :data }})
|
181
|
+
sort&.downcase == "asc" ? sort_migrations(migrations) : sort_migrations(migrations).reverse
|
87
182
|
end
|
88
183
|
|
89
|
-
def self.
|
90
|
-
|
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
|
91
220
|
end
|
92
221
|
|
93
|
-
def self.
|
94
|
-
|
95
|
-
|
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
|
96
252
|
end
|
97
253
|
|
98
|
-
|
99
|
-
|
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
|
100
264
|
end
|
101
265
|
|
102
|
-
def
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
end
|
110
|
-
db_list_schema = ActiveRecord::SchemaMigration.normalized_versions.sort.sort
|
111
|
-
migrations = db_list_data.map do |d|
|
112
|
-
{
|
113
|
-
version: d.to_i, kind: :data
|
114
|
-
}
|
115
|
-
end +
|
116
|
-
db_list_schema.map do |d|
|
117
|
-
{
|
118
|
-
version: d.to_i, kind: :schema
|
119
|
-
}
|
120
|
-
end
|
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
|
121
273
|
|
122
|
-
|
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
|
123
280
|
end
|
124
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
|
125
289
|
end
|
126
290
|
end
|
@@ -11,7 +11,8 @@ module DataMigrate
|
|
11
11
|
migrations
|
12
12
|
end
|
13
13
|
|
14
|
-
|
14
|
+
data_migrator = DataMigrate::RailsHelper.data_migrator(:up, selected_migrations, DataMigrate::RailsHelper.schema_migration, DataMigrate::RailsHelper.internal_metadata, target_version)
|
15
|
+
data_migrator.migrate
|
15
16
|
end
|
16
17
|
|
17
18
|
def down(target_version = nil)
|
@@ -22,11 +23,13 @@ module DataMigrate
|
|
22
23
|
migrations
|
23
24
|
end
|
24
25
|
|
25
|
-
|
26
|
+
data_migrator = DataMigrate::RailsHelper.data_migrator(:down, selected_migrations, DataMigrate::RailsHelper.schema_migration, DataMigrate::RailsHelper.internal_metadata, target_version)
|
27
|
+
data_migrator.migrate
|
26
28
|
end
|
27
29
|
|
28
30
|
def run(direction, target_version)
|
29
|
-
|
31
|
+
data_migrator = DataMigrate::RailsHelper.data_migrator(direction, migrations, DataMigrate::RailsHelper.schema_migration, DataMigrate::RailsHelper.internal_metadata, target_version)
|
32
|
+
data_migrator.run
|
30
33
|
end
|
31
34
|
|
32
35
|
def current_version
|
@@ -40,12 +43,12 @@ module DataMigrate
|
|
40
43
|
end
|
41
44
|
|
42
45
|
def migrations_status
|
43
|
-
db_list =
|
46
|
+
db_list = DataMigrate::RailsHelper.data_schema_migration.normalized_versions
|
44
47
|
|
45
48
|
file_list = migration_files.map do |file|
|
46
49
|
version, name, scope = parse_migration_filename(file)
|
47
50
|
raise ActiveRecord::IllegalMigrationNameError.new(file) unless version
|
48
|
-
version =
|
51
|
+
version = DataMigrate::RailsHelper.schema_migration.normalize_migration_number(version)
|
49
52
|
status = db_list.delete(version) ? "up" : "down"
|
50
53
|
[status, version, (name + scope).humanize]
|
51
54
|
end.compact
|
@@ -60,15 +63,15 @@ module DataMigrate
|
|
60
63
|
private
|
61
64
|
|
62
65
|
def get_all_versions
|
63
|
-
if DataMigrate::
|
64
|
-
|
66
|
+
if DataMigrate::RailsHelper.data_schema_migration.table_exists?
|
67
|
+
DataMigrate::RailsHelper.data_schema_migration.normalized_versions.map(&:to_i)
|
65
68
|
else
|
66
69
|
[]
|
67
70
|
end
|
68
71
|
end
|
69
72
|
|
70
73
|
def move(direction, steps)
|
71
|
-
migrator =
|
74
|
+
migrator = DataMigrate::RailsHelper.data_migrator(direction, migrations)
|
72
75
|
|
73
76
|
if current_version != 0 && !migrator.current_migration
|
74
77
|
raise ActiveRecord::UnknownMigrationVersionError.new(current_version)
|
@@ -0,0 +1,91 @@
|
|
1
|
+
module DataMigrate
|
2
|
+
class RailsHelper
|
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
|
+
|
10
|
+
def rails_version_equal_to_or_higher_than_7_1
|
11
|
+
return @equal_to_or_higher_than_7_1 if defined?(@equal_to_or_higher_than_7_1)
|
12
|
+
|
13
|
+
@equal_to_or_higher_than_7_1 = Gem::Dependency.new("railties", ">= 7.1.0.alpha").match?("railties", Gem.loaded_specs["railties"].version, true)
|
14
|
+
end
|
15
|
+
|
16
|
+
def rails_version_equal_to_or_higher_than_7_0
|
17
|
+
return @rails_version_equal_to_or_higher_than_7_0 if defined?(@rails_version_equal_to_or_higher_than_7_0)
|
18
|
+
|
19
|
+
@rails_version_equal_to_or_higher_than_7_0 = Gem::Dependency.new("railties", ">= 7.0").match?("railties", Gem.loaded_specs["railties"].version, true)
|
20
|
+
end
|
21
|
+
|
22
|
+
def internal_metadata
|
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
|
26
|
+
ActiveRecord::Base.connection.internal_metadata
|
27
|
+
else
|
28
|
+
ActiveRecord::InternalMetadata
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def schema_migration
|
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
|
36
|
+
ActiveRecord::Base.connection.schema_migration
|
37
|
+
else
|
38
|
+
ActiveRecord::SchemaMigration
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def schema_migration_versions
|
43
|
+
if rails_version_equal_to_or_higher_than_7_1
|
44
|
+
schema_migration.versions
|
45
|
+
else
|
46
|
+
schema_migration.all.pluck(:version)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def schema_create_version(version)
|
51
|
+
if rails_version_equal_to_or_higher_than_7_1
|
52
|
+
schema_migration.create_version(version)
|
53
|
+
else
|
54
|
+
schema_migration.create(version: version)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def data_schema_delete_version(version)
|
59
|
+
if rails_version_equal_to_or_higher_than_7_1
|
60
|
+
data_schema_migration.delete_version(version)
|
61
|
+
else
|
62
|
+
data_schema_migration.where(version: version.to_s).delete_all
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def data_schema_migration
|
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
|
70
|
+
DataMigrate::DataSchemaMigration.new(ActiveRecord::Tasks::DatabaseTasks.migration_connection)
|
71
|
+
else
|
72
|
+
DataMigrate::DataSchemaMigration
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def data_migrator(
|
77
|
+
direction,
|
78
|
+
migrations,
|
79
|
+
schema_migration = DataMigrate::RailsHelper.schema_migration,
|
80
|
+
internal_metadata = DataMigrate::RailsHelper.internal_metadata,
|
81
|
+
target_version = nil
|
82
|
+
)
|
83
|
+
if rails_version_equal_to_or_higher_than_7_1
|
84
|
+
DataMigrate::DataMigrator.new(direction, migrations, schema_migration, internal_metadata, target_version)
|
85
|
+
else
|
86
|
+
DataMigrate::DataMigrator.new(direction, migrations, schema_migration, target_version)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -26,7 +26,7 @@ module DataMigrate
|
|
26
26
|
|
27
27
|
def initialize(connection)
|
28
28
|
@connection = connection
|
29
|
-
all_versions =
|
29
|
+
all_versions = DataMigrate::RailsHelper.data_schema_migration.normalized_versions
|
30
30
|
|
31
31
|
@version = begin
|
32
32
|
all_versions.max
|
@@ -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
|
@@ -5,14 +7,14 @@ module DataMigrate
|
|
5
7
|
def self.pending_schema_migrations
|
6
8
|
all_migrations = DataMigrate::MigrationContext.new(migrations_paths).migrations
|
7
9
|
sort_migrations(
|
8
|
-
ActiveRecord::Migrator.new(:up, all_migrations,
|
10
|
+
ActiveRecord::Migrator.new(:up, all_migrations, DataMigrate::RailsHelper.schema_migration, DataMigrate::RailsHelper.internal_metadata).
|
9
11
|
pending_migrations.
|
10
12
|
map {|m| { version: m.version, kind: :schema }}
|
11
13
|
)
|
12
14
|
end
|
13
15
|
|
14
16
|
def self.run(direction, migration_paths, version)
|
15
|
-
ActiveRecord::MigrationContext.new(migration_paths,
|
17
|
+
ActiveRecord::MigrationContext.new(migration_paths, DataMigrate::RailsHelper.schema_migration).run(direction, version)
|
16
18
|
end
|
17
19
|
|
18
20
|
def self.sort_migrations(set1, set2 = nil)
|
@@ -22,9 +24,10 @@ module DataMigrate
|
|
22
24
|
|
23
25
|
def self.migrations_paths
|
24
26
|
spec_name = DataMigrate.config.spec_name
|
25
|
-
|
27
|
+
# The positional argument true is to include pre-release versions, such as 7.1.0.alpha
|
28
|
+
if spec_name && Gem::Dependency.new("railties", ">= 7.0").match?("railties", Gem.loaded_specs["railties"].version, true)
|
26
29
|
ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, name: spec_name).migrations_paths
|
27
|
-
elsif spec_name
|
30
|
+
elsif spec_name && Gem::Dependency.new("railties", "~> 6.1").match?("railties", Gem.loaded_specs["railties"].version)
|
28
31
|
ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, spec_name: spec_name).migrations_paths
|
29
32
|
else
|
30
33
|
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
|
@@ -22,22 +24,18 @@ module DataMigrate
|
|
22
24
|
private
|
23
25
|
|
24
26
|
def table_name
|
25
|
-
DataMigrate::
|
27
|
+
DataMigrate::RailsHelper.data_schema_migration.table_name
|
26
28
|
end
|
27
29
|
|
28
30
|
def output(stream)
|
29
|
-
unless DataMigrate::
|
31
|
+
unless DataMigrate::RailsHelper.data_schema_migration.table_exists?
|
30
32
|
stream.puts "Data migrations table does not exist yet."
|
31
33
|
return
|
32
34
|
end
|
33
35
|
|
34
36
|
# output
|
35
|
-
|
36
|
-
|
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.1").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
|