data_migrate 7.0.2 → 8.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -1
- data/.travis.yml +5 -7
- data/Appraisals +4 -8
- data/Changelog.md +8 -0
- data/Gemfile +0 -7
- data/README.md +1 -1
- data/gemfiles/{rails_5.1.gemfile → rails_7.0.gemfile} +1 -1
- data/lib/data_migrate/database_tasks.rb +14 -0
- data/lib/data_migrate/schema_dumper.rb +1 -1
- data/lib/data_migrate/status_service_five.rb +6 -2
- data/lib/data_migrate/tasks/data_migrate_tasks.rb +93 -8
- data/lib/data_migrate/version.rb +1 -1
- data/lib/data_migrate.rb +10 -32
- data/lib/generators/data_migration/data_migration_generator.rb +1 -7
- data/spec/data_migrate/database_tasks_spec.rb +4 -9
- data/spec/data_migrate/migration_context_spec.rb +6 -14
- data/spec/data_migrate/schema_migration_spec.rb +21 -22
- data/spec/data_migrate/tasks/data_migrate_tasks_spec.rb +46 -0
- data/spec/generators/data_migration/data_migration_generator_spec.rb +7 -7
- data/spec/spec_helper.rb +3 -13
- data/tasks/databases.rake +2 -63
- metadata +4 -20
- data/Gemfile.rails5 +0 -10
- data/Gemfile.rails5.1 +0 -10
- data/gemfiles/rails_4.1.gemfile +0 -7
- data/gemfiles/rails_4.2.gemfile +0 -9
- data/gemfiles/rails_5.0.gemfile +0 -8
- data/lib/data_migrate/data_migrator.rb +0 -101
- data/lib/data_migrate/migration.rb +0 -26
- data/lib/data_migrate/migration_five.rb +0 -26
- data/lib/data_migrate/schema_migration.rb +0 -31
- data/lib/data_migrate/status_service.rb +0 -65
- data/spec/db/5.0/20091231235959_some_name.rb +0 -9
- data/spec/db/5.0/20171231235959_super_update.rb +0 -9
- data/spec/db/migrate/4.2/20131111111111_late_migration.rb +0 -9
- data/spec/db/migrate/4.2/20202020202011_db_migration.rb +0 -9
- data/spec/db/migrate/5.0/20131111111111_late_migration.rb +0 -9
- data/spec/db/migrate/5.0/20202020202011_db_migration.rb +0 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 86bd9c5c658d19081e2f3832e2637197c53ba841fb779a0424dae6b6d87c6685
|
4
|
+
data.tar.gz: 8698b30801c19530248088d6521de33c1688f548e7ce8b218ccd75ca4058bffb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4303810721d723a76494aee931ca4026b20590f762d9600f056cafd2e8af3e955067f56c0ecdea271f53aa0c1f82eb1f803078e20dd3483ec2a311a3cb5a2d55
|
7
|
+
data.tar.gz: ebee6c3e420f9d272daf4f428cdd71a24f9453fb1b845aee783b1348a45ce18a6539a6d60ce40885f39c8180035c940c71be7a685988fba1780445a8df1bc287
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
2.7.5
|
data/.travis.yml
CHANGED
@@ -1,16 +1,14 @@
|
|
1
1
|
language: ruby
|
2
2
|
rvm:
|
3
|
-
- 2.6
|
4
3
|
- 2.7
|
4
|
+
- 3.0
|
5
5
|
script: bundle exec rspec
|
6
6
|
gemfile:
|
7
|
-
- gemfiles/rails_5.1.gemfile
|
8
7
|
- gemfiles/rails_5.2.gemfile
|
9
8
|
- gemfiles/rails_6.0.gemfile
|
10
9
|
- gemfiles/rails_6.1.gemfile
|
11
|
-
|
10
|
+
- gemfiles/rails_7.0.gemfile
|
11
|
+
jobs:
|
12
12
|
exclude:
|
13
|
-
- rvm:
|
14
|
-
gemfile: gemfiles/
|
15
|
-
- rvm: 2.4.4
|
16
|
-
gemfile: gemfiles/rails_6.1.gemfile
|
13
|
+
- rvm: 3.0
|
14
|
+
gemfile: gemfiles/rails_5.2.gemfile
|
data/Appraisals
CHANGED
@@ -1,11 +1,3 @@
|
|
1
|
-
appraise 'rails-5.0' do
|
2
|
-
gem 'rails', '5.0.7.2'
|
3
|
-
end
|
4
|
-
|
5
|
-
appraise 'rails-5.1' do
|
6
|
-
gem 'rails', '5.1.7'
|
7
|
-
end
|
8
|
-
|
9
1
|
appraise 'rails-5.2' do
|
10
2
|
gem 'rails', '~> 5.2.3'
|
11
3
|
end
|
@@ -17,3 +9,7 @@ end
|
|
17
9
|
appraise 'rails-6.1' do
|
18
10
|
gem 'rails', '~> 6.1.0'
|
19
11
|
end
|
12
|
+
|
13
|
+
appraise 'rails-7.0' do
|
14
|
+
gem 'rails', '~> 7.0'
|
15
|
+
end
|
data/Changelog.md
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## 8.0.0.rc2
|
4
|
+
|
5
|
+
Bug fixes [gdott9](https://github.com/gdott9)
|
6
|
+
|
7
|
+
## 8.0.0.rc1
|
8
|
+
Add support for Rails 7
|
9
|
+
Removed support for Rails versions below 5.2. Now are supported only versions Rails 5.2 and up
|
10
|
+
|
3
11
|
## 7.0.2
|
4
12
|
|
5
13
|
Remove magic comment in migration files [y-yagi](https://github.com/y-yagi)
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -23,6 +23,20 @@ module DataMigrate
|
|
23
23
|
ENV["DATA_SCHEMA"] || File.join(schema_location, filename)
|
24
24
|
end
|
25
25
|
|
26
|
+
def schema_dump_path(db_config, format = ActiveRecord.schema_format)
|
27
|
+
return ENV["DATA_SCHEMA"] if ENV["DATA_SCHEMA"]
|
28
|
+
|
29
|
+
filename = if db_config.primary?
|
30
|
+
schema_file_type(format)
|
31
|
+
else
|
32
|
+
[db_config.name, schema_file_type(format)].join("_")
|
33
|
+
end
|
34
|
+
|
35
|
+
return unless filename
|
36
|
+
|
37
|
+
File.dirname(filename) == schema_location ? filename : File.join(schema_location, filename)
|
38
|
+
end
|
39
|
+
|
26
40
|
def schema_location
|
27
41
|
db_dir
|
28
42
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module DataMigrate
|
2
2
|
class StatusService
|
3
3
|
class << self
|
4
|
-
def dump(connection = ActiveRecord::Base.connection, stream =
|
4
|
+
def dump(connection = ActiveRecord::Base.connection, stream = $stdout)
|
5
5
|
new(connection).dump(stream)
|
6
6
|
stream
|
7
7
|
end
|
@@ -32,7 +32,11 @@ module DataMigrate
|
|
32
32
|
end
|
33
33
|
|
34
34
|
# output
|
35
|
-
|
35
|
+
if ActiveRecord.version >= Gem::Version.new('7.0')
|
36
|
+
stream.puts "\ndatabase: #{ActiveRecord::Base.connection_db_config.configuration_hash[:database]}\n\n"
|
37
|
+
else
|
38
|
+
stream.puts "\ndatabase: #{ActiveRecord::Base.connection_config[:database]}\n\n"
|
39
|
+
end
|
36
40
|
stream.puts "#{'Status'.center(8)} #{'Migration ID'.ljust(14)} Migration Name"
|
37
41
|
stream.puts "-" * 50
|
38
42
|
db_list.each do |status, version, name|
|
@@ -2,12 +2,17 @@ module DataMigrate
|
|
2
2
|
module Tasks
|
3
3
|
module DataMigrateTasks
|
4
4
|
extend self
|
5
|
+
|
6
|
+
def schema_migrations_path
|
7
|
+
File.join('db', 'migrate')
|
8
|
+
end
|
9
|
+
|
5
10
|
def migrations_paths
|
6
11
|
@migrations_paths ||= DataMigrate.config.data_migrations_path
|
7
12
|
end
|
8
13
|
|
9
14
|
def dump
|
10
|
-
if
|
15
|
+
if dump_schema_after_migration?
|
11
16
|
filename = DataMigrate::DatabaseTasks.schema_file
|
12
17
|
ActiveRecord::Base.establish_connection(DataMigrate.config.db_configuration) if DataMigrate.config.db_configuration
|
13
18
|
File.open(filename, "w:utf-8") do |file|
|
@@ -17,14 +22,10 @@ module DataMigrate
|
|
17
22
|
end
|
18
23
|
|
19
24
|
def migrate
|
20
|
-
DataMigrate::DataMigrator.assure_data_schema_table
|
21
25
|
target_version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
else
|
26
|
-
DataMigrate::DataMigrator.migrate(migrations_paths, ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
|
27
|
-
end
|
26
|
+
|
27
|
+
DataMigrate::DataMigrator.assure_data_schema_table
|
28
|
+
DataMigrate::MigrationContext.new(migrations_paths).migrate(target_version)
|
28
29
|
end
|
29
30
|
|
30
31
|
def abort_if_pending_migrations(migrations, message)
|
@@ -36,6 +37,90 @@ module DataMigrate
|
|
36
37
|
abort message
|
37
38
|
end
|
38
39
|
end
|
40
|
+
|
41
|
+
def dump_schema_after_migration?
|
42
|
+
if ActiveRecord.respond_to?(:dump_schema_after_migration)
|
43
|
+
ActiveRecord.dump_schema_after_migration
|
44
|
+
else
|
45
|
+
ActiveRecord::Base.dump_schema_after_migration
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def status
|
50
|
+
config = connect_to_database
|
51
|
+
return unless config
|
52
|
+
|
53
|
+
connection = ActiveRecord::Base.connection
|
54
|
+
puts "\ndatabase: #{config['database']}\n\n"
|
55
|
+
DataMigrate::StatusService.dump(connection)
|
56
|
+
end
|
57
|
+
|
58
|
+
def status_with_schema
|
59
|
+
config = connect_to_database
|
60
|
+
return unless config
|
61
|
+
|
62
|
+
db_list_data = ActiveRecord::Base.connection.select_values(
|
63
|
+
"SELECT version FROM #{DataMigrate::DataSchemaMigration.table_name}"
|
64
|
+
)
|
65
|
+
db_list_schema = ActiveRecord::Base.connection.select_values(
|
66
|
+
"SELECT version FROM #{ActiveRecord::SchemaMigration.schema_migrations_table_name}"
|
67
|
+
)
|
68
|
+
file_list = []
|
69
|
+
|
70
|
+
Dir.foreach(File.join(Rails.root, migrations_paths)) do |file|
|
71
|
+
# only files matching "20091231235959_some_name.rb" pattern
|
72
|
+
if match_data = /(\d{14})_(.+)\.rb/.match(file)
|
73
|
+
status = db_list_data.delete(match_data[1]) ? 'up' : 'down'
|
74
|
+
file_list << [status, match_data[1], match_data[2], 'data']
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
Dir.foreach(File.join(Rails.root, schema_migrations_path)) do |file|
|
79
|
+
# only files matching "20091231235959_some_name.rb" pattern
|
80
|
+
if match_data = /(\d{14})_(.+)\.rb/.match(file)
|
81
|
+
status = db_list_schema.delete(match_data[1]) ? 'up' : 'down'
|
82
|
+
file_list << [status, match_data[1], match_data[2], 'schema']
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
file_list.sort!{|a,b| "#{a[1]}_#{a[3] == 'data' ? 1 : 0}" <=> "#{b[1]}_#{b[3] == 'data' ? 1 : 0}" }
|
87
|
+
|
88
|
+
# output
|
89
|
+
puts "\ndatabase: #{config['database']}\n\n"
|
90
|
+
puts "#{"Status".center(8)} #{"Type".center(8)} #{"Migration ID".ljust(14)} Migration Name"
|
91
|
+
puts "-" * 60
|
92
|
+
file_list.each do |file|
|
93
|
+
puts "#{file[0].center(8)} #{file[3].center(8)} #{file[1].ljust(14)} #{file[2].humanize}"
|
94
|
+
end
|
95
|
+
db_list_schema.each do |version|
|
96
|
+
puts "#{'up'.center(8)} #{version.ljust(14)} *** NO SCHEMA FILE ***"
|
97
|
+
end
|
98
|
+
db_list_data.each do |version|
|
99
|
+
puts "#{'up'.center(8)} #{version.ljust(14)} *** NO DATA FILE ***"
|
100
|
+
end
|
101
|
+
puts
|
102
|
+
end
|
103
|
+
|
104
|
+
private
|
105
|
+
|
106
|
+
def connect_to_database
|
107
|
+
config = if ActiveRecord.version < Gem::Version.new('6.1')
|
108
|
+
ActiveRecord::Base.configurations[Rails.env || 'development']
|
109
|
+
else
|
110
|
+
ActiveRecord::Base.configurations.find_db_config(Rails.env || 'development').configuration_hash
|
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
|
121
|
+
end
|
122
|
+
config
|
123
|
+
end
|
39
124
|
end
|
40
125
|
end
|
41
126
|
end
|
data/lib/data_migrate/version.rb
CHANGED
data/lib/data_migrate.rb
CHANGED
@@ -1,44 +1,22 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
elsif Rails::VERSION::MAJOR == 5 && Rails::VERSION::MINOR == 2
|
6
|
-
require File.join(File.dirname(__FILE__), "data_migrate", "data_migrator_five")
|
7
|
-
else
|
8
|
-
require File.join(File.dirname(__FILE__), "data_migrate", "data_migrator")
|
9
|
-
end
|
10
|
-
require File.join(File.dirname(__FILE__), "data_migrate",
|
11
|
-
"data_schema_migration")
|
3
|
+
require File.join(File.dirname(__FILE__), "data_migrate", "data_migrator_five")
|
4
|
+
require File.join(File.dirname(__FILE__), "data_migrate", "data_schema_migration")
|
12
5
|
require File.join(File.dirname(__FILE__), "data_migrate", "data_schema")
|
13
6
|
require File.join(File.dirname(__FILE__), "data_migrate", "database_tasks")
|
14
7
|
require File.join(File.dirname(__FILE__), "data_migrate", "schema_dumper")
|
15
|
-
|
16
|
-
|
17
|
-
require File.join(File.dirname(__FILE__), "data_migrate", "schema_migration_six")
|
18
|
-
elsif Rails::VERSION::MAJOR == 5 && Rails::VERSION::MINOR == 2
|
19
|
-
require File.join(File.dirname(__FILE__), "data_migrate", "status_service_five")
|
20
|
-
require File.join(File.dirname(__FILE__), "data_migrate", "schema_migration_five")
|
21
|
-
else
|
22
|
-
require File.join(File.dirname(__FILE__), "data_migrate", "status_service")
|
23
|
-
require File.join(File.dirname(__FILE__), "data_migrate", "schema_migration")
|
24
|
-
end
|
25
|
-
|
26
|
-
if Rails::VERSION::MAJOR == 6
|
27
|
-
require File.join(File.dirname(__FILE__), "data_migrate", "migration_context")
|
28
|
-
# require File.join(File.dirname(__FILE__), "data_migrate", "migration_five")
|
29
|
-
elsif Rails::VERSION::MAJOR == 5
|
30
|
-
if Rails::VERSION::MINOR == 2
|
31
|
-
require File.join(File.dirname(__FILE__), "data_migrate", "migration_context")
|
32
|
-
else
|
33
|
-
require File.join(File.dirname(__FILE__), "data_migrate", "migration_five")
|
34
|
-
end
|
35
|
-
else
|
36
|
-
require File.join(File.dirname(__FILE__), "data_migrate", "migration")
|
37
|
-
end
|
8
|
+
require File.join(File.dirname(__FILE__), "data_migrate", "status_service_five")
|
9
|
+
require File.join(File.dirname(__FILE__), "data_migrate", "migration_context")
|
38
10
|
require File.join(File.dirname(__FILE__), "data_migrate", "railtie")
|
39
11
|
require File.join(File.dirname(__FILE__), "data_migrate", "tasks/data_migrate_tasks")
|
40
12
|
require File.join(File.dirname(__FILE__), "data_migrate", "legacy_migrator")
|
41
13
|
require File.join(File.dirname(__FILE__), "data_migrate", "config")
|
42
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
|
20
|
+
|
43
21
|
module DataMigrate
|
44
22
|
end
|
@@ -27,13 +27,7 @@ module DataMigrate
|
|
27
27
|
end
|
28
28
|
|
29
29
|
def migration_base_class_name
|
30
|
-
|
31
|
-
"ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]"
|
32
|
-
elsif ActiveRecord.version >= Gem::Version.new("5.2")
|
33
|
-
"DataMigrate::MigrationContext"
|
34
|
-
else
|
35
|
-
"ActiveRecord::Migration"
|
36
|
-
end
|
30
|
+
"ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]"
|
37
31
|
end
|
38
32
|
|
39
33
|
def data_migrations_file_path
|
@@ -5,16 +5,10 @@ require "spec_helper"
|
|
5
5
|
describe DataMigrate::DatabaseTasks do
|
6
6
|
let(:subject) { DataMigrate::DatabaseTasks }
|
7
7
|
let(:migration_path) {
|
8
|
-
if Rails::VERSION::MAJOR ==
|
9
|
-
"spec/db/migrate/
|
10
|
-
elsif Rails::VERSION::MAJOR == 5
|
11
|
-
if Rails::VERSION::MINOR == 2
|
12
|
-
"spec/db/migrate/5.2"
|
13
|
-
else
|
14
|
-
"spec/db/migrate/5.0"
|
15
|
-
end
|
8
|
+
if Rails::VERSION::MAJOR == 5
|
9
|
+
"spec/db/migrate/5.2"
|
16
10
|
else
|
17
|
-
"spec/db/migrate/
|
11
|
+
"spec/db/migrate/6.0"
|
18
12
|
end
|
19
13
|
}
|
20
14
|
let(:data_migrations_path) {
|
@@ -32,6 +26,7 @@ describe DataMigrate::DatabaseTasks do
|
|
32
26
|
# Rails.application.config.paths["db"].first
|
33
27
|
# @see https://github.com/rails/rails/blob/a7d49ef78c36df2d1ca876451f30915ada1079a5/activerecord/lib/active_record/tasks/database_tasks.rb#L54
|
34
28
|
allow(subject).to receive(:db_dir).and_return("db")
|
29
|
+
allow(ActiveRecord::Tasks::DatabaseTasks).to receive(:db_dir).and_return("db")
|
35
30
|
end
|
36
31
|
|
37
32
|
before do
|
@@ -2,28 +2,20 @@ require "spec_helper"
|
|
2
2
|
|
3
3
|
describe DataMigrate::DataMigrator do
|
4
4
|
let(:context) {
|
5
|
-
if (Rails::VERSION::MAJOR ==
|
6
|
-
DataMigrate::MigrationContext.new("spec/db/data-6.0")
|
7
|
-
else
|
5
|
+
if (Rails::VERSION::MAJOR == 5)
|
8
6
|
DataMigrate::MigrationContext.new("spec/db/data")
|
7
|
+
else
|
8
|
+
DataMigrate::MigrationContext.new("spec/db/data-6.0")
|
9
9
|
end
|
10
10
|
}
|
11
11
|
let(:schema_context) {
|
12
|
-
if (Rails::VERSION::MAJOR ==
|
13
|
-
ActiveRecord::MigrationContext.new("spec/db/migrate/6.0", ActiveRecord::Base.connection.schema_migration)
|
14
|
-
else
|
12
|
+
if (Rails::VERSION::MAJOR == 5)
|
15
13
|
ActiveRecord::MigrationContext.new("spec/db/migrate/5.2")
|
14
|
+
else
|
15
|
+
ActiveRecord::MigrationContext.new("spec/db/migrate/6.0", ActiveRecord::Base.connection.schema_migration)
|
16
16
|
end
|
17
17
|
}
|
18
18
|
|
19
|
-
before do
|
20
|
-
unless (Rails::VERSION::MAJOR == 5 and
|
21
|
-
Rails::VERSION::MINOR == 2) ||
|
22
|
-
Rails::VERSION::MAJOR == 6
|
23
|
-
skip("Tests are only applicable for Rails 5.2")
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
19
|
after do
|
28
20
|
begin
|
29
21
|
ActiveRecord::Migration.drop_table("data_migrations")
|
@@ -4,16 +4,10 @@ require "spec_helper"
|
|
4
4
|
|
5
5
|
describe DataMigrate::SchemaMigration do
|
6
6
|
let(:migration_path) {
|
7
|
-
if Rails::VERSION::MAJOR ==
|
8
|
-
"spec/db/migrate/
|
9
|
-
elsif Rails::VERSION::MAJOR == 5
|
10
|
-
if Rails::VERSION::MINOR == 2
|
11
|
-
"spec/db/migrate/5.2"
|
12
|
-
else
|
13
|
-
"spec/db/migrate/5.0"
|
14
|
-
end
|
7
|
+
if Rails::VERSION::MAJOR == 5
|
8
|
+
"spec/db/migrate/5.2"
|
15
9
|
else
|
16
|
-
"spec/db/migrate/
|
10
|
+
"spec/db/migrate/6.0"
|
17
11
|
end
|
18
12
|
}
|
19
13
|
|
@@ -72,22 +66,27 @@ describe DataMigrate::SchemaMigration do
|
|
72
66
|
if Rails.version > '6'
|
73
67
|
describe :migrations_paths do
|
74
68
|
context 'when a db_name is configured' do
|
69
|
+
let(:config) { double(:config) }
|
75
70
|
let(:paths) { ['spec/db/migrate/6.0', 'spec/db/components/migrate/6.0'] }
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
config.spec_name = 'primary'
|
82
|
-
end
|
71
|
+
let(:config_options) do
|
72
|
+
if Rails.version > '6.1'
|
73
|
+
{ env_name: 'test', name: 'primary' }
|
74
|
+
else
|
75
|
+
{ env_name: 'test', spec_name: 'primary' }
|
83
76
|
end
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
end
|
77
|
+
end
|
78
|
+
|
79
|
+
before do
|
80
|
+
DataMigrate.configure do |config|
|
81
|
+
config.spec_name = 'primary'
|
90
82
|
end
|
83
|
+
|
84
|
+
allow(ActiveRecord::Base.configurations)
|
85
|
+
.to receive(:configs_for)
|
86
|
+
.with(config_options)
|
87
|
+
.and_return(config)
|
88
|
+
|
89
|
+
allow(config).to receive(:migrations_paths).and_return(paths)
|
91
90
|
end
|
92
91
|
|
93
92
|
it 'lists schema migration paths' do
|
@@ -115,4 +115,50 @@ describe DataMigrate::Tasks::DataMigrateTasks do
|
|
115
115
|
end
|
116
116
|
end
|
117
117
|
end
|
118
|
+
|
119
|
+
describe :status do
|
120
|
+
let(:db_config) do
|
121
|
+
{
|
122
|
+
adapter: "sqlite3",
|
123
|
+
database: "spec/db/test.db"
|
124
|
+
}
|
125
|
+
end
|
126
|
+
|
127
|
+
before do
|
128
|
+
if Rails::VERSION::MAJOR == 5
|
129
|
+
ActiveRecord::Base.configurations['test'] = db_config
|
130
|
+
else
|
131
|
+
hash_config = ActiveRecord::DatabaseConfigurations::HashConfig.new('test', 'test', db_config)
|
132
|
+
config_obj = ActiveRecord::DatabaseConfigurations.new([hash_config])
|
133
|
+
allow(ActiveRecord::Base).to receive(:configurations).and_return(config_obj)
|
134
|
+
end
|
135
|
+
|
136
|
+
allow(Rails).to receive(:root) { '.' }
|
137
|
+
|
138
|
+
if Rails::VERSION::MAJOR == 5
|
139
|
+
allow(DataMigrate::Tasks::DataMigrateTasks).to receive(:schema_migrations_path) { 'spec/db/migrate/5.2' }
|
140
|
+
else
|
141
|
+
allow(DataMigrate::Tasks::DataMigrateTasks).to receive(:schema_migrations_path) { 'spec/db/migrate/6.0' }
|
142
|
+
end
|
143
|
+
|
144
|
+
DataMigrate::Tasks::DataMigrateTasks.migrate
|
145
|
+
end
|
146
|
+
|
147
|
+
after do
|
148
|
+
ActiveRecord::Migration.drop_table("data_migrations")
|
149
|
+
end
|
150
|
+
|
151
|
+
it "should display data migration status" do
|
152
|
+
expect {
|
153
|
+
DataMigrate::Tasks::DataMigrateTasks.status
|
154
|
+
}.to output(/up 20091231235959 Some name/).to_stdout
|
155
|
+
end
|
156
|
+
|
157
|
+
it "should display schema and data migration status" do
|
158
|
+
expect {
|
159
|
+
DataMigrate::Tasks::DataMigrateTasks.status_with_schema
|
160
|
+
}.to output(match(/up data 20091231235959 Some name/)
|
161
|
+
.and match(/down schema 20131111111111 Late migration/)).to_stdout
|
162
|
+
end
|
163
|
+
end
|
118
164
|
end
|
@@ -8,8 +8,12 @@ describe DataMigrate::Generators::DataMigrationGenerator do
|
|
8
8
|
describe :next_migration_number do
|
9
9
|
it "next migration" do
|
10
10
|
Timecop.freeze("2016-12-03 22:15:26 -0800") do
|
11
|
-
|
12
|
-
|
11
|
+
if ActiveRecord.version >= Gem::Version.new('7.0')
|
12
|
+
expect(ActiveRecord).to receive(:timestamped_migrations) { true }
|
13
|
+
else
|
14
|
+
expect(ActiveRecord::Base).to receive(:timestamped_migrations) { true }
|
15
|
+
end
|
16
|
+
expect(subject.next_migration_number(1).to_s).to eq("20161204061526")
|
13
17
|
end
|
14
18
|
end
|
15
19
|
end
|
@@ -17,11 +21,7 @@ describe DataMigrate::Generators::DataMigrationGenerator do
|
|
17
21
|
describe :migration_base_class_name do
|
18
22
|
let(:subject) { DataMigrate::Generators::DataMigrationGenerator.new(['my_migration']) }
|
19
23
|
it "returns the correct base class name" do
|
20
|
-
|
21
|
-
expect(subject.send(:migration_base_class_name)).to eq("ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]")
|
22
|
-
else
|
23
|
-
expect(subject.send(:migration_base_class_name)).to eq('ActiveRecord::Migration')
|
24
|
-
end
|
24
|
+
expect(subject.send(:migration_base_class_name)).to eq("ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]")
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
data/spec/spec_helper.rb
CHANGED
@@ -20,23 +20,13 @@ RSpec.configure do |config|
|
|
20
20
|
if example.metadata[:no_override]
|
21
21
|
else
|
22
22
|
@prev_data_migrations_path = DataMigrate.config.data_migrations_path
|
23
|
-
if Rails::VERSION::MAJOR ==
|
23
|
+
if Rails::VERSION::MAJOR == 5
|
24
24
|
DataMigrate.configure do |config|
|
25
|
-
config.data_migrations_path = "spec/db/
|
26
|
-
end
|
27
|
-
elsif Rails::VERSION::MAJOR == 5
|
28
|
-
if Rails::VERSION::MINOR == 2
|
29
|
-
DataMigrate.configure do |config|
|
30
|
-
config.data_migrations_path = "spec/db/data"
|
31
|
-
end
|
32
|
-
else
|
33
|
-
DataMigrate.configure do |config|
|
34
|
-
config.data_migrations_path = "spec/db/5.0"
|
35
|
-
end
|
25
|
+
config.data_migrations_path = "spec/db/data"
|
36
26
|
end
|
37
27
|
else
|
38
28
|
DataMigrate.configure do |config|
|
39
|
-
config.data_migrations_path = "spec/db/
|
29
|
+
config.data_migrations_path = "spec/db/6.0"
|
40
30
|
end
|
41
31
|
end
|
42
32
|
end
|
data/tasks/databases.rake
CHANGED
@@ -114,49 +114,7 @@ namespace :db do
|
|
114
114
|
namespace :status do
|
115
115
|
desc "Display status of data and schema migrations"
|
116
116
|
task :with_data => :environment do
|
117
|
-
|
118
|
-
next unless config
|
119
|
-
|
120
|
-
db_list_data = ActiveRecord::Base.connection.select_values(
|
121
|
-
"SELECT version FROM #{DataMigrate::DataSchemaMigration.table_name}"
|
122
|
-
)
|
123
|
-
db_list_schema = ActiveRecord::Base.connection.select_values(
|
124
|
-
"SELECT version FROM #{ActiveRecord::SchemaMigration.schema_migrations_table_name}"
|
125
|
-
)
|
126
|
-
file_list = []
|
127
|
-
|
128
|
-
Dir.foreach(File.join(Rails.root, data_migrations_path)) do |file|
|
129
|
-
# only files matching "20091231235959_some_name.rb" pattern
|
130
|
-
if match_data = /(\d{14})_(.+)\.rb/.match(file)
|
131
|
-
status = db_list_data.delete(match_data[1]) ? 'up' : 'down'
|
132
|
-
file_list << [status, match_data[1], match_data[2], 'data']
|
133
|
-
end
|
134
|
-
end
|
135
|
-
|
136
|
-
Dir.foreach(File.join(Rails.root, 'db', 'migrate')) do |file|
|
137
|
-
# only files matching "20091231235959_some_name.rb" pattern
|
138
|
-
if match_data = /(\d{14})_(.+)\.rb/.match(file)
|
139
|
-
status = db_list_schema.delete(match_data[1]) ? 'up' : 'down'
|
140
|
-
file_list << [status, match_data[1], match_data[2], 'schema']
|
141
|
-
end
|
142
|
-
end
|
143
|
-
|
144
|
-
file_list.sort!{|a,b| "#{a[1]}_#{a[3] == 'data' ? 1 : 0}" <=> "#{b[1]}_#{b[3] == 'data' ? 1 : 0}" }
|
145
|
-
|
146
|
-
# output
|
147
|
-
puts "\ndatabase: #{config['database']}\n\n"
|
148
|
-
puts "#{"Status".center(8)} #{"Type".center(8)} #{"Migration ID".ljust(14)} Migration Name"
|
149
|
-
puts "-" * 60
|
150
|
-
file_list.each do |file|
|
151
|
-
puts "#{file[0].center(8)} #{file[3].center(8)} #{file[1].ljust(14)} #{file[2].humanize}"
|
152
|
-
end
|
153
|
-
db_list_schema.each do |version|
|
154
|
-
puts "#{'up'.center(8)} #{version.ljust(14)} *** NO SCHEMA FILE ***"
|
155
|
-
end
|
156
|
-
db_list_data.each do |version|
|
157
|
-
puts "#{'up'.center(8)} #{version.ljust(14)} *** NO DATA FILE ***"
|
158
|
-
end
|
159
|
-
puts
|
117
|
+
DataMigrate::Tasks::DataMigrateTasks.status_with_schema
|
160
118
|
end
|
161
119
|
end
|
162
120
|
end # END OF MIGRATE NAME SPACE
|
@@ -272,11 +230,7 @@ namespace :data do
|
|
272
230
|
|
273
231
|
desc "Display status of data migrations"
|
274
232
|
task :status => :environment do
|
275
|
-
|
276
|
-
ActiveRecord::Base.establish_connection(config)
|
277
|
-
connection = ActiveRecord::Base.connection
|
278
|
-
puts "\ndatabase: #{config['database']}\n\n"
|
279
|
-
DataMigrate::StatusService.dump(connection)
|
233
|
+
DataMigrate::Tasks::DataMigrateTasks.status
|
280
234
|
end
|
281
235
|
end
|
282
236
|
|
@@ -358,21 +312,6 @@ def sort_string migration
|
|
358
312
|
"#{migration[:version]}_#{migration[:kind] == :data ? 1 : 0}"
|
359
313
|
end
|
360
314
|
|
361
|
-
def connect_to_database
|
362
|
-
config = ActiveRecord::Base.configurations[Rails.env || 'development']
|
363
|
-
ActiveRecord::Base.establish_connection(config)
|
364
|
-
|
365
|
-
unless DataMigrate::DataSchemaMigration.table_exists?
|
366
|
-
puts 'Data migrations table does not exist yet.'
|
367
|
-
config = nil
|
368
|
-
end
|
369
|
-
unless ActiveRecord::SchemaMigration.table_exists?
|
370
|
-
puts 'Schema migrations table does not exist yet.'
|
371
|
-
config = nil
|
372
|
-
end
|
373
|
-
config
|
374
|
-
end
|
375
|
-
|
376
315
|
def past_migrations(sort=nil)
|
377
316
|
DataMigrate::DatabaseTasks.past_migrations(sort)
|
378
317
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: data_migrate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 8.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew J Vargo
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2022-03-03 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activerecord
|
@@ -202,40 +202,30 @@ files:
|
|
202
202
|
- Appraisals
|
203
203
|
- Changelog.md
|
204
204
|
- Gemfile
|
205
|
-
- Gemfile.rails5
|
206
|
-
- Gemfile.rails5.1
|
207
205
|
- Gemfile.rails5.2
|
208
206
|
- Gemfile.rails6.1
|
209
207
|
- LICENSE
|
210
208
|
- README.md
|
211
209
|
- Rakefile
|
212
210
|
- data_migrate.gemspec
|
213
|
-
- gemfiles/rails_4.1.gemfile
|
214
|
-
- gemfiles/rails_4.2.gemfile
|
215
|
-
- gemfiles/rails_5.0.gemfile
|
216
|
-
- gemfiles/rails_5.1.gemfile
|
217
211
|
- gemfiles/rails_5.2.gemfile
|
218
212
|
- gemfiles/rails_6.0.gemfile
|
219
213
|
- gemfiles/rails_6.1.gemfile
|
214
|
+
- gemfiles/rails_7.0.gemfile
|
220
215
|
- lib/capistrano/data_migrate.rb
|
221
216
|
- lib/capistrano/data_migrate/migrate.rb
|
222
217
|
- lib/data_migrate.rb
|
223
218
|
- lib/data_migrate/config.rb
|
224
|
-
- lib/data_migrate/data_migrator.rb
|
225
219
|
- lib/data_migrate/data_migrator_five.rb
|
226
220
|
- lib/data_migrate/data_schema.rb
|
227
221
|
- lib/data_migrate/data_schema_migration.rb
|
228
222
|
- lib/data_migrate/database_tasks.rb
|
229
223
|
- lib/data_migrate/legacy_migrator.rb
|
230
|
-
- lib/data_migrate/migration.rb
|
231
224
|
- lib/data_migrate/migration_context.rb
|
232
|
-
- lib/data_migrate/migration_five.rb
|
233
225
|
- lib/data_migrate/railtie.rb
|
234
226
|
- lib/data_migrate/schema_dumper.rb
|
235
|
-
- lib/data_migrate/schema_migration.rb
|
236
227
|
- lib/data_migrate/schema_migration_five.rb
|
237
228
|
- lib/data_migrate/schema_migration_six.rb
|
238
|
-
- lib/data_migrate/status_service.rb
|
239
229
|
- lib/data_migrate/status_service_five.rb
|
240
230
|
- lib/data_migrate/tasks/data_migrate_tasks.rb
|
241
231
|
- lib/data_migrate/version.rb
|
@@ -256,8 +246,6 @@ files:
|
|
256
246
|
- spec/data_migrate/schema_migration_spec.rb
|
257
247
|
- spec/data_migrate/status_service_spec.rb
|
258
248
|
- spec/data_migrate/tasks/data_migrate_tasks_spec.rb
|
259
|
-
- spec/db/5.0/20091231235959_some_name.rb
|
260
|
-
- spec/db/5.0/20171231235959_super_update.rb
|
261
249
|
- spec/db/6.0/20091231235959_some_name.rb
|
262
250
|
- spec/db/6.0/20171231235959_super_update.rb
|
263
251
|
- spec/db/data-6.0/20091231235959_some_name.rb
|
@@ -270,10 +258,6 @@ files:
|
|
270
258
|
- spec/db/data/partial_schema/test_data_schema.rb
|
271
259
|
- spec/db/data/schema/data_schema.rb
|
272
260
|
- spec/db/data/schema/test_data_schema.rb
|
273
|
-
- spec/db/migrate/4.2/20131111111111_late_migration.rb
|
274
|
-
- spec/db/migrate/4.2/20202020202011_db_migration.rb
|
275
|
-
- spec/db/migrate/5.0/20131111111111_late_migration.rb
|
276
|
-
- spec/db/migrate/5.0/20202020202011_db_migration.rb
|
277
261
|
- spec/db/migrate/5.2/20131111111111_late_migration.rb
|
278
262
|
- spec/db/migrate/5.2/20202020202011_db_migration.rb
|
279
263
|
- spec/db/migrate/6.0/20131111111111_late_migration.rb
|
@@ -307,7 +291,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
307
291
|
- !ruby/object:Gem::Version
|
308
292
|
version: '0'
|
309
293
|
requirements: []
|
310
|
-
rubygems_version: 3.2.
|
294
|
+
rubygems_version: 3.2.32
|
311
295
|
signing_key:
|
312
296
|
specification_version: 4
|
313
297
|
summary: Rake tasks to migrate data alongside schema changes.
|
data/Gemfile.rails5
DELETED
data/Gemfile.rails5.1
DELETED
data/gemfiles/rails_4.1.gemfile
DELETED
data/gemfiles/rails_4.2.gemfile
DELETED
data/gemfiles/rails_5.0.gemfile
DELETED
@@ -1,101 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "active_record"
|
4
|
-
|
5
|
-
module DataMigrate
|
6
|
-
class DataMigrator < ActiveRecord::Migrator
|
7
|
-
|
8
|
-
def record_version_state_after_migrating(version)
|
9
|
-
if down?
|
10
|
-
migrated.delete(version)
|
11
|
-
DataMigrate::DataSchemaMigration.where(version: version.to_s).delete_all
|
12
|
-
else
|
13
|
-
migrated << version
|
14
|
-
DataMigrate::DataSchemaMigration.create!(version: version.to_s)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
def load_migrated(connection = ActiveRecord::Base.connection)
|
19
|
-
self.class.get_all_versions(connection)
|
20
|
-
end
|
21
|
-
|
22
|
-
class << self
|
23
|
-
alias_method :migrations_status_orig, :migrations_status
|
24
|
-
|
25
|
-
def migrations_status
|
26
|
-
migrations_status_orig([DataMigrate.config.data_migrations_path])
|
27
|
-
end
|
28
|
-
|
29
|
-
def current_version(connection = ActiveRecord::Base.connection)
|
30
|
-
get_all_versions(connection).max || 0
|
31
|
-
end
|
32
|
-
|
33
|
-
def get_all_versions(connection = ActiveRecord::Base.connection)
|
34
|
-
if table_exists?(connection, schema_migrations_table_name)
|
35
|
-
DataMigrate::DataSchemaMigration.all.map { |x| x.version.to_i }.sort
|
36
|
-
else
|
37
|
-
[]
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
def schema_migrations_table_name
|
42
|
-
ActiveRecord::Base.table_name_prefix + "data_migrations" +
|
43
|
-
ActiveRecord::Base.table_name_suffix
|
44
|
-
end
|
45
|
-
|
46
|
-
def migrations_path
|
47
|
-
DataMigrate.config.data_migrations_path
|
48
|
-
end
|
49
|
-
|
50
|
-
##
|
51
|
-
# Provides the full migrations_path filepath
|
52
|
-
# @return (String)
|
53
|
-
def full_migrations_path
|
54
|
-
File.join(Rails.root, *migrations_path.split(File::SEPARATOR))
|
55
|
-
end
|
56
|
-
|
57
|
-
def assure_data_schema_table
|
58
|
-
ActiveRecord::Base.establish_connection(db_config)
|
59
|
-
sm_table = DataMigrate::DataMigrator.schema_migrations_table_name
|
60
|
-
|
61
|
-
unless table_exists?(ActiveRecord::Base.connection, sm_table)
|
62
|
-
create_table(sm_table)
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
##
|
67
|
-
# Compares the given filename with what we expect data migration
|
68
|
-
# filenames to be, eg the "20091231235959_some_name.rb" pattern
|
69
|
-
# @param (String) filename
|
70
|
-
# @return (MatchData)
|
71
|
-
def match(filename)
|
72
|
-
/(\d{14})_(.+)\.rb$/.match(filename)
|
73
|
-
end
|
74
|
-
|
75
|
-
private
|
76
|
-
|
77
|
-
def create_table(sm_table)
|
78
|
-
ActiveRecord::Base.connection.create_table(sm_table, id: false) do |schema_migrations_table|
|
79
|
-
schema_migrations_table.string :version, primary_key: true
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
def table_exists?(connection, table_name)
|
84
|
-
# Avoid the warning that table_exists? prints in Rails 5.0 due a
|
85
|
-
# change in behavior between Rails 5.0 and Rails 5.1 of this method
|
86
|
-
# with respect to database views.
|
87
|
-
if ActiveRecord.version >= Gem::Version.new("5.0") &&
|
88
|
-
ActiveRecord.version < Gem::Version.new("5.1")
|
89
|
-
connection.data_source_exists?(table_name)
|
90
|
-
else
|
91
|
-
connection.table_exists?(schema_migrations_table_name)
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
def db_config
|
96
|
-
ActiveRecord::Base.configurations[Rails.env || "development"] ||
|
97
|
-
ENV["DATABASE_URL"]
|
98
|
-
end
|
99
|
-
end
|
100
|
-
end
|
101
|
-
end
|
@@ -1,26 +0,0 @@
|
|
1
|
-
module DataMigrate
|
2
|
-
class Migration < ::ActiveRecord::Migration
|
3
|
-
|
4
|
-
class << self
|
5
|
-
def check_pending!(connection = ::ActiveRecord::Base.connection)
|
6
|
-
raise ActiveRecord::PendingMigrationError if DataMigrator::Migrator.needs_migration?(connection)
|
7
|
-
end
|
8
|
-
|
9
|
-
def migrate(direction)
|
10
|
-
new.migrate direction
|
11
|
-
end
|
12
|
-
|
13
|
-
def table_name
|
14
|
-
ActiveRecord::Base.table_name_prefix + "data_migrations" + ActiveRecord::Base.table_name_suffix
|
15
|
-
end
|
16
|
-
|
17
|
-
def primary_key
|
18
|
-
"version"
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def initialize(name = self.class.name, version = nil)
|
23
|
-
super(name, version)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
@@ -1,26 +0,0 @@
|
|
1
|
-
module DataMigrate
|
2
|
-
class MigrationFive < ::ActiveRecord::Migration[5.0]
|
3
|
-
|
4
|
-
class << self
|
5
|
-
def check_pending!(connection = ::ActiveRecord::Base.connection)
|
6
|
-
raise ActiveRecord::PendingMigrationError if DataMigrator::Migrator.needs_migration?(connection)
|
7
|
-
end
|
8
|
-
|
9
|
-
def migrate(direction)
|
10
|
-
new.migrate direction
|
11
|
-
end
|
12
|
-
|
13
|
-
def table_name
|
14
|
-
ActiveRecord::Base.table_name_prefix + 'data_migrations' + ActiveRecord::Base.table_name_suffix
|
15
|
-
end
|
16
|
-
|
17
|
-
def primary_key
|
18
|
-
"version"
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def initialize(name = self.class.name, version = nil)
|
23
|
-
super(name, version)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
@@ -1,31 +0,0 @@
|
|
1
|
-
module DataMigrate
|
2
|
-
# Helper class to getting access to db schema
|
3
|
-
# to allow data/schema combiation tasks
|
4
|
-
class SchemaMigration
|
5
|
-
def self.pending_schema_migrations
|
6
|
-
all_migrations = DataMigrate::DataMigrator.migrations(migrations_paths)
|
7
|
-
sort_migrations(
|
8
|
-
ActiveRecord::Migrator.new(:up, all_migrations).
|
9
|
-
pending_migrations.
|
10
|
-
map {|m| { version: m.version, name: m.name, kind: :schema }}
|
11
|
-
)
|
12
|
-
end
|
13
|
-
|
14
|
-
def self.run(direction, migration_paths, version)
|
15
|
-
ActiveRecord::Migrator.run(direction, migration_paths, version)
|
16
|
-
end
|
17
|
-
|
18
|
-
def self.sort_migrations(set1, set2 = nil)
|
19
|
-
migrations = set1 + (set2 || [])
|
20
|
-
migrations.sort {|a, b| sort_string(a) <=> sort_string(b)}
|
21
|
-
end
|
22
|
-
|
23
|
-
def self.migrations_paths
|
24
|
-
Rails.application.config.paths["db/migrate"].to_a
|
25
|
-
end
|
26
|
-
|
27
|
-
def self.sort_string(migration)
|
28
|
-
"#{migration[:version]}_#{migration[:kind] == :data ? 1 : 0}"
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
@@ -1,65 +0,0 @@
|
|
1
|
-
module DataMigrate
|
2
|
-
class StatusService
|
3
|
-
class << self
|
4
|
-
def dump(connection = ActiveRecord::Base.connection, stream = STDOUT)
|
5
|
-
new(connection).dump(stream)
|
6
|
-
stream
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
|
-
def initialize(connection)
|
11
|
-
@connection = connection
|
12
|
-
end
|
13
|
-
|
14
|
-
def root_folder
|
15
|
-
Rails.root
|
16
|
-
end
|
17
|
-
|
18
|
-
def dump(stream)
|
19
|
-
unless @connection.table_exists?(table_name)
|
20
|
-
stream.puts "Data migrations table does not exist yet."
|
21
|
-
return
|
22
|
-
end
|
23
|
-
sql = "SELECT version FROM #{DataMigrate::DataMigrator.schema_migrations_table_name}"
|
24
|
-
db_list = ActiveRecord::Base.connection.select_values(sql)
|
25
|
-
output(stream, db_list)
|
26
|
-
end
|
27
|
-
|
28
|
-
private
|
29
|
-
|
30
|
-
def table_name
|
31
|
-
DataMigrate::DataMigrator.schema_migrations_table_name
|
32
|
-
end
|
33
|
-
|
34
|
-
def output(stream, db_list)
|
35
|
-
stream.puts "#{"Status".center(8)} #{"Migration ID".ljust(14)} Migration Name"
|
36
|
-
stream.puts "-" * 50
|
37
|
-
list = migration_files(db_list) + migration_list(db_list)
|
38
|
-
list.sort! {|line1, line2| line1[1] <=> line2[1]}
|
39
|
-
list.each do |file|
|
40
|
-
stream.puts "#{file[0].center(8)} #{file[1].ljust(14)} #{file[2]}"
|
41
|
-
end
|
42
|
-
stream.puts
|
43
|
-
end
|
44
|
-
|
45
|
-
def migration_list(db_list)
|
46
|
-
list = []
|
47
|
-
db_list.each do |version|
|
48
|
-
list << ["up", version, "********** NO FILE *************"]
|
49
|
-
end
|
50
|
-
list
|
51
|
-
end
|
52
|
-
|
53
|
-
def migration_files(db_list)
|
54
|
-
file_list = []
|
55
|
-
Dir.foreach(File.join(root_folder, DataMigrate.config.data_migrations_path)) do |file|
|
56
|
-
# only files matching "20091231235959_some_name.rb" pattern
|
57
|
-
if match_data = DataMigrate::DataMigrator.match(file)
|
58
|
-
status = db_list.delete(match_data[1]) ? "up" : "down"
|
59
|
-
file_list << [status, match_data[1], match_data[2].humanize]
|
60
|
-
end
|
61
|
-
end
|
62
|
-
file_list
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|