data_migrate 11.1.0 → 11.2.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 +7 -0
- data/README.md +4 -12
- data/lib/data_migrate/database_tasks.rb +3 -1
- data/lib/data_migrate/rails_helper.rb +1 -0
- data/lib/data_migrate/version.rb +1 -1
- metadata +2 -51
- data/.github/workflows/build.yml +0 -39
- data/.github/workflows/gempush.yml +0 -28
- data/.gitignore +0 -12
- data/.hound.yml +0 -4
- data/.overcommit.yml +0 -21
- data/.rbenv-gemsets +0 -2
- data/.rspec +0 -3
- data/.rubocop.yml +0 -2
- data/.ruby-style.yml +0 -1061
- data/Appraisals +0 -19
- data/Gemfile +0 -3
- data/Gemfile.lock +0 -192
- data/Rakefile +0 -2
- data/data_migrate.gemspec +0 -41
- data/gemfiles/rails_6.1.gemfile +0 -7
- data/gemfiles/rails_6.1.gemfile.lock +0 -237
- data/gemfiles/rails_7.0.gemfile +0 -7
- data/gemfiles/rails_7.0.gemfile.lock +0 -238
- data/gemfiles/rails_7.1.gemfile +0 -7
- data/gemfiles/rails_7.1.gemfile.lock +0 -277
- data/gemfiles/rails_7.2.gemfile +0 -7
- data/gemfiles/rails_7.2.gemfile.lock +0 -274
- data/gemfiles/rails_8.0.gemfile +0 -7
- data/gemfiles/rails_8.0.gemfile.lock +0 -268
- data/screenshot.png +0 -0
- data/spec/data_migrate/config_spec.rb +0 -69
- data/spec/data_migrate/data_migrator_spec.rb +0 -84
- data/spec/data_migrate/data_schema_migration_spec.rb +0 -33
- data/spec/data_migrate/data_spec.rb +0 -74
- data/spec/data_migrate/database_tasks_spec.rb +0 -214
- data/spec/data_migrate/migration.rb +0 -17
- data/spec/data_migrate/migration_context_spec.rb +0 -117
- data/spec/data_migrate/schema_dumper_spec.rb +0 -36
- data/spec/data_migrate/schema_migration_spec.rb +0 -104
- data/spec/data_migrate/status_service_spec.rb +0 -76
- data/spec/data_migrate/tasks/data_migrate_tasks_spec.rb +0 -129
- data/spec/db/data/20091231235959_some_name.rb +0 -9
- data/spec/db/data/20171231235959_super_update.rb +0 -9
- data/spec/db/data/20181128000207_excluded_file.rb.other_ext +0 -1
- data/spec/db/data/partial_schema/data_schema.rb +0 -1
- data/spec/db/data/partial_schema/test_data_schema.rb +0 -1
- data/spec/db/data/schema/data_schema.rb +0 -1
- data/spec/db/data/schema/test_data_schema.rb +0 -1
- data/spec/db/data_two/20241231235959_data_two_update.rb +0 -9
- data/spec/db/migrate/20131111111111_late_migration.rb +0 -9
- data/spec/db/migrate/20202020202011_db_migration.rb +0 -9
- data/spec/generators/data_migration/data_migration_generator_spec.rb +0 -101
- data/spec/spec_helper.rb +0 -28
- data/tasks/.gitkeep +0 -0
@@ -1,33 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe DataMigrate::DataSchemaMigration do
|
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
|
25
|
-
end
|
26
|
-
|
27
|
-
describe :index_name do
|
28
|
-
it "returns correct primary key name" do
|
29
|
-
expect(subject.primary_key).to eq("version")
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
@@ -1,74 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "spec_helper"
|
4
|
-
|
5
|
-
describe DataMigrate::Data do
|
6
|
-
let(:subject) { DataMigrate::Data }
|
7
|
-
let(:fixture_file_timestamps) do
|
8
|
-
%w[20091231235959 20101231235959 20111231235959]
|
9
|
-
end
|
10
|
-
|
11
|
-
around do |example|
|
12
|
-
Dir.mktmpdir do |temp_dir|
|
13
|
-
@temp_dir = temp_dir
|
14
|
-
|
15
|
-
# create the fake data migration files
|
16
|
-
fixture_file_timestamps.each do |timestamp|
|
17
|
-
FileUtils.touch File.join(temp_dir, "#{timestamp}_data_migration.rb")
|
18
|
-
end
|
19
|
-
|
20
|
-
example.run
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
describe :define do
|
25
|
-
after do
|
26
|
-
ActiveRecord::Migration.drop_table("data_migrations")
|
27
|
-
end
|
28
|
-
|
29
|
-
context "when no version is supplied" do
|
30
|
-
it "returns nil" do
|
31
|
-
expect(subject.define(version: nil)).to be_nil
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
context "when a version is supplied" do
|
36
|
-
before do
|
37
|
-
allow(DataMigrate::DataMigrator).
|
38
|
-
to receive(:full_migrations_path).and_return(@temp_dir)
|
39
|
-
end
|
40
|
-
|
41
|
-
it "sets the current version to the supplied version" do
|
42
|
-
version = fixture_file_timestamps[1]
|
43
|
-
|
44
|
-
expect(DataMigrate::DataMigrator.current_version).not_to eq version.to_i
|
45
|
-
subject.define(version: version)
|
46
|
-
expect(DataMigrate::DataMigrator.current_version).to eq version.to_i
|
47
|
-
end
|
48
|
-
|
49
|
-
it "creates entries for migration versions that come " \
|
50
|
-
"before the supplied version" do
|
51
|
-
|
52
|
-
version = fixture_file_timestamps[1]
|
53
|
-
|
54
|
-
subject.define(version: version)
|
55
|
-
|
56
|
-
sql_select = <<-SQL
|
57
|
-
SELECT version
|
58
|
-
FROM #{DataMigrate::RailsHelper.data_schema_migration.table_name}
|
59
|
-
SQL
|
60
|
-
|
61
|
-
db_list_data = ActiveRecord::Base.connection.
|
62
|
-
select_values(sql_select).map(&:to_i)
|
63
|
-
expect(db_list_data).to match_array(
|
64
|
-
[fixture_file_timestamps[0], fixture_file_timestamps[1]].map(&:to_i)
|
65
|
-
)
|
66
|
-
|
67
|
-
# The last remaining migration (fixture_file_timestamps[2]) was
|
68
|
-
# not included as part of the supplied version and so should not
|
69
|
-
# appear in the data_migrations table.
|
70
|
-
expect(db_list_data).not_to include(fixture_file_timestamps[2])
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|
@@ -1,214 +0,0 @@
|
|
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) { "spec/db/migrate" }
|
8
|
-
let(:data_migrations_path) { DataMigrate.config.data_migrations_path }
|
9
|
-
|
10
|
-
before do
|
11
|
-
# In a normal Rails installation, db_dir would defer to
|
12
|
-
# Rails.application.config.paths["db"].first
|
13
|
-
# @see https://github.com/rails/rails/blob/a7d49ef78c36df2d1ca876451f30915ada1079a5/activerecord/lib/active_record/tasks/database_tasks.rb#L54
|
14
|
-
allow(subject).to receive(:db_dir).and_return("db")
|
15
|
-
allow(ActiveRecord::Tasks::DatabaseTasks).to receive(:db_dir).and_return("db")
|
16
|
-
end
|
17
|
-
|
18
|
-
before do
|
19
|
-
allow(DataMigrate::Tasks::DataMigrateTasks).to receive(:migrations_paths) do
|
20
|
-
data_migrations_path
|
21
|
-
end
|
22
|
-
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: "spec/db/test.db")
|
23
|
-
hash_config = ActiveRecord::DatabaseConfigurations::HashConfig.new(
|
24
|
-
'test', 'test', adapter: "sqlite3", database: "spec/db/test.db"
|
25
|
-
)
|
26
|
-
config_obj = ActiveRecord::DatabaseConfigurations.new([hash_config])
|
27
|
-
allow(ActiveRecord::Base).to receive(:configurations).and_return(config_obj)
|
28
|
-
end
|
29
|
-
|
30
|
-
context "migrations" do
|
31
|
-
after do
|
32
|
-
ActiveRecord::Migration.drop_table("data_migrations") rescue nil
|
33
|
-
ActiveRecord::Migration.drop_table("schema_migrations") rescue nil
|
34
|
-
end
|
35
|
-
|
36
|
-
before do
|
37
|
-
DataMigrate::RailsHelper.schema_migration.create_table
|
38
|
-
|
39
|
-
allow(DataMigrate::SchemaMigration).to receive(:migrations_paths) { migration_path }
|
40
|
-
allow(DataMigrate::DatabaseTasks).to receive(:data_migrations_path) do
|
41
|
-
data_migrations_path
|
42
|
-
end.at_least(:once)
|
43
|
-
end
|
44
|
-
|
45
|
-
describe :past_migrations do
|
46
|
-
it "returns past migration records" do
|
47
|
-
subject.forward
|
48
|
-
migrations = subject.past_migrations
|
49
|
-
expect(migrations.count).to eq 1
|
50
|
-
expect(migrations.first[:version]).to eq 20091231235959
|
51
|
-
end
|
52
|
-
|
53
|
-
it "shows nothing without any migrations" do
|
54
|
-
migrations = subject.past_migrations
|
55
|
-
expect(migrations.count).to eq 0
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
describe :forward do
|
60
|
-
it "run forward default amount of times" do
|
61
|
-
subject.forward
|
62
|
-
versions = DataMigrate::RailsHelper.data_schema_migration.normalized_versions
|
63
|
-
expect(versions.count).to eq(1)
|
64
|
-
end
|
65
|
-
|
66
|
-
it "run forward defined number of times" do
|
67
|
-
subject.forward(2)
|
68
|
-
versions = DataMigrate::RailsHelper.data_schema_migration.normalized_versions
|
69
|
-
expect(versions.count).to eq(1)
|
70
|
-
expect(versions.first).to eq "20091231235959"
|
71
|
-
versions = DataMigrate::RailsHelper.schema_migration.normalized_versions
|
72
|
-
expect(versions.count).to eq(1)
|
73
|
-
expect(versions.first).to eq "20131111111111"
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
if DataMigrate::RailsHelper.rails_version_equal_to_or_higher_than_7_0
|
78
|
-
describe :schema_dump_path do
|
79
|
-
before do
|
80
|
-
allow(ActiveRecord::Base).to receive(:configurations)
|
81
|
-
.and_return(ActiveRecord::DatabaseConfigurations.new([db_config]))
|
82
|
-
end
|
83
|
-
|
84
|
-
context "for primary database" do
|
85
|
-
let(:db_config) do
|
86
|
-
ActiveRecord::DatabaseConfigurations::HashConfig.new("development", "primary", {})
|
87
|
-
end
|
88
|
-
|
89
|
-
context "for :ruby db format" do
|
90
|
-
it 'returns the data schema path' do
|
91
|
-
allow(ActiveRecord).to receive(:schema_format).and_return(:ruby)
|
92
|
-
expect(subject.schema_dump_path(db_config)).to eq("db/data_schema.rb")
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
context "for :sql db format" do
|
97
|
-
it 'returns the data schema path' do
|
98
|
-
allow(ActiveRecord).to receive(:schema_format).and_return(:sql)
|
99
|
-
expect(subject.schema_dump_path(db_config, :sql)).to eq("db/data_schema.rb")
|
100
|
-
end
|
101
|
-
end
|
102
|
-
end
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
describe :prepare_all_with_data do
|
107
|
-
let(:db_config) do
|
108
|
-
ActiveRecord::DatabaseConfigurations::HashConfig.new(
|
109
|
-
'test',
|
110
|
-
'primary',
|
111
|
-
adapter: "sqlite3",
|
112
|
-
database: "spec/db/test.db"
|
113
|
-
)
|
114
|
-
end
|
115
|
-
|
116
|
-
let(:pool) { double("ConnectionPool") }
|
117
|
-
let(:connection) { double("Connection") }
|
118
|
-
|
119
|
-
before do
|
120
|
-
allow(subject).to receive(:each_current_configuration).and_yield(db_config)
|
121
|
-
allow(subject).to receive(:with_temporary_pool).with(db_config).and_yield(pool)
|
122
|
-
allow(pool).to receive(:connection).and_return(connection)
|
123
|
-
allow(subject).to receive(:schema_dump_path).and_return("db/data_schema.rb")
|
124
|
-
allow(File).to receive(:exist?).and_return(true)
|
125
|
-
allow(subject).to receive(:load_schema)
|
126
|
-
allow(subject).to receive(:load_schema_current)
|
127
|
-
allow(subject).to receive(:migrate_with_data)
|
128
|
-
allow(subject).to receive(:dump_schema)
|
129
|
-
allow(DataMigrate::Tasks::DataMigrateTasks).to receive(:dump)
|
130
|
-
allow(subject).to receive(:load_seed)
|
131
|
-
|
132
|
-
configurations = ActiveRecord::DatabaseConfigurations.new([db_config])
|
133
|
-
allow(ActiveRecord::Base).to receive(:configurations).and_return(configurations)
|
134
|
-
end
|
135
|
-
|
136
|
-
context "when the database does not exist" do
|
137
|
-
before do
|
138
|
-
allow(subject).to receive(:database_exists?).with(connection).and_return(false)
|
139
|
-
allow_any_instance_of(ActiveRecord::Tasks::DatabaseTasks).to receive(:create)
|
140
|
-
.and_return(true)
|
141
|
-
end
|
142
|
-
|
143
|
-
it "creates the database" do
|
144
|
-
expect_any_instance_of(ActiveRecord::Tasks::DatabaseTasks).to receive(:create)
|
145
|
-
.with(db_config)
|
146
|
-
subject.prepare_all_with_data
|
147
|
-
end
|
148
|
-
|
149
|
-
it "loads the schema" do
|
150
|
-
expect(subject).to receive(:load_schema).with(
|
151
|
-
db_config,
|
152
|
-
subject.send(:schema_format),
|
153
|
-
nil
|
154
|
-
)
|
155
|
-
subject.prepare_all_with_data
|
156
|
-
end
|
157
|
-
|
158
|
-
it "loads the current data schema" do
|
159
|
-
expect(subject).to receive(:load_schema_current).with(:ruby, ENV["DATA_SCHEMA"])
|
160
|
-
subject.prepare_all_with_data
|
161
|
-
end
|
162
|
-
|
163
|
-
it "runs migrations with data" do
|
164
|
-
expect(subject).to receive(:migrate_with_data)
|
165
|
-
subject.prepare_all_with_data
|
166
|
-
end
|
167
|
-
|
168
|
-
it "dumps the schema after migration" do
|
169
|
-
expect(subject).to receive(:dump_schema).with(db_config)
|
170
|
-
expect(DataMigrate::Tasks::DataMigrateTasks).to receive(:dump)
|
171
|
-
subject.prepare_all_with_data
|
172
|
-
end
|
173
|
-
|
174
|
-
it "loads seed data" do
|
175
|
-
expect(subject).to receive(:load_seed)
|
176
|
-
subject.prepare_all_with_data
|
177
|
-
end
|
178
|
-
end
|
179
|
-
|
180
|
-
context "when the database exists" do
|
181
|
-
before do
|
182
|
-
allow(subject).to receive(:database_exists?).with(connection).and_return(true)
|
183
|
-
end
|
184
|
-
|
185
|
-
it "does not create the database" do
|
186
|
-
expect(ActiveRecord::Tasks::DatabaseTasks).not_to receive(:create)
|
187
|
-
subject.prepare_all_with_data
|
188
|
-
end
|
189
|
-
|
190
|
-
it "does not load the schema" do
|
191
|
-
expect(subject).not_to receive(:load_schema)
|
192
|
-
expect(subject).not_to receive(:load_schema_current)
|
193
|
-
subject.prepare_all_with_data
|
194
|
-
end
|
195
|
-
|
196
|
-
it "runs migrations with data" do
|
197
|
-
expect(subject).to receive(:migrate_with_data)
|
198
|
-
subject.prepare_all_with_data
|
199
|
-
end
|
200
|
-
|
201
|
-
it "dumps the schema after migration" do
|
202
|
-
expect(subject).to receive(:dump_schema).with(db_config)
|
203
|
-
expect(DataMigrate::Tasks::DataMigrateTasks).to receive(:dump)
|
204
|
-
subject.prepare_all_with_data
|
205
|
-
end
|
206
|
-
|
207
|
-
it "does not load seed data" do
|
208
|
-
expect(subject).not_to receive(:load_seed)
|
209
|
-
subject.prepare_all_with_data
|
210
|
-
end
|
211
|
-
end
|
212
|
-
end
|
213
|
-
end
|
214
|
-
end
|
@@ -1,17 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
|
-
describe DataMigrate::Migration do
|
6
|
-
describe ".table_name" do
|
7
|
-
it "returns correct table name" do
|
8
|
-
expect(subject.table_name).to eq("data_migrations")
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
describe ".index_name" do
|
13
|
-
it "returns correct primary key name" do
|
14
|
-
expect(subject.primary_key).to eq("version")
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
@@ -1,117 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "spec_helper"
|
4
|
-
|
5
|
-
describe DataMigrate::DataMigrator do
|
6
|
-
let(:context) { DataMigrate::MigrationContext.new("spec/db/data") }
|
7
|
-
let(:schema_context) { ActiveRecord::MigrationContext.new("spec/db/migrate", ar_schema_migration) }
|
8
|
-
let(:db_config) do
|
9
|
-
{
|
10
|
-
adapter: "sqlite3",
|
11
|
-
database: "spec/db/test.db"
|
12
|
-
}
|
13
|
-
end
|
14
|
-
|
15
|
-
before do
|
16
|
-
ActiveRecord::Base.establish_connection(db_config)
|
17
|
-
DataMigrate::RailsHelper.schema_migration.create_table
|
18
|
-
DataMigrate::RailsHelper.data_schema_migration.create_table
|
19
|
-
end
|
20
|
-
|
21
|
-
after do
|
22
|
-
ActiveRecord::Migration.drop_table("data_migrations") rescue nil
|
23
|
-
ActiveRecord::Migration.drop_table("schema_migrations") rescue nil
|
24
|
-
end
|
25
|
-
|
26
|
-
describe "migrate" do
|
27
|
-
it "migrates existing file" do
|
28
|
-
context.migrate(nil)
|
29
|
-
context.migrations_status
|
30
|
-
versions = DataMigrate::RailsHelper.data_schema_migration.normalized_versions
|
31
|
-
expect(versions.count).to eq(2)
|
32
|
-
expect(versions).to include("20091231235959")
|
33
|
-
expect(versions).to include("20171231235959")
|
34
|
-
end
|
35
|
-
|
36
|
-
it "undo migration" do
|
37
|
-
context.migrate(nil)
|
38
|
-
context.run(:down, 20171231235959)
|
39
|
-
versions = DataMigrate::RailsHelper.data_schema_migration.normalized_versions
|
40
|
-
expect(versions.count).to eq(1)
|
41
|
-
expect(versions).to include("20091231235959")
|
42
|
-
end
|
43
|
-
|
44
|
-
it "does not do anything if migration is undone twice" do
|
45
|
-
context.migrate(nil)
|
46
|
-
expect {
|
47
|
-
context.run(:down, 20171231235959)
|
48
|
-
}.to output(/Undoing SuperUpdate/).to_stdout
|
49
|
-
expect {
|
50
|
-
context.run(:down, 20171231235959)
|
51
|
-
}.not_to output(/Undoing SuperUpdate/).to_stdout
|
52
|
-
end
|
53
|
-
|
54
|
-
it "runs a specific migration" do
|
55
|
-
context.run(:up, 20171231235959)
|
56
|
-
versions = DataMigrate::RailsHelper.data_schema_migration.normalized_versions
|
57
|
-
expect(versions.count).to eq(1)
|
58
|
-
expect(versions).to include("20171231235959")
|
59
|
-
end
|
60
|
-
|
61
|
-
it "does not do anything if migration is ran twice" do
|
62
|
-
expect {
|
63
|
-
context.run(:up, 20171231235959)
|
64
|
-
}.to output(/Doing SuperUpdate/).to_stdout
|
65
|
-
expect {
|
66
|
-
context.run(:down, 20171231235959)
|
67
|
-
}.not_to output(/Doing SuperUpdate/).to_stdout
|
68
|
-
end
|
69
|
-
|
70
|
-
it "alerts for an invalid specific migration" do
|
71
|
-
expect {
|
72
|
-
context.run(:up, 201712312)
|
73
|
-
}.to raise_error(
|
74
|
-
ActiveRecord::UnknownMigrationVersionError,
|
75
|
-
/No migration with version number 201712312/
|
76
|
-
)
|
77
|
-
end
|
78
|
-
|
79
|
-
it "rolls back latest migration" do
|
80
|
-
context.migrate(nil)
|
81
|
-
expect {
|
82
|
-
context.rollback
|
83
|
-
}.to output(/Undoing SuperUpdate/).to_stdout
|
84
|
-
versions = DataMigrate::RailsHelper.data_schema_migration.normalized_versions
|
85
|
-
expect(versions.count).to eq(1)
|
86
|
-
expect(versions).to include("20091231235959")
|
87
|
-
end
|
88
|
-
|
89
|
-
it "rolls back 2 migrations" do
|
90
|
-
context.migrate(nil)
|
91
|
-
schema_context.migrate(nil)
|
92
|
-
expect {
|
93
|
-
context.rollback(2)
|
94
|
-
}.to output(/Undoing SomeName/).to_stdout
|
95
|
-
versions = DataMigrate::RailsHelper.data_schema_migration.normalized_versions
|
96
|
-
expect(versions.count).to eq(0)
|
97
|
-
end
|
98
|
-
|
99
|
-
it "rolls back 2 migrations" do
|
100
|
-
context.migrate(nil)
|
101
|
-
expect {
|
102
|
-
context.rollback(2)
|
103
|
-
}.to output(/Undoing SomeName/).to_stdout
|
104
|
-
versions = DataMigrate::RailsHelper.data_schema_migration.normalized_versions
|
105
|
-
expect(versions.count).to eq(0)
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
|
-
# schema migration changed in Rails 7.2, from the connection to the pool object.
|
110
|
-
def ar_schema_migration
|
111
|
-
if ActiveRecord::Base.connection_pool.respond_to?(:schema_migration)
|
112
|
-
ActiveRecord::Base.connection_pool.schema_migration
|
113
|
-
else
|
114
|
-
ActiveRecord::Base.connection.schema_migration
|
115
|
-
end
|
116
|
-
end
|
117
|
-
end
|
@@ -1,36 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "spec_helper"
|
4
|
-
|
5
|
-
describe DataMigrate::SchemaDumper do
|
6
|
-
let(:subject) { DataMigrate::SchemaDumper }
|
7
|
-
let(:fixture_file_timestamps) do
|
8
|
-
%w[20091231235959 20101231235959 20111231235959]
|
9
|
-
end
|
10
|
-
|
11
|
-
before do
|
12
|
-
DataMigrate::RailsHelper.schema_migration.create_table
|
13
|
-
DataMigrate::RailsHelper.data_schema_migration.create_table
|
14
|
-
|
15
|
-
fixture_file_timestamps.map do |t|
|
16
|
-
DataMigrate::RailsHelper.data_schema_migration.create_version(t)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
after do
|
21
|
-
ActiveRecord::Migration.drop_table("data_migrations") rescue nil
|
22
|
-
ActiveRecord::Migration.drop_table("schema_migrations") rescue nil
|
23
|
-
end
|
24
|
-
|
25
|
-
describe ".dump" do
|
26
|
-
it "writes the define method with the version key to the stream" do
|
27
|
-
stream = StringIO.new
|
28
|
-
DataMigrate::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
|
29
|
-
stream.rewind
|
30
|
-
|
31
|
-
last_version = fixture_file_timestamps.last
|
32
|
-
expected = "DataMigrate::Data.define(version: #{last_version})"
|
33
|
-
expect(stream.read).to include expected
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
@@ -1,104 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "spec_helper"
|
4
|
-
|
5
|
-
describe DataMigrate::SchemaMigration do
|
6
|
-
let(:subject) { DataMigrate::SchemaMigration }
|
7
|
-
let(:migration_path) { "spec/db/migrate" }
|
8
|
-
let(:fixture_file_timestamps) do
|
9
|
-
%w[20091231235959 20101231235959 20111231235959]
|
10
|
-
end
|
11
|
-
let(:db_config) do
|
12
|
-
{
|
13
|
-
adapter: "sqlite3",
|
14
|
-
database: "spec/db/test.db"
|
15
|
-
}
|
16
|
-
end
|
17
|
-
|
18
|
-
before do
|
19
|
-
ActiveRecord::Base.establish_connection(db_config)
|
20
|
-
DataMigrate::RailsHelper.schema_migration.create_table
|
21
|
-
DataMigrate::RailsHelper.data_schema_migration.create_table
|
22
|
-
end
|
23
|
-
|
24
|
-
after do
|
25
|
-
ActiveRecord::Migration.drop_table("data_migrations") rescue nil
|
26
|
-
ActiveRecord::Migration.drop_table("schema_migrations") rescue nil
|
27
|
-
end
|
28
|
-
|
29
|
-
describe :pending_schema_migrations do
|
30
|
-
it "list sorted schema migrations" do
|
31
|
-
expect(subject).to receive(:migrations_paths) {
|
32
|
-
migration_path
|
33
|
-
}
|
34
|
-
|
35
|
-
migrations = subject.pending_schema_migrations
|
36
|
-
|
37
|
-
expect(migrations.count).to eq 2
|
38
|
-
expect(migrations[0][:version]).to eq(20131111111111)
|
39
|
-
expect(migrations[1][:version]).to eq(20202020202011)
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
describe :run do
|
44
|
-
it "can run up task" do
|
45
|
-
expect {
|
46
|
-
subject.run(:up, migration_path, 20202020202011)
|
47
|
-
}.to output(/20202020202011 DbMigration: migrating/).to_stdout
|
48
|
-
versions = DataMigrate::RailsHelper.schema_migration.normalized_versions
|
49
|
-
expect(versions.first).to eq("20202020202011")
|
50
|
-
end
|
51
|
-
|
52
|
-
it "can run down task" do
|
53
|
-
subject.run(:up, migration_path, 20202020202011)
|
54
|
-
|
55
|
-
expect {
|
56
|
-
subject.run(:down, migration_path, 20202020202011)
|
57
|
-
}.to output(/Undoing DbMigration/).to_stdout
|
58
|
-
|
59
|
-
versions = DataMigrate::RailsHelper.schema_migration.normalized_versions
|
60
|
-
|
61
|
-
expect(versions.count).to eq(0)
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
describe :migrations_paths do
|
66
|
-
context 'when a db_name is configured' do
|
67
|
-
let(:config) { double(:config) }
|
68
|
-
let(:paths) { ['spec/db/migrate', 'spec/db/migrate/other'] }
|
69
|
-
let(:specification_name) { "primary" }
|
70
|
-
let(:config_options) do
|
71
|
-
if Gem::Dependency.new("railties", "~> 6.1").match?("railties", Gem.loaded_specs["railties"].version)
|
72
|
-
{ env_name: Rails.env, spec_name: specification_name }
|
73
|
-
else
|
74
|
-
{ env_name: Rails.env, name: specification_name }
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
before do
|
79
|
-
@original_config_spec_name = DataMigrate.config.spec_name
|
80
|
-
|
81
|
-
DataMigrate.configure do |config|
|
82
|
-
config.spec_name = specification_name
|
83
|
-
end
|
84
|
-
|
85
|
-
allow(ActiveRecord::Base.configurations)
|
86
|
-
.to receive(:configs_for)
|
87
|
-
.with(config_options)
|
88
|
-
.and_return(config)
|
89
|
-
allow(config).to receive(:migrations_paths).and_return(paths)
|
90
|
-
end
|
91
|
-
|
92
|
-
after do
|
93
|
-
DataMigrate.configure do |config|
|
94
|
-
config.spec_name = @original_config_spec_name
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
it 'lists schema migration paths' do
|
99
|
-
expect(subject.migrations_paths.size).to eq(paths.count)
|
100
|
-
expect(subject.migrations_paths).to eq(paths)
|
101
|
-
end
|
102
|
-
end
|
103
|
-
end
|
104
|
-
end
|
@@ -1,76 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "spec_helper"
|
4
|
-
|
5
|
-
describe DataMigrate::StatusService do
|
6
|
-
let(:subject) { DataMigrate::StatusService }
|
7
|
-
let(:stream) { StringIO.new }
|
8
|
-
let(:stream_data) { stream.read }
|
9
|
-
let(:connection_db_config) do
|
10
|
-
if Gem::Dependency.new("railties", ">= 6.1").match?("railties", Gem.loaded_specs["railties"].version)
|
11
|
-
ActiveRecord::Base.connection_db_config
|
12
|
-
else
|
13
|
-
ActiveRecord::Base.configurations.configs_for.first
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
context "table does not exists" do
|
18
|
-
before do
|
19
|
-
allow_any_instance_of(subject).to receive(:table_name) { "bogus"}
|
20
|
-
|
21
|
-
subject.dump(connection_db_config, stream)
|
22
|
-
stream.rewind
|
23
|
-
end
|
24
|
-
|
25
|
-
it "show error message" do
|
26
|
-
expect(stream_data).to include("Data migrations table does not exist")
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
context "table exists" do
|
31
|
-
let(:fixture_file_timestamps) do
|
32
|
-
%w[20091231235959 20101231235959 20111231235959]
|
33
|
-
end
|
34
|
-
|
35
|
-
before do
|
36
|
-
DataMigrate::RailsHelper.schema_migration.create_table
|
37
|
-
DataMigrate::RailsHelper.data_schema_migration.create_table
|
38
|
-
|
39
|
-
fixture_file_timestamps.map do |t|
|
40
|
-
DataMigrate::RailsHelper.data_schema_migration.create_version(t)
|
41
|
-
end
|
42
|
-
|
43
|
-
subject.dump(connection_db_config, stream)
|
44
|
-
stream.rewind
|
45
|
-
end
|
46
|
-
|
47
|
-
after do
|
48
|
-
ActiveRecord::Migration.drop_table("data_migrations") rescue nil
|
49
|
-
ActiveRecord::Migration.drop_table("schema_migrations") rescue nil
|
50
|
-
end
|
51
|
-
|
52
|
-
it "shows successfully executed migration" do
|
53
|
-
expected = " up 20091231235959 Some name"
|
54
|
-
expect(stream_data).to include expected
|
55
|
-
end
|
56
|
-
|
57
|
-
it "excludes files without .rb extension" do
|
58
|
-
expected = "20181128000207 Excluded file"
|
59
|
-
expect(stream_data).to_not include expected
|
60
|
-
end
|
61
|
-
|
62
|
-
it "shows missing file migration" do
|
63
|
-
expected = " up 20101231235959 ********** NO FILE **********"
|
64
|
-
expect(stream_data).to include expected
|
65
|
-
end
|
66
|
-
|
67
|
-
it "shows migration that has not run yet" do
|
68
|
-
expected = " down 20171231235959 Super update"
|
69
|
-
expect(stream_data).to include expected
|
70
|
-
end
|
71
|
-
|
72
|
-
it "outputs migrations in chronological order" do
|
73
|
-
expect(stream_data.index("20091231235959")).to be < stream_data.index("20111231235959")
|
74
|
-
end
|
75
|
-
end
|
76
|
-
end
|