data_migrate 3.2.2 → 3.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Changelog.md +5 -1
- data/README.md +5 -3
- data/lib/data_migrate.rb +11 -5
- data/lib/data_migrate/data_migrator.rb +29 -8
- data/lib/data_migrate/data_schema.rb +43 -0
- data/lib/data_migrate/database_tasks.rb +20 -0
- data/lib/data_migrate/schema_dumper.rb +44 -0
- data/lib/data_migrate/version.rb +1 -1
- data/spec/data_migrate/data_migrator_spec.rb +19 -2
- data/spec/data_migrate/data_schema_spec.rb +85 -0
- data/spec/data_migrate/database_tasks_spec.rb +18 -0
- data/spec/data_migrate/schema_dumper_spec.rb +44 -0
- data/tasks/databases.rake +46 -1
- metadata +12 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dec20b6f10e44509c0d0d737e378f6a6b0eaa189
|
4
|
+
data.tar.gz: b92eca354a600c6e244f1bb91c337a48232bc9eb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a6cabd8cfe7a43d0ff38877b398b3d622e30c947ac9f343e9d3f74ff8cc743b0b437048be919adb1a5aa8b23a44fe8678f1338411feceb96b60d30adb1ae595a
|
7
|
+
data.tar.gz: fdc38c467d8fed2a1b529521f9e2615dfd41079dcb278536d861069e378936be933d71c2923b135c276315ec78497da386778192b80cb5fb86996b3f4c975ce5
|
data/Changelog.md
CHANGED
@@ -1,9 +1,13 @@
|
|
1
1
|
Changelog
|
2
2
|
=========
|
3
3
|
|
4
|
+
## 3.3.0
|
5
|
+
The concept of schema:dump to data migrations, thanks to
|
6
|
+
[tobyndockerill](https://github.com/tobyndockerill)
|
7
|
+
|
4
8
|
## 3.2.1
|
5
9
|
|
6
|
-
data_migrate table into rails schema dump,
|
10
|
+
data_migrate table into rails schema dump, thanks to
|
7
11
|
[jturkel](https://github.com/jturkel)
|
8
12
|
|
9
13
|
|
data/README.md
CHANGED
@@ -117,24 +117,26 @@ You can generate a data migration as you would a schema migration:
|
|
117
117
|
### Rake Tasks
|
118
118
|
|
119
119
|
$> rake -T data
|
120
|
+
rake data:dump # Create a db/data_schema.rb file that stores the current data version
|
120
121
|
rake data:forward # Pushes the schema to the next version (specify steps w/ STEP=n)
|
121
122
|
rake data:migrate # Migrate data migrations (options: VERSION=x, VERBOSE=false)
|
122
123
|
rake data:migrate:down # Runs the "down" for a given migration VERSION
|
123
|
-
rake data:migrate:redo # Rollbacks the database one migration and re migrate up (options: STEP=x,
|
124
|
+
rake data:migrate:redo # Rollbacks the database one migration and re migrate up (options: STEP=x, VERSION=x)
|
124
125
|
rake data:migrate:status # Display status of data migrations
|
125
126
|
rake data:migrate:up # Runs the "up" for a given migration VERSION
|
126
127
|
rake data:rollback # Rolls the schema back to the previous version (specify steps w/ STEP=n)
|
127
128
|
rake data:version # Retrieves the current schema version number for data migrations
|
128
129
|
rake db:forward:with_data # Pushes the schema to the next version (specify steps w/ STEP=n)
|
129
130
|
rake db:migrate:down:with_data # Runs the "down" for a given migration VERSION
|
130
|
-
rake db:migrate:redo:with_data # Rollbacks the database one migration and re migrate up (options: STEP=x,
|
131
|
+
rake db:migrate:redo:with_data # Rollbacks the database one migration and re migrate up (options: STEP=x, VERSION=x)
|
131
132
|
rake db:migrate:status:with_data # Display status of data and schema migrations
|
132
133
|
rake db:migrate:up:with_data # Runs the "up" for a given migration VERSION
|
133
134
|
rake db:migrate:with_data # Migrate the database data and schema (options: VERSION=x, VERBOSE=false)
|
134
135
|
rake db:rollback:with_data # Rolls the schema back to the previous version (specify steps w/ STEP=n)
|
136
|
+
rake db:schema:load:with_data # Load both schema.rb and data_schema.rb file into the database
|
135
137
|
rake db:version:with_data # Retrieves the current schema version numbers for data and schema migrations
|
136
138
|
|
137
|
-
Tasks work as they would with the 'vanilla' db version.
|
139
|
+
Tasks work as they would with the 'vanilla' db version. The 'with_data' addition to the 'db' tasks will run the task in the context of both the data and schema migrations. That is, rake db:rollback:with_data will check to see if it was a schema or data migration invoked last, and do that. Tasks invoked in that space also have an additional line of output, indicating if the action is performed on data or schema.
|
138
140
|
|
139
141
|
With 'up' and 'down', you can specify the option 'BOTH', which defaults to false. Using true, will migrate both the data and schema (in the desired direction) if they both match the version provided. Again, going up, schema is given precedence. Down its data.
|
140
142
|
|
data/lib/data_migrate.rb
CHANGED
@@ -1,8 +1,14 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require File.join(File.dirname(__FILE__), "data_migrate", "data_migrator")
|
4
|
+
require File.join(File.dirname(__FILE__), "data_migrate",
|
5
|
+
"data_schema_migration")
|
6
|
+
require File.join(File.dirname(__FILE__), "data_migrate", "data_schema")
|
7
|
+
require File.join(File.dirname(__FILE__), "data_migrate", "database_tasks")
|
8
|
+
require File.join(File.dirname(__FILE__), "data_migrate", "schema_dumper")
|
3
9
|
if Rails::VERSION::MAJOR >= 5
|
4
|
-
require File.join(File.dirname(__FILE__),
|
10
|
+
require File.join(File.dirname(__FILE__), "data_migrate", "migration_five")
|
5
11
|
else
|
6
|
-
require File.join(File.dirname(__FILE__),
|
12
|
+
require File.join(File.dirname(__FILE__), "data_migrate", "migration")
|
7
13
|
end
|
8
|
-
require File.join(File.dirname(__FILE__),
|
14
|
+
require File.join(File.dirname(__FILE__), "data_migrate", "railtie")
|
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_record"
|
2
4
|
|
3
5
|
module DataMigrate
|
4
6
|
|
@@ -33,11 +35,19 @@ module DataMigrate
|
|
33
35
|
end
|
34
36
|
|
35
37
|
def schema_migrations_table_name
|
36
|
-
ActiveRecord::Base.table_name_prefix +
|
38
|
+
ActiveRecord::Base.table_name_prefix + "data_migrations" +
|
39
|
+
ActiveRecord::Base.table_name_suffix
|
37
40
|
end
|
38
41
|
|
39
42
|
def migrations_path
|
40
|
-
|
43
|
+
"db/data"
|
44
|
+
end
|
45
|
+
|
46
|
+
##
|
47
|
+
# Provides the full migrations_path filepath
|
48
|
+
# @return (String)
|
49
|
+
def full_migrations_path
|
50
|
+
File.join(Rails.root, *migrations_path.split(File::SEPARATOR))
|
41
51
|
end
|
42
52
|
|
43
53
|
def assure_data_schema_table
|
@@ -49,6 +59,15 @@ module DataMigrate
|
|
49
59
|
end
|
50
60
|
end
|
51
61
|
|
62
|
+
##
|
63
|
+
# Compares the given filename with what we expect data migration
|
64
|
+
# filenames to be, eg the "20091231235959_some_name.rb" pattern
|
65
|
+
# @param (String) filename
|
66
|
+
# @return (MatchData)
|
67
|
+
def match(filename)
|
68
|
+
/(\d{14})_(.+)\.rb/.match(filename)
|
69
|
+
end
|
70
|
+
|
52
71
|
private
|
53
72
|
|
54
73
|
def create_table(sm_table)
|
@@ -66,9 +85,11 @@ module DataMigrate
|
|
66
85
|
end
|
67
86
|
|
68
87
|
def table_exists?(connection, table_name)
|
69
|
-
# Avoid the warning that table_exists? prints in Rails 5.0 due a
|
70
|
-
# Rails 5.0 and Rails 5.1 of this method
|
71
|
-
|
88
|
+
# Avoid the warning that table_exists? prints in Rails 5.0 due a
|
89
|
+
# change in behavior between Rails 5.0 and Rails 5.1 of this method
|
90
|
+
# with respect to database views.
|
91
|
+
if ActiveRecord.version >= Gem::Version.new("5.0") &&
|
92
|
+
ActiveRecord.version < Gem::Version.new("5.1")
|
72
93
|
connection.data_source_exists?(table_name)
|
73
94
|
else
|
74
95
|
connection.table_exists?(schema_migrations_table_name)
|
@@ -76,9 +97,9 @@ module DataMigrate
|
|
76
97
|
end
|
77
98
|
|
78
99
|
def db_config
|
79
|
-
ActiveRecord::Base.configurations[Rails.env ||
|
100
|
+
ActiveRecord::Base.configurations[Rails.env || "development"] ||
|
101
|
+
ENV["DATABASE_URL"]
|
80
102
|
end
|
81
|
-
|
82
103
|
end
|
83
104
|
end
|
84
105
|
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DataMigrate
|
4
|
+
##
|
5
|
+
# Provides the definition method for data_schema.rb
|
6
|
+
class Data < ActiveRecord::Schema
|
7
|
+
# This method is based on the following two methods
|
8
|
+
# ActiveRecord::Schema#define
|
9
|
+
# ActiveRecord::ConnectionAdapters::SchemaStatements
|
10
|
+
# #assume_migrated_upto_version
|
11
|
+
def define(info)
|
12
|
+
DataMigrate::DataMigrator.assure_data_schema_table
|
13
|
+
|
14
|
+
return if info[:version].blank?
|
15
|
+
|
16
|
+
version = info[:version].to_i
|
17
|
+
table_name = DataMigrate::DataMigrator.schema_migrations_table_name
|
18
|
+
sm_table = quote_table_name(table_name)
|
19
|
+
migrated = select_values("SELECT version FROM #{sm_table}").map(&:to_i)
|
20
|
+
|
21
|
+
versions = []
|
22
|
+
Dir.foreach(DataMigrate::DataMigrator.full_migrations_path) do |file|
|
23
|
+
match_data = DataMigrate::DataMigrator.match(file)
|
24
|
+
versions << match_data[1].to_i if match_data
|
25
|
+
end
|
26
|
+
|
27
|
+
unless migrated.include?(version)
|
28
|
+
execute "INSERT INTO #{sm_table} (version) VALUES ('#{version}')"
|
29
|
+
end
|
30
|
+
|
31
|
+
inserted = Set.new
|
32
|
+
(versions - migrated).each do |v|
|
33
|
+
if inserted.include?(v)
|
34
|
+
raise "Duplicate data migration #{v}. Please renumber your data " \
|
35
|
+
"migrations to resolve the conflict."
|
36
|
+
elsif v < version
|
37
|
+
execute "INSERT INTO #{sm_table} (version) VALUES ('#{v}')"
|
38
|
+
inserted << v
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DataMigrate
|
4
|
+
##
|
5
|
+
# This class extends DatabaseTasks to override the schema_file method.
|
6
|
+
class DatabaseTasks
|
7
|
+
extend ActiveRecord::Tasks::DatabaseTasks
|
8
|
+
|
9
|
+
def self.schema_file(format = ActiveRecord::Base.schema_format)
|
10
|
+
case format
|
11
|
+
when :ruby
|
12
|
+
File.join(db_dir, "data_schema.rb")
|
13
|
+
else
|
14
|
+
message = "Only Ruby-based data_schema files are supported " \
|
15
|
+
"at this time."
|
16
|
+
Kernel.abort message
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DataMigrate
|
4
|
+
##
|
5
|
+
# Provides the capability to write the current data schema version to
|
6
|
+
# the data_schema file Based on ActiveRecord::SchemaDumper
|
7
|
+
class SchemaDumper
|
8
|
+
private_class_method :new
|
9
|
+
|
10
|
+
class << self
|
11
|
+
def dump(connection = ActiveRecord::Base.connection, stream = STDOUT)
|
12
|
+
new(connection).dump(stream)
|
13
|
+
stream
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def dump(stream)
|
18
|
+
define_params = @version ? "version: #{@version}" : ""
|
19
|
+
|
20
|
+
if stream.respond_to?(:external_encoding) && stream.external_encoding
|
21
|
+
stream.puts "# encoding: #{stream.external_encoding.name}"
|
22
|
+
end
|
23
|
+
|
24
|
+
stream.puts "DataMigrate::Data.define(#{define_params})"
|
25
|
+
|
26
|
+
stream
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def initialize(connection)
|
32
|
+
@connection = connection
|
33
|
+
all_versions = DataMigrate::DataSchemaMigration.all.map do |x|
|
34
|
+
x.version.to_i
|
35
|
+
end
|
36
|
+
|
37
|
+
@version = begin
|
38
|
+
all_versions.max
|
39
|
+
rescue StandardError
|
40
|
+
0
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/lib/data_migrate/version.rb
CHANGED
@@ -2,12 +2,12 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe DataMigrate::DataMigrator do
|
4
4
|
let(:subject) { DataMigrate::DataMigrator }
|
5
|
-
let(:db_config)
|
5
|
+
let(:db_config) do
|
6
6
|
{
|
7
7
|
adapter: "sqlite3",
|
8
8
|
database: "spec/db/test.db"
|
9
9
|
}
|
10
|
-
|
10
|
+
end
|
11
11
|
|
12
12
|
describe :assure_data_schema_table do
|
13
13
|
before do
|
@@ -42,4 +42,21 @@ describe DataMigrate::DataMigrator do
|
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
|
+
describe :match do
|
46
|
+
context "when the file does not match" do
|
47
|
+
it "returns nil" do
|
48
|
+
expect(subject.match("not_a_data_migration_file")).to be_nil
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context "when the file matches" do
|
53
|
+
it "returns a valid MatchData object" do
|
54
|
+
match_data = subject.match("20091231235959_some_name.rb")
|
55
|
+
|
56
|
+
expect(match_data[0]).to eq "20091231235959_some_name.rb"
|
57
|
+
expect(match_data[1]).to eq "20091231235959"
|
58
|
+
expect(match_data[2]).to eq "some_name"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
45
62
|
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
describe DataMigrate::Data do
|
4
|
+
let(:subject) { DataMigrate::Data }
|
5
|
+
let(:db_config) do
|
6
|
+
{
|
7
|
+
adapter: "sqlite3",
|
8
|
+
database: "spec/db/test.db"
|
9
|
+
}
|
10
|
+
end
|
11
|
+
let(:fixture_file_timestamps) do
|
12
|
+
%w[20091231235959 20101231235959 20111231235959]
|
13
|
+
end
|
14
|
+
|
15
|
+
around do |example|
|
16
|
+
Dir.mktmpdir do |temp_dir|
|
17
|
+
@temp_dir = temp_dir
|
18
|
+
|
19
|
+
# create the fake data migration files
|
20
|
+
fixture_file_timestamps.each do |timestamp|
|
21
|
+
FileUtils.touch File.join(temp_dir, "#{timestamp}_data_migration.rb")
|
22
|
+
end
|
23
|
+
|
24
|
+
example.run
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe :define do
|
29
|
+
before do
|
30
|
+
expect(DataMigrate::DataMigrator).
|
31
|
+
to receive(:db_config) { db_config }.at_least(:once)
|
32
|
+
ActiveRecord::Base.establish_connection(db_config)
|
33
|
+
ActiveRecord::Base.connection.initialize_schema_migrations_table
|
34
|
+
end
|
35
|
+
|
36
|
+
after do
|
37
|
+
ActiveRecord::Migration.drop_table("data_migrations")
|
38
|
+
end
|
39
|
+
|
40
|
+
context "when no version is supplied" do
|
41
|
+
it "returns nil" do
|
42
|
+
expect(subject.define(version: nil)).to be_nil
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context "when a version is supplied" do
|
47
|
+
before do
|
48
|
+
allow(DataMigrate::DataMigrator).
|
49
|
+
to receive(:full_migrations_path).and_return(@temp_dir)
|
50
|
+
end
|
51
|
+
|
52
|
+
it "sets the current version to the supplied version" do
|
53
|
+
version = fixture_file_timestamps[1]
|
54
|
+
|
55
|
+
expect(DataMigrate::DataMigrator.current_version).not_to eq version.to_i
|
56
|
+
subject.define(version: version)
|
57
|
+
expect(DataMigrate::DataMigrator.current_version).to eq version.to_i
|
58
|
+
end
|
59
|
+
|
60
|
+
it "creates entries for migration versions that come " \
|
61
|
+
"before the supplied version" do
|
62
|
+
|
63
|
+
version = fixture_file_timestamps[1]
|
64
|
+
|
65
|
+
subject.define(version: version)
|
66
|
+
|
67
|
+
sql_select = <<-SQL
|
68
|
+
SELECT version
|
69
|
+
FROM #{DataMigrate::DataMigrator.schema_migrations_table_name}
|
70
|
+
SQL
|
71
|
+
|
72
|
+
db_list_data = ActiveRecord::Base.connection.
|
73
|
+
select_values(sql_select).map(&:to_i)
|
74
|
+
expect(db_list_data).to match_array(
|
75
|
+
[fixture_file_timestamps[0], fixture_file_timestamps[1]].map(&:to_i)
|
76
|
+
)
|
77
|
+
|
78
|
+
# The last remaining migration (fixture_file_timestamps[2]) was
|
79
|
+
# not included as part of the supplied version and so should not
|
80
|
+
# appear in the data_migrations table.
|
81
|
+
expect(db_list_data).not_to include(fixture_file_timestamps[2])
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
describe DataMigrate::DatabaseTasks do
|
4
|
+
let(:subject) { DataMigrate::DatabaseTasks }
|
5
|
+
|
6
|
+
before do
|
7
|
+
# In a normal Rails installation, db_dir would defer to
|
8
|
+
# Rails.application.config.paths["db"].first
|
9
|
+
# @see https://github.com/rails/rails/blob/a7d49ef78c36df2d1ca876451f30915ada1079a5/activerecord/lib/active_record/tasks/database_tasks.rb#L54
|
10
|
+
allow(subject).to receive(:db_dir).and_return("db")
|
11
|
+
end
|
12
|
+
|
13
|
+
describe :schema_file do
|
14
|
+
it "returns the correct data schema file path" do
|
15
|
+
expect(subject.schema_file).to eq "db/data_schema.rb"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
describe DataMigrate::SchemaDumper do
|
4
|
+
let(:subject) { DataMigrate::SchemaDumper }
|
5
|
+
let(:db_config) do
|
6
|
+
{
|
7
|
+
adapter: "sqlite3",
|
8
|
+
database: "spec/db/test.db"
|
9
|
+
}
|
10
|
+
end
|
11
|
+
let(:fixture_file_timestamps) do
|
12
|
+
%w[20091231235959 20101231235959 20111231235959]
|
13
|
+
end
|
14
|
+
|
15
|
+
describe :dump do
|
16
|
+
before do
|
17
|
+
expect(DataMigrate::DataMigrator).
|
18
|
+
to receive(:db_config) { db_config }.at_least(:once)
|
19
|
+
ActiveRecord::Base.establish_connection(db_config)
|
20
|
+
|
21
|
+
ActiveRecord::Base.connection.initialize_schema_migrations_table
|
22
|
+
DataMigrate::DataMigrator.assure_data_schema_table
|
23
|
+
|
24
|
+
ActiveRecord::Base.connection.execute <<-SQL
|
25
|
+
INSERT INTO #{DataMigrate::DataMigrator.schema_migrations_table_name}
|
26
|
+
VALUES #{fixture_file_timestamps.map { |t| "(#{t})" }.join(', ')}
|
27
|
+
SQL
|
28
|
+
end
|
29
|
+
|
30
|
+
after do
|
31
|
+
ActiveRecord::Migration.drop_table("data_migrations")
|
32
|
+
end
|
33
|
+
|
34
|
+
it "writes the define method with the version key to the stream" do
|
35
|
+
stream = StringIO.new
|
36
|
+
DataMigrate::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
|
37
|
+
stream.rewind
|
38
|
+
|
39
|
+
last_version = fixture_file_timestamps.last
|
40
|
+
expected = "DataMigrate::Data.define(version: #{last_version})"
|
41
|
+
expect(stream.read).to include expected
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/tasks/databases.rake
CHANGED
@@ -58,6 +58,7 @@ namespace :db do
|
|
58
58
|
end
|
59
59
|
|
60
60
|
Rake::Task["db:_dump"].invoke
|
61
|
+
Rake::Task["data:dump"].invoke
|
61
62
|
end
|
62
63
|
|
63
64
|
namespace :redo do
|
@@ -98,6 +99,7 @@ namespace :db do
|
|
98
99
|
end
|
99
100
|
|
100
101
|
Rake::Task["db:_dump"].invoke
|
102
|
+
Rake::Task["data:dump"].invoke
|
101
103
|
end
|
102
104
|
end
|
103
105
|
|
@@ -125,6 +127,7 @@ namespace :db do
|
|
125
127
|
end
|
126
128
|
|
127
129
|
Rake::Task["db:_dump"].invoke
|
130
|
+
Rake::Task["data:dump"].invoke
|
128
131
|
end
|
129
132
|
end
|
130
133
|
|
@@ -190,6 +193,7 @@ namespace :db do
|
|
190
193
|
end
|
191
194
|
|
192
195
|
Rake::Task["db:_dump"].invoke
|
196
|
+
Rake::Task["data:dump"].invoke
|
193
197
|
end
|
194
198
|
end
|
195
199
|
|
@@ -212,6 +216,7 @@ namespace :db do
|
|
212
216
|
end
|
213
217
|
|
214
218
|
Rake::Task["db:_dump"].invoke
|
219
|
+
Rake::Task["data:dump"].invoke
|
215
220
|
end
|
216
221
|
end
|
217
222
|
|
@@ -223,6 +228,17 @@ namespace :db do
|
|
223
228
|
puts "Current Data version: #{DataMigrate::DataMigrator.current_version}"
|
224
229
|
end
|
225
230
|
end
|
231
|
+
|
232
|
+
namespace :schema do
|
233
|
+
namespace :load do
|
234
|
+
desc "Load both schema.rb and data_schema.rb file into the database"
|
235
|
+
task with_data: :environment do
|
236
|
+
Rake::Task["db:schema:load"].invoke
|
237
|
+
|
238
|
+
DataMigrate::DatabaseTasks.load_schema_current(:ruby, ENV["SCHEMA"])
|
239
|
+
end
|
240
|
+
end
|
241
|
+
end
|
226
242
|
end
|
227
243
|
|
228
244
|
namespace :data do
|
@@ -231,6 +247,8 @@ namespace :data do
|
|
231
247
|
assure_data_schema_table
|
232
248
|
#ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
|
233
249
|
DataMigrate::DataMigrator.migrate("db/data/", ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
|
250
|
+
|
251
|
+
Rake::Task["data:dump"].invoke
|
234
252
|
end
|
235
253
|
|
236
254
|
namespace :migrate do
|
@@ -252,6 +270,7 @@ namespace :data do
|
|
252
270
|
version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
|
253
271
|
raise "VERSION is required" unless version
|
254
272
|
DataMigrate::DataMigrator.run(:up, "db/data/", version)
|
273
|
+
Rake::Task["data:dump"].invoke
|
255
274
|
end
|
256
275
|
|
257
276
|
desc 'Runs the "down" for a given migration VERSION.'
|
@@ -260,6 +279,7 @@ namespace :data do
|
|
260
279
|
raise "VERSION is required" unless version
|
261
280
|
assure_data_schema_table
|
262
281
|
DataMigrate::DataMigrator.run(:down, "db/data/", version)
|
282
|
+
Rake::Task["data:dump"].invoke
|
263
283
|
end
|
264
284
|
|
265
285
|
desc "Display status of data migrations"
|
@@ -274,7 +294,7 @@ namespace :data do
|
|
274
294
|
file_list = []
|
275
295
|
Dir.foreach(File.join(Rails.root, 'db', 'data')) do |file|
|
276
296
|
# only files matching "20091231235959_some_name.rb" pattern
|
277
|
-
if match_data =
|
297
|
+
if match_data = DataMigrate::DataMigrator.match(file)
|
278
298
|
status = db_list.delete(match_data[1]) ? 'up' : 'down'
|
279
299
|
file_list << [status, match_data[1], match_data[2]]
|
280
300
|
end
|
@@ -298,6 +318,7 @@ namespace :data do
|
|
298
318
|
assure_data_schema_table
|
299
319
|
step = ENV['STEP'] ? ENV['STEP'].to_i : 1
|
300
320
|
DataMigrate::DataMigrator.rollback('db/data/', step)
|
321
|
+
Rake::Task["data:dump"].invoke
|
301
322
|
end
|
302
323
|
|
303
324
|
desc 'Pushes the schema to the next version (specify steps w/ STEP=n).'
|
@@ -310,6 +331,7 @@ namespace :data do
|
|
310
331
|
migrations.each do | pending_migration |
|
311
332
|
DataMigrate::DataMigrator.run(:up, "db/data/", pending_migration[:version])
|
312
333
|
end
|
334
|
+
Rake::Task["data:dump"].invoke
|
313
335
|
end
|
314
336
|
|
315
337
|
desc "Retrieves the current schema version number for data migrations"
|
@@ -317,6 +339,29 @@ namespace :data do
|
|
317
339
|
assure_data_schema_table
|
318
340
|
puts "Current data version: #{DataMigrate::DataMigrator.current_version}"
|
319
341
|
end
|
342
|
+
|
343
|
+
desc "Create a db/data_schema.rb file that stores the current data version"
|
344
|
+
task dump: :environment do
|
345
|
+
if ActiveRecord::Base.dump_schema_after_migration
|
346
|
+
case ActiveRecord::Base.schema_format
|
347
|
+
when :ruby
|
348
|
+
filename = DataMigrate::DatabaseTasks.schema_file
|
349
|
+
File.open(filename, "w:utf-8") do |file|
|
350
|
+
DataMigrate::SchemaDumper.dump(ActiveRecord::Base.connection, file)
|
351
|
+
end
|
352
|
+
else
|
353
|
+
raise <<-MSG.strip_heredoc
|
354
|
+
only Ruby-based data_schema files are supported at this time
|
355
|
+
(unknown schema format #{ActiveRecord::Base.schema_format})
|
356
|
+
MSG
|
357
|
+
end
|
358
|
+
end
|
359
|
+
|
360
|
+
# Allow this task to be called as many times as required. An example
|
361
|
+
# is the migrate:redo task, which calls other two internally
|
362
|
+
# that depend on this one.
|
363
|
+
Rake::Task["data:dump"].reenable
|
364
|
+
end
|
320
365
|
end
|
321
366
|
|
322
367
|
def pending_migrations
|
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: 3.
|
4
|
+
version: 3.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew J Vargo
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2017-
|
12
|
+
date: 2017-12-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -166,10 +166,13 @@ files:
|
|
166
166
|
- lib/capistrano/data_migrate/migrate.rb
|
167
167
|
- lib/data_migrate.rb
|
168
168
|
- lib/data_migrate/data_migrator.rb
|
169
|
+
- lib/data_migrate/data_schema.rb
|
169
170
|
- lib/data_migrate/data_schema_migration.rb
|
171
|
+
- lib/data_migrate/database_tasks.rb
|
170
172
|
- lib/data_migrate/migration.rb
|
171
173
|
- lib/data_migrate/migration_five.rb
|
172
174
|
- lib/data_migrate/railtie.rb
|
175
|
+
- lib/data_migrate/schema_dumper.rb
|
173
176
|
- lib/data_migrate/version.rb
|
174
177
|
- lib/generators/data_migrate.rb
|
175
178
|
- lib/generators/data_migration/data_migration_generator.rb
|
@@ -178,7 +181,10 @@ files:
|
|
178
181
|
- screenshot.png
|
179
182
|
- spec/data_migrate/data_migrator_spec.rb
|
180
183
|
- spec/data_migrate/data_schema_migration_spec.rb
|
184
|
+
- spec/data_migrate/data_schema_spec.rb
|
185
|
+
- spec/data_migrate/database_tasks_spec.rb
|
181
186
|
- spec/data_migrate/migration.rb
|
187
|
+
- spec/data_migrate/schema_dumper_spec.rb
|
182
188
|
- spec/generators/data_migration/data_migration_generator_spec.rb
|
183
189
|
- spec/spec_helper.rb
|
184
190
|
- tasks/.gitkeep
|
@@ -206,13 +212,16 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
206
212
|
version: '0'
|
207
213
|
requirements: []
|
208
214
|
rubyforge_project: data_migrate
|
209
|
-
rubygems_version: 2.
|
215
|
+
rubygems_version: 2.5.2
|
210
216
|
signing_key:
|
211
217
|
specification_version: 4
|
212
218
|
summary: Rake tasks to migrate data alongside schema changes.
|
213
219
|
test_files:
|
214
220
|
- spec/data_migrate/data_migrator_spec.rb
|
215
221
|
- spec/data_migrate/data_schema_migration_spec.rb
|
222
|
+
- spec/data_migrate/data_schema_spec.rb
|
223
|
+
- spec/data_migrate/database_tasks_spec.rb
|
216
224
|
- spec/data_migrate/migration.rb
|
225
|
+
- spec/data_migrate/schema_dumper_spec.rb
|
217
226
|
- spec/generators/data_migration/data_migration_generator_spec.rb
|
218
227
|
- spec/spec_helper.rb
|