Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix data:schema:load for structure.sql #281

Merged
merged 8 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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