data_migrate 10.0.3.rc → 11.0.0.rc

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/build.yml +4 -4
  3. data/.github/workflows/gempush.yml +6 -8
  4. data/.gitignore +0 -1
  5. data/Appraisals +10 -5
  6. data/Changelog.md +32 -6
  7. data/Gemfile +0 -1
  8. data/Gemfile.lock +188 -0
  9. data/README.md +13 -4
  10. data/data_migrate.gemspec +2 -2
  11. data/gemfiles/rails_6.1.gemfile +0 -1
  12. data/gemfiles/rails_6.1.gemfile.lock +233 -0
  13. data/gemfiles/rails_7.0.gemfile +1 -2
  14. data/gemfiles/rails_7.0.gemfile.lock +234 -0
  15. data/gemfiles/{rails_6.0.gemfile → rails_7.1.gemfile} +1 -2
  16. data/gemfiles/rails_7.1.gemfile.lock +266 -0
  17. data/gemfiles/rails_7.2.gemfile +7 -0
  18. data/gemfiles/rails_7.2.gemfile.lock +265 -0
  19. data/lib/data_migrate/data_migrator.rb +15 -23
  20. data/lib/data_migrate/data_schema.rb +2 -2
  21. data/lib/data_migrate/data_schema_migration.rb +24 -7
  22. data/lib/data_migrate/database_configurations_wrapper.rb +11 -0
  23. data/lib/data_migrate/database_tasks.rb +194 -48
  24. data/lib/data_migrate/migration_context.rb +11 -8
  25. data/lib/data_migrate/rails_helper.rb +90 -0
  26. data/lib/data_migrate/schema_dumper.rb +1 -1
  27. data/lib/data_migrate/schema_migration.rb +5 -4
  28. data/lib/data_migrate/status_service.rb +3 -3
  29. data/lib/data_migrate/tasks/data_migrate_tasks.rb +13 -14
  30. data/lib/data_migrate/test.rb +14 -0
  31. data/lib/data_migrate/version.rb +1 -1
  32. data/lib/data_migrate.rb +2 -1
  33. data/spec/data_migrate/data_migrator_spec.rb +17 -14
  34. data/spec/data_migrate/data_schema_migration_spec.rb +25 -8
  35. data/spec/data_migrate/data_spec.rb +1 -1
  36. data/spec/data_migrate/database_tasks_spec.rb +34 -19
  37. data/spec/data_migrate/migration_context_spec.rb +25 -9
  38. data/spec/data_migrate/schema_dumper_spec.rb +6 -3
  39. data/spec/data_migrate/schema_migration_spec.rb +13 -6
  40. data/spec/data_migrate/status_service_spec.rb +6 -3
  41. data/spec/data_migrate/tasks/data_migrate_tasks_spec.rb +13 -14
  42. data/spec/db/data/20091231235959_some_name.rb +1 -1
  43. data/spec/db/data/20171231235959_super_update.rb +1 -1
  44. data/spec/db/migrate/20131111111111_late_migration.rb +1 -1
  45. data/spec/db/migrate/20202020202011_db_migration.rb +1 -1
  46. data/tasks/databases.rake +25 -93
  47. metadata +21 -15
  48. data/Gemfile.rails6.1 +0 -11
  49. data/lib/data_migrate/legacy_migrator.rb +0 -22
  50. data/spec/data_migrate/legacy_migrator_spec.rb +0 -38
@@ -11,7 +11,8 @@ module DataMigrate
11
11
  migrations
12
12
  end
13
13
 
14
- DataMigrator.new(:up, selected_migrations, target_version).migrate
14
+ data_migrator = DataMigrate::RailsHelper.data_migrator(:up, selected_migrations, DataMigrate::RailsHelper.schema_migration, DataMigrate::RailsHelper.internal_metadata, target_version)
15
+ data_migrator.migrate
15
16
  end
16
17
 
17
18
  def down(target_version = nil)
@@ -22,11 +23,13 @@ module DataMigrate
22
23
  migrations
23
24
  end
24
25
 
25
- DataMigrator.new(:down, selected_migrations, target_version).migrate
26
+ data_migrator = DataMigrate::RailsHelper.data_migrator(:down, selected_migrations, DataMigrate::RailsHelper.schema_migration, DataMigrate::RailsHelper.internal_metadata, target_version)
27
+ data_migrator.migrate
26
28
  end
