data_migrate 3.0.1 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +6 -1
- data/.hound.yml +4 -0
- data/.overcommit.yml +21 -0
- data/.rspec +3 -0
- data/.rubocop.yml +2 -0
- data/.ruby-style.yml +1061 -0
- data/.travis.yml +16 -0
- data/Appraisals +17 -0
- data/Changelog.md +35 -0
- data/Gemfile.rails5 +5 -0
- data/Gemfile.rails5.1 +5 -0
- data/Gemfile.rails5.2 +5 -0
- data/README.md +58 -41
- data/data_migrate.gemspec +12 -1
- data/gemfiles/rails_4.1.gemfile +7 -0
- data/gemfiles/rails_4.2.gemfile +8 -0
- data/gemfiles/rails_5.0.gemfile +7 -0
- data/gemfiles/rails_5.1.gemfile +7 -0
- data/gemfiles/rails_5.2.gemfile +7 -0
- data/lib/data_migrate/data_migrator.rb +53 -8
- data/lib/data_migrate/data_migrator_five.rb +84 -0
- data/lib/data_migrate/data_schema.rb +63 -0
- data/lib/data_migrate/data_schema_migration.rb +1 -0
- data/lib/data_migrate/database_tasks.rb +82 -0
- data/lib/data_migrate/migration.rb +1 -1
- data/lib/data_migrate/migration_context.rb +90 -0
- data/lib/data_migrate/migration_five.rb +26 -0
- data/lib/data_migrate/railtie.rb +1 -1
- data/lib/data_migrate/schema_dumper.rb +42 -0
- data/lib/data_migrate/schema_migration.rb +31 -0
- data/lib/data_migrate/schema_migration_five.rb +31 -0
- data/lib/data_migrate/status_service.rb +65 -0
- data/lib/data_migrate/status_service_five.rb +48 -0
- data/lib/data_migrate/tasks/data_migrate_tasks.rb +25 -0
- data/lib/data_migrate/version.rb +1 -1
- data/lib/data_migrate.rb +30 -4
- data/lib/generators/data_migration/data_migration_generator.rb +17 -16
- data/lib/generators/data_migration/templates/data_migration.rb +4 -4
- data/screenshot.png +0 -0
- data/spec/data_migrate/data_migrator_spec.rb +50 -0
- data/spec/data_migrate/data_schema_migration_spec.rb +16 -0
- data/spec/data_migrate/data_spec.rb +87 -0
- data/spec/data_migrate/database_tasks_spec.rb +114 -0
- data/spec/data_migrate/migration.rb +19 -0
- data/spec/data_migrate/migration_context_spec.rb +107 -0
- data/spec/data_migrate/schema_dumper_spec.rb +46 -0
- data/spec/data_migrate/schema_migration_spec.rb +69 -0
- data/spec/data_migrate/status_service_spec.rb +93 -0
- data/spec/data_migrate/tasks/data_migrate_tasks_spec.rb +50 -0
- data/spec/db/4.2/20091231235959_some_name.rb +9 -0
- data/spec/db/4.2/20171231235959_super_update.rb +9 -0
- data/spec/db/5.0/20091231235959_some_name.rb +9 -0
- data/spec/db/5.0/20171231235959_super_update.rb +9 -0
- data/spec/db/data/20091231235959_some_name.rb +9 -0
- data/spec/db/data/20171231235959_super_update.rb +9 -0
- data/spec/db/migrate/4.2/20131111111111_late_migration.rb +9 -0
- data/spec/db/migrate/4.2/20202020202011_db_migration.rb +9 -0
- data/spec/db/migrate/5.0/20131111111111_late_migration.rb +9 -0
- data/spec/db/migrate/5.0/20202020202011_db_migration.rb +9 -0
- data/spec/db/migrate/5.2/20131111111111_late_migration.rb +9 -0
- data/spec/db/migrate/5.2/20202020202011_db_migration.rb +9 -0
- data/spec/generators/data_migration/data_migration_generator_spec.rb +27 -0
- data/spec/spec_helper.rb +9 -0
- data/tasks/databases.rake +69 -62
- metadata +221 -6
@@ -0,0 +1,87 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
describe DataMigrate::Data do
|
6
|
+
let(:subject) { DataMigrate::Data }
|
7
|
+
let(:db_config) do
|
8
|
+
{
|
9
|
+
adapter: "sqlite3",
|
10
|
+
database: "spec/db/test.db"
|
11
|
+
}
|
12
|
+
end
|
13
|
+
let(:fixture_file_timestamps) do
|
14
|
+
%w[20091231235959 20101231235959 20111231235959]
|
15
|
+
end
|
16
|
+
|
17
|
+
around do |example|
|
18
|
+
Dir.mktmpdir do |temp_dir|
|
19
|
+
@temp_dir = temp_dir
|
20
|
+
|
21
|
+
# create the fake data migration files
|
22
|
+
fixture_file_timestamps.each do |timestamp|
|
23
|
+
FileUtils.touch File.join(temp_dir, "#{timestamp}_data_migration.rb")
|
24
|
+
end
|
25
|
+
|
26
|
+
example.run
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe :define do
|
31
|
+
before do
|
32
|
+
allow(DataMigrate::DataMigrator).
|
33
|
+
to receive(:db_config) { db_config }
|
34
|
+
ActiveRecord::Base.establish_connection(db_config)
|
35
|
+
ActiveRecord::SchemaMigration.create_table
|
36
|
+
end
|
37
|
+
|
38
|
+
after do
|
39
|
+
ActiveRecord::Migration.drop_table("data_migrations")
|
40
|
+
end
|
41
|
+
|
42
|
+
context "when no version is supplied" do
|
43
|
+
it "returns nil" do
|
44
|
+
expect(subject.define(version: nil)).to be_nil
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
context "when a version is supplied" do
|
49
|
+
before do
|
50
|
+
allow(DataMigrate::DataMigrator).
|
51
|
+
to receive(:full_migrations_path).and_return(@temp_dir)
|
52
|
+
end
|
53
|
+
|
54
|
+
it "sets the current version to the supplied version" do
|
55
|
+
version = fixture_file_timestamps[1]
|
56
|
+
|
57
|
+
expect(DataMigrate::DataMigrator.current_version).not_to eq version.to_i
|
58
|
+
subject.define(version: version)
|
59
|
+
expect(DataMigrate::DataMigrator.current_version).to eq version.to_i
|
60
|
+
end
|
61
|
+
|
62
|
+
it "creates entries for migration versions that come " \
|
63
|
+
"before the supplied version" do
|
64
|
+
|
65
|
+
version = fixture_file_timestamps[1]
|
66
|
+
|
67
|
+
subject.define(version: version)
|
68
|
+
|
69
|
+
sql_select = <<-SQL
|
70
|
+
SELECT version
|
71
|
+
FROM #{DataMigrate::DataSchemaMigration.table_name}
|
72
|
+
SQL
|
73
|
+
|
74
|
+
db_list_data = ActiveRecord::Base.connection.
|
75
|
+
select_values(sql_select).map(&:to_i)
|
76
|
+
expect(db_list_data).to match_array(
|
77
|
+
[fixture_file_timestamps[0], fixture_file_timestamps[1]].map(&:to_i)
|
78
|
+
)
|
79
|
+
|
80
|
+
# The last remaining migration (fixture_file_timestamps[2]) was
|
81
|
+
# not included as part of the supplied version and so should not
|
82
|
+
# appear in the data_migrations table.
|
83
|
+
expect(db_list_data).not_to include(fixture_file_timestamps[2])
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
describe DataMigrate::DatabaseTasks do
|
6
|
+
let(:subject) { DataMigrate::DatabaseTasks }
|
7
|
+
let(:migration_path) {
|
8
|
+
if Rails::VERSION::MAJOR == 5
|
9
|
+
if Rails::VERSION::MINOR == 2
|
10
|
+
"spec/db/migrate/5.2"
|
11
|
+
else
|
12
|
+
"spec/db/migrate/5.0"
|
13
|
+
end
|
14
|
+
else
|
15
|
+
"spec/db/migrate/4.2"
|
16
|
+
end
|
17
|
+
}
|
18
|
+
let(:data_migrations_path) {
|
19
|
+
if Rails::VERSION::MAJOR == 5
|
20
|
+
if Rails::VERSION::MINOR == 2
|
21
|
+
"spec/db/data"
|
22
|
+
else
|
23
|
+
"spec/db/5.0"
|
24
|
+
end
|
25
|
+
else
|
26
|
+
"spec/db/4.2"
|
27
|
+
end
|
28
|
+
}
|
29
|
+
let(:db_config) do
|
30
|
+
{
|
31
|
+
adapter: "sqlite3",
|
32
|
+
database: "spec/db/test.db"
|
33
|
+
}
|
34
|
+
end
|
35
|
+
|
36
|
+
before do
|
37
|
+
# In a normal Rails installation, db_dir would defer to
|
38
|
+
# Rails.application.config.paths["db"].first
|
39
|
+
# @see https://github.com/rails/rails/blob/a7d49ef78c36df2d1ca876451f30915ada1079a5/activerecord/lib/active_record/tasks/database_tasks.rb#L54
|
40
|
+
allow(subject).to receive(:db_dir).and_return("db")
|
41
|
+
end
|
42
|
+
|
43
|
+
before do
|
44
|
+
allow(DataMigrate::Tasks::DataMigrateTasks).to receive(:migrations_paths) {
|
45
|
+
data_migrations_path
|
46
|
+
}
|
47
|
+
allow(DataMigrate::DataMigrator).to receive(:db_config) { db_config }
|
48
|
+
ActiveRecord::Base.establish_connection(db_config)
|
49
|
+
end
|
50
|
+
|
51
|
+
describe :data_schema_file do
|
52
|
+
it "returns the correct data schema file path" do
|
53
|
+
expect(subject.data_schema_file).to eq "db/data_schema.rb"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
context "migrations" do
|
58
|
+
after do
|
59
|
+
begin
|
60
|
+
ActiveRecord::Migration.drop_table("data_migrations")
|
61
|
+
rescue ActiveRecord::StatementInvalid
|
62
|
+
end
|
63
|
+
ActiveRecord::Migration.drop_table("schema_migrations")
|
64
|
+
end
|
65
|
+
|
66
|
+
before do
|
67
|
+
ActiveRecord::Base.establish_connection(db_config)
|
68
|
+
ActiveRecord::SchemaMigration.create_table
|
69
|
+
|
70
|
+
allow(DataMigrate::SchemaMigration).to receive(:migrations_paths) {
|
71
|
+
migration_path
|
72
|
+
}
|
73
|
+
allow(DataMigrate::DatabaseTasks).to receive(:data_migrations_path) {
|
74
|
+
data_migrations_path
|
75
|
+
}.at_least(:once)
|
76
|
+
allow(DataMigrate::DatabaseTasks).to receive(:schema_migrations_path) {
|
77
|
+
migration_path
|
78
|
+
}.at_least(:once)
|
79
|
+
end
|
80
|
+
|
81
|
+
describe :past_migrations do
|
82
|
+
it do
|
83
|
+
subject.forward
|
84
|
+
m = subject.past_migrations
|
85
|
+
expect(m.count).to eq 1
|
86
|
+
expect(m.first[:version]).to eq 20091231235959
|
87
|
+
end
|
88
|
+
|
89
|
+
it "shows nothing without any migrations" do
|
90
|
+
m = subject.past_migrations
|
91
|
+
expect(m.count).to eq 0
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
describe :forward do
|
96
|
+
|
97
|
+
it "run forward default amount of times" do
|
98
|
+
subject.forward
|
99
|
+
versions = DataMigrate::DataSchemaMigration.normalized_versions
|
100
|
+
expect(versions.count).to eq(1)
|
101
|
+
end
|
102
|
+
|
103
|
+
it "run forward defined number of times" do
|
104
|
+
subject.forward(2)
|
105
|
+
versions = DataMigrate::DataSchemaMigration.normalized_versions
|
106
|
+
expect(versions.count).to eq(1)
|
107
|
+
expect(versions.first).to eq "20091231235959"
|
108
|
+
versions = ActiveRecord::SchemaMigration.normalized_versions
|
109
|
+
expect(versions.count).to eq(1)
|
110
|
+
expect(versions.first).to eq "20131111111111"
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
if Rails::VERSION::MAJOR >= 5
|
4
|
+
subject = DataMigrate::MigrationFive
|
5
|
+
else
|
6
|
+
subject = DataMigrate::Migration
|
7
|
+
end
|
8
|
+
|
9
|
+
describe subject do
|
10
|
+
it "uses correct table name" do
|
11
|
+
expect(subject.table_name).to eq("data_migrations")
|
12
|
+
end
|
13
|
+
|
14
|
+
it "uses correct index name" do
|
15
|
+
expect(subject).to receive(:table_name_prefix) { "" }
|
16
|
+
expect(subject).to receive(:table_name_suffix) { "" }
|
17
|
+
expect(subject.index_name).to eq("unique_data_migrations")
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe DataMigrate::DataMigrator do
|
4
|
+
let(:context) {
|
5
|
+
DataMigrate::MigrationContext.new("spec/db/data")
|
6
|
+
}
|
7
|
+
|
8
|
+
before do
|
9
|
+
unless Rails::VERSION::MAJOR == 5 and
|
10
|
+
Rails::VERSION::MINOR == 2
|
11
|
+
skip("Tests are only applicable for Rails 5.2")
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
after do
|
16
|
+
begin
|
17
|
+
ActiveRecord::Migration.drop_table("data_migrations")
|
18
|
+
rescue StandardError
|
19
|
+
nil
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
let(:db_config) do
|
24
|
+
{
|
25
|
+
adapter: "sqlite3",
|
26
|
+
database: "spec/db/test.db"
|
27
|
+
}
|
28
|
+
end
|
29
|
+
|
30
|
+
describe :migrate do
|
31
|
+
before do
|
32
|
+
ActiveRecord::Base.establish_connection(db_config)
|
33
|
+
ActiveRecord::SchemaMigration.create_table
|
34
|
+
end
|
35
|
+
|
36
|
+
it "migrates existing file" do
|
37
|
+
context.migrate(nil)
|
38
|
+
context.migrations_status
|
39
|
+
versions = DataMigrate::DataSchemaMigration.normalized_versions
|
40
|
+
expect(versions.count).to eq(2)
|
41
|
+
expect(versions).to include("20091231235959")
|
42
|
+
expect(versions).to include("20171231235959")
|
43
|
+
end
|
44
|
+
|
45
|
+
it "undo migration" do
|
46
|
+
context.migrate(nil)
|
47
|
+
context.run(:down, 20171231235959)
|
48
|
+
versions = DataMigrate::DataSchemaMigration.normalized_versions
|
49
|
+
expect(versions.count).to eq(1)
|
50
|
+
expect(versions).to include("20091231235959")
|
51
|
+
end
|
52
|
+
|
53
|
+
it "does not do anything if migration is undone twice" do
|
54
|
+
context.migrate(nil)
|
55
|
+
expect {
|
56
|
+
context.run(:down, 20171231235959)
|
57
|
+
}.to output(/Undoing SuperUpdate/).to_stdout
|
58
|
+
expect {
|
59
|
+
context.run(:down, 20171231235959)
|
60
|
+
}.not_to output(/Undoing SuperUpdate/).to_stdout
|
61
|
+
end
|
62
|
+
|
63
|
+
it "runs a specific migration" do
|
64
|
+
context.run(:up, 20171231235959)
|
65
|
+
versions = DataMigrate::DataSchemaMigration.normalized_versions
|
66
|
+
expect(versions.count).to eq(1)
|
67
|
+
expect(versions).to include("20171231235959")
|
68
|
+
end
|
69
|
+
|
70
|
+
it "does not do anything if migration is ran twice" do
|
71
|
+
expect {
|
72
|
+
context.run(:up, 20171231235959)
|
73
|
+
}.to output(/Doing SuperUpdate/).to_stdout
|
74
|
+
expect {
|
75
|
+
context.run(:down, 20171231235959)
|
76
|
+
}.not_to output(/Doing SuperUpdate/).to_stdout
|
77
|
+
end
|
78
|
+
|
79
|
+
it "alerts for an invalid specific migration" do
|
80
|
+
expect {
|
81
|
+
context.run(:up, 201712312)
|
82
|
+
}.to raise_error(
|
83
|
+
ActiveRecord::UnknownMigrationVersionError,
|
84
|
+
/No migration with version number 201712312/
|
85
|
+
)
|
86
|
+
end
|
87
|
+
|
88
|
+
it "rolls back latest migration" do
|
89
|
+
context.migrate(nil)
|
90
|
+
expect {
|
91
|
+
context.rollback
|
92
|
+
}.to output(/Undoing SuperUpdate/).to_stdout
|
93
|
+
versions = DataMigrate::DataSchemaMigration.normalized_versions
|
94
|
+
expect(versions.count).to eq(1)
|
95
|
+
expect(versions).to include("20091231235959")
|
96
|
+
end
|
97
|
+
|
98
|
+
it "rolls back 2 migrations" do
|
99
|
+
context.migrate(nil)
|
100
|
+
expect {
|
101
|
+
context.rollback(2)
|
102
|
+
}.to output(/Undoing SomeName/).to_stdout
|
103
|
+
versions = DataMigrate::DataSchemaMigration.normalized_versions
|
104
|
+
expect(versions.count).to eq(0)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
describe DataMigrate::SchemaDumper do
|
6
|
+
let(:subject) { DataMigrate::SchemaDumper }
|
7
|
+
let(:db_config) do
|
8
|
+
{
|
9
|
+
adapter: "sqlite3",
|
10
|
+
database: "spec/db/test.db"
|
11
|
+
}
|
12
|
+
end
|
13
|
+
let(:fixture_file_timestamps) do
|
14
|
+
%w[20091231235959 20101231235959 20111231235959]
|
15
|
+
end
|
16
|
+
|
17
|
+
describe :dump do
|
18
|
+
before do
|
19
|
+
allow(DataMigrate::DataMigrator).
|
20
|
+
to receive(:db_config) { db_config }.at_least(:once)
|
21
|
+
ActiveRecord::Base.establish_connection(db_config)
|
22
|
+
|
23
|
+
ActiveRecord::SchemaMigration.create_table
|
24
|
+
DataMigrate::DataMigrator.assure_data_schema_table
|
25
|
+
|
26
|
+
ActiveRecord::Base.connection.execute <<-SQL
|
27
|
+
INSERT INTO #{DataMigrate::DataSchemaMigration.table_name}
|
28
|
+
VALUES #{fixture_file_timestamps.map { |t| "(#{t})" }.join(', ')}
|
29
|
+
SQL
|
30
|
+
end
|
31
|
+
|
32
|
+
after do
|
33
|
+
ActiveRecord::Migration.drop_table("data_migrations")
|
34
|
+
end
|
35
|
+
|
36
|
+
it "writes the define method with the version key to the stream" do
|
37
|
+
stream = StringIO.new
|
38
|
+
DataMigrate::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
|
39
|
+
stream.rewind
|
40
|
+
|
41
|
+
last_version = fixture_file_timestamps.last
|
42
|
+
expected = "DataMigrate::Data.define(version: #{last_version})"
|
43
|
+
expect(stream.read).to include expected
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
describe DataMigrate::SchemaMigration do
|
6
|
+
let(:migration_path) {
|
7
|
+
if Rails::VERSION::MAJOR == 5
|
8
|
+
if Rails::VERSION::MINOR == 2
|
9
|
+
"spec/db/migrate/5.2"
|
10
|
+
else
|
11
|
+
"spec/db/migrate/5.0"
|
12
|
+
end
|
13
|
+
else
|
14
|
+
"spec/db/migrate/4.2"
|
15
|
+
end
|
16
|
+
}
|
17
|
+
|
18
|
+
let(:subject) { DataMigrate::SchemaMigration }
|
19
|
+
let(:db_config) do
|
20
|
+
{
|
21
|
+
adapter: "sqlite3",
|
22
|
+
database: "spec/db/test.db"
|
23
|
+
}
|
24
|
+
end
|
25
|
+
let(:fixture_file_timestamps) do
|
26
|
+
%w[20091231235959 20101231235959 20111231235959]
|
27
|
+
end
|
28
|
+
|
29
|
+
before do
|
30
|
+
ActiveRecord::Base.establish_connection(db_config)
|
31
|
+
ActiveRecord::SchemaMigration.create_table
|
32
|
+
end
|
33
|
+
|
34
|
+
after do
|
35
|
+
ActiveRecord::Migration.drop_table("schema_migrations")
|
36
|
+
end
|
37
|
+
|
38
|
+
describe :pending_schema_migrations do
|
39
|
+
it "list sorted schema migrations" do
|
40
|
+
expect(subject).to receive(:migrations_paths) {
|
41
|
+
migration_path
|
42
|
+
}
|
43
|
+
migrations = subject.pending_schema_migrations
|
44
|
+
|
45
|
+
expect(migrations.count).to eq 2
|
46
|
+
expect(migrations[0][:version]).to eq(20131111111111)
|
47
|
+
expect(migrations[1][:version]).to eq(20202020202011)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe :run do
|
52
|
+
it do
|
53
|
+
expect {
|
54
|
+
subject.run(:up, migration_path, 20202020202011)
|
55
|
+
}.to output(/20202020202011 DbMigration: migrating/).to_stdout
|
56
|
+
versions = ActiveRecord::SchemaMigration.normalized_versions
|
57
|
+
expect(versions.first).to eq("20202020202011")
|
58
|
+
end
|
59
|
+
|
60
|
+
it "undo migration" do
|
61
|
+
subject.run(:up, migration_path, 20202020202011)
|
62
|
+
expect {
|
63
|
+
subject.run(:down, migration_path, 20202020202011)
|
64
|
+
}.to output(/Undoing DbMigration/).to_stdout
|
65
|
+
versions = ActiveRecord::SchemaMigration.normalized_versions
|
66
|
+
expect(versions.count).to eq(0)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require "spec_helper"
|
3
|
+
|
4
|
+
describe DataMigrate::StatusService do
|
5
|
+
let(:subject) { DataMigrate::SchemaDumper }
|
6
|
+
let(:db_config) do
|
7
|
+
{
|
8
|
+
adapter: "sqlite3",
|
9
|
+
database: "spec/db/test.db"
|
10
|
+
}
|
11
|
+
end
|
12
|
+
let(:service) { DataMigrate::StatusService }
|
13
|
+
|
14
|
+
context "table does not exists" do
|
15
|
+
before do
|
16
|
+
ActiveRecord::Base.establish_connection(db_config)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "show error message" do
|
20
|
+
allow_any_instance_of(service).to receive(:table_name) { "bogus"}
|
21
|
+
stream = StringIO.new
|
22
|
+
|
23
|
+
service.dump(ActiveRecord::Base.connection, stream)
|
24
|
+
|
25
|
+
stream.rewind
|
26
|
+
expected = "Data migrations table does not exist"
|
27
|
+
expect(stream.read).to include expected
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context "table exists" do
|
32
|
+
let(:fixture_file_timestamps) do
|
33
|
+
%w[20091231235959 20101231235959 20111231235959]
|
34
|
+
end
|
35
|
+
|
36
|
+
before do
|
37
|
+
allow(DataMigrate::DataMigrator).
|
38
|
+
to receive(:db_config) { db_config }.at_least(:once)
|
39
|
+
ActiveRecord::Base.establish_connection(db_config)
|
40
|
+
|
41
|
+
ActiveRecord::SchemaMigration.create_table
|
42
|
+
DataMigrate::DataMigrator.assure_data_schema_table
|
43
|
+
|
44
|
+
ActiveRecord::Base.connection.execute <<-SQL
|
45
|
+
INSERT INTO #{DataMigrate::DataSchemaMigration.table_name}
|
46
|
+
VALUES #{fixture_file_timestamps.map { |t| "(#{t})" }.join(', ')}
|
47
|
+
SQL
|
48
|
+
|
49
|
+
allow_any_instance_of(service).to receive(:root_folder) { "spec" }
|
50
|
+
end
|
51
|
+
|
52
|
+
after do
|
53
|
+
ActiveRecord::Migration.drop_table("data_migrations")
|
54
|
+
end
|
55
|
+
|
56
|
+
it "shows successfully executed migration" do
|
57
|
+
stream = StringIO.new
|
58
|
+
service.dump(ActiveRecord::Base.connection, stream)
|
59
|
+
stream.rewind
|
60
|
+
|
61
|
+
expected = " up 20091231235959 Some name"
|
62
|
+
expect(stream.read).to include expected
|
63
|
+
end
|
64
|
+
|
65
|
+
it "shows missing file migration" do
|
66
|
+
stream = StringIO.new
|
67
|
+
service.dump(ActiveRecord::Base.connection, stream)
|
68
|
+
stream.rewind
|
69
|
+
|
70
|
+
expected = " up 20101231235959 ********** NO FILE **********"
|
71
|
+
s = stream.read
|
72
|
+
expect(s).to include expected
|
73
|
+
end
|
74
|
+
|
75
|
+
it "shows migration that has not run yet" do
|
76
|
+
stream = StringIO.new
|
77
|
+
service.dump(ActiveRecord::Base.connection, stream)
|
78
|
+
stream.rewind
|
79
|
+
|
80
|
+
expected = " down 20171231235959 Super update"
|
81
|
+
s = stream.read
|
82
|
+
expect(s).to include expected
|
83
|
+
end
|
84
|
+
|
85
|
+
it "outputs migrations in chronological order" do
|
86
|
+
stream = StringIO.new
|
87
|
+
service.dump(ActiveRecord::Base.connection, stream)
|
88
|
+
stream.rewind
|
89
|
+
s = stream.read
|
90
|
+
expect(s.index("20091231235959")).to be < s.index("20111231235959")
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
describe DataMigrate::Tasks::DataMigrateTasks do
|
6
|
+
let(:db_config) do
|
7
|
+
{
|
8
|
+
adapter: "sqlite3",
|
9
|
+
database: "spec/db/test.db"
|
10
|
+
}
|
11
|
+
end
|
12
|
+
|
13
|
+
before do
|
14
|
+
if Rails::VERSION::MAJOR == 5
|
15
|
+
if Rails::VERSION::MINOR == 2
|
16
|
+
allow(DataMigrate::Tasks::DataMigrateTasks).to receive(:migrations_paths) {
|
17
|
+
"spec/db/data"
|
18
|
+
}
|
19
|
+
else
|
20
|
+
allow(DataMigrate::Tasks::DataMigrateTasks).to receive(:migrations_paths) {
|
21
|
+
"spec/db/5.0"
|
22
|
+
}
|
23
|
+
end
|
24
|
+
else
|
25
|
+
allow(DataMigrate::Tasks::DataMigrateTasks).to receive(:migrations_paths) {
|
26
|
+
"spec/db/4.2"
|
27
|
+
}
|
28
|
+
end
|
29
|
+
allow(DataMigrate::DataMigrator).to receive(:db_config) { db_config }
|
30
|
+
ActiveRecord::Base.establish_connection(db_config)
|
31
|
+
end
|
32
|
+
|
33
|
+
after do
|
34
|
+
ActiveRecord::Migration.drop_table("data_migrations")
|
35
|
+
end
|
36
|
+
|
37
|
+
describe :migrate do
|
38
|
+
it do
|
39
|
+
expect {
|
40
|
+
DataMigrate::Tasks::DataMigrateTasks.migrate
|
41
|
+
}.to output(/20091231235959 SomeName: migrating/).to_stdout
|
42
|
+
end
|
43
|
+
|
44
|
+
it do
|
45
|
+
expect {
|
46
|
+
DataMigrate::Tasks::DataMigrateTasks.migrate
|
47
|
+
}.to output(/20171231235959 SuperUpdate: migrating/).to_stdout
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|