data_migrate 8.5.0 → 9.2.0
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 +34 -0
- data/.github/workflows/gempush.yml +6 -7
- data/.gitignore +3 -2
- data/Appraisals +5 -9
- data/Changelog.md +44 -0
- data/Gemfile.lock +148 -0
- data/README.md +9 -7
- data/data_migrate.gemspec +1 -10
- data/gemfiles/rails_6.1.gemfile +1 -1
- data/gemfiles/rails_6.1.gemfile.lock +227 -0
- data/gemfiles/rails_7.0.gemfile.lock +229 -0
- data/gemfiles/{rails_6.0.gemfile → rails_7.1.gemfile} +1 -1
- data/gemfiles/rails_7.1.gemfile.lock +262 -0
- data/lib/data_migrate/config.rb +1 -1
- data/lib/data_migrate/{data_migrator_five.rb → data_migrator.rb} +11 -34
- data/lib/data_migrate/data_schema.rb +2 -2
- data/lib/data_migrate/data_schema_migration.rb +24 -7
- data/lib/data_migrate/database_tasks.rb +37 -74
- data/lib/data_migrate/migration_context.rb +11 -8
- data/lib/data_migrate/rails_helper.rb +79 -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 +16 -37
- data/lib/data_migrate/version.rb +1 -1
- data/lib/data_migrate.rb +4 -9
- data/spec/data_migrate/config_spec.rb +13 -10
- data/spec/data_migrate/data_migrator_spec.rb +25 -43
- data/spec/data_migrate/data_schema_migration_spec.rb +25 -8
- data/spec/data_migrate/data_spec.rb +1 -12
- data/spec/data_migrate/database_tasks_spec.rb +43 -76
- data/spec/data_migrate/migration.rb +11 -13
- data/spec/data_migrate/migration_context_spec.rb +20 -39
- data/spec/data_migrate/schema_dumper_spec.rb +11 -21
- data/spec/data_migrate/schema_migration_spec.rb +46 -41
- data/spec/data_migrate/status_service_spec.rb +29 -55
- data/spec/data_migrate/tasks/data_migrate_tasks_spec.rb +32 -67
- data/spec/db/data/20091231235959_some_name.rb +1 -1
- data/spec/db/data/20171231235959_super_update.rb +1 -1
- data/spec/db/migrate/{5.2/20131111111111_late_migration.rb → 20131111111111_late_migration.rb} +1 -1
- data/spec/db/migrate/{6.0/20202020202011_db_migration.rb → 20202020202011_db_migration.rb} +1 -1
- data/spec/spec_helper.rb +2 -8
- data/tasks/databases.rake +15 -13
- metadata +20 -34
- data/.ruby-version +0 -1
- data/.travis.yml +0 -14
- data/Gemfile.rails5.2 +0 -10
- data/gemfiles/rails_5.2.gemfile +0 -8
- data/lib/data_migrate/legacy_migrator.rb +0 -22
- data/lib/data_migrate/schema_migration_five.rb +0 -31
- data/spec/data_migrate/legacy_migrator_spec.rb +0 -50
- 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-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/20202020202011_db_migration.rb +0 -9
- data/spec/db/migrate/6.0/20131111111111_late_migration.rb +0 -9
@@ -9,36 +9,23 @@ module DataMigrate
|
|
9
9
|
extend ActiveRecord::Tasks::DatabaseTasks
|
10
10
|
|
11
11
|
class << self
|
12
|
-
def
|
13
|
-
"data_schema.rb"
|
12
|
+
def schema_file(_format = nil)
|
13
|
+
File.join(db_dir, "data_schema.rb")
|
14
14
|
end
|
15
15
|
|
16
|
-
def
|
17
|
-
|
18
|
-
schema_file_type(format)
|
19
|
-
else
|
20
|
-
"#{namespace}_#{schema_file_type(format)}"
|
21
|
-
end
|
22
|
-
|
23
|
-
ENV["DATA_SCHEMA"] || File.join(schema_location, filename)
|
16
|
+
def schema_file_type(_format = nil)
|
17
|
+
"data_schema.rb"
|
24
18
|
end
|
25
19
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
filename = if db_config.primary?
|
20
|
+
# This method is removed in Rails 7.0
|
21
|
+
def dump_filename(spec_name, format = ActiveRecord::Base.schema_format)
|
22
|
+
filename = if spec_name == "primary"
|
30
23
|
schema_file_type(format)
|
31
24
|
else
|
32
|
-
|
25
|
+
"#{spec_name}_#{schema_file_type(format)}"
|
33
26
|
end
|
34
27
|
|
35
|
-
|
36
|
-
|
37
|
-
File.dirname(filename) == schema_location ? filename : File.join(schema_location, filename)
|
38
|
-
end
|
39
|
-
|
40
|
-
def schema_location
|
41
|
-
db_dir
|
28
|
+
ENV["DATA_SCHEMA"] || File.join(db_dir, filename)
|
42
29
|
end
|
43
30
|
|
44
31
|
def check_schema_file(filename)
|
@@ -55,9 +42,8 @@ module DataMigrate
|
|
55
42
|
)
|
56
43
|
end
|
57
44
|
|
58
|
-
def sort_migrations
|
59
|
-
migrations
|
60
|
-
migrations.sort{|a,b| sort_string(a) <=> sort_string(b)}
|
45
|
+
def sort_migrations(*migrations)
|
46
|
+
migrations.flatten.sort { |a, b| sort_string(a) <=> sort_string(b) }
|
61
47
|
end
|
62
48
|
|
63
49
|
def sort_string migration
|
@@ -81,15 +67,26 @@ module DataMigrate
|
|
81
67
|
)
|
82
68
|
end
|
83
69
|
end
|
84
|
-
end
|
85
70
|
|
86
|
-
|
87
|
-
|
88
|
-
|
71
|
+
def schema_dump_path(db_config, format = ActiveRecord.schema_format)
|
72
|
+
return ENV["DATA_SCHEMA"] if ENV["DATA_SCHEMA"]
|
73
|
+
|
74
|
+
# We only require a schema.rb file for the primary database
|
75
|
+
return unless db_config.primary?
|
76
|
+
|
77
|
+
File.join(File.dirname(ActiveRecord::Tasks::DatabaseTasks.schema_dump_path(db_config, format)), schema_file_type)
|
78
|
+
end
|
79
|
+
|
80
|
+
# Override this method from `ActiveRecord::Tasks::DatabaseTasks`
|
81
|
+
# to ensure that the sha saved in ar_internal_metadata table
|
82
|
+
# is from the original schema.rb file
|
83
|
+
def schema_sha1(file)
|
84
|
+
ActiveRecord::Tasks::DatabaseTasks.schema_dump_path(ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env, name: "primary"))
|
85
|
+
end
|
89
86
|
end
|
90
87
|
|
91
88
|
def self.forward(step = 1)
|
92
|
-
DataMigrate::DataMigrator.
|
89
|
+
DataMigrate::DataMigrator.create_data_schema_table
|
93
90
|
migrations = pending_migrations.reverse.pop(step).reverse
|
94
91
|
migrations.each do | pending_migration |
|
95
92
|
if pending_migration[:kind] == :data
|
@@ -97,63 +94,29 @@ module DataMigrate
|
|
97
94
|
DataMigrate::DataMigrator.run(:up, data_migrations_path, pending_migration[:version])
|
98
95
|
elsif pending_migration[:kind] == :schema
|
99
96
|
ActiveRecord::Migration.write("== %s %s" % ["Schema", "=" * 69])
|
100
|
-
DataMigrate::SchemaMigration.run(:up,
|
97
|
+
DataMigrate::SchemaMigration.run(:up, DataMigrate::SchemaMigration.migrations_paths, pending_migration[:version])
|
101
98
|
end
|
102
99
|
end
|
103
100
|
end
|
104
101
|
|
105
|
-
def self.data_migrations_path
|
106
|
-
DataMigrate.config.data_migrations_path
|
107
|
-
end
|
108
|
-
|
109
|
-
def self.schema_migrations_path
|
110
|
-
"db/migrate/"
|
111
|
-
end
|
112
|
-
|
113
|
-
def self.pending_migrations
|
114
|
-
sort_migrations(pending_schema_migrations, pending_data_migrations)
|
115
|
-
end
|
116
|
-
|
117
102
|
def self.pending_data_migrations
|
118
103
|
data_migrations = DataMigrate::DataMigrator.migrations(data_migrations_path)
|
119
|
-
|
120
|
-
|
104
|
+
data_migrator = DataMigrate::RailsHelper.data_migrator(:up, data_migrations)
|
105
|
+
sort_migrations(
|
106
|
+
data_migrator.pending_migrations.map { |m| { version: m.version, name: m.name, kind: :data } }
|
107
|
+
)
|
121
108
|
end
|
122
109
|
|
123
110
|
def self.pending_schema_migrations
|
124
111
|
::DataMigrate::SchemaMigration.pending_schema_migrations
|
125
112
|
end
|
126
113
|
|
127
|
-
def self.sort_migrations(set1, set2 = nil)
|
128
|
-
migrations = set1 + (set2 || [])
|
129
|
-
migrations.sort {|a, b| sort_string(a) <=> sort_string(b)}
|
130
|
-
end
|
131
|
-
|
132
|
-
def self.sort_string(migration)
|
133
|
-
"#{migration[:version]}_#{migration[:kind] == :data ? 1 : 0}"
|
134
|
-
end
|
135
|
-
|
136
114
|
def self.past_migrations(sort = nil)
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
[]
|
143
|
-
end
|
144
|
-
db_list_schema = ActiveRecord::SchemaMigration.normalized_versions.sort.sort
|
145
|
-
migrations = db_list_data.map do |d|
|
146
|
-
{
|
147
|
-
version: d.to_i, kind: :data
|
148
|
-
}
|
149
|
-
end +
|
150
|
-
db_list_schema.map do |d|
|
151
|
-
{
|
152
|
-
version: d.to_i, kind: :schema
|
153
|
-
}
|
154
|
-
end
|
155
|
-
|
156
|
-
sort == "asc" ? sort_migrations(migrations) : sort_migrations(migrations).reverse
|
115
|
+
data_versions = DataMigrate::RailsHelper.data_schema_migration.table_exists? ? DataMigrate::RailsHelper.data_schema_migration.normalized_versions : []
|
116
|
+
schema_versions = DataMigrate::RailsHelper.schema_migration.normalized_versions
|
117
|
+
migrations = data_versions.map { |v| { version: v.to_i, kind: :data } } + schema_versions.map { |v| { version: v.to_i, kind: :schema } }
|
118
|
+
|
119
|
+
sort&.downcase == "asc" ? sort_migrations(migrations) : sort_migrations(migrations).reverse
|
157
120
|
end
|
158
121
|
end
|
159
122
|
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,79 @@
|
|
1
|
+
module DataMigrate
|
2
|
+
class RailsHelper
|
3
|
+
class << self
|
4
|
+
def rails_version_equal_to_or_higher_than_7_1
|
5
|
+
return @equal_to_or_higher_than_7_1 if defined?(@equal_to_or_higher_than_7_1)
|
6
|
+
|
7
|
+
@equal_to_or_higher_than_7_1 = Gem::Dependency.new("railties", ">= 7.1.0.alpha").match?("railties", Gem.loaded_specs["railties"].version, true)
|
8
|
+
end
|
9
|
+
|
10
|
+
def rails_version_equal_to_or_higher_than_7_0
|
11
|
+
return @rails_version_equal_to_or_higher_than_7_0 if defined?(@rails_version_equal_to_or_higher_than_7_0)
|
12
|
+
|
13
|
+
@rails_version_equal_to_or_higher_than_7_0 = Gem::Dependency.new("railties", ">= 7.0").match?("railties", Gem.loaded_specs["railties"].version, true)
|
14
|
+
end
|
15
|
+
|
16
|
+
def internal_metadata
|
17
|
+
if rails_version_equal_to_or_higher_than_7_1
|
18
|
+
ActiveRecord::Base.connection.internal_metadata
|
19
|
+
else
|
20
|
+
ActiveRecord::InternalMetadata
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def schema_migration
|
25
|
+
if rails_version_equal_to_or_higher_than_7_1
|
26
|
+
ActiveRecord::Base.connection.schema_migration
|
27
|
+
else
|
28
|
+
ActiveRecord::SchemaMigration
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def schema_migration_versions
|
33
|
+
if rails_version_equal_to_or_higher_than_7_1
|
34
|
+
schema_migration.versions
|
35
|
+
else
|
36
|
+
schema_migration.all.pluck(:version)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def schema_create_version(version)
|
41
|
+
if rails_version_equal_to_or_higher_than_7_1
|
42
|
+
schema_migration.create_version(version)
|
43
|
+
else
|
44
|
+
schema_migration.create(version: version)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def data_schema_delete_version(version)
|
49
|
+
if rails_version_equal_to_or_higher_than_7_1
|
50
|
+
data_schema_migration.delete_version(version)
|
51
|
+
else
|
52
|
+
data_schema_migration.where(version: version.to_s).delete_all
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def data_schema_migration
|
57
|
+
if rails_version_equal_to_or_higher_than_7_1
|
58
|
+
DataMigrate::DataSchemaMigration.new(ActiveRecord::Tasks::DatabaseTasks.migration_connection)
|
59
|
+
else
|
60
|
+
DataMigrate::DataSchemaMigration
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def data_migrator(
|
65
|
+
direction,
|
66
|
+
migrations,
|
67
|
+
schema_migration = DataMigrate::RailsHelper.schema_migration,
|
68
|
+
internal_metadata = DataMigrate::RailsHelper.internal_metadata,
|
69
|
+
target_version = nil
|
70
|
+
)
|
71
|
+
if rails_version_equal_to_or_higher_than_7_1
|
72
|
+
DataMigrate::DataMigrator.new(direction, migrations, schema_migration, internal_metadata, target_version)
|
73
|
+
else
|
74
|
+
DataMigrate::DataMigrator.new(direction, migrations, schema_migration, target_version)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
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
|
@@ -1,12 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module DataMigrate
|
2
4
|
module Tasks
|
3
5
|
module DataMigrateTasks
|
4
6
|
extend self
|
5
7
|
|
6
|
-
def schema_migrations_path
|
7
|
-
File.join('db', 'migrate')
|
8
|
-
end
|
9
|
-
|
10
8
|
def migrations_paths
|
11
9
|
@migrations_paths ||= DataMigrate.config.data_migrations_path
|
12
10
|
end
|
@@ -24,13 +22,13 @@ module DataMigrate
|
|
24
22
|
def migrate
|
25
23
|
target_version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
|
26
24
|
|
27
|
-
DataMigrate::DataMigrator.
|
25
|
+
DataMigrate::DataMigrator.create_data_schema_table
|
28
26
|
DataMigrate::MigrationContext.new(migrations_paths).migrate(target_version)
|
29
27
|
end
|
30
28
|
|
31
29
|
def abort_if_pending_migrations(migrations, message)
|
32
30
|
if migrations.any?
|
33
|
-
puts "You have #{migrations.size} pending #{migrations.size
|
31
|
+
puts "You have #{migrations.size} pending #{'migration'.pluralize(migrations.size)}:"
|
34
32
|
migrations.each do |pending_migration|
|
35
33
|
puts " %4d %s" % [pending_migration[:version], pending_migration[:name]]
|
36
34
|
end
|
@@ -47,24 +45,14 @@ module DataMigrate
|
|
47
45
|
end
|
48
46
|
|
49
47
|
def status
|
50
|
-
|
51
|
-
return unless config
|
52
|
-
|
53
|
-
connection = ActiveRecord::Base.connection
|
54
|
-
puts "\ndatabase: #{config['database']}\n\n"
|
55
|
-
DataMigrate::StatusService.dump(connection)
|
48
|
+
DataMigrate::StatusService.dump
|
56
49
|
end
|
57
50
|
|
58
51
|
def status_with_schema
|
59
|
-
config = connect_to_database
|
60
|
-
return unless config
|
61
|
-
|
62
52
|
db_list_data = ActiveRecord::Base.connection.select_values(
|
63
|
-
"SELECT version FROM #{DataMigrate::
|
64
|
-
)
|
65
|
-
db_list_schema = ActiveRecord::Base.connection.select_values(
|
66
|
-
"SELECT version FROM #{ActiveRecord::SchemaMigration.schema_migrations_table_name}"
|
53
|
+
"SELECT version FROM #{DataMigrate::RailsHelper.data_schema_migration.table_name}"
|
67
54
|
)
|
55
|
+
db_list_schema = DataMigrate::RailsHelper.schema_migration_versions
|
68
56
|
file_list = []
|
69
57
|
|
70
58
|
Dir.foreach(File.join(Rails.root, migrations_paths)) do |file|
|
@@ -75,7 +63,9 @@ module DataMigrate
|
|
75
63
|
end
|
76
64
|
end
|
77
65
|
|
78
|
-
|
66
|
+
DataMigrate::SchemaMigration.migrations_paths.map do |path|
|
67
|
+
Dir.children(path) if Dir.exist?(path)
|
68
|
+
end.flatten.compact.each do |file|
|
79
69
|
# only files matching "20091231235959_some_name.rb" pattern
|
80
70
|
if match_data = /(\d{14})_(.+)\.rb/.match(file)
|
81
71
|
status = db_list_schema.delete(match_data[1]) ? 'up' : 'down'
|
@@ -86,7 +76,7 @@ module DataMigrate
|
|
86
76
|
file_list.sort!{|a,b| "#{a[1]}_#{a[3] == 'data' ? 1 : 0}" <=> "#{b[1]}_#{b[3] == 'data' ? 1 : 0}" }
|
87
77
|
|
88
78
|
# output
|
89
|
-
puts "\ndatabase: #{
|
79
|
+
puts "\ndatabase: #{database_name}\n\n"
|
90
80
|
puts "#{"Status".center(8)} #{"Type".center(8)} #{"Migration ID".ljust(14)} Migration Name"
|
91
81
|
puts "-" * 60
|
92
82
|
file_list.each do |file|
|
@@ -103,23 +93,12 @@ module DataMigrate
|
|
103
93
|
|
104
94
|
private
|
105
95
|
|
106
|
-
def
|
107
|
-
|
108
|
-
ActiveRecord::Base.
|
109
|
-
|
110
|
-
ActiveRecord::Base.
|
111
|
-
end
|
112
|
-
ActiveRecord::Base.establish_connection(config)
|
113
|
-
|
114
|
-
unless DataMigrate::DataSchemaMigration.table_exists?
|
115
|
-
puts 'Data migrations table does not exist yet.'
|
116
|
-
config = nil
|
117
|
-
end
|
118
|
-
unless ActiveRecord::SchemaMigration.table_exists?
|
119
|
-
puts 'Schema migrations table does not exist yet.'
|
120
|
-
config = nil
|
96
|
+
def database_name
|
97
|
+
if Gem::Dependency.new("railties", "~> 7.0").match?("railties", Gem.loaded_specs["railties"].version)
|
98
|
+
ActiveRecord::Base.connection_db_config.database
|
99
|
+
elsif Gem::Dependency.new("railties", "~> 6.1").match?("railties", Gem.loaded_specs["railties"].version)
|
100
|
+
ActiveRecord::Base.connection_config[:database]
|
121
101
|
end
|
122
|
-
config
|
123
102
|
end
|
124
103
|
end
|
125
104
|
end
|
data/lib/data_migrate/version.rb
CHANGED
data/lib/data_migrate.rb
CHANGED
@@ -1,22 +1,17 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require File.join(File.dirname(__FILE__), "data_migrate", "
|
3
|
+
require File.join(File.dirname(__FILE__), "data_migrate", "rails_helper")
|
4
|
+
require File.join(File.dirname(__FILE__), "data_migrate", "data_migrator")
|
4
5
|
require File.join(File.dirname(__FILE__), "data_migrate", "data_schema_migration")
|
5
6
|
require File.join(File.dirname(__FILE__), "data_migrate", "data_schema")
|
6
7
|
require File.join(File.dirname(__FILE__), "data_migrate", "database_tasks")
|
7
8
|
require File.join(File.dirname(__FILE__), "data_migrate", "schema_dumper")
|
8
|
-
require File.join(File.dirname(__FILE__), "data_migrate", "
|
9
|
+
require File.join(File.dirname(__FILE__), "data_migrate", "status_service")
|
9
10
|
require File.join(File.dirname(__FILE__), "data_migrate", "migration_context")
|
10
11
|
require File.join(File.dirname(__FILE__), "data_migrate", "railtie")
|
11
12
|
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
|
-
|
15
|
-
if Rails::VERSION::MAJOR == 5
|
16
|
-
require File.join(File.dirname(__FILE__), "data_migrate", "schema_migration_five")
|
17
|
-
else
|
18
|
-
require File.join(File.dirname(__FILE__), "data_migrate", "schema_migration_six")
|
19
|
-
end
|
14
|
+
require File.join(File.dirname(__FILE__), "data_migrate", "schema_migration")
|
20
15
|
|
21
16
|
module DataMigrate
|
22
17
|
def self.root
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "spec_helper"
|
2
4
|
|
3
5
|
describe DataMigrate::Config do
|
@@ -11,41 +13,42 @@ describe DataMigrate::Config do
|
|
11
13
|
|
12
14
|
describe "data migration path configured" do
|
13
15
|
subject { DataMigrate.config.data_migrations_path }
|
16
|
+
let(:data_migrations_path) { "db/awesome/" }
|
17
|
+
|
14
18
|
before do
|
15
|
-
@
|
19
|
+
@original_data_migrations_path = DataMigrate.config.data_migrations_path
|
20
|
+
|
16
21
|
DataMigrate.configure do |config|
|
17
|
-
config.data_migrations_path =
|
22
|
+
config.data_migrations_path = data_migrations_path
|
18
23
|
end
|
19
24
|
end
|
20
25
|
|
21
26
|
after do
|
22
27
|
DataMigrate.configure do |config|
|
23
|
-
config.data_migrations_path = @
|
28
|
+
config.data_migrations_path = @original_data_migrations_path
|
24
29
|
end
|
25
30
|
end
|
26
31
|
|
27
32
|
it "equals the custom data migration path" do
|
28
|
-
is_expected.to eq
|
33
|
+
is_expected.to eq(data_migrations_path)
|
29
34
|
end
|
30
35
|
end
|
31
36
|
|
32
37
|
describe "data template path configured" do
|
33
38
|
subject { DataMigrate.config.data_template_path }
|
39
|
+
let(:data_template_path) { File.join(DataMigrate.root, "generators", "data_migration", "templates", "data_migration.rb") }
|
34
40
|
|
35
41
|
before do
|
36
|
-
@
|
42
|
+
@original_data_migrations_path = DataMigrate.config.data_template_path
|
43
|
+
|
37
44
|
DataMigrate.configure do |config|
|
38
45
|
config.data_template_path = data_template_path
|
39
46
|
end
|
40
47
|
end
|
41
48
|
|
42
|
-
let(:data_template_path) do
|
43
|
-
File.join(DataMigrate.root, "generators", "data_migration", "templates", "data_migration.rb")
|
44
|
-
end
|
45
|
-
|
46
49
|
after do
|
47
50
|
DataMigrate.configure do |config|
|
48
|
-
config.data_template_path = @
|
51
|
+
config.data_template_path = @original_data_migrations_path
|
49
52
|
end
|
50
53
|
end
|
51
54
|
|