Brainstorming approaches to reconcile Rails 8's default multi-DB setup with DATABASE_URL

Playing with it more, I’m increasingly preferring the ergonomics of providing an option to set a single secret to control the placement of all of the databases. DRY and all that. Sure, if you absolutely want to place the four databases in four different places, you should be able to do that, but otherwise, everything should follow from the single DATABASE_URL secret.

The cleanest implementation I’m finding is to place this into the configuration. Yes, it is mildly ugly, but it is explicit, fully backwards compatible, and can still be overridden with environment variables like CACHE_DATABASE_URL. It seems that url, if present, takes precedence.

diff --git a/config/database.yml b/config/database.yml
index d8c045c..9522215 100644
--- a/config/database.yml
+++ b/config/database.yml
@@ -81,18 +81,22 @@ test:
 production:
   primary: &primary_production
     <<: *default
     database: depot_pg_production
     username: depot_pg
     password: <%= ENV["DEPOT_PG_DATABASE_PASSWORD"] %>
+    url: <%= ENV["DATABASE_URL"] %>
   cache:
     <<: *primary_production
     database: depot_pg_production_cache
     migrations_paths: db/cache_migrate
+    url: <%= URI.parse(ENV["DATABASE_URL"]).tap { |url| url.path += "_cache" } if ENV["DATABASE_URL"] %>
   queue:
     <<: *primary_production
     database: depot_pg_production_queue
     migrations_paths: db/queue_migrate
+    url: <%= URI.parse(ENV["DATABASE_URL"]).tap { |url| url.path += "_queue" } if ENV["DATABASE_URL"] %>
   cable:
     <<: *primary_production
     database: depot_pg_production_cable
     migrations_paths: db/cable_migrate
+    url: <%= URI.parse(ENV["DATABASE_URL"]).tap { |url| url.path += "_cable" } if ENV["DATABASE_URL"] %>

I can make fly launch insert these lines in config/database.yml if the definition of production is a map of maps, and contains more than just the definition for the primary database.