27
29
 
28
30
  def run(direction, target_version)
29
- DataMigrator.new(direction, migrations, target_version).run
31
+ data_migrator = DataMigrate::RailsHelper.data_migrator(direction, migrations, DataMigrate::RailsHelper.schema_migration, DataMigrate::RailsHelper.internal_metadata, target_version)
32
+ data_migrator.run
30
33
  end
31
34
 
32
35
  def current_version
@@ -40,12 +43,12 @@ module DataMigrate
40
43
  end
41
44
 
42
45
  def migrations_status
43
- db_list = DataSchemaMigration.normalized_versions
46
+ db_list = DataMigrate::RailsHelper.data_schema_migration.normalized_versions
44
47
 
45
48
  file_list = migration_files.map do |file|
46
49
  version, name, scope = parse_migration_filename(file)
47
50
  raise ActiveRecord::IllegalMigrationNameError.new(file) unless version
48
- version = ActiveRecord::SchemaMigration.normalize_migration_number(version)
51
+ version = DataMigrate::RailsHelper.schema_migration.normalize_migration_number(version)
49
52
  status = db_list.delete(version) ? "up" : "down"
50
53
  [status, version, (name + scope).humanize]
51
54
  end.compact
@@ -60,15 +63,15 @@ module DataMigrate
60
63
  private
61
64
 
62
65
  def get_all_versions
63
- if DataMigrate::DataSchemaMigration.table_exists?
64
- DataSchemaMigration.normalized_versions.map(&:to_i)
66
+ if DataMigrate::RailsHelper.data_schema_migration.table_exists?
67
+ DataMigrate::RailsHelper.data_schema_migration.normalized_versions.map(&:to_i)
65
68
  else
66
69
  []
67
70
  end
68
71
  end
69
72
 
70
73
  def move(direction, steps)
71
- migrator = DataMigrator.new(direction, migrations)
74
+ migrator = DataMigrate::RailsHelper.data_migrator(direction, migrations)
72
75
 
73
76
  if current_version != 0 && !migrator.current_migration
74
77
  raise ActiveRecord::UnknownMigrationVersionError.new(current_version)
