data_migrate 9.1.0 → 11.2.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.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/Changelog.md +40 -0
  3. data/README.md +22 -15
  4. data/lib/data_migrate/data_migrator.rb +15 -23
  5. data/lib/data_migrate/data_schema.rb +7 -7
  6. data/lib/data_migrate/data_schema_migration.rb +24 -7
  7. data/lib/data_migrate/database_configurations_wrapper.rb +11 -0
  8. data/lib/data_migrate/database_tasks.rb +224 -61
  9. data/lib/data_migrate/migration_context.rb +11 -8
  10. data/lib/data_migrate/rails_helper.rb +91 -0
  11. data/lib/data_migrate/schema_dumper.rb +1 -1
  12. data/lib/data_migrate/schema_migration.rb +5 -4
  13. data/lib/data_migrate/status_service.rb +3 -3
  14. data/lib/data_migrate/tasks/data_migrate_tasks.rb +11 -9
  15. data/lib/data_migrate/version.rb +1 -1
  16. data/lib/data_migrate.rb +2 -1
  17. data/lib/generators/data_migration/data_migration_generator.rb +2 -1
  18. data/tasks/databases.rake +9 -46
  19. metadata +13 -54
  20. data/.github/workflows/build.yml +0 -34
  21. data/.github/workflows/gempush.yml +0 -28
  22. data/.gitignore +0 -13
  23. data/.hound.yml +0 -4
  24. data/.overcommit.yml +0 -21
  25. data/.rbenv-gemsets +0 -2
  26. data/.rspec +0 -3
  27. data/.rubocop.yml +0 -2
  28. data/.ruby-style.yml +0 -1061
  29. data/Appraisals +0 -11
  30. data/Gemfile +0 -4
  31. data/Gemfile.rails6.1 +0 -11
  32. data/Rakefile +0 -2
  33. data/data_migrate.gemspec +0 -41
  34. data/gemfiles/rails_6.0.gemfile +0 -8
  35. data/gemfiles/rails_6.1.gemfile +0 -8
  36. data/gemfiles/rails_7.0.gemfile +0 -8
  37. data/lib/data_migrate/legacy_migrator.rb +0 -22
  38. data/screenshot.png +0 -0
  39. data/spec/data_migrate/config_spec.rb +0 -69
  40. data/spec/data_migrate/data_migrator_spec.rb +0 -80
  41. data/spec/data_migrate/data_schema_migration_spec.rb +0 -16
  42. data/spec/data_migrate/data_spec.rb +0 -74
  43. data/spec/data_migrate/database_tasks_spec.rb +0 -90
  44. data/spec/data_migrate/legacy_migrator_spec.rb +0 -38
  45. data/spec/data_migrate/migration.rb +0 -17
  46. data/spec/data_migrate/migration_context_spec.rb +0 -101
  47. data/spec/data_migrate/schema_dumper_spec.rb +0 -33
  48. data/spec/data_migrate/schema_migration_spec.rb +0 -97
  49. data/spec/data_migrate/status_service_spec.rb +0 -73
  50. data/spec/data_migrate/tasks/data_migrate_tasks_spec.rb +0 -129
  51. data/spec/db/data/20091231235959_some_name.rb +0 -9
  52. data/spec/db/data/20171231235959_super_update.rb +0 -9
  53. data/spec/db/data/20181128000207_excluded_file.rb.other_ext +0 -1
  54. data/spec/db/data/partial_schema/data_schema.rb +0 -1
  55. data/spec/db/data/partial_schema/test_data_schema.rb +0 -1
  56. data/spec/db/data/schema/data_schema.rb +0 -1
  57. data/spec/db/data/schema/test_data_schema.rb +0 -1
  58. data/spec/db/migrate/20131111111111_late_migration.rb +0 -9
  59. data/spec/db/migrate/20202020202011_db_migration.rb +0 -9
  60. data/spec/generators/data_migration/data_migration_generator_spec.rb +0 -101
  61. data/spec/spec_helper.rb +0 -28
  62. data/tasks/.gitkeep +0 -0
