data_migrate 9.4.2 → 10.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/build.yml +3 -3
- data/.github/workflows/gempush.yml +8 -6
- data/.gitignore +2 -3
- data/.ruby-version +1 -0
- data/.travis.yml +14 -0
- data/Appraisals +5 -5
- data/Changelog.md +2 -46
- data/Gemfile +1 -0
- data/Gemfile.rails6.1 +11 -0
- data/README.md +5 -7
- data/data_migrate.gemspec +2 -2
- data/gemfiles/{rails_7.1.gemfile → rails_6.0.gemfile} +2 -1
- data/gemfiles/rails_6.1.gemfile +1 -0
- data/gemfiles/rails_7.0.gemfile +2 -1
- data/lib/data_migrate/data_migrator.rb +23 -15
- data/lib/data_migrate/data_schema.rb +2 -2
- data/lib/data_migrate/data_schema_migration.rb +7 -24
- data/lib/data_migrate/database_tasks.rb +48 -194
- data/lib/data_migrate/legacy_migrator.rb +22 -0
- data/lib/data_migrate/migration_context.rb +8 -11
- data/lib/data_migrate/schema_dumper.rb +1 -1
- data/lib/data_migrate/schema_migration.rb +4 -5
- data/lib/data_migrate/status_service.rb +4 -4
- data/lib/data_migrate/tasks/data_migrate_tasks.rb +15 -14
- data/lib/data_migrate/version.rb +1 -1
- data/lib/data_migrate.rb +1 -2
- data/spec/data_migrate/data_migrator_spec.rb +14 -17
- data/spec/data_migrate/data_schema_migration_spec.rb +8 -25
- data/spec/data_migrate/data_spec.rb +1 -1
- data/spec/data_migrate/database_tasks_spec.rb +19 -34
- data/spec/data_migrate/legacy_migrator_spec.rb +38 -0
- data/spec/data_migrate/migration_context_spec.rb +8 -15
- data/spec/data_migrate/schema_dumper_spec.rb +3 -6
- data/spec/data_migrate/schema_migration_spec.rb +6 -13
- data/spec/data_migrate/status_service_spec.rb +4 -7
- data/spec/data_migrate/tasks/data_migrate_tasks_spec.rb +14 -13
- data/spec/db/data/20091231235959_some_name.rb +1 -1
- data/spec/db/data/20171231235959_super_update.rb +1 -1
- data/spec/db/migrate/20131111111111_late_migration.rb +1 -1
- data/spec/db/migrate/20202020202011_db_migration.rb +1 -1
- data/tasks/databases.rake +83 -25
- metadata +19 -21
- data/Gemfile.lock +0 -184
- data/gemfiles/rails_6.1.gemfile.lock +0 -229
- data/gemfiles/rails_7.0.gemfile.lock +0 -230
- data/gemfiles/rails_7.1.gemfile.lock +0 -263
- data/lib/data_migrate/database_configurations_wrapper.rb +0 -11
- data/lib/data_migrate/rails_helper.rb +0 -79
- data/lib/data_migrate/test.rb +0 -14
@@ -5,143 +5,67 @@ require "data_migrate/config"
|
|
5
5
|
module DataMigrate
|
6
6
|
##
|
7
7
|
# This class extends DatabaseTasks to add a schema_file method.
|
8
|
-
|
8
|
+
class DatabaseTasks
|
9
9
|
extend ActiveRecord::Tasks::DatabaseTasks
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
def with_temporary_connection_for_each(env: ActiveRecord::Tasks::DatabaseTasks.env, name: nil, &block) # :nodoc:
|
15
|
-
if name
|
16
|
-
db_config = ActiveRecord::Base.configurations.configs_for(env_name: env, name: name)
|
17
|
-
with_temporary_connection(db_config, &block)
|
18
|
-
else
|
19
|
-
ActiveRecord::Base.configurations.configs_for(env_name: env, name: name).each do |db_config|
|
20
|
-
with_temporary_connection(db_config, &block)
|
21
|
-
end
|
22
|
-
end
|
10
|
+
|
11
|
+
class << self
|
12
|
+
def schema_file_type(_format = nil)
|
13
|
+
"data_schema.rb"
|
23
14
|
end
|
24
15
|
|
25
|
-
def
|
26
|
-
|
27
|
-
|
16
|
+
def dump_filename(spec_name, format = ActiveRecord::Base.schema_format)
|
17
|
+
filename = if spec_name == "primary"
|
18
|
+
schema_file_type(format)
|
19
|
+
else
|
20
|
+
"#{spec_name}_#{schema_file_type(format)}"
|
28
21
|
end
|
29
|
-
end
|
30
22
|
|
31
|
-
|
32
|
-
ActiveRecord::Base
|
23
|
+
ENV["DATA_SCHEMA"] || File.join(db_dir, filename)
|
33
24
|
end
|
34
25
|
|
35
|
-
def
|
36
|
-
|
26
|
+
def check_schema_file(filename)
|
27
|
+
unless File.exist?(filename)
|
28
|
+
message = +%{#{filename} doesn't exist yet. Run `rake data:migrate` to create it, then try again.}
|
29
|
+
Kernel.abort message
|
30
|
+
end
|
37
31
|
end
|
38
32
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
ensure
|
45
|
-
migration_class.connection_handler.establish_connection(original_db_config)
|
33
|
+
def pending_migrations
|
34
|
+
sort_migrations(
|
35
|
+
pending_schema_migrations,
|
36
|
+
pending_data_migrations
|
37
|
+
)
|
46
38
|
end
|
47
|
-
end
|
48
|
-
|
49
|
-
def db_configs_with_versions
|
50
|
-
db_configs_with_versions = Hash.new { |h, k| h[k] = [] }
|
51
|
-
|
52
|
-
with_temporary_connection_for_each do |conn|
|
53
|
-
db_config = conn.pool.db_config
|
54
|
-
if db_config.primary?
|
55
|
-
versions_to_run = DataMigrate::DatabaseTasks.pending_data_migrations.map { |m| m[:version] }
|
56
|
-
target_version = ActiveRecord::Tasks::DatabaseTasks.target_version
|
57
39
|
|
58
|
-
|
59
|
-
|
60
|
-
db_configs_with_versions[version] << DatabaseConfigurationWrapper.new(db_config)
|
61
|
-
end
|
62
|
-
end
|
40
|
+
def sort_migrations(*migrations)
|
41
|
+
migrations.flatten.sort { |a, b| sort_string(a) <=> sort_string(b) }
|
63
42
|
end
|
64
43
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
def schema_file(_format = nil)
|
69
|
-
File.join(db_dir, "data_schema.rb")
|
70
|
-
end
|
71
|
-
|
72
|
-
def schema_file_type(_format = nil)
|
73
|
-
"data_schema.rb"
|
74
|
-
end
|
75
|
-
|
76
|
-
# This method is removed in Rails 7.0
|
77
|
-
def dump_filename(spec_name, format = ActiveRecord::Base.schema_format)
|
78
|
-
filename = if spec_name == "primary"
|
79
|
-
schema_file_type(format)
|
80
|
-
else
|
81
|
-
"#{spec_name}_#{schema_file_type(format)}"
|
44
|
+
def sort_string migration
|
45
|
+
"#{migration[:version]}_#{migration[:kind] == :data ? 1 : 0}"
|
82
46
|
end
|
83
47
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
def check_schema_file(filename)
|
88
|
-
unless File.exist?(filename)
|
89
|
-
message = +%{#{filename} doesn't exist yet. Run `rake data:migrate` to create it, then try again.}
|
90
|
-
Kernel.abort message
|
48
|
+
def data_migrations_path
|
49
|
+
::DataMigrate.config.data_migrations_path
|
91
50
|
end
|
92
|
-
end
|
93
|
-
|
94
|
-
def pending_migrations
|
95
|
-
sort_migrations(
|
96
|
-
pending_schema_migrations,
|
97
|
-
pending_data_migrations
|
98
|
-
)
|
99
|
-
end
|
100
|
-
|
101
|
-
def sort_migrations(*migrations)
|
102
|
-
migrations.flatten.sort { |a, b| sort_string(a) <=> sort_string(b) }
|
103
|
-
end
|
104
|
-
|
105
|
-
def sort_string migration
|
106
|
-
"#{migration[:version]}_#{migration[:kind] == :data ? 1 : 0}"
|
107
|
-
end
|
108
51
|
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
::DataMigrate::SchemaMigration.migrations_paths,
|
122
|
-
migration[:version]
|
123
|
-
)
|
52
|
+
def run_migration(migration, direction)
|
53
|
+
if migration[:kind] == :data
|
54
|
+
::ActiveRecord::Migration.write("== %s %s" % ['Data', "=" * 71])
|
55
|
+
::DataMigrate::DataMigrator.run(direction, data_migrations_path, migration[:version])
|
56
|
+
else
|
57
|
+
::ActiveRecord::Migration.write("== %s %s" % ['Schema', "=" * 69])
|
58
|
+
::DataMigrate::SchemaMigration.run(
|
59
|
+
direction,
|
60
|
+
::DataMigrate::SchemaMigration.migrations_paths,
|
61
|
+
migration[:version]
|
62
|
+
)
|
63
|
+
end
|
124
64
|
end
|
125
65
|
end
|
126
66
|
|
127
|
-
def
|
128
|
-
|
129
|
-
|
130
|
-
# We only require a schema.rb file for the primary database
|
131
|
-
return unless db_config.primary?
|
132
|
-
|
133
|
-
File.join(File.dirname(ActiveRecord::Tasks::DatabaseTasks.schema_dump_path(db_config, format)), schema_file_type)
|
134
|
-
end
|
135
|
-
|
136
|
-
# Override this method from `ActiveRecord::Tasks::DatabaseTasks`
|
137
|
-
# to ensure that the sha saved in ar_internal_metadata table
|
138
|
-
# is from the original schema.rb file
|
139
|
-
def schema_sha1(file)
|
140
|
-
ActiveRecord::Tasks::DatabaseTasks.schema_dump_path(ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env, name: "primary"))
|
141
|
-
end
|
142
|
-
|
143
|
-
def forward(step = 1)
|
144
|
-
DataMigrate::DataMigrator.create_data_schema_table
|
67
|
+
def self.forward(step = 1)
|
68
|
+
DataMigrate::DataMigrator.assure_data_schema_table
|
145
69
|
migrations = pending_migrations.reverse.pop(step).reverse
|
146
70
|
migrations.each do | pending_migration |
|
147
71
|
if pending_migration[:kind] == :data
|
@@ -154,92 +78,22 @@ module DataMigrate
|
|
154
78
|
end
|
155
79
|
end
|
156
80
|
|
157
|
-
def pending_data_migrations
|
81
|
+
def self.pending_data_migrations
|
158
82
|
data_migrations = DataMigrate::DataMigrator.migrations(data_migrations_path)
|
159
|
-
|
160
|
-
|
161
|
-
data_migrator.pending_migrations.map { |m| { version: m.version, name: m.name, kind: :data } }
|
162
|
-
)
|
83
|
+
sort_migrations(DataMigrate::DataMigrator.new(:up, data_migrations ).
|
84
|
+
pending_migrations.map {|m| { version: m.version, name: m.name, kind: :data }})
|
163
85
|
end
|
164
86
|
|
165
|
-
def pending_schema_migrations
|
87
|
+
def self.pending_schema_migrations
|
166
88
|
::DataMigrate::SchemaMigration.pending_schema_migrations
|
167
89
|
end
|
168
90
|
|
169
|
-
def past_migrations(sort = nil)
|
170
|
-
data_versions = DataMigrate::
|
171
|
-
schema_versions =
|
91
|
+
def self.past_migrations(sort = nil)
|
92
|
+
data_versions = DataMigrate::DataSchemaMigration.table_exists? ? DataMigrate::DataSchemaMigration.normalized_versions : []
|
93
|
+
schema_versions = ActiveRecord::SchemaMigration.normalized_versions
|
172
94
|
migrations = data_versions.map { |v| { version: v.to_i, kind: :data } } + schema_versions.map { |v| { version: v.to_i, kind: :schema } }
|
173
95
|
|
174
96
|
sort&.downcase == "asc" ? sort_migrations(migrations) : sort_migrations(migrations).reverse
|
175
97
|
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
|
-
db_configs = ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env)
|
183
|
-
|
184
|
-
schema_mapped_versions = ActiveRecord::Tasks::DatabaseTasks.db_configs_with_versions(db_configs)
|
185
|
-
data_mapped_versions = DataMigrate::DatabaseTasks.db_configs_with_versions
|
186
|
-
|
187
|
-
mapped_versions = schema_mapped_versions.merge(data_mapped_versions) do |_key, schema_db_configs, data_db_configs|
|
188
|
-
schema_db_configs + data_db_configs
|
189
|
-
end
|
190
|
-
|
191
|
-
mapped_versions.sort.each do |version, db_configs|
|
192
|
-
db_configs.each do |db_config|
|
193
|
-
if is_data_migration = db_config.is_a?(DataMigrate::DatabaseConfigurationWrapper)
|
194
|
-
db_config = db_config.db_config
|
195
|
-
end
|
196
|
-
|
197
|
-
DataMigrate::DatabaseTasks.with_temporary_connection(db_config) do
|
198
|
-
if is_data_migration
|
199
|
-
DataMigrate::DataMigrator.run(:up, DataMigrate::DatabaseTasks.data_migrations_path, version)
|
200
|
-
else
|
201
|
-
ActiveRecord::Tasks::DatabaseTasks.migrate(version)
|
202
|
-
end
|
203
|
-
end
|
204
|
-
end
|
205
|
-
end
|
206
|
-
end
|
207
|
-
|
208
|
-
def self.prepare_all_with_data
|
209
|
-
seed = false
|
210
|
-
|
211
|
-
each_current_configuration(env) do |db_config|
|
212
|
-
next unless db_config.primary?
|
213
|
-
|
214
|
-
with_temporary_pool(db_config) do
|
215
|
-
begin
|
216
|
-
database_initialized = migration_connection.schema_migration.table_exists?
|
217
|
-
rescue ActiveRecord::NoDatabaseError
|
218
|
-
create(db_config)
|
219
|
-
retry
|
220
|
-
end
|
221
|
-
|
222
|
-
unless database_initialized
|
223
|
-
if File.exist?(schema_dump_path(db_config))
|
224
|
-
load_schema(db_config, ActiveRecord.schema_format, nil)
|
225
|
-
load_schema_current(
|
226
|
-
:ruby,
|
227
|
-
ENV["DATA_SCHEMA"]
|
228
|
-
)
|
229
|
-
end
|
230
|
-
|
231
|
-
seed = true
|
232
|
-
end
|
233
|
-
|
234
|
-
migrate_with_data
|
235
|
-
if ActiveRecord.dump_schema_after_migration
|
236
|
-
dump_schema(db_config)
|
237
|
-
DataMigrate::Tasks::DataMigrateTasks.dump
|
238
|
-
end
|
239
|
-
end
|
240
|
-
end
|
241
|
-
|
242
|
-
load_seed if seed
|
243
|
-
end
|
244
98
|
end
|
245
99
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module DataMigrate
|
2
|
+
class LegacyMigrator
|
3
|
+
def initialize(migrations_paths = "db/data")
|
4
|
+
@migrations_paths = migrations_paths || "db/data"
|
5
|
+
end
|
6
|
+
|
7
|
+
def migrate
|
8
|
+
dates =
|
9
|
+
DataMigrate::DataMigrator.migrations(@migrations_paths).collect(&:version)
|
10
|
+
legacy = ActiveRecord::SchemaMigration.where(version: dates)
|
11
|
+
legacy.each do |v|
|
12
|
+
begin
|
13
|
+
version = v.version
|
14
|
+
puts "Creating #{version} in data schema"
|
15
|
+
DataMigrate::DataSchemaMigration.create(version: version)
|
16
|
+
rescue ActiveRecord::RecordNotUnique
|
17
|
+
nil
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -11,8 +11,7 @@ module DataMigrate
|
|
11
11
|
migrations
|
12
12
|
end
|
13
13
|
|
14
|
-
|
15
|
-
data_migrator.migrate
|
14
|
+
DataMigrator.new(:up, selected_migrations, target_version).migrate
|
16
15
|
end
|
17
16
|
|
18
17
|
def down(target_version = nil)
|
@@ -23,13 +22,11 @@ module DataMigrate
|
|
23
22
|
migrations
|
24
23
|
end
|
25
24
|
|
26
|
-
|
27
|
-
data_migrator.migrate
|
25
|
+
DataMigrator.new(:down, selected_migrations, target_version).migrate
|
28
26
|
end
|
29
27
|
|
30
28
|
def run(direction, target_version)
|
31
|
-
|
32
|
-
data_migrator.run
|
29
|
+
DataMigrator.new(direction, migrations, target_version).run
|
33
30
|
end
|
34
31
|
|
35
32
|
def current_version
|
@@ -43,12 +40,12 @@ module DataMigrate
|
|
43
40
|
end
|
44
41
|
|
45
42
|
def migrations_status
|
46
|
-
db_list =
|
43
|
+
db_list = DataSchemaMigration.normalized_versions
|
47
44
|
|
48
45
|
file_list = migration_files.map do |file|
|
49
46
|
version, name, scope = parse_migration_filename(file)
|
50
47
|
raise ActiveRecord::IllegalMigrationNameError.new(file) unless version
|
51
|
-
version =
|
48
|
+
version = ActiveRecord::SchemaMigration.normalize_migration_number(version)
|
52
49
|
status = db_list.delete(version) ? "up" : "down"
|
53
50
|
[status, version, (name + scope).humanize]
|
54
51
|
end.compact
|
@@ -63,15 +60,15 @@ module DataMigrate
|
|
63
60
|
private
|
64
61
|
|
65
62
|
def get_all_versions
|
66
|
-
if DataMigrate::
|
67
|
-
|
63
|
+
if DataMigrate::DataSchemaMigration.table_exists?
|
64
|
+
DataSchemaMigration.normalized_versions.map(&:to_i)
|
68
65
|
else
|
69
66
|
[]
|
70
67
|
end
|
71
68
|
end
|
72
69
|
|
73
70
|
def move(direction, steps)
|
74
|
-
migrator =
|
71
|
+
migrator = DataMigrator.new(direction, migrations)
|
75
72
|
|
76
73
|
if current_version != 0 && !migrator.current_migration
|
77
74
|
raise ActiveRecord::UnknownMigrationVersionError.new(current_version)
|
@@ -26,7 +26,7 @@ module DataMigrate
|
|
26
26
|
|
27
27
|
def initialize(connection)
|
28
28
|
@connection = connection
|
29
|
-
all_versions =
|
29
|
+
all_versions = DataSchemaMigration.normalized_versions
|
30
30
|
|
31
31
|
@version = begin
|
32
32
|
all_versions.max
|
@@ -7,14 +7,14 @@ module DataMigrate
|
|
7
7
|
def self.pending_schema_migrations
|
8
8
|
all_migrations = DataMigrate::MigrationContext.new(migrations_paths).migrations
|
9
9
|
sort_migrations(
|
10
|
-
ActiveRecord::Migrator.new(:up, all_migrations,
|
10
|
+
ActiveRecord::Migrator.new(:up, all_migrations, ActiveRecord::Base.connection.schema_migration).
|
11
11
|
pending_migrations.
|
12
12
|
map {|m| { version: m.version, kind: :schema }}
|
13
13
|
)
|
14
14
|
end
|
15
15
|
|
16
16
|
def self.run(direction, migration_paths, version)
|
17
|
-
ActiveRecord::MigrationContext.new(migration_paths,
|
17
|
+
ActiveRecord::MigrationContext.new(migration_paths, ActiveRecord::Base.connection.schema_migration).run(direction, version)
|
18
18
|
end
|
19
19
|
|
20
20
|
def self.sort_migrations(set1, set2 = nil)
|
@@ -24,10 +24,9 @@ module DataMigrate
|
|
24
24
|
|
25
25
|
def self.migrations_paths
|
26
26
|
spec_name = DataMigrate.config.spec_name
|
27
|
-
|
28
|
-
if spec_name && Gem::Dependency.new("railties", ">= 7.0").match?("railties", Gem.loaded_specs["railties"].version, true)
|
27
|
+
if spec_name && Gem::Dependency.new("rails", "~> 7.0").match?("rails", Gem.loaded_specs["rails"].version)
|
29
28
|
ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, name: spec_name).migrations_paths
|
30
|
-
elsif spec_name && Gem::Dependency.new("
|
29
|
+
elsif spec_name && Gem::Dependency.new("rails", "~> 6.0").match?("rails", Gem.loaded_specs["rails"].version)
|
31
30
|
ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, spec_name: spec_name).migrations_paths
|
32
31
|
else
|
33
32
|
Rails.application.config.paths["db/migrate"].to_a
|
@@ -24,11 +24,11 @@ module DataMigrate
|
|
24
24
|
private
|
25
25
|
|
26
26
|
def table_name
|
27
|
-
DataMigrate::
|
27
|
+
DataMigrate::DataSchemaMigration.table_name
|
28
28
|
end
|
29
29
|
|
30
30
|
def output(stream)
|
31
|
-
unless DataMigrate::
|
31
|
+
unless DataMigrate::DataSchemaMigration.table_exists?
|
32
32
|
stream.puts "Data migrations table does not exist yet."
|
33
33
|
return
|
34
34
|
end
|
@@ -44,9 +44,9 @@ module DataMigrate
|
|
44
44
|
end
|
45
45
|
|
46
46
|
def database_name
|
47
|
-
if Gem::Dependency.new("
|
47
|
+
if Gem::Dependency.new("rails", "~> 7.0").match?("rails", Gem.loaded_specs["rails"].version)
|
48
48
|
ActiveRecord::Base.connection_db_config.configuration_hash[:database]
|
49
|
-
elsif Gem::Dependency.new("
|
49
|
+
elsif Gem::Dependency.new("rails", "~> 6.0").match?("rails", Gem.loaded_specs["rails"].version)
|
50
50
|
ActiveRecord::Base.connection_config[:database]
|
51
51
|
end
|
52
52
|
end
|
@@ -9,9 +9,9 @@ module DataMigrate
|
|
9
9
|
@migrations_paths ||= DataMigrate.config.data_migrations_path
|
10
10
|
end
|
11
11
|
|
12
|
-
def dump
|
12
|
+
def dump(db_config)
|
13
13
|
if dump_schema_after_migration?
|
14
|
-
filename = DataMigrate::DatabaseTasks.
|
14
|
+
filename = DataMigrate::DatabaseTasks.dump_filename(spec_name(db_config), ActiveRecord::Base.schema_format)
|
15
15
|
ActiveRecord::Base.establish_connection(DataMigrate.config.db_configuration) if DataMigrate.config.db_configuration
|
16
16
|
File.open(filename, "w:utf-8") do |file|
|
17
17
|
DataMigrate::SchemaDumper.dump(ActiveRecord::Base.connection, file)
|
@@ -22,7 +22,7 @@ module DataMigrate
|
|
22
22
|
def migrate
|
23
23
|
target_version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
|
24
24
|
|
25
|
-
DataMigrate::DataMigrator.
|
25
|
+
DataMigrate::DataMigrator.assure_data_schema_table
|
26
26
|
DataMigrate::MigrationContext.new(migrations_paths).migrate(target_version)
|
27
27
|
end
|
28
28
|
|
@@ -44,15 +44,16 @@ module DataMigrate
|
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
|
-
def status
|
48
|
-
|
47
|
+
def status(db_config)
|
48
|
+
puts "\ndatabase: #{spec_name(db_config)}\n\n"
|
49
|
+
DataMigrate::StatusService.dump(ActiveRecord::Base.connection)
|
49
50
|
end
|
50
51
|
|
51
|
-
def status_with_schema
|
52
|
+
def status_with_schema(db_config)
|
52
53
|
db_list_data = ActiveRecord::Base.connection.select_values(
|
53
|
-
"SELECT version FROM #{DataMigrate::
|
54
|
+
"SELECT version FROM #{DataMigrate::DataSchemaMigration.table_name}"
|
54
55
|
)
|
55
|
-
db_list_schema =
|
56
|
+
db_list_schema = ActiveRecord::SchemaMigration.all.pluck(:version)
|
56
57
|
file_list = []
|
57
58
|
|
58
59
|
Dir.foreach(File.join(Rails.root, migrations_paths)) do |file|
|
@@ -76,7 +77,7 @@ module DataMigrate
|
|
76
77
|
file_list.sort!{|a,b| "#{a[1]}_#{a[3] == 'data' ? 1 : 0}" <=> "#{b[1]}_#{b[3] == 'data' ? 1 : 0}" }
|
77
78
|
|
78
79
|
# output
|
79
|
-
puts "\ndatabase: #{
|
80
|
+
puts "\ndatabase: #{spec_name(db_config)}\n\n"
|
80
81
|
puts "#{"Status".center(8)} #{"Type".center(8)} #{"Migration ID".ljust(14)} Migration Name"
|
81
82
|
puts "-" * 60
|
82
83
|
file_list.each do |file|
|
@@ -93,11 +94,11 @@ module DataMigrate
|
|
93
94
|
|
94
95
|
private
|
95
96
|
|
96
|
-
def
|
97
|
-
if Gem::Dependency.new("
|
98
|
-
|
99
|
-
elsif Gem::Dependency.new("
|
100
|
-
|
97
|
+
def spec_name(db_config)
|
98
|
+
if Gem::Dependency.new("rails", "~> 7.0").match?("rails", Gem.loaded_specs["rails"].version)
|
99
|
+
db_config.name
|
100
|
+
elsif Gem::Dependency.new("rails", "~> 6.0").match?("rails", Gem.loaded_specs["rails"].version)
|
101
|
+
db_config.spec_name
|
101
102
|
end
|
102
103
|
end
|
103
104
|
end
|
data/lib/data_migrate/version.rb
CHANGED
data/lib/data_migrate.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require File.join(File.dirname(__FILE__), "data_migrate", "rails_helper")
|
4
3
|
require File.join(File.dirname(__FILE__), "data_migrate", "data_migrator")
|
5
4
|
require File.join(File.dirname(__FILE__), "data_migrate", "data_schema_migration")
|
6
5
|
require File.join(File.dirname(__FILE__), "data_migrate", "data_schema")
|
@@ -10,9 +9,9 @@ require File.join(File.dirname(__FILE__), "data_migrate", "status_service")
|
|
10
9
|
require File.join(File.dirname(__FILE__), "data_migrate", "migration_context")
|
11
10
|
require File.join(File.dirname(__FILE__), "data_migrate", "railtie")
|
12
11
|
require File.join(File.dirname(__FILE__), "data_migrate", "tasks/data_migrate_tasks")
|
12
|
+
require File.join(File.dirname(__FILE__), "data_migrate", "legacy_migrator")
|
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")
|
16
15
|
|
17
16
|
module DataMigrate
|
18
17
|
def self.root
|
@@ -3,8 +3,7 @@
|
|
3
3
|
require "spec_helper"
|
4
4
|
|
5
5
|
describe DataMigrate::DataMigrator do
|
6
|
-
let(:
|
7
|
-
|
6
|
+
let(:subject) { DataMigrate::DataMigrator }
|
8
7
|
let(:db_config) do
|
9
8
|
{
|
10
9
|
adapter: "sqlite3",
|
@@ -14,8 +13,8 @@ describe DataMigrate::DataMigrator do
|
|
14
13
|
|
15
14
|
before do
|
16
15
|
ActiveRecord::Base.establish_connection(db_config)
|
17
|
-
|
18
|
-
DataMigrate::
|
16
|
+
::ActiveRecord::SchemaMigration.create_table
|
17
|
+
DataMigrate::DataSchemaMigration.create_table
|
19
18
|
end
|
20
19
|
|
21
20
|
after do
|
@@ -24,24 +23,22 @@ describe DataMigrate::DataMigrator do
|
|
24
23
|
end
|
25
24
|
|
26
25
|
describe ".load_migrated" do
|
27
|
-
let(:migrator) { DataMigrate::RailsHelper.data_migrator(:up, []) }
|
28
|
-
|
29
26
|
it "loads migrated versions" do
|
30
|
-
DataMigrate::
|
31
|
-
|
32
|
-
DataMigrate::
|
33
|
-
|
34
|
-
migrated =
|
27
|
+
DataMigrate::DataSchemaMigration.create(version: 20090000000000)
|
28
|
+
::ActiveRecord::SchemaMigration.create(version: 20100000000000)
|
29
|
+
DataMigrate::DataSchemaMigration.create(version: 20110000000000)
|
30
|
+
::ActiveRecord::SchemaMigration.create(version: 20120000000000)
|
31
|
+
migrated = subject.new(:up, []).load_migrated
|
35
32
|
expect(migrated.count).to eq 2
|
36
33
|
expect(migrated).to include 20090000000000
|
37
34
|
expect(migrated).to include 20110000000000
|
38
35
|
end
|
39
36
|
end
|
40
37
|
|
41
|
-
describe :
|
38
|
+
describe :assure_data_schema_table do
|
42
39
|
it "creates the data_migrations table" do
|
43
40
|
ActiveRecord::Migration.drop_table("data_migrations") rescue nil
|
44
|
-
|
41
|
+
subject.assure_data_schema_table
|
45
42
|
expect(
|
46
43
|
ActiveRecord::Base.connection.table_exists?("data_migrations")
|
47
44
|
).to eq true
|
@@ -50,7 +47,7 @@ describe DataMigrate::DataMigrator do
|
|
50
47
|
|
51
48
|
describe "#migrations_status" do
|
52
49
|
it "returns all migrations statuses" do
|
53
|
-
status =
|
50
|
+
status = subject.migrations_status
|
54
51
|
expect(status.length).to eq 2
|
55
52
|
expect(status.first).to eq ["down", "20091231235959", "Some name"]
|
56
53
|
expect(status.second).to eq ["down", "20171231235959", "Super update"]
|
@@ -60,19 +57,19 @@ describe DataMigrate::DataMigrator do
|
|
60
57
|
describe :match do
|
61
58
|
context "when the file does not match" do
|
62
59
|
it "returns nil" do
|
63
|
-
expect(
|
60
|
+
expect(subject.match("not_a_data_migration_file")).to be_nil
|
64
61
|
end
|
65
62
|
end
|
66
63
|
|
67
64
|
context "when the file doesn't end in .rb" do
|
68
65
|
it "returns nil" do
|
69
|
-
expect(
|
66
|
+
expect(subject.match("20091231235959_some_name.rb.un~")).to be_nil
|
70
67
|
end
|
71
68
|
end
|
72
69
|
|
73
70
|
context "when the file matches" do
|
74
71
|
it "returns a valid MatchData object" do
|
75
|
-
match_data =
|
72
|
+
match_data = subject.match("20091231235959_some_name.rb")
|
76
73
|
|
77
74
|
expect(match_data[0]).to eq "20091231235959_some_name.rb"
|
78
75
|
expect(match_data[1]).to eq "20091231235959"
|
@@ -1,33 +1,16 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe DataMigrate::DataSchemaMigration do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
describe :table_name do
|
9
|
-
it "returns correct table name" do
|
10
|
-
expect(subject.table_name).to eq("data_migrations")
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
describe :index_name do
|
15
|
-
it "returns correct primary key name" do
|
16
|
-
expect(subject.primary_key).to eq("version")
|
17
|
-
end
|
18
|
-
end
|
19
|
-
else
|
20
|
-
let(:subject) { DataMigrate::DataSchemaMigration }
|
21
|
-
describe :table_name do
|
22
|
-
it "returns correct table name" do
|
23
|
-
expect(subject.table_name).to eq("data_migrations")
|
24
|
-
end
|
4
|
+
let(:subject) { DataMigrate::DataSchemaMigration }
|
5
|
+
describe :table_name do
|
6
|
+
it "returns correct table name" do
|
7
|
+
expect(subject.table_name).to eq("data_migrations")
|
25
8
|
end
|
9
|
+
end
|
26
10
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
end
|
11
|
+
describe :index_name do
|
12
|
+
it "returns correct primary key name" do
|
13
|
+
expect(subject.primary_key).to eq("version")
|
31
14
|
end
|
32
15
|
end
|
33
16
|
end
|