diff --git a/Gemfile b/Gemfile index 3519cfe7c..7fbab721f 100644 --- a/Gemfile +++ b/Gemfile @@ -7,8 +7,10 @@ gem "propshaft" # Use sqlite3 as the database for Active Record. Also in :desktop so the bundled # desktop app (which always runs on SQLite, never Postgres) ships the adapter. gem "sqlite3", ">= 2.1", groups: [ :development, :test, :desktop ] -# Use postgresql as the database for production -gem "pg", group: :production +# Use postgresql as the database for production, and opt-in for local +# development/test via DB_ADAPTER=postgresql or DATABASE_URL (SQLite -> Postgres +# migration). Requires libpq (e.g. `brew install libpq` / postgresql). +gem "pg", groups: [ :development, :test, :production ] # Use the Puma web server [https://github.com/puma/puma] gem "puma", ">= 5.0" # Bundle JavaScript via esbuild [https://github.com/rails/jsbundling-rails] diff --git a/config/database.yml b/config/database.yml index ee16ebdfb..6e603ab48 100644 --- a/config/database.yml +++ b/config/database.yml @@ -4,7 +4,16 @@ # Ensure the SQLite 3 gem is defined in your Gemfile # gem "sqlite3" # -<% adapter = ENV["DATABASE_URL"].to_s.then { |u| u.empty? ? "sqlite3" : URI.parse(u).scheme } %> +<% + # Adapter selection (development/test): + # 1. DATABASE_URL set -> use its scheme (postgres cutover, CI, etc.) + # 2. DB_ADAPTER=postgresql -> local Postgres, no URL required + # 3. otherwise -> sqlite3 (zero-config default) + database_url = ENV["DATABASE_URL"].to_s + raw_adapter = database_url.empty? ? ENV.fetch("DB_ADAPTER", "sqlite3") : URI.parse(database_url).scheme + # Normalize the "postgres" URL scheme to Rails' registered "postgresql" adapter name. + adapter = raw_adapter == "postgres" ? "postgresql" : raw_adapter +%> default: &default adapter: <%= adapter %> pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> @@ -13,18 +22,35 @@ default: &default variables: journal_mode: WAL synchronous: normal + <% elsif database_url.empty? %> + encoding: unicode + host: <%= ENV.fetch("DATABASE_HOST", "localhost") %> + username: <%= ENV.fetch("DATABASE_USERNAME", "collavre_user") %> + password: <%= ENV["DATABASE_PASSWORD"] %> <% end %> development: <<: *default + <% if adapter == "sqlite3" %> database: storage/development.sqlite3 + <% elsif database_url.empty? %> + database: <%= ENV.fetch("DATABASE_NAME", "collavre_db") %> + <% else %> + url: <%= database_url %> + <% end %> # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". # Do not set this db to the same as development or production. test: <<: *default + <% if adapter == "sqlite3" %> database: storage/test.sqlite3 + <% elsif database_url.empty? %> + database: <%= ENV.fetch("TEST_DATABASE_NAME", "collavre_test") %> + <% else %> + url: <%= database_url %> + <% end %> production: primary: diff --git a/env.template b/env.template index 3b8b7570c..6c2b6111d 100644 --- a/env.template +++ b/env.template @@ -7,6 +7,15 @@ KAMAL_REGISTRY_PASSWORD= RAILS_MASTER_KEY= SECRET_KEY_BASE= # DATABASE_URL=sqlite3:storage/production-primary.sqlite3 +# Local development/test on Postgres (SQLite -> Postgres migration). Leave unset +# for the default SQLite. Set DB_ADAPTER=postgresql to use the local collavre_db +# without a DATABASE_URL; the vars below override the built-in local defaults. +# DB_ADAPTER=postgresql +# DATABASE_HOST=localhost +# DATABASE_USERNAME=collavre_user +# DATABASE_PASSWORD= +# DATABASE_NAME=collavre_db +# TEST_DATABASE_NAME=collavre_test PORT=80 # Run the Solid Queue supervisor inside Puma on this deploy target (default: true). # Set to false when several targets share one database, so that exactly one of