@@ -0,0 +1,90 @@
1
+ module DataMigrate
2
+ class RailsHelper
3
+ class << self
4
+ def rails_version_equal_to_or_higher_than_7_2
5
+ return @equal_to_or_higher_than_7_2 if defined?(@equal_to_or_higher_than_7_2)
6
+
7
+ @equal_to_or_higher_than_7_2 = Gem::Dependency.new("railties", ">= 7.2.0.alpha").match?("railties", Gem.loaded_specs["railties"].version, true)
8
+ end
9
+ def rails_version_equal_to_or_higher_than_7_1
10
+ return @equal_to_or_higher_than_7_1 if defined?(@equal_to_or_higher_than_7_1)
11
+
12
+ @equal_to_or_higher_than_7_1 = Gem::Dependency.new("railties", ">= 7.1.0.alpha").match?("railties", Gem.loaded_specs["railties"].version, true)
13
+ end
14
+
15
+ def rails_version_equal_to_or_higher_than_7_0
16
+ return @rails_version_equal_to_or_higher_than_7_0 if defined?(@rails_version_equal_to_or_higher_than_7_0)
17
+
18
+ @rails_version_equal_to_or_higher_than_7_0 = Gem::Dependency.new("railties", ">= 7.0").match?("railties", Gem.loaded_specs["railties"].version, true)
19
+ end
20
+
21
+ def internal_metadata
22
+ if rails_version_equal_to_or_higher_than_7_2
23
+ ActiveRecord::Base.connection_pool.internal_metadata
24
+ elsif rails_version_equal_to_or_higher_than_7_1
25
+ ActiveRecord::Base.connection.internal_metadata
26
+ else
27
+ ActiveRecord::InternalMetadata
28
+ end
29
+ end
30
+
31
+ def schema_migration
32
+ if rails_version_equal_to_or_higher_than_7_2
33
+ ActiveRecord::Base.connection_pool.schema_migration
34
+ elsif rails_version_equal_to_or_higher_than_7_1
35
+ ActiveRecord::Base.connection.schema_migration
36
+ else
37
+ ActiveRecord::SchemaMigration
38
+ end
39
+ end
40
+
41
+ def schema_migration_versions
42
+ if rails_version_equal_to_or_higher_than_7_1
43
+ schema_migration.versions
44
+ else
45
+ schema_migration.all.pluck(:version)
46
+ end
47
+ end
48
+
49
+ def schema_create_version(version)
50
+ if rails_version_equal_to_or_higher_than_7_1
51
+ schema_migration.create_version(version)
52
+ else
53
+ schema_migration.create(version: version)
54
+ end
55
+ end
56
+
57
+ def data_schema_delete_version(version)
58
+ if rails_version_equal_to_or_higher_than_7_1
59
+ data_schema_migration.delete_version(version)
60
+ else
61
+ data_schema_migration.where(version: version.to_s).delete_all
62
+ end
63
+ end
64
+
65
+ def data_schema_migration
66
+ if rails_version_equal_to_or_higher_than_7_2
67
+ DataMigrate::DataSchemaMigration.new(ActiveRecord::Tasks::DatabaseTasks.migration_connection_pool)
68
+ elsif rails_version_equal_to_or_higher_than_7_1
69
+ DataMigrate::DataSchemaMigration.new(ActiveRecord::Tasks::DatabaseTasks.migration_connection)
70
+ else
71
+ DataMigrate::DataSchemaMigration
72
+ end
73
+ end
74
+
75
+ def data_migrator(
76
+ direction,
77
+ migrations,
78
+ schema_migration = DataMigrate::RailsHelper.schema_migration,
79
+ internal_metadata = DataMigrate::RailsHelper.internal_metadata,
80
+ target_version = nil
81
+ )
82
+ if rails_version_equal_to_or_higher_than_7_1
83
+ DataMigrate::DataMigrator.new(direction, migrations, schema_migration, internal_metadata, target_version)
84
+ else
85
+ DataMigrate::DataMigrator.new(direction, migrations, schema_migration, target_version)
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
@@ -26,7 +26,7 @@ module DataMigrate
26
26
 
27
27
  def initialize(connection)
28
28
  @connection = connection
29
- all_versions = DataSchemaMigration.normalized_versions
29
+ all_versions = DataMigrate::RailsHelper.data_schema_migration.normalized_versions
30
30
 
31
31
  @version = begin
32
32
  all_versions.max
@@ -7,14 +7,14 @@ module DataMigrate
7
7
  def self.pending_schema_migrations
8
8
  all_migrations = DataMigrate::MigrationContext.new(migrations_paths).migrations
9
9
  sort_migrations(
10
- ActiveRecord::Migrator.new(:up, all_migrations, ActiveRecord::Base.connection.schema_migration).
10
+ ActiveRecord::Migrator.new(:up, all_migrations, DataMigrate::RailsHelper.schema_migration, DataMigrate::RailsHelper.internal_metadata).
11
11
  pending_migrations.
12
12
  map {|m| { version: m.version, kind: :schema }}
13
13
  )
14
14
  end
15
15
 
16
16
  def self.run(direction, migration_paths, version)
17
- ActiveRecord::MigrationContext.new(migration_paths, ActiveRecord::Base.connection.schema_migration).run(direction, version)
17
+ ActiveRecord::MigrationContext.new(migration_paths, DataMigrate::RailsHelper.schema_migration).run(direction, version)
18
18
  end
19
19
 
20
20
  def self.sort_migrations(set1, set2 = nil)
@@ -24,9 +24,10 @@ module DataMigrate
24
24
 
25
25
  def self.migrations_paths
26
26
  spec_name = DataMigrate.config.spec_name
27
- if spec_name && Gem::Dependency.new("railties", "~> 7.0").match?("railties", Gem.loaded_specs["railties"].version)
27
+ # The positional argument true is to include pre-release versions, such as 7.1.0.alpha
28
+ if spec_name && Gem::Dependency.new("railties", ">= 7.0").match?("railties", Gem.loaded_specs["railties"].version, true)
28
29
  ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, name: spec_name).migrations_paths
