Skip to content

Commit 0e52946

Browse files
committed
Use skip_solid_cache? in templates
Adding this highlighted that we need to add support to the change template for deciding whether to include the solid cache configuration when migrating.
1 parent 9ac46c7 commit 0e52946

File tree

10 files changed

+68
-30
lines changed

10 files changed

+68
-30
lines changed

railties/lib/rails/generators/app_base.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ def cable_gemfile_entry
649649
end
650650

651651
def solid_cache_gemfile_entry
652-
return if options[:skip_solid_cache]
652+
return if skip_solid_cache?
653653

654654
comment = "Use Solid Cache for caching (https://github.com/rails/solid_cache)"
655655
GemfileEntry.new("solid_cache", ">= 1.0.1", comment, {}, false)

railties/lib/rails/generators/rails/app/templates/config/databases/mysql.yml.tt

+6-6
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ default: &default
2222
<% end -%>
2323

2424
development:
25-
primary:<% unless options[:skip_solid_cache] %> &development_primary<% end %>
25+
primary:<% unless skip_solid_cache? %> &development_primary<% end %>
2626
<<: *default
2727
database: <%= app_name %>_development
28-
<% unless options[:skip_solid_cache] -%>
28+
<% unless skip_solid_cache? -%>
2929
cache:
3030
<<: *development_primary
3131
<% end -%>
@@ -34,10 +34,10 @@ development:
3434
# re-generated from your development database when you run "rake".
3535
# Do not set this db to the same as development or production.
3636
test:
37-
primary:<% unless options[:skip_solid_cache] %> &test_primary<% end %>
37+
primary:<% unless skip_solid_cache? %> &test_primary<% end %>
3838
<<: *default
3939
database: <%= app_name %>_test
40-
<% unless options[:skip_solid_cache] -%>
40+
<% unless skip_solid_cache? -%>
4141
cache:
4242
<<: *test_primary
4343
<% end -%>
@@ -63,12 +63,12 @@ test:
6363
# for a full overview on how database connection configuration can be specified.
6464
#
6565
production:
66-
primary:<% unless options[:skip_solid_cache] %> &production_primary<% end %>
66+
primary:<% unless skip_solid_cache? %> &production_primary<% end %>
6767
<<: *default
6868
database: <%= app_name %>_production
6969
username: <%= app_name %>
7070
password: <%%= ENV["<%= app_name.upcase %>_DATABASE_PASSWORD"] %>
71-
<% unless options[:skip_solid_cache] -%>
71+
<% unless skip_solid_cache? -%>
7272
cache:
7373
<<: *production_primary
7474
<% end -%>

railties/lib/rails/generators/rails/app/templates/config/databases/postgresql.yml.tt

+6-6
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ default: &default
2727
<% end %>
2828

2929
development:
30-
primary:<% unless options[:skip_solid_cache] %> &development_primary<% end %>
30+
primary:<% unless skip_solid_cache? %> &development_primary<% end %>
3131
<<: *default
3232
database: <%= app_name %>_development
3333

@@ -57,7 +57,7 @@ development:
5757
# log, notice, warning, error, fatal, and panic
5858
# Defaults to warning.
5959
#min_messages: notice
60-
<% unless options[:skip_solid_cache] -%>
60+
<% unless skip_solid_cache? -%>
6161
cache:
6262
<<: *development_primary
6363
<% end -%>
@@ -66,10 +66,10 @@ development:
6666
# re-generated from your development database when you run "rake".
6767
# Do not set this db to the same as development or production.
6868
test:
69-
primary: <% unless options[:skip_solid_cache] %> &test_primary<% end %>
69+
primary: <% unless skip_solid_cache? %> &test_primary<% end %>
7070
<<: *default
7171
database: <%= app_name %>_test
72-
<% unless options[:skip_solid_cache] -%>
72+
<% unless skip_solid_cache? -%>
7373
cache:
7474
<<: *test_primary
7575
<% end -%>
@@ -95,12 +95,12 @@ test:
9595
# for a full overview on how database connection configuration can be specified.
9696
#
9797
production:
98-
primary:<% unless options[:skip_solid_cache] %> &production_primary<% end %>
98+
primary:<% unless skip_solid_cache? %> &production_primary<% end %>
9999
<<: *default
100100
database: <%= app_name %>_production
101101
username: <%= app_name %>
102102
password: <%%= ENV["<%= app_name.upcase %>_DATABASE_PASSWORD"] %>
103-
<% unless options[:skip_solid_cache] -%>
103+
<% unless skip_solid_cache? -%>
104104
cache:
105105
<<: *production_primary
106106
<% end -%>

