From bdc33597a505d950c70b4f6ccf6ad5cf8f00c0ff Mon Sep 17 00:00:00 2001 From: Vincent Prigent Date: Sat, 28 Sep 2024 12:22:49 +1200 Subject: [PATCH] fix: allow db:schema:load to work for rails 6.0 and 6.1 --- Appraisals | 2 +- Changelog.md | 5 +++++ lib/data_migrate/database_tasks.rb | 33 +++++++++++------------------- lib/data_migrate/version.rb | 2 +- 4 files changed, 19 insertions(+), 23 deletions(-) diff --git a/Appraisals b/Appraisals index 12b662af..89312e6c 100644 --- a/Appraisals +++ b/Appraisals @@ -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 diff --git a/Changelog.md b/Changelog.md index 675574b9..8dcee22e 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,5 +1,10 @@ # Changelog + +## 9.1.3 + +- Fix #342 for schema load on rails 6.x. (#343) + ## 9.1.2 - Fix #281 to maintain rails 6.0 support. (#342) diff --git a/lib/data_migrate/database_tasks.rb b/lib/data_migrate/database_tasks.rb index 2f69442a..16f975d1 100644 --- a/lib/data_migrate/database_tasks.rb +++ b/lib/data_migrate/database_tasks.rb @@ -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"] + if 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 @@ -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 diff --git a/lib/data_migrate/version.rb b/lib/data_migrate/version.rb index fc027fdd..e28b4298 100644 --- a/lib/data_migrate/version.rb +++ b/lib/data_migrate/version.rb @@ -1,3 +1,3 @@ module DataMigrate - VERSION = "9.1.2".freeze + VERSION = "9.1.3".freeze end