29
- elsif spec_name && Gem::Dependency.new("railties", "~> 6.0").match?("railties", Gem.loaded_specs["railties"].version)
30
+ elsif spec_name && Gem::Dependency.new("railties", "~> 6.1").match?("railties", Gem.loaded_specs["railties"].version)
30
31
  ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, spec_name: spec_name).migrations_paths
31
32
  else
32
33
  Rails.application.config.paths["db/migrate"].to_a
@@ -24,11 +24,11 @@ module DataMigrate
24
24
  private
25
25
 
26
26
  def table_name
27
- DataMigrate::DataSchemaMigration.table_name
27
+ DataMigrate::RailsHelper.data_schema_migration.table_name
28
28
  end
29
29
 
30
30
  def output(stream)
31
- unless DataMigrate::DataSchemaMigration.table_exists?
31
+ unless DataMigrate::RailsHelper.data_schema_migration.table_exists?
32
32
  stream.puts "Data migrations table does not exist yet."
33
33
  return
34
34
  end
@@ -46,7 +46,7 @@ module DataMigrate
46
46
  def database_name
47
47
  if Gem::Dependency.new("railties", "~> 7.0").match?("railties", Gem.loaded_specs["railties"].version)
48
48
  ActiveRecord::Base.connection_db_config.configuration_hash[:database]
49
- elsif Gem::Dependency.new("railties", "~> 6.0").match?("railties", Gem.loaded_specs["railties"].version)
49
+ elsif Gem::Dependency.new("railties", "~> 6.1").match?("railties", Gem.loaded_specs["railties"].version)
50
50
  ActiveRecord::Base.connection_config[:database]
51
51
  end
52
52
  end
@@ -9,9 +9,9 @@ module DataMigrate
9
9
  @migrations_paths ||= DataMigrate.config.data_migrations_path
10
10
  end
11
11
 
12
- def dump(db_config)
12
+ def dump
13
13
  if dump_schema_after_migration?
14
- filename = DataMigrate::DatabaseTasks.dump_filename(spec_name(db_config), ActiveRecord::Base.schema_format)
14
+ filename = DataMigrate::DatabaseTasks.schema_file
15
15
  ActiveRecord::Base.establish_connection(DataMigrate.config.db_configuration) if DataMigrate.config.db_configuration
16
16
  File.open(filename, "w:utf-8") do |file|
17
17
  DataMigrate::SchemaDumper.dump(ActiveRecord::Base.connection, file)
@@ -22,7 +22,7 @@ module DataMigrate
22
22
  def migrate
23
23
  target_version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
24
24
 
25
- DataMigrate::DataMigrator.assure_data_schema_table
25
+ DataMigrate::DataMigrator.create_data_schema_table
26
26
  DataMigrate::MigrationContext.new(migrations_paths).migrate(target_version)
27
27
  end
28
28
 
@@ -44,16 +44,15 @@ module DataMigrate
44
44
  end
45
45
  end
46
46
 
47
- def status(db_config)
48
- puts "\ndatabase: #{spec_name(db_config)}\n\n"
49
- DataMigrate::StatusService.dump(ActiveRecord::Base.connection)
47
+ def status
48
+ DataMigrate::StatusService.dump
50
49
  end
51
50
 
52
- def status_with_schema(db_config)
51
+ def status_with_schema
53
52
  db_list_data = ActiveRecord::Base.connection.select_values(
54
- "SELECT version FROM #{DataMigrate::DataSchemaMigration.table_name}"
53
+ "SELECT version FROM #{DataMigrate::RailsHelper.data_schema_migration.table_name}"
55
54
  )
56
- db_list_schema = ActiveRecord::SchemaMigration.all.pluck(:version)
55
+ db_list_schema = DataMigrate::RailsHelper.schema_migration_versions
57
56
  file_list = []
58
57
 
59
58
  Dir.foreach(File.join(Rails.root, migrations_paths)) do |file|
@@ -77,7 +76,7 @@ module DataMigrate
77
76
  file_list.sort!{|a,b| "#{a[1]}_#{a[3] == 'data' ? 1 : 0}" <=> "#{b[1]}_#{b[3] == 'data' ? 1 : 0}" }
78
77
 
79
78
  # output
