From 3156e508387fa107f45ce69aac32c19bf5012d02 Mon Sep 17 00:00:00 2001 From: Jerome Dalbert Date: Tue, 14 May 2024 18:49:58 -0700 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20configure=20Kamal=20storage=20v?= =?UTF-8?q?olume=20if=20not=20needed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Configuring a persistent storage volume in Kamal is needed only for sqlite or Active Storage. If using a different database or the --skip-active-storage option, this configuration can be skipped. --- railties/lib/rails/generators/app_base.rb | 6 +++- .../rails/app/templates/config/deploy.yml.tt | 2 ++ .../test/generators/app_generator_test.rb | 28 +++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/railties/lib/rails/generators/app_base.rb b/railties/lib/rails/generators/app_base.rb index 941540fb562..ba2841319dd 100644 --- a/railties/lib/rails/generators/app_base.rb +++ b/railties/lib/rails/generators/app_base.rb @@ -366,6 +366,10 @@ module Rails options[:skip_active_storage] end + def skip_storage? # :doc: + skip_active_storage? && !sqlite3? + end + def skip_action_cable? # :doc: options[:skip_action_cable] end @@ -784,7 +788,7 @@ module Rails def dockerfile_chown_directories directories = %w(log tmp) - directories << "storage" unless skip_active_storage? && !sqlite3? + directories << "storage" unless skip_storage? directories << "db" unless skip_active_record? directories.sort diff --git a/railties/lib/rails/generators/rails/app/templates/config/deploy.yml.tt b/railties/lib/rails/generators/rails/app/templates/config/deploy.yml.tt index eab99a56db1..0b59d32aaaf 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/deploy.yml.tt +++ b/railties/lib/rails/generators/rails/app/templates/config/deploy.yml.tt @@ -31,11 +31,13 @@ env: # clear: # DB_HOST: 192.168.0.2 +<% unless skip_storage? %> # Use a persistent storage volume for sqlite database files and local Active Storage files. # Recommended to change this to a mounted volume path that is backed up off server. volumes: - "<%= app_name %>_storage:/rails/storage" +<% end %> # Bridge fingerprinted assets, like JS and CSS, between versions to avoid # hitting 404 on in-flight requests. Combines all files from new and old # version inside the asset_path. diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb index 21a1587c74d..2534f5de057 100644 --- a/railties/test/generators/app_generator_test.rb +++ b/railties/test/generators/app_generator_test.rb @@ -668,6 +668,34 @@ class AppGeneratorTest < Rails::Generators::TestCase assert_no_file ".env.erb" end + def test_inclusion_of_kamal_storage_volume + run_generator_and_bundler [destination_root] + + assert_file "config/deploy.yml" do |content| + assert_match(%r{storage:/rails/storage}, content) + end + end + + def test_inclusion_of_kamal_storage_volume_if_only_skip_active_storage_is_given + run_generator_and_bundler [destination_root, "--skip-active-storage"] + + assert_file "config/deploy.yml" do |content| + assert_match(%r{storage:/rails/storage}, content) + end + end + + def test_kamal_storage_volume_is_skipped_if_required + run_generator_and_bundler [ + destination_root, + "--skip-active-storage", + "--database=postgresql" + ] + + assert_file "config/deploy.yml" do |content| + assert_no_match(%r{storage:/rails/storage}, content) + end + end + def test_usage_read_from_file assert_called(File, :read, returns: "USAGE FROM FILE") do assert_equal "USAGE FROM FILE", Rails::Generators::AppGenerator.desc