Skip to content

Commit

Permalink
fix: allow db:schema:load to work for rails 6.0 and 6.1
Browse files Browse the repository at this point in the history
  • Loading branch information
vprigent committed Sep 27, 2024
1 parent c20e031 commit f94afe6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ appraise 'rails-6.1' do
end

appraise 'rails-7.0' do
gem 'rails', '~> 7.0'
gem 'rails', '~> 7.0.0'
end
33 changes: 12 additions & 21 deletions lib/data_migrate/database_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,28 +68,23 @@ def run_migration(migration, direction)
end
end

def schema_dump_path(db_config, format = ActiveRecord.schema_format)
return ENV["DATA_SCHEMA"] if ENV["DATA_SCHEMA"]
unless Gem::Dependency.new("railties", "< 7.0").match?("railties", Gem.loaded_specs["railties"].version)
# inspired from https://github.com/rails/rails/commit/059d64b874ef8092cca3bc4a7dad06ebd6eeb8ff
def schema_dump_path(db_config, format = ActiveRecord.schema_format)
return ENV["DATA_SCHEMA"] if ENV["DATA_SCHEMA"]

# We only require a schema.rb file for the primary database
return unless db_config.primary?
# We only require a schema.rb file for the primary database
return unless db_config.primary?

if rails_version_lower_than_6_1?
return 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

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)
if rails_version_lower_than_6_1?
return super(file.gsub(/data_schema.rb\z/, 'schema.rb'))
# 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)
ActiveRecord::Tasks::DatabaseTasks.schema_dump_path(ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env, name: "primary"))
end

ActiveRecord::Tasks::DatabaseTasks.schema_dump_path(ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env, name: "primary"))
end
end

Expand Down Expand Up @@ -124,9 +119,5 @@ def self.past_migrations(sort = nil)

sort&.downcase == "asc" ? sort_migrations(migrations) : sort_migrations(migrations).reverse
end

def self.rails_version_lower_than_6_1?
Gem::Dependency.new("railties", "< 6.1").match?("railties", Gem.loaded_specs["railties"].version)
end
end
end

0 comments on commit f94afe6

Please sign in to comment.