80
- puts "\ndatabase: #{spec_name(db_config)}\n\n"
79
+ puts "\ndatabase: #{database_name}\n\n"
81
80
  puts "#{"Status".center(8)} #{"Type".center(8)} #{"Migration ID".ljust(14)} Migration Name"
82
81
  puts "-" * 60
83
82
  file_list.each do |file|
@@ -94,11 +93,11 @@ module DataMigrate
94
93
 
95
94
  private
96
95
 
97
- def spec_name(db_config)
96
+ def database_name
98
97
  if Gem::Dependency.new("railties", "~> 7.0").match?("railties", Gem.loaded_specs["railties"].version)
99
- db_config.name
100
- elsif Gem::Dependency.new("railties", "~> 6.0").match?("railties", Gem.loaded_specs["railties"].version)
101
- db_config.spec_name
98
+ ActiveRecord::Base.connection_db_config.database
99
+ elsif Gem::Dependency.new("railties", "~> 6.1").match?("railties", Gem.loaded_specs["railties"].version)
100
+ ActiveRecord::Base.connection_config[:database]
102
101
  end
103
102
  end
104
103
  end
@@ -0,0 +1,14 @@
1
+ module Base
2
+ extend self
3
+
4
+ def foo
5
+ puts "Base#foo called"
6
+ end
7
+ end
8
+
9
+ module Child
10
+ extend Base
11
+ extend self
12
+
13
+ puts "foo: #{respond_to?(:foo)}"
14
+ end
@@ -1,3 +1,3 @@
1
1
  module DataMigrate
2
- VERSION = "10.0.3.rc".freeze
2
+ VERSION = "11.0.0.rc".freeze
3
3
  end
data/lib/data_migrate.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require File.join(File.dirname(__FILE__), "data_migrate", "rails_helper")
3
4
  require File.join(File.dirname(__FILE__), "data_migrate", "data_migrator")
4
5
  require File.join(File.dirname(__FILE__), "data_migrate", "data_schema_migration")
5
6
  require File.join(File.dirname(__FILE__), "data_migrate", "data_schema")
@@ -9,9 +10,9 @@ require File.join(File.dirname(__FILE__), "data_migrate", "status_service")
9
10
  require File.join(File.dirname(__FILE__), "data_migrate", "migration_context")
10
11
  require File.join(File.dirname(__FILE__), "data_migrate", "railtie")
11
12
  require File.join(File.dirname(__FILE__), "data_migrate", "tasks/data_migrate_tasks")
12
- require File.join(File.dirname(__FILE__), "data_migrate", "legacy_migrator")
13
13
  require File.join(File.dirname(__FILE__), "data_migrate", "config")
14
14
  require File.join(File.dirname(__FILE__), "data_migrate", "schema_migration")
15
+ require File.join(File.dirname(__FILE__), "data_migrate", "database_configurations_wrapper")
15
16
 
16
17
  module DataMigrate
17
18
  def self.root
@@ -3,7 +3,8 @@
3
3
  require "spec_helper"
4
4
 
5
5
  describe DataMigrate::DataMigrator do
6
- let(:subject) { DataMigrate::DataMigrator }
6
+ let(:described_class) { DataMigrate::DataMigrator }
7
+
7
8
  let(:db_config) do
