From c67bf60c000c459384b5f4d235d30b20b1e10c29 Mon Sep 17 00:00:00 2001 From: Chris Todorov Date: Wed, 6 Oct 2021 17:18:17 -0700 Subject: [PATCH 1/2] Introduce a configuration value for migration path Since Rails 6 multiple database support is a native feature, which allows users to configure separate read/write databases. This change allows users to configure the path to the migrations folder from the default "db/migrate" if they are using the multiple database support and have specified a different database for the Solidus engine. This will ensure the check that all migrations are installed can look at a user configurable folder. One change required to make the folder path configurable is to run the migration check after 'load_config_initializers' instead of before. This will allow the value of `migration_path` configuration to be set in an initializer. Co-authored-by: Adam Mueller Co-authored-by: Alex Blackie Co-authored-by: Benjamin Willems Co-authored-by: Jared Norman --- core/lib/spree/app_configuration.rb | 12 ++++++++++- core/lib/spree/core/engine.rb | 2 +- core/lib/spree/migrations.rb | 2 +- core/spec/lib/spree/app_configuration_spec.rb | 20 +++++++++++++++++++ core/spec/lib/spree/migrations_spec.rb | 6 +++++- 5 files changed, 38 insertions(+), 4 deletions(-) diff --git a/core/lib/spree/app_configuration.rb b/core/lib/spree/app_configuration.rb index e0276aa5ac1..d167d0c83f2 100644 --- a/core/lib/spree/app_configuration.rb +++ b/core/lib/spree/app_configuration.rb @@ -263,7 +263,6 @@ class AppConfiguration < Preferences::Configuration # @return [] Track on_hand values for variants / products. (default: true) preference :track_inventory_levels, :boolean, default: true - # Other configurations # Allows restricting what currencies will be available. @@ -525,6 +524,17 @@ def payment_canceller # Enumerable of taxons adhering to the present_taxon_class interface class_name_attribute :taxon_attachment_module, default: 'Spree::Taxon::ActiveStorageAttachment' + # Configures the absolute path that contains the Solidus engine + # migrations. This will be checked at app boot to confirm that all Solidus + # migrations are installed. + # + # @!attribute [rw] migration_path + # @return [Pathname] the configured path. (default: `Rails.root.join('db', 'migrate')`) + attr_writer :migration_path + def migration_path + @migration_path ||= ::Rails.root.join('db', 'migrate') + end + # Allows providing your own class instance for generating order numbers. # # @!attribute [rw] order_number_generator diff --git a/core/lib/spree/core/engine.rb b/core/lib/spree/core/engine.rb index 1055a58d322..a42590f1cf3 100644 --- a/core/lib/spree/core/engine.rb +++ b/core/lib/spree/core/engine.rb @@ -40,7 +40,7 @@ class Engine < ::Rails::Engine ] end - initializer "spree.core.checking_migrations", before: :load_config_initializers do |_app| + initializer "spree.core.checking_migrations", after: :load_config_initializers do |_app| Migrations.new(config, engine_name).check end diff --git a/core/lib/spree/migrations.rb b/core/lib/spree/migrations.rb index 63a05a21956..340ba31343b 100644 --- a/core/lib/spree/migrations.rb +++ b/core/lib/spree/migrations.rb @@ -66,7 +66,7 @@ def app_migrations end def app_dir - "#{Rails.root}/db/migrate" + Spree::Config.migration_path end def engine_dir diff --git a/core/spec/lib/spree/app_configuration_spec.rb b/core/spec/lib/spree/app_configuration_spec.rb index 58a9360d6e7..5296419e6fc 100644 --- a/core/spec/lib/spree/app_configuration_spec.rb +++ b/core/spec/lib/spree/app_configuration_spec.rb @@ -129,6 +129,26 @@ class DummyClass; end; end end + describe "#migration_path" do + subject { config_instance.migration_path } + + let(:config_instance) { described_class.new } + + it "has a default value" do + expect(subject.to_s).to end_with "db/migrate" + end + + context "with a custom value" do + before do + config_instance.migration_path = "db/secondary_database" + end + + it "returns the configured value" do + expect(subject).to eq "db/secondary_database" + end + end + end + it 'has a default admin VAT location with nil values by default' do expect(prefs.admin_vat_location).to eq(Spree::Tax::TaxLocation.new) expect(prefs.admin_vat_location.state_id).to eq(nil) diff --git a/core/spec/lib/spree/migrations_spec.rb b/core/spec/lib/spree/migrations_spec.rb index 0b6a0817d6c..5aa67450e8d 100644 --- a/core/spec/lib/spree/migrations_spec.rb +++ b/core/spec/lib/spree/migrations_spec.rb @@ -10,10 +10,14 @@ module Spree let(:config) { double("Config", root: "dir") } let(:engine_dir) { "dir/db/migrate" } - let(:app_dir) { "#{Rails.root}/db/migrate" } + let(:app_dir) { 'app/db/migrate' } subject { described_class.new(config, "spree") } + before do + stub_spree_preferences(migration_path: app_dir) + end + it "detects missing migrations" do expect(Dir).to receive(:entries).with(app_dir).and_return app_migrations expect(Dir).to receive(:entries).with(engine_dir).and_return engine_migrations From 9eaacc62a0e645e2f0f15ef0aa45db66bdd69ba0 Mon Sep 17 00:00:00 2001 From: Chris Todorov Date: Thu, 14 Oct 2021 11:29:35 -0700 Subject: [PATCH 2/2] Add changelog entry for `migration_path` change --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e3755ce545..70e36549fc4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## Solidus 3.2.0.alpha (master, unreleased) +### Core + +- Add configuration option for `migration_path` [#4190](https://github.com/solidusio/solidus/pull/4190) ([SuperGoodSoft](https://github.com/supergoodsoft/)) + ## Solidus 3.1.1 (v3.1, 2021-09-20) - Add deprecation path for arity-zero preference defaults [#4170](https://github.com/solidusio/solidus/pull/4170) ([waiting-for-dev](https://github.com/waiting-for-dev))