Skip to content

Commit

Permalink
Merge pull request #281 from ilyakatz/fix-schema-load-for-structure-sql
Browse files Browse the repository at this point in the history
  • Loading branch information
ngan authored Aug 2, 2023
2 parents dd6b3b2 + 6fd7216 commit 89bd24c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
5 changes: 3 additions & 2 deletions lib/data_migrate/database_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def schema_file_type(_format = nil)
"data_schema.rb"
end

# This method is removed in Rails 7.0
def dump_filename(spec_name, format = ActiveRecord::Base.schema_format)
filename = if spec_name == "primary"
schema_file_type(format)
Expand Down Expand Up @@ -73,14 +74,14 @@ def schema_dump_path(db_config, format = ActiveRecord.schema_format)
# We only require a schema.rb file for the primary database
return unless db_config.primary?

super.gsub(/(_)?schema\.rb\z/, '\1data_schema.rb')
File.join(File.dirname(ActiveRecord::Tasks::DatabaseTasks.schema_dump_path(db_config, format)), schema_file_type)
end

# Override this method from `ActiveRecord::Tasks::DatabaseTasks`
# to ensure that the sha saved in ar_internal_metadata table
# is from the original schema.rb file
def schema_sha1(file)
super(file.gsub(/data_schema.rb\z/, 'schema.rb'))
ActiveRecord::Tasks::DatabaseTasks.schema_dump_path(ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env, name: "primary"))
end
end

Expand Down
12 changes: 10 additions & 2 deletions lib/data_migrate/rails_helper.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
module DataMigrate
class DataMigrate::RailsHelper
class RailsHelper
class << self
def rails_version_equal_to_or_higher_than_7_1
@equal_to_or_higher_than_7_1 ||= Gem::Dependency.new("railties", ">= 7.1.0.alpha").match?("railties", Gem.loaded_specs["railties"].version)
return @equal_to_or_higher_than_7_1 if defined?(@equal_to_or_higher_than_7_1)

@equal_to_or_higher_than_7_1 = Gem::Dependency.new("railties", ">= 7.1.0.alpha").match?("railties", Gem.loaded_specs["railties"].version, true)
end

def rails_version_equal_to_or_higher_than_7_0
return @rails_version_equal_to_or_higher_than_7_0 if defined?(@rails_version_equal_to_or_higher_than_7_0)

@rails_version_equal_to_or_higher_than_7_0 = Gem::Dependency.new("railties", ">= 7.0").match?("railties", Gem.loaded_specs["railties"].version, true)
end

def internal_metadata
Expand Down
37 changes: 28 additions & 9 deletions spec/data_migrate/database_tasks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@
let(:data_migrations_path) {
DataMigrate.config.data_migrations_path
}
let(:db_config) do
{
adapter: "sqlite3",
database: "spec/db/test.db"
}
end

before do
# In a normal Rails installation, db_dir would defer to
Expand All @@ -27,8 +21,8 @@
allow(DataMigrate::Tasks::DataMigrateTasks).to receive(:migrations_paths) {
data_migrations_path
}
ActiveRecord::Base.establish_connection(db_config)
hash_config = ActiveRecord::DatabaseConfigurations::HashConfig.new('test', 'test', db_config)
ActiveRecord::Base.establish_connection({ adapter: "sqlite3", database: "spec/db/test.db" })
hash_config = ActiveRecord::DatabaseConfigurations::HashConfig.new('test', 'test', { adapter: "sqlite3", database: "spec/db/test.db" })
config_obj = ActiveRecord::DatabaseConfigurations.new([hash_config])
allow(ActiveRecord::Base).to receive(:configurations).and_return(config_obj)
end
Expand Down Expand Up @@ -65,7 +59,6 @@
end

describe :forward do

it "run forward default amount of times" do
subject.forward
versions = DataMigrate::RailsHelper.data_schema_migration.normalized_versions
Expand All @@ -82,5 +75,31 @@
expect(versions.first).to eq "20131111111111"
end
end

if DataMigrate::RailsHelper.rails_version_equal_to_or_higher_than_7_0
describe :schema_dump_path do
before do
allow(ActiveRecord::Base).to receive(:configurations).and_return(ActiveRecord::DatabaseConfigurations.new([db_config]))
end

context "for primary database" do
let(:db_config) { ActiveRecord::DatabaseConfigurations::HashConfig.new("development", "primary", {} ) }

context "for :ruby db format" do
it 'returns the data schema path' do
allow(ActiveRecord).to receive(:schema_format).and_return(:ruby)
expect(subject.schema_dump_path(db_config)).to eq("db/data_schema.rb")
end
end

context "for :sql db format" do
it 'returns the data schema path' do
allow(ActiveRecord).to receive(:schema_format).and_return(:sql)
expect(subject.schema_dump_path(db_config, :sql)).to eq("db/data_schema.rb")
end
end
end
end
end
end
end

0 comments on commit 89bd24c

Please sign in to comment.