railties/lib/rails/generators/rails/app/templates/config/databases/sqlite3.yml.tt

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ development:
1313
primary:
1414
<<: *default
1515
database: storage/development.sqlite3
16-
<% unless options[:skip_solid_cache] -%>
16+
<% unless skip_solid_cache? -%>
1717
cache:
1818
<<: *default
1919
database: storage/development_cache.sqlite3
@@ -27,7 +27,7 @@ test:
2727
primary:
2828
<<: *default
2929
database: storage/test.sqlite3
30-
<% unless options[:skip_solid_cache] -%>
30+
<% unless skip_solid_cache? -%>
3131
cache:
3232
<<: *default
3333
database: storage/test_cache.sqlite3
@@ -45,7 +45,7 @@ production:
4545
primary:
4646
<<: *default
4747
# database: path/to/persistent/storage/production.sqlite3
48-
<% unless options[:skip_solid_cache] -%>
48+
<% unless skip_solid_cache? -%>
4949
cache:
5050
<<: *default
5151
# database: path/to/persistent/storage/production_cache.sqlite3
@@ -58,7 +58,7 @@ production:
5858
primary:
5959
<<: *default
6060
database: storage/production.sqlite3
61-
<% unless options[:skip_solid_cache] -%>
61+
<% unless skip_solid_cache? -%>
6262
cache:
6363
<<: *default
6464
database: storage/production_cache.sqlite3

railties/lib/rails/generators/rails/app/templates/config/databases/trilogy.yml.tt

+6-6
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ default: &default
2222
<% end -%>
2323

2424
development:
25-
primary:<% unless options[:skip_solid_cache] %> &development_primary<% end %>
25+
primary:<% unless skip_solid_cache? %> &development_primary<% end %>
2626
<<: *default
2727
database: <%= app_name %>_development
28-
<% unless options[:skip_solid_cache] -%>
28+
<% unless skip_solid_cache? -%>
2929
cache:
3030
<<: *development_primary
3131
<% end -%>
@@ -34,10 +34,10 @@ development:
3434
# re-generated from your development database when you run "rake".
3535
# Do not set this db to the same as development or production.
3636
test:
37-
primary:<% unless options[:skip_solid_cache] %> &test_primary<% end %>
37+
primary:<% unless skip_solid_cache? %> &test_primary<% end %>
3838
<<: *default
3939
database: <%= app_name %>_test
40-
<% unless options[:skip_solid_cache] -%>
40+
<% unless skip_solid_cache? -%>
4141
cache:
4242
<<: *test_primary
4343
<% end -%>
@@ -63,12 +63,12 @@ test:
6363
# for a full overview on how database connection configuration can be specified.
6464
#
6565
production:
66-
primary:<% unless options[:skip_solid_cache] %> &production_primary<% end %>
66+
primary:<% unless skip_solid_cache? %> &production_primary<% end %>
6767
<<: *default
6868
database: <%= app_name %>_production
6969
username: <%= app_name %>
7070
password: <%%= ENV["<%= app_name.upcase %>_DATABASE_PASSWORD"] %>
71-
<% unless options[:skip_solid_cache] -%>
71+
<% unless skip_solid_cache? -%>
7272
cache:
7373
<<: *production_primary
7474
<% end -%>

railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ Rails.application.configure do
7373
# want to log everything, set the level to "debug".
7474
config.log_level = ENV.fetch("RAILS_LOG_LEVEL", "info")
7575

76-
<%- if options[:skip_solid_cache] -%>
76+
<%- if skip_solid_cache? -%>
7777
# Use a different cache store in production.
7878
# config.cache_store = :mem_cache_store
7979
<%- else -%>

railties/lib/rails/generators/rails/db/system/change/change_generator.rb

+14-4
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,12 @@ def edit_devcontainer_files
6060
end
6161

6262
def move_migrations
63-
if database.name == "sqlite3"
64-
move_solid_cache_migrations from: "db/migrate", to: "db/cache/migrate"
65-
else
66-
move_solid_cache_migrations from: "db/cache/migrate", to: "db/migrate"
63+
unless skip_solid_cache?
64+
if database.name == "sqlite3"
65+
move_solid_cache_migrations from: "db/migrate", to: "db/cache/migrate"
66+
else
67+
move_solid_cache_migrations from: "db/cache/migrate", to: "db/migrate"
68+
end
6769
end
6870
end
6971

@@ -208,6 +210,14 @@ def devcontainer?
208210
@devcontainer = File.exist?(File.expand_path(".devcontainer", destination_root))
209211
end
210212