@@ -1,97 +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
-
12
- before do
13
- ActiveRecord::SchemaMigration.create_table
14
- DataMigrate::DataSchemaMigration.create_table
15
- end
16
-
17
- after do
18
- ActiveRecord::Migration.drop_table("data_migrations") rescue nil
19
- ActiveRecord::Migration.drop_table("schema_migrations") rescue nil
20
- end
21
-
22
- describe :pending_schema_migrations do
23
- it "list sorted schema migrations" do
24
- expect(subject).to receive(:migrations_paths) {
25
- migration_path
26
- }
27
-
28
- migrations = subject.pending_schema_migrations
29
-
30
- expect(migrations.count).to eq 2
31
- expect(migrations[0][:version]).to eq(20131111111111)
32
- expect(migrations[1][:version]).to eq(20202020202011)
33
- end
34
- end
35
-
36
- describe :run do
37
- it "can run up task" do
38
- expect {
39
- subject.run(:up, migration_path, 20202020202011)
40
- }.to output(/20202020202011 DbMigration: migrating/).to_stdout
41
- versions = ActiveRecord::SchemaMigration.normalized_versions
42
- expect(versions.first).to eq("20202020202011")
43
- end
44
-
45
- it "can run down task" do
46
- subject.run(:up, migration_path, 20202020202011)
47
-
48
- expect {
49
- subject.run(:down, migration_path, 20202020202011)
50
- }.to output(/Undoing DbMigration/).to_stdout
51
-
52
- versions = ActiveRecord::SchemaMigration.normalized_versions
53
-
54
- expect(versions.count).to eq(0)
55
- end
56
- end
57
-
58
- describe :migrations_paths do
59
- context 'when a db_name is configured' do
60
- let(:config) { double(:config) }
61
- let(:paths) { ['spec/db/migrate', 'spec/db/migrate/other'] }
62
- let(:specification_name) { "primary" }
63
- let(:config_options) do
64
- if Gem::Dependency.new("railties", "~> 6.0").match?("railties", Gem.loaded_specs["railties"].version)
65
- { env_name: Rails.env, spec_name: specification_name }
66
- elsif Gem::Dependency.new("railties", "~> 7.0").match?("railties", Gem.loaded_specs["railties"].version)
67
- { env_name: Rails.env, name: specification_name }
68
- end
69
- end
70
-
71
- before do
72
- @original_config_spec_name = DataMigrate.config.spec_name
73
-
74
- DataMigrate.configure do |config|
75
- config.spec_name = specification_name
76
- end
77
-
78
- allow(ActiveRecord::Base.configurations)
79
- .to receive(:configs_for)
80
- .with(config_options)
81
- .and_return(config)
82
- allow(config).to receive(:migrations_paths).and_return(paths)
83
- end
84
-
85
- after do
86
- DataMigrate.configure do |config|
87
- config.spec_name = @original_config_spec_name
88
- end
89
- end
90
-
91
- it 'lists schema migration paths' do
92
- expect(subject.migrations_paths.size).to eq(paths.count)
93
- expect(subject.migrations_paths).to eq(paths)
94
- end
95
- end
96
- end
97
- end
@@ -1,73 +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
- ActiveRecord::SchemaMigration.create_table
37
- DataMigrate::DataSchemaMigration.create_table
38
- DataMigrate::DataSchemaMigration.create(fixture_file_timestamps.map { |t| { version: t } })
39
-
40
- subject.dump(connection_db_config, stream)
41
- stream.rewind
42
- end
43
-
44
- after do
45
- ActiveRecord::Migration.drop_table("data_migrations") rescue nil
46
- ActiveRecord::Migration.drop_table("schema_migrations") rescue nil
47
- end
48
-
49
- it "shows successfully executed migration" do
50
- expected = " up 20091231235959 Some name"
51
- expect(stream_data).to include expected
52
- end
53
-
54
- it "excludes files without .rb extension" do
55
- expected = "20181128000207 Excluded file"
56
- expect(stream_data).to_not include expected
57
- end
58
-
59
- it "shows missing file migration" do
60
- expected = " up 20101231235959 ********** NO FILE **********"
61
- expect(stream_data).to include expected
62
- end
63
-
64
- it "shows migration that has not run yet" do
65
- expected = " down 20171231235959 Super update"
66
- expect(stream_data).to include expected
67
- end
68
-
69
- it "outputs migrations in chronological order" do
70
- expect(stream_data.index("20091231235959")).to be < stream_data.index("20111231235959")
71
- end
72
- end
73
- end
@@ -1,129 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- describe DataMigrate::Tasks::DataMigrateTasks do
6
- let(:connection_db_config) do
7
- if Gem::Dependency.new("railties", ">= 6.1").match?("railties", Gem.loaded_specs["railties"].version)
8
- ActiveRecord::Base.connection_db_config
9
- else
10
- ActiveRecord::Base.configurations.configs_for.first
11
- end
12
- end
13
-
14
- before do
15
- ActiveRecord::SchemaMigration.create_table
16
- DataMigrate::DataSchemaMigration.create_table
17
- end
18
-
19
- after do
20
- ActiveRecord::Migration.drop_table("data_migrations") rescue nil
21
- ActiveRecord::Migration.drop_table("schema_migrations") rescue nil
22
- end
23
-
24
- describe :dump do
25
- before do
26
- allow(DataMigrate::DatabaseTasks).to receive(:db_dir).and_return("spec/db")
27
- DataMigrate::Tasks::DataMigrateTasks.migrate
28
- end
29
-
30
- context 'when not given a separate db config' do
31
- it 'does not override the default connection' do
32
- expect(ActiveRecord::Base).not_to receive(:establish_connection)
33
- expect(DataMigrate::SchemaDumper).to receive(:dump)
34
-
35
- DataMigrate::Tasks::DataMigrateTasks.dump
36
- end
37
- end
38
-
39
- context 'when given a separate db config' do
40
- let(:override_config) do
41
- {
42
- 'host' => '127.0.0.1',
43
- 'database' => 'other_test',
44
- 'adapter' => 'sqlite3',
45
- 'username' => 'root',
46
- 'password' => nil,
47
- }
48
- end
49
- let(:paths) { ["spec/db/migrate"] }
50
-
51
- before do
52
- DataMigrate.configure do |config|
53
- config.db_configuration = override_config
54
- end
55
- end
56
-
57
- it 'overrides the default connection' do
58
- expect(ActiveRecord::Base).to receive(:establish_connection).with(override_config)
59
- DataMigrate::Tasks::DataMigrateTasks.dump
60
- end
61
- end
62
- end
63
-
64
- describe :migrate do
65
- it "first run should run the first pending migration" do
66
- expect { DataMigrate::Tasks::DataMigrateTasks.migrate }.to output(/20091231235959 SomeName: migrating/).to_stdout
67
- end
68
-
69
- it "second run should run the second pending migration" do
70
- expect { DataMigrate::Tasks::DataMigrateTasks.migrate }.to output(/20171231235959 SuperUpdate: migrating/).to_stdout
71
- end
72
- end
73
-
74
- describe :abort_if_pending_migrations do
75
- subject { DataMigrate::Tasks::DataMigrateTasks.abort_if_pending_migrations(migrations, message) }
76
-
77
- let(:message) { "ABORT_MESSAGE" }
78
-
79
- context "when there are no pending migrations" do
80
- let(:migrations) { [] }
81
-
82
- it "shouldn't do anything" do
83
- expect { subject }.to_not raise_error
84
- end
85
- end
86
-
87
- context "when there are pending migrations" do
88
- let(:migrations) do
89
- [{
90
- name: "A",
91
- version: 1
92
- }, {
93
- name: 'B',
94
- version: 2
95
- }]
96
- end
97
-
98
- it "should abort with given message and print names and versions of pending migrations" do
99
- expect { subject }
100
- .to raise_error(SystemExit, message)
101
- .and output(match(/You have #{migrations.count} pending migrations:/)
102
- .and match(Regexp.new(migrations.map { |m| m.slice(:version, :name)
103
- .values.join("\\W+") }.join("\\W+")))).to_stdout
104
- end
105
- end
106
- end
107
-
108
- describe ".status" do
109
- before do
110
- allow(Rails).to receive(:root) { "." }
111
- allow(Rails).to receive(:application) { OpenStruct.new(config: OpenStruct.new(paths: { "db/migrate" => ["spec/db/migrate"] })) }
112
-
113
- DataMigrate::Tasks::DataMigrateTasks.migrate
114
- end
115
-
116
- it "should display data migration status" do
117
- expect {
118
- DataMigrate::Tasks::DataMigrateTasks.status
119
- }.to output(/up 20091231235959 Some name/).to_stdout
120
- end
121
-
122
- it "should display schema and data migration status" do
123
- expect {
124
- DataMigrate::Tasks::DataMigrateTasks.status_with_schema
125
- }.to output(match(/up data 20091231235959 Some name/)
126
- .and match(/down schema 20131111111111 Late migration/)).to_stdout
127
- end
128
- end
129
- end
@@ -1,9 +0,0 @@
1
- class SomeName < ActiveRecord::Migration[6.0]
2
- def up
3
- puts "Doing data migration"
4
- end
5
-
6
- def down
7
- puts "Undoing SomeName"
8
- end
9
- end
@@ -1,9 +0,0 @@
1
- class SuperUpdate < ActiveRecord::Migration[6.0]
2
- def up
3
- puts "Doing SuperUpdate"
4
- end
5
-
6
- def down
7
- puts "Undoing SuperUpdate"
8
- end
9
- end
@@ -1 +0,0 @@
1
- # This file should be excluded
@@ -1 +0,0 @@
1
- DataMigrate::Data.define(version: 20091231235959)
@@ -1 +0,0 @@
1
- DataMigrate::Data.define(version: 20091231235959)
@@ -1 +0,0 @@
1
- DataMigrate::Data.define(version: 20171231235959)
@@ -1 +0,0 @@
1
- DataMigrate::Data.define(version: 20171231235959)
@@ -1,9 +0,0 @@
1
- class LateMigration < ActiveRecord::Migration[6.0]
2
- def up
3
- puts "Doing schema LateMigration"
4
- end
5
-
6
- def down
7
- puts "Undoing LateMigration"
8
- end
9
- end
@@ -1,9 +0,0 @@
1
- class DbMigration < ActiveRecord::Migration[6.0]
2
- def up
3
- puts "Doing schema migration"
4
- end
5
-
6
- def down
7
- puts "Undoing DbMigration"
8
- end
9
- end
@@ -1,101 +0,0 @@
1
- require 'spec_helper'
2
- require 'rails/generators'
3
- require 'rails/generators/migration'
4
- require 'generators/data_migration/data_migration_generator'
5
-
6
- describe DataMigrate::Generators::DataMigrationGenerator do
7
- subject { DataMigrate::Generators::DataMigrationGenerator }
8
-
9
- describe :next_migration_number do
10
- it "next migration" do
11
- Timecop.freeze("2016-12-03 22:15:26 -0800") do
12
- if ActiveRecord.version >= Gem::Version.new('7.0')
13
- expect(ActiveRecord).to receive(:timestamped_migrations) { true }
14
- else
15
- expect(ActiveRecord::Base).to receive(:timestamped_migrations) { true }
16
- end
17
- expect(subject.next_migration_number(1).to_s).to eq("20161204061526")
18
- end
19
- end
20
- end
21
-
22
- describe :migration_base_class_name do
23
- subject { generator.send(:migration_base_class_name) }
24
-
25
- let(:generator) { DataMigrate::Generators::DataMigrationGenerator.new(['my_migration']) }
26
-
27
- it "returns the correct base class name" do
28
- is_expected.to eq("ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]")
29
- end
30
- end
31
-
32
- describe :create_data_migration do
33
- subject { DataMigrate::Generators::DataMigrationGenerator.new(['my_migration']) }
34
-
35
- let(:data_migrations_file_path) { 'abc/my_migration.rb' }
36
-
37
- context 'when custom data migrations path has a trailing slash' do
38
- before do
39
- DataMigrate.config.data_migrations_path = 'abc/'
40
- end
41
-
42
- it 'returns correct file path' do
43
- is_expected.to receive(:migration_template).with(
44
- 'data_migration.rb', data_migrations_file_path
45
- )
46
-
47
- subject.create_data_migration
48
- end
49
- end
50
-
51
- context 'when custom data migrations path does not have a trailing slash' do
52
- before do
53
- DataMigrate.config.data_migrations_path = 'abc'
54
- end
55
-
56
- it 'returns correct file path' do
57
- is_expected.to receive(:migration_template).with(
58
- 'data_migration.rb', data_migrations_file_path
59
- )
60
-
61
- subject.create_data_migration
62
- end
63
- end
64
- end
65
-
66
- describe ".source_root" do
67
- subject { described_class.source_root }
68
-
69
- let(:default_source_root) do
70
- File.expand_path(
71
- File.dirname(File.join(DataMigrate.root, "generators", "data_migration", "templates", "data_migration.rb"))
72
- )
73
- end
74
-
75
- it { is_expected.to eq default_source_root }
76
-
77
- context "when DateMigrate.config.data_template_path is set" do
78
- before do
79
- @before = DataMigrate.config.data_template_path
80
- DataMigrate.configure do |config|
81
- config.data_template_path = data_template_path
82
- end
83
- end
84
-
85
- let(:data_template_path) do
86
- File.join(DataMigrate.root, "generators", "data_migration", "templates", "data_migration.rb")
87
- end
88
- let(:expected_source_root) { File.dirname(data_template_path) }
89
-
90
- after do
91
- DataMigrate.configure do |config|
92
- config.data_template_path = @before
93
- end
94
- end
95
-
96
- it "reads directory from config data template path" do
97
- is_expected.to eq expected_source_root
98
- end
99
- end
100
- end
101
- end
data/spec/spec_helper.rb DELETED
@@ -1,28 +0,0 @@
1
- require 'rspec'
2
- require 'rails'
3
- require 'sqlite3'
4
- require 'data_migrate'
5
- require 'pry'
6
- require 'timecop'
7
-
8
- RSpec.configure do |config|
9
- config.mock_with :rspec do |mocks|
10
- mocks.verify_partial_doubles = true
11
- end
12
-
13
- config.after(:each) do
14
- DataMigrate.configure do |config|
15
- config.data_migrations_path = @prev_data_migrations_path
16
- end
17
- end
18
-
19
- config.before(:each) do |example|
20
- if example.metadata[:no_override]
21
- else
22
- @prev_data_migrations_path = DataMigrate.config.data_migrations_path
23
- DataMigrate.configure do |config|
24
- config.data_migrations_path = "spec/db/data"
25
- end
26
- end
27
- end
28
- end
data/tasks/.gitkeep DELETED
File without changes