From bab914d4e0660341be130e1be1e767c50e84d5fa Mon Sep 17 00:00:00 2001 From: Jorge Manrubia Date: Sun, 23 Nov 2025 04:32:22 +0100 Subject: [PATCH] Move db check to the Fizzy module --- config/database.yml | 9 +++------ lib/fizzy.rb | 27 ++++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/config/database.yml b/config/database.yml index 913ba7cba..2a9a3aa2e 100644 --- a/config/database.yml +++ b/config/database.yml @@ -1,7 +1,4 @@ <% - database_adapter = ENV.fetch("DATABASE_ADAPTER", "mysql") - use_sqlite = database_adapter == "sqlite" - if ENV["MIGRATE"].present? mysql_app_user_key = "MYSQL_ALTER_USER" mysql_app_password_key = "MYSQL_ALTER_PASSWORD" @@ -17,7 +14,7 @@ %> default: &default - <% if use_sqlite %> + <% if Fizzy.db_adapter.sqlite? %> adapter: sqlite3 pool: 5 timeout: 5000 @@ -33,7 +30,7 @@ default: &default <% end %> development: - <% if use_sqlite %> + <% if Fizzy.db_adapter.sqlite? %> primary: <<: *default database: storage/development.sqlite3 @@ -69,7 +66,7 @@ development: # re-generated from your development database when you run "rake". # Do not set this db to the same as development or production. test: - <% if use_sqlite %> + <% if Fizzy.db_adapter.sqlite? %> primary: <<: *default database: storage/test.sqlite3 diff --git a/lib/fizzy.rb b/lib/fizzy.rb index 7bda12252..16a1ca3a4 100644 --- a/lib/fizzy.rb +++ b/lib/fizzy.rb @@ -1,6 +1,27 @@ module Fizzy - def self.saas? - return @saas if defined?(@saas) - @saas = !!(ENV["SAAS"] || File.exist?(File.expand_path("../../tmp/saas.txt", __dir__))) + class << self + def saas? + return @saas if defined?(@saas) + @saas = !!(ENV["SAAS"] || File.exist?(File.expand_path("../../tmp/saas.txt", __dir__))) + end + + def db_adapter + @db_adapter ||= DbAdapter.new ENV.fetch("DATABASE_ADAPTER", saas? ? "mysql" : "sqlite") + end + end + + class DbAdapter + def initialize(name) + @name = name.to_s + end + + def to_s + @name + end + + # Not using inquiry so that it works before Rails env loads. + def sqlite? + @name == "sqlite" + end end end