213+
def skip_solid_cache?
214+
return @skip_solid_cache if defined?(@skip_solid_cache)
215+
216+
@skip_solid_cache = \
217+
Dir.glob(File.expand_path("db/cache/migrate/*.solid_cache.rb", destination_root)).empty? &&
218+
Dir.glob(File.expand_path("db/migrate/*.solid_cache.rb", destination_root)).empty?
219+
end
220+
211221
def move_solid_cache_migrations(from:, to:)
212222
migrations_from_folder = File.expand_path(from, destination_root)
213223
migrations_to_folder = File.expand_path(to, destination_root)

railties/lib/rails/generators/rails/devcontainer/devcontainer_generator.rb

+4
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ def devcontainer?
5555
true
5656
end
5757

58+
def skip_solid_cache?
59+
options[:skip_solid_cache]
60+
end
61+
5862
def app_name
5963
options[:app_name]
6064
end

railties/test/generators/db_system_change_generator_test.rb

+21-2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class ChangeGeneratorTest < Rails::Generators::TestCase
4040
assert_match "adapter: postgresql", content
4141
assert_match "database: tmp_production", content
4242
assert_match "host: <%= ENV[\"DB_HOST\"] %>", content
43+
assert_match "cache:\n <<: *production_primary", content
4344
end
4445

4546
assert_file("Gemfile") do |content|
@@ -85,6 +86,7 @@ class ChangeGeneratorTest < Rails::Generators::TestCase
8586
assert_file("config/database.yml") do |content|
8687
assert_match "adapter: mysql2", content
8788
assert_match "database: tmp_production", content
89+
assert_match "cache:\n <<: *production_primary", content
8890
end
8991

9092
assert_file("Gemfile") do |content|
@@ -129,6 +131,7 @@ class ChangeGeneratorTest < Rails::Generators::TestCase
129131
assert_file("config/database.yml") do |content|
130132
assert_match "adapter: sqlite3", content
131133
assert_match "storage/development.sqlite3", content
134+
assert_match "cache:\n <<: *default", content
132135
end
133136

134137
assert_file("Gemfile") do |content|
@@ -186,9 +189,9 @@ class ChangeGeneratorTest < Rails::Generators::TestCase
186189

187190
assert_equal expected_mariadb_config, compose_config["services"]["mariadb"]
188191
assert_includes compose_config["volumes"].keys, "mariadb-data"
189-
190-
assert_solid_cache_migration here: "db/migrate", not_here: "db/cache/migrate"
191192
end
193+
194+
assert_solid_cache_migration here: "db/migrate", not_here: "db/cache/migrate"
192195
end
193196

194197
test "change from versioned gem to other versioned gem" do
@@ -198,6 +201,7 @@ class ChangeGeneratorTest < Rails::Generators::TestCase
198201
assert_file("config/database.yml") do |content|
199202
assert_match "adapter: mysql2", content
200203
assert_match "database: tmp_production", content
204+
assert_match "cache:\n <<: *production_primary", content
201205
end
202206

203207
assert_file("Gemfile") do |content|
@@ -223,6 +227,21 @@ class ChangeGeneratorTest < Rails::Generators::TestCase
223227
assert_not_includes compose_config.keys, "volumes"
224228
end
225229
end
230+
231+
test "change without solid cache" do
232+
rm_r(File.expand_path("db/cache/migrate", destination_root))
233+
234+
run_generator ["--to", "postgresql"]
235+
236+
assert_file("config/database.yml") do |content|
237+
assert_match "adapter: postgresql", content
238+
assert_match "database: tmp_production", content
239+
assert_match "host: <%= ENV[\"DB_HOST\"] %>", content
240+
assert_no_match "cache:\n <<: *production_primary", content
241+
end
242+
243+
assert_no_solid_cache_migration
244+
end
226245
end
227246
end
228247
end

railties/test/generators/generators_test_helper.rb

+5
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ def assert_solid_cache_migration(here:, not_here:)
144144
assert_no_file "#{not_here}/20240821135818_create_solid_cache_entries.solid_cache.rb"
145145
end
146146

147+
def assert_no_solid_cache_migration
148+
assert_no_file "db/migrate/20240821135818_create_solid_cache_entries.solid_cache.rb"
149+
assert_no_file "db/cache/migrate/20240821135818_create_solid_cache_entries.solid_cache.rb"
150+
end
151+
147152
def run_app_update(app_root = destination_root, flags: "--force")
148153
Dir.chdir(app_root) do
149154
gemfile_contents = File.read("Gemfile")

0 commit comments

Comments
 (0)