My opinionated ruby on rails template
1# frozen_string_literal: true
2
3say 'Configuring PostgreSQL with multi-database setup...', :green
4
5# Only add pg gem if not already present (Rails 8+ includes it by default)
6gem 'pg' unless File.read('Gemfile').match?(/gem ['"]pg['"]/) rescue false
7
8say ' Creating database.yml...', :cyan
9file 'config/database.yml', <<~YAML, force: true
10 # PostgreSQL for all environments (with Row Level Security support)
11 # Ensure PostgreSQL is running locally for development
12 default: &default
13 adapter: postgresql
14 encoding: unicode
15 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
16 prepared_statements: true
17 advisory_locks: true
18 url: <%= ENV['DATABASE_URL'] %>
19
20 development:
21 primary: &primary_development
22 <<: *default
23 database: #{app_name}_development
24 queue:
25 <<: *primary_development
26 database: #{app_name}_queue_development
27 migrations_paths: db/queue_migrate
28 cache:
29 <<: *primary_development
30 database: #{app_name}_cache_development
31 migrations_paths: db/cache_migrate
32 cable:
33 <<: *primary_development
34 database: #{app_name}_cable_development
35 migrations_paths: db/cable_migrate
36
37 test:
38 primary: &primary_test
39 <<: *default
40 database: #{app_name}_test
41 queue:
42 <<: *primary_test
43 database: #{app_name}_queue_test
44 migrations_paths: db/queue_migrate
45 cache:
46 <<: *primary_test
47 database: #{app_name}_cache_test
48 migrations_paths: db/cache_migrate
49 cable:
50 <<: *primary_test
51 database: #{app_name}_cable_test
52 migrations_paths: db/cable_migrate
53
54 production:
55 primary: &primary_production
56 <<: *default
57 database: #{app_name}_production
58 username: #{app_name}
59 password: <%= ENV["#{app_name.upcase}_DATABASE_PASSWORD"] %>
60 queue:
61 <<: *primary_production
62 database: #{app_name}_queue_production
63 migrations_paths: db/queue_migrate
64 cache:
65 <<: *primary_production
66 database: #{app_name}_cache_production
67 migrations_paths: db/cache_migrate
68 cable:
69 <<: *primary_production
70 database: #{app_name}_cable_production
71 migrations_paths: db/cable_migrate
72YAML
73
74say 'Database configuration complete!', :green
75say ' Primary database: #{app_name}_[env]', :cyan
76say ' Queue database: #{app_name}_queue_[env]', :cyan
77say ' Cache database: #{app_name}_cache_[env]', :cyan
78say ' Cable database: #{app_name}_cable_[env]', :cyan