data_migrate 9.1.1 → 9.3.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 +2 -1
- data/.gitignore +0 -1
- data/Appraisals +5 -5
- data/Changelog.md +7 -0
- data/Gemfile +0 -1
- data/Gemfile.lock +184 -0
- data/README.md +3 -3
- data/data_migrate.gemspec +2 -2
- data/gemfiles/rails_6.1.gemfile +0 -1
- data/gemfiles/rails_6.1.gemfile.lock +229 -0
- data/gemfiles/rails_7.0.gemfile +1 -2
- data/gemfiles/rails_7.0.gemfile.lock +230 -0
- data/gemfiles/{rails_6.0.gemfile → rails_7.1.gemfile} +1 -2
- data/gemfiles/rails_7.1.gemfile.lock +263 -0
- data/lib/data_migrate/data_migrator.rb +11 -23
- data/lib/data_migrate/data_schema.rb +1 -1
- data/lib/data_migrate/data_schema_migration.rb +24 -7
- data/lib/data_migrate/database_configurations_wrapper.rb +11 -0
- data/lib/data_migrate/database_tasks.rb +120 -63
- 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.rb +5 -4
- data/lib/data_migrate/status_service.rb +3 -3
- data/lib/data_migrate/tasks/data_migrate_tasks.rb +3 -3
- data/lib/data_migrate/test.rb +14 -0
- data/lib/data_migrate/version.rb +1 -1
- data/lib/data_migrate.rb +2 -1
- data/spec/data_migrate/data_migrator_spec.rb +16 -13
- data/spec/data_migrate/data_schema_migration_spec.rb +25 -8
- data/spec/data_migrate/data_spec.rb +1 -1
- data/spec/data_migrate/database_tasks_spec.rb +34 -18
- data/spec/data_migrate/migration_context_spec.rb +15 -8
- data/spec/data_migrate/schema_dumper_spec.rb +6 -3
- data/spec/data_migrate/schema_migration_spec.rb +13 -6
- data/spec/data_migrate/status_service_spec.rb +6 -3
- data/spec/data_migrate/tasks/data_migrate_tasks_spec.rb +8 -8
- 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 +21 -38
- metadata +19 -15
- data/Gemfile.rails6.1 +0 -11
- data/lib/data_migrate/legacy_migrator.rb +0 -22
- data/spec/data_migrate/legacy_migrator_spec.rb +0 -38
@@ -5,87 +5,142 @@ require "data_migrate/config"
|
|
5
5
|
module DataMigrate
|
6
6
|
##
|
7
7
|
# This class extends DatabaseTasks to add a schema_file method.
|
8
|
-
|
8
|
+
module DatabaseTasks
|
9
9
|
extend ActiveRecord::Tasks::DatabaseTasks
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
end
|
19
|
-
|
20
|
-
# This method is removed in Rails 7.0
|
21
|
-
def dump_filename(spec_name, format = ActiveRecord::Base.schema_format)
|
22
|
-
filename = if spec_name == "primary"
|
23
|
-
schema_file_type(format)
|
10
|
+
extend self
|
11
|
+
|
12
|
+
# These method are only introduced in Rails 7.1
|
13
|
+
unless respond_to?(:with_temporary_connection_for_each)
|
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)
|
24
18
|
else
|
25
|
-
|
19
|
+
ActiveRecord::Base.configurations.configs_for(env_name: env, name: name).each do |db_config|
|
20
|
+
with_temporary_connection(db_config, &block)
|
21
|
+
end
|
26
22
|
end
|
27
|
-
|
28
|
-
ENV["DATA_SCHEMA"] || File.join(db_dir, filename)
|
29
23
|
end
|
30
24
|
|
31
|
-
def
|
32
|
-
|
33
|
-
|
34
|
-
Kernel.abort message
|
25
|
+
def with_temporary_connection(db_config) # :nodoc:
|
26
|
+
with_temporary_pool(db_config) do |pool|
|
27
|
+
yield pool.connection
|
35
28
|
end
|
36
29
|
end
|
37
30
|
|
38
|
-
def
|
39
|
-
|
40
|
-
pending_schema_migrations,
|
41
|
-
pending_data_migrations
|
42
|
-
)
|
31
|
+
def migration_class # :nodoc:
|
32
|
+
ActiveRecord::Base
|
43
33
|
end
|
44
34
|
|
45
|
-
def
|
46
|
-
|
35
|
+
def migration_connection # :nodoc:
|
36
|
+
migration_class.connection
|
47
37
|
end
|
48
38
|
|
49
|
-
def
|
50
|
-
|
51
|
-
|
39
|
+
private def with_temporary_pool(db_config)
|
40
|
+
original_db_config = migration_class.connection_db_config
|
41
|
+
pool = migration_class.connection_handler.establish_connection(db_config)
|
52
42
|
|
53
|
-
|
54
|
-
|
43
|
+
yield pool
|
44
|
+
ensure
|
45
|
+
migration_class.connection_handler.establish_connection(original_db_config)
|
55
46
|
end
|
47
|
+
end
|
56
48
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
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
|
+
|
58
|
+
versions_to_run.each do |version|
|
59
|
+
next if target_version && target_version != version
|
60
|
+
db_configs_with_versions[version] << DatabaseConfigurationWrapper.new(db_config)
|
61
|
+
end
|
68
62
|
end
|
69
63
|
end
|
70
64
|
|
71
|
-
|
72
|
-
|
65
|
+
db_configs_with_versions
|
66
|
+
end
|
73
67
|
|
74
|
-
|
75
|
-
|
68
|
+
def schema_file(_format = nil)
|
69
|
+
File.join(db_dir, "data_schema.rb")
|
70
|
+
end
|
76
71
|
|
77
|
-
|
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)}"
|
78
82
|
end
|
79
83
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
84
|
+
ENV["DATA_SCHEMA"] || File.join(db_dir, filename)
|
85
|
+
end
|
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
|
85
91
|
end
|
86
92
|
end
|
87
93
|
|
88
|
-
def
|
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
|
+
|
109
|
+
def data_migrations_path
|
110
|
+
::DataMigrate.config.data_migrations_path
|
111
|
+
end
|
112
|
+
|
113
|
+
def run_migration(migration, direction)
|
114
|
+
if migration[:kind] == :data
|
115
|
+
::ActiveRecord::Migration.write("== %s %s" % ['Data', "=" * 71])
|
116
|
+
::DataMigrate::DataMigrator.run(direction, data_migrations_path, migration[:version])
|
117
|
+
else
|
118
|
+
::ActiveRecord::Migration.write("== %s %s" % ['Schema', "=" * 69])
|
119
|
+
::DataMigrate::SchemaMigration.run(
|
120
|
+
direction,
|
121
|
+
::DataMigrate::SchemaMigration.migrations_paths,
|
122
|
+
migration[:version]
|
123
|
+
)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def schema_dump_path(db_config, format = ActiveRecord.schema_format)
|
128
|
+
return ENV["DATA_SCHEMA"] if ENV["DATA_SCHEMA"]
|
129
|
+
|
130
|
+
# We only require a schema.rb file for the primary database
|
131
|
+
return unless db_config.primary?
|
132
|
+
|
133
|
+
File.join(File.dirname(ActiveRecord::Tasks::DatabaseTasks.schema_dump_path(db_config, format)), schema_file_type)
|
134
|
+
end
|
135
|
+
|
136
|
+
# Override this method from `ActiveRecord::Tasks::DatabaseTasks`
|
137
|
+
# to ensure that the sha saved in ar_internal_metadata table
|
138
|
+
# is from the original schema.rb file
|
139
|
+
def schema_sha1(file)
|
140
|
+
ActiveRecord::Tasks::DatabaseTasks.schema_dump_path(ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env, name: "primary"))
|
141
|
+
end
|
142
|
+
|
143
|
+
def forward(step = 1)
|
89
144
|
DataMigrate::DataMigrator.create_data_schema_table
|
90
145
|
migrations = pending_migrations.reverse.pop(step).reverse
|
91
146
|
migrations.each do | pending_migration |
|
@@ -99,19 +154,21 @@ module DataMigrate
|
|
99
154
|
end
|
100
155
|
end
|
101
156
|
|
102
|
-
def
|
157
|
+
def pending_data_migrations
|
103
158
|
data_migrations = DataMigrate::DataMigrator.migrations(data_migrations_path)
|
104
|
-
|
105
|
-
|
159
|
+
data_migrator = DataMigrate::RailsHelper.data_migrator(:up, data_migrations)
|
160
|
+
sort_migrations(
|
161
|
+
data_migrator.pending_migrations.map { |m| { version: m.version, name: m.name, kind: :data } }
|
162
|
+
)
|
106
163
|
end
|
107
164
|
|
108
|
-
def
|
165
|
+
def pending_schema_migrations
|
109
166
|
::DataMigrate::SchemaMigration.pending_schema_migrations
|
110
167
|
end
|
111
168
|
|
112
|
-
def
|
113
|
-
data_versions = DataMigrate::
|
114
|
-
schema_versions =
|
169
|
+
def past_migrations(sort = nil)
|
170
|
+
data_versions = DataMigrate::RailsHelper.data_schema_migration.table_exists? ? DataMigrate::RailsHelper.data_schema_migration.normalized_versions : []
|
171
|
+
schema_versions = DataMigrate::RailsHelper.schema_migration.normalized_versions
|
115
172
|
migrations = data_versions.map { |v| { version: v.to_i, kind: :data } } + schema_versions.map { |v| { version: v.to_i, kind: :schema } }
|
116
173
|
|
117
174
|
sort&.downcase == "asc" ? sort_migrations(migrations) : sort_migrations(migrations).reverse
|
@@ -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
|
@@ -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, DataMigrate::RailsHelper.schema_migration, DataMigrate::RailsHelper.internal_metadata).
|
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, DataMigrate::RailsHelper.schema_migration).run(direction, version)
|
18
18
|
end
|
19
19
|
|
20
20
|
def self.sort_migrations(set1, set2 = nil)
|
@@ -24,9 +24,10 @@ module DataMigrate
|
|
24
24
|
|
25
25
|
def self.migrations_paths
|
26
26
|
spec_name = DataMigrate.config.spec_name
|
27
|
-
|
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)
|
28
29
|
ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, name: spec_name).migrations_paths
|
29
|
-
elsif spec_name && Gem::Dependency.new("railties", "~> 6.
|
30
|
+
elsif spec_name && Gem::Dependency.new("railties", "~> 6.1").match?("railties", Gem.loaded_specs["railties"].version)
|
30
31
|
ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, spec_name: spec_name).migrations_paths
|
31
32
|
else
|
32
33
|
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::RailsHelper.data_schema_migration.table_name
|
28
28
|
end
|
29
29
|
|
30
30
|
def output(stream)
|
31
|
-
unless DataMigrate::
|
31
|
+
unless DataMigrate::RailsHelper.data_schema_migration.table_exists?
|
32
32
|
stream.puts "Data migrations table does not exist yet."
|
33
33
|
return
|
34
34
|
end
|
@@ -46,7 +46,7 @@ module DataMigrate
|
|
46
46
|
def database_name
|
47
47
|
if Gem::Dependency.new("railties", "~> 7.0").match?("railties", Gem.loaded_specs["railties"].version)
|
48
48
|
ActiveRecord::Base.connection_db_config.configuration_hash[:database]
|
49
|
-
elsif Gem::Dependency.new("railties", "~> 6.
|
49
|
+
elsif Gem::Dependency.new("railties", "~> 6.1").match?("railties", Gem.loaded_specs["railties"].version)
|
50
50
|
ActiveRecord::Base.connection_config[:database]
|
51
51
|
end
|
52
52
|
end
|
@@ -50,9 +50,9 @@ module DataMigrate
|
|
50
50
|
|
51
51
|
def status_with_schema
|
52
52
|
db_list_data = ActiveRecord::Base.connection.select_values(
|
53
|
-
"SELECT version FROM #{DataMigrate::
|
53
|
+
"SELECT version FROM #{DataMigrate::RailsHelper.data_schema_migration.table_name}"
|
54
54
|
)
|
55
|
-
db_list_schema =
|
55
|
+
db_list_schema = DataMigrate::RailsHelper.schema_migration_versions
|
56
56
|
file_list = []
|
57
57
|
|
58
58
|
Dir.foreach(File.join(Rails.root, migrations_paths)) do |file|
|
@@ -96,7 +96,7 @@ module DataMigrate
|
|
96
96
|
def database_name
|
97
97
|
if Gem::Dependency.new("railties", "~> 7.0").match?("railties", Gem.loaded_specs["railties"].version)
|
98
98
|
ActiveRecord::Base.connection_db_config.database
|
99
|
-
elsif Gem::Dependency.new("railties", "~> 6.
|
99
|
+
elsif Gem::Dependency.new("railties", "~> 6.1").match?("railties", Gem.loaded_specs["railties"].version)
|
100
100
|
ActiveRecord::Base.connection_config[:database]
|
101
101
|
end
|
102
102
|
end
|
data/lib/data_migrate/version.rb
CHANGED
data/lib/data_migrate.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require File.join(File.dirname(__FILE__), "data_migrate", "rails_helper")
|
3
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")
|
@@ -9,9 +10,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
14
|
require File.join(File.dirname(__FILE__), "data_migrate", "schema_migration")
|
15
|
+
require File.join(File.dirname(__FILE__), "data_migrate", "database_configurations_wrapper")
|
15
16
|
|
16
17
|
module DataMigrate
|
17
18
|
def self.root
|
@@ -3,7 +3,8 @@
|
|
3
3
|
require "spec_helper"
|
4
4
|
|
5
5
|
describe DataMigrate::DataMigrator do
|
6
|
-
let(:
|
6
|
+
let(:described_class) { DataMigrate::DataMigrator }
|
7
|
+
|
7
8
|
let(:db_config) do
|
8
9
|
{
|
9
10
|
adapter: "sqlite3",
|
@@ -13,8 +14,8 @@ describe DataMigrate::DataMigrator do
|
|
13
14
|
|
14
15
|
before do
|
15
16
|
ActiveRecord::Base.establish_connection(db_config)
|
16
|
-
::
|
17
|
-
DataMigrate::
|
17
|
+
DataMigrate::RailsHelper.schema_migration.create_table
|
18
|
+
DataMigrate::RailsHelper.data_schema_migration.create_table
|
18
19
|
end
|
19
20
|
|
20
21
|
after do
|
@@ -23,12 +24,14 @@ describe DataMigrate::DataMigrator do
|
|
23
24
|
end
|
24
25
|
|
25
26
|
describe ".load_migrated" do
|
27
|
+
let(:migrator) { DataMigrate::RailsHelper.data_migrator(:up, []) }
|
28
|
+
|
26
29
|
it "loads migrated versions" do
|
27
|
-
DataMigrate::
|
28
|
-
::
|
29
|
-
DataMigrate::
|
30
|
-
::
|
31
|
-
migrated =
|
30
|
+
DataMigrate::RailsHelper.data_schema_migration.create_version(20090000000000)
|
31
|
+
DataMigrate::RailsHelper.schema_create_version(20100000000000)
|
32
|
+
DataMigrate::RailsHelper.data_schema_migration.create_version(20110000000000)
|
33
|
+
DataMigrate::RailsHelper.schema_create_version(20120000000000)
|
34
|
+
migrated = migrator.load_migrated
|
32
35
|
expect(migrated.count).to eq 2
|
33
36
|
expect(migrated).to include 20090000000000
|
34
37
|
expect(migrated).to include 20110000000000
|
@@ -38,7 +41,7 @@ describe DataMigrate::DataMigrator do
|
|
38
41
|
describe :create_data_schema_table do
|
39
42
|
it "creates the data_migrations table" do
|
40
43
|
ActiveRecord::Migration.drop_table("data_migrations") rescue nil
|
41
|
-
|
44
|
+
described_class.create_data_schema_table
|
42
45
|
expect(
|
43
46
|
ActiveRecord::Base.connection.table_exists?("data_migrations")
|
44
47
|
).to eq true
|
@@ -47,7 +50,7 @@ describe DataMigrate::DataMigrator do
|
|
47
50
|
|
48
51
|
describe "#migrations_status" do
|
49
52
|
it "returns all migrations statuses" do
|
50
|
-
status =
|
53
|
+
status = described_class.migrations_status
|
51
54
|
expect(status.length).to eq 2
|
52
55
|
expect(status.first).to eq ["down", "20091231235959", "Some name"]
|
53
56
|
expect(status.second).to eq ["down", "20171231235959", "Super update"]
|
@@ -57,19 +60,19 @@ describe DataMigrate::DataMigrator do
|
|
57
60
|
describe :match do
|
58
61
|
context "when the file does not match" do
|
59
62
|
it "returns nil" do
|
60
|
-
expect(
|
63
|
+
expect(described_class.match("not_a_data_migration_file")).to be_nil
|
61
64
|
end
|
62
65
|
end
|
63
66
|
|
64
67
|
context "when the file doesn't end in .rb" do
|
65
68
|
it "returns nil" do
|
66
|
-
expect(
|
69
|
+
expect(described_class.match("20091231235959_some_name.rb.un~")).to be_nil
|
67
70
|
end
|
68
71
|
end
|
69
72
|
|
70
73
|
context "when the file matches" do
|
71
74
|
it "returns a valid MatchData object" do
|
72
|
-
match_data =
|
75
|
+
match_data = described_class.match("20091231235959_some_name.rb")
|
73
76
|
|
74
77
|
expect(match_data[0]).to eq "20091231235959_some_name.rb"
|
75
78
|
expect(match_data[1]).to eq "20091231235959"
|
@@ -1,16 +1,33 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe DataMigrate::DataSchemaMigration do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
if DataMigrate::RailsHelper.rails_version_equal_to_or_higher_than_7_1
|
5
|
+
let(:connection) { double(:connection) }
|
6
|
+
let(:subject) { DataMigrate::DataSchemaMigration.new(connection) }
|
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
|
8
25
|
end
|
9
|
-
end
|
10
26
|
|
11
|
-
|
12
|
-
|
13
|
-
|
27
|
+
describe :index_name do
|
28
|
+
it "returns correct primary key name" do
|
29
|
+
expect(subject.primary_key).to eq("version")
|
30
|
+
end
|
14
31
|
end
|
15
32
|
end
|
16
33
|
end
|