From 7e9bf43ec8f6c2b278cebbfbfa05fb73dca98a61 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Sat, 13 Mar 2021 16:13:52 +0000 Subject: [PATCH] Guard against calls to the database during db:create Rails 5.2 has changed the way initializers are called during certain rake tasks including `db:create`. Initializers that were previously not loaded are now loaded (basically the whole app is loaded). This means any calls to #table_exists? that appear in the app will throw fatal errors as the database doesn't exist yet during that task, but those calls are made before `db:create` has even started, which means the database can't be created. There are also a few other places in Spree code where #table_exists? is called, and they already call #connected? first to guard against this issue. --- config/application.rb | 3 ++- lib/spree/localized_number.rb | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/config/application.rb b/config/application.rb index f76a58f203..d2876c06fb 100644 --- a/config/application.rb +++ b/config/application.rb @@ -79,7 +79,8 @@ module Openfoodnetwork initializer 'ofn.spree_locale_settings', before: 'spree.promo.environment' do |app| Spree::Config['checkout_zone'] = ENV['CHECKOUT_ZONE'] Spree::Config['currency'] = ENV['CURRENCY'] - if Spree::Country.table_exists? + + if ActiveRecord::Base.connected? && Spree::Country.table_exists? country = Spree::Country.find_by(iso: ENV['DEFAULT_COUNTRY_CODE']) Spree::Config['default_country_id'] = country.id if country.present? else diff --git a/lib/spree/localized_number.rb b/lib/spree/localized_number.rb index f14d4ed176..f6089b2930 100644 --- a/lib/spree/localized_number.rb +++ b/lib/spree/localized_number.rb @@ -9,6 +9,8 @@ module Spree # It also adds a validation on the input format. # It accepts as arguments a variable number of attribute as symbols def localize_number(*attributes) + return unless connected? + validate :validate_localizable_number attributes.each do |attribute|