8
9
  {
9
10
  adapter: "sqlite3",
@@ -13,8 +14,8 @@ describe DataMigrate::DataMigrator do
13
14
 
14
15
  before do
15
16
  ActiveRecord::Base.establish_connection(db_config)
16
- ::ActiveRecord::SchemaMigration.create_table
17
- DataMigrate::DataSchemaMigration.create_table
17
+ DataMigrate::RailsHelper.schema_migration.create_table
18
+ DataMigrate::RailsHelper.data_schema_migration.create_table
18
19
  end
19
20
 
20
21
  after do
@@ -23,22 +24,24 @@ describe DataMigrate::DataMigrator do
23
24
  end
24
25
 
25
26
  describe ".load_migrated" do
27
+ let(:migrator) { DataMigrate::RailsHelper.data_migrator(:up, []) }
28
+
26
29
  it "loads migrated versions" do
27
- DataMigrate::DataSchemaMigration.create(version: 20090000000000)
28
- ::ActiveRecord::SchemaMigration.create(version: 20100000000000)
29
- DataMigrate::DataSchemaMigration.create(version: 20110000000000)
30
- ::ActiveRecord::SchemaMigration.create(version: 20120000000000)
31
- migrated = subject.new(:up, []).load_migrated
30
+ DataMigrate::RailsHelper.data_schema_migration.create_version(20090000000000)
31
+ DataMigrate::RailsHelper.schema_create_version(20100000000000)
32
+ DataMigrate::RailsHelper.data_schema_migration.create_version(20110000000000)
33
+ DataMigrate::RailsHelper.schema_create_version(20120000000000)
34
+ migrated = migrator.load_migrated
32
35
  expect(migrated.count).to eq 2
33
36
  expect(migrated).to include 20090000000000
34
37
  expect(migrated).to include 20110000000000
35
38
  end
36
39
  end
37
40
 
38
- describe :assure_data_schema_table do
41
+ describe :create_data_schema_table do
39
42
  it "creates the data_migrations table" do
40
43
  ActiveRecord::Migration.drop_table("data_migrations") rescue nil
41
- subject.assure_data_schema_table
44
+ described_class.create_data_schema_table
42
45
  expect(
43
46
  ActiveRecord::Base.connection.table_exists?("data_migrations")
44
47
  ).to eq true
@@ -47,7 +50,7 @@ describe DataMigrate::DataMigrator do
47
50
 
48
51
  describe "#migrations_status" do
49
52
  it "returns all migrations statuses" do
50
- status = subject.migrations_status
53
+ status = described_class.migrations_status
51
54
  expect(status.length).to eq 2
52
55
  expect(status.first).to eq ["down", "20091231235959", "Some name"]
53
56
  expect(status.second).to eq ["down", "20171231235959", "Super update"]
@@ -57,19 +60,19 @@ describe DataMigrate::DataMigrator do
57
60
  describe :match do
58
61
  context "when the file does not match" do
59
62
  it "returns nil" do
60
- expect(subject.match("not_a_data_migration_file")).to be_nil
63
+ expect(described_class.match("not_a_data_migration_file")).to be_nil
61
64
  end
62
65
  end
63
66
 
64
67
  context "when the file doesn't end in .rb" do
65
68
  it "returns nil" do
66
- expect(subject.match("20091231235959_some_name.rb.un~")).to be_nil
69
+ expect(described_class.match("20091231235959_some_name.rb.un~")).to be_nil
67
70
  end
68
71
  end
69
72
 
70
73
  context "when the file matches" do
71
74
  it "returns a valid MatchData object" do
72
- match_data = subject.match("20091231235959_some_name.rb")
75
+ match_data = described_class.match("20091231235959_some_name.rb")
73
76
 
74
77
  expect(match_data[0]).to eq "20091231235959_some_name.rb"
75
78
  expect(match_data[1]).to eq "20091231235959"
@@ -1,16 +1,33 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe DataMigrate::DataSchemaMigration do
4
- let(:subject) { DataMigrate::DataSchemaMigration }
5
- describe :table_name do
6
- it "returns correct table name" do
7
- expect(subject.table_name).to eq("data_migrations")
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
8
25
  end
9
- end
10
26
 
11
- describe :index_name do
12
- it "returns correct primary key name" do
13
- expect(subject.primary_key).to eq("version")
27
+ describe :index_name do
28
+ it "returns correct primary key name" do
29
+ expect(subject.primary_key).to eq("version")
30
+ end
14
31
  end
15
32
  end
16
33
  end
@@ -55,7 +55,7 @@ describe DataMigrate::Data do
55
55
 
56
56
  sql_select = <<-SQL
57
57
  SELECT version
58
- FROM #{DataMigrate::DataSchemaMigration.table_name}
58
+ FROM #{DataMigrate::RailsHelper.data_schema_migration.table_name}
59
59
  SQL
60
60
 
61
61
  db_list_data = ActiveRecord::Base.connection.
@@ -8,12 +8,6 @@ describe DataMigrate::DatabaseTasks do
8
8
  let(:data_migrations_path) {
9
9
  DataMigrate.config.data_migrations_path
10
10
  }
11
- let(:db_config) do
12
- {
13
- adapter: "sqlite3",
14
- database: "spec/db/test.db"
15
- }
16
- end
17
11
 
18
12
  before do
19
13
  # In a normal Rails installation, db_dir would defer to
@@ -27,14 +21,10 @@ describe DataMigrate::DatabaseTasks do
27
21
  allow(DataMigrate::Tasks::DataMigrateTasks).to receive(:migrations_paths) {
28
22
  data_migrations_path
29
23
  }
30
- ActiveRecord::Base.establish_connection(db_config)
31
- if Gem::Dependency.new("railties", ">= 6.1").match?("railties", Gem.loaded_specs["railties"].version)
32
- hash_config = ActiveRecord::DatabaseConfigurations::HashConfig.new('test', 'test', db_config)
33
- config_obj = ActiveRecord::DatabaseConfigurations.new([hash_config])
34
- allow(ActiveRecord::Base).to receive(:configurations).and_return(config_obj)
35
- else
36
- ActiveRecord::Base.configurations[:test] = db_config
37
- end
24
+ ActiveRecord::Base.establish_connection({ adapter: "sqlite3", database: "spec/db/test.db" })
25
+ hash_config = ActiveRecord::DatabaseConfigurations::HashConfig.new('test', 'test', { adapter: "sqlite3", database: "spec/db/test.db" })
26
+ config_obj = ActiveRecord::DatabaseConfigurations.new([hash_config])
27
+ allow(ActiveRecord::Base).to receive(:configurations).and_return(config_obj)
38
28
  end
39
29
 
40
30
  context "migrations" do
@@ -44,7 +34,7 @@ describe DataMigrate::DatabaseTasks do
44
34
  end
45
35
 
46
36
  before do
47
- ActiveRecord::SchemaMigration.create_table
37
+ DataMigrate::RailsHelper.schema_migration.create_table
48
38
 
49
39
  allow(DataMigrate::SchemaMigration).to receive(:migrations_paths) {
50
40
  migration_path
@@ -69,22 +59,47 @@ describe DataMigrate::DatabaseTasks do
69
59
  end
70
60
 
71
61
  describe :forward do
72
-
73
62
  it "run forward default amount of times" do
74
63
  subject.forward
75
- versions = DataMigrate::DataSchemaMigration.normalized_versions
64
+ versions = DataMigrate::RailsHelper.data_schema_migration.normalized_versions
76
65
  expect(versions.count).to eq(1)
77
66
  end
78
67
 
79
68
  it "run forward defined number of times" do
80
69
  subject.forward(2)
81
- versions = DataMigrate::DataSchemaMigration.normalized_versions
70
+ versions = DataMigrate::RailsHelper.data_schema_migration.normalized_versions
82
71
  expect(versions.count).to eq(1)
83
72
  expect(versions.first).to eq "20091231235959"
84
- versions = ActiveRecord::SchemaMigration.normalized_versions
73
+ versions = DataMigrate::RailsHelper.schema_migration.normalized_versions
85
74
  expect(versions.count).to eq(1)
86
75
  expect(versions.first).to eq "20131111111111"
87
76
  end
88
77
  end
78
+
79
+ if DataMigrate::RailsHelper.rails_version_equal_to_or_higher_than_7_0
80
+ describe :schema_dump_path do
81
+ before do
82
+ allow(ActiveRecord::Base).to receive(:configurations).and_return(ActiveRecord::DatabaseConfigurations.new([db_config]))
83
+ end
84
+
85
+ context "for primary database" do
86
+ let(:db_config) { ActiveRecord::DatabaseConfigurations::HashConfig.new("development", "primary", {} ) }
87
+
88
+ context "for :ruby db format" do
89
+ it 'returns the data schema path' do
90
+ allow(ActiveRecord).to receive(:schema_format).and_return(:ruby)
91
+ expect(subject.schema_dump_path(db_config)).to eq("db/data_schema.rb")
92
+ end
93
+ end
94
+
95
+ context "for :sql db format" do
96
+ it 'returns the data schema path' do
97
+ allow(ActiveRecord).to receive(:schema_format).and_return(:sql)
98
+ expect(subject.schema_dump_path(db_config, :sql)).to eq("db/data_schema.rb")
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
89
104
  end
90
105
  end
@@ -4,11 +4,18 @@ require "spec_helper"
4
4
 
5
5
  describe DataMigrate::DataMigrator do
6
6
  let(:context) { DataMigrate::MigrationContext.new("spec/db/data") }
7
- let(:schema_context) { ActiveRecord::MigrationContext.new("spec/db/migrate", ActiveRecord::Base.connection.schema_migration) }
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
8
14
 
9
15
  before do
10
- ActiveRecord::SchemaMigration.create_table
11
- DataMigrate::DataSchemaMigration.create_table
16
+ ActiveRecord::Base.establish_connection(db_config)
17
+ DataMigrate::RailsHelper.schema_migration.create_table
18
+ DataMigrate::RailsHelper.data_schema_migration.create_table
12
19
  end
13
20
 
14
21
  after do
@@ -20,7 +27,7 @@ describe DataMigrate::DataMigrator do
20
27
  it "migrates existing file" do
21
28
  context.migrate(nil)
22
29
  context.migrations_status
23
- versions = DataMigrate::DataSchemaMigration.normalized_versions
30
+ versions = DataMigrate::RailsHelper.data_schema_migration.normalized_versions
24
31
  expect(versions.count).to eq(2)
25
32
  expect(versions).to include("20091231235959")
26
33
  expect(versions).to include("20171231235959")
@@ -29,7 +36,7 @@ describe DataMigrate::DataMigrator do
29
36
  it "undo migration" do
30
37
  context.migrate(nil)
31
38
  context.run(:down, 20171231235959)
32
- versions = DataMigrate::DataSchemaMigration.normalized_versions
39
+ versions = DataMigrate::RailsHelper.data_schema_migration.normalized_versions
33
40
  expect(versions.count).to eq(1)
34
41
  expect(versions).to include("20091231235959")
35
42
  end
@@ -46,7 +53,7 @@ describe DataMigrate::DataMigrator do
46
53
 
47
54
  it "runs a specific migration" do
48
55
  context.run(:up, 20171231235959)
49
- versions = DataMigrate::DataSchemaMigration.normalized_versions
56
+ versions = DataMigrate::RailsHelper.data_schema_migration.normalized_versions
50
57
  expect(versions.count).to eq(1)
51
58
  expect(versions).to include("20171231235959")
52
59
  end
@@ -74,7 +81,7 @@ describe DataMigrate::DataMigrator do
74
81
  expect {
75
82
  context.rollback
76
83
  }.to output(/Undoing SuperUpdate/).to_stdout
77
- versions = DataMigrate::DataSchemaMigration.normalized_versions
84
+ versions = DataMigrate::RailsHelper.data_schema_migration.normalized_versions
78
85
  expect(versions.count).to eq(1)
79
86
  expect(versions).to include("20091231235959")
80
87
  end
@@ -85,7 +92,7 @@ describe DataMigrate::DataMigrator do
85
92
  expect {
86
93
  context.rollback(2)
87
94
  }.to output(/Undoing SomeName/).to_stdout
88
- versions = DataMigrate::DataSchemaMigration.normalized_versions
95
+ versions = DataMigrate::RailsHelper.data_schema_migration.normalized_versions
89
96
  expect(versions.count).to eq(0)
90
97
  end
91
98
 
@@ -94,8 +101,17 @@ describe DataMigrate::DataMigrator do
94
101
  expect {
95
102
  context.rollback(2)
96
103
  }.to output(/Undoing SomeName/).to_stdout
97
- versions = DataMigrate::DataSchemaMigration.normalized_versions
104
+ versions = DataMigrate::RailsHelper.data_schema_migration.normalized_versions
98
105
  expect(versions.count).to eq(0)
99
106
  end
100
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
101
117
  end
@@ -9,9 +9,12 @@ describe DataMigrate::SchemaDumper do
9
9
  end
10
10
 
11
11
  before do
12
- ActiveRecord::SchemaMigration.create_table
13
- DataMigrate::DataSchemaMigration.create_table
14
- DataMigrate::DataSchemaMigration.create(fixture_file_timestamps.map { |t| { version: t } })
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
15
18
  end
16
19
 
17
20
  after do