From 170530e52ec390f0abb8b249ebb5176e1a251759 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Mon, 6 May 2024 02:58:31 +0200 Subject: [PATCH] Unify database configuration files We had three files that were almost identical, and we can use environment variables to specify the differences. Note we're using the `PGUSER` and `PGPASSWORD` variables, since these variables will automatically be used by the PostgreSQL client when we have a blank `username` and `password` keys in the `database.yml` file (which we did until now). The difference between these variables and the `POSTGRES_USER` and `POSTGRES_PASSWORD` variables is that the `PG` variables are used by the client connecting to the database, while the `POSTGRES_` variables are used by the Docker postgresql image when creating the database superuser. For consistency with the code in our github workflows (and everywhere else in the postgres world), we're respecting this double standard. The fact that there are two different names for what's basically the same thing makes the code confusing, though, particularly when running the docker-compose commands, since we get the password from an environment variable but we have to assign two different environment variables with it. So we're accepting both `PGPASSWORD` and `POSTGRES_PASSWORD` variables in the database configuration file. This way, developers using docker-compose can use `POSTGRES_PASSWORD` for everything and it'll work fine. We're also making `PGPASSWORD` default to `POSTGRES_PASSWORD` so we don't get a warning if we only set `POSTGRES_PASSWORD`: ``` WARN[0000] The "PGPASSWORD" variable is not set. Defaulting to a blank string. ``` Also note we're using `DB_HOST` instead of `PGHOST` because that's the variable Rails currently uses by default for new applications [1]. Finally, note we're using `.presence` in the `ENV` calls in the database.yml file. The `PGPASSWORD` variable was set to an empty string when running docker-compose, so using `ENV["PGPASSWORD"] ||` wouldn't work. [1] https://github.com/rails/rails/blob/c90a8701e5/railties/lib/rails/generators/rails/app/templates/config/databases/postgresql.yml.tt#L22 --- .gitlab-ci.yml | 4 +++- config/database-docker.yml.example | 29 ----------------------------- config/database.yml.example | 6 +++--- config/database.yml.gitlab | 12 ------------ docker-compose.yml | 4 ++++ docs/en/installation/docker.md | 2 +- docs/es/installation/docker.md | 2 +- 7 files changed, 12 insertions(+), 47 deletions(-) delete mode 100644 config/database-docker.yml.example delete mode 100644 config/database.yml.gitlab diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index f9c9a5291..39235b609 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -12,6 +12,9 @@ tests: paths: - vendor/ variables: + DB_HOST: postgres + PGUSER: consul + PGPASSWORD: password POSTGRES_USER: consul POSTGRES_PASSWORD: password RAILS_ENV: test @@ -20,7 +23,6 @@ tests: script: - apt-get update && apt-get install -y nodejs npm chromium - for i in config/*.example; do cp "$i" "${i/.example}"; done - - for i in config/*.gitlab; do cp "$i" "${i/.gitlab}"; done - bundle --without development - npm clean-install - bundle exec rake db:setup diff --git a/config/database-docker.yml.example b/config/database-docker.yml.example deleted file mode 100644 index 2fb012a61..000000000 --- a/config/database-docker.yml.example +++ /dev/null @@ -1,29 +0,0 @@ -default: &default - adapter: postgresql - encoding: unicode - #host: localhost - host: database #<--the name of the db in the docker-compose - pool: 5 - port: 5432 - schema_search_path: "public,shared_extensions" - username: postgres - password: <%= ENV["POSTGRES_PASSWORD"] %> - -development: &development - <<: *default - database: consul_development - -# The staging, preproduction and production dbs are only needed -# for running rake assets:precompile locally before deploying -staging: - <<: *development - -preproduction: - <<: *development - -production: - <<: *development - -test: - <<: *default - database: consul_test diff --git a/config/database.yml.example b/config/database.yml.example index 2e5e8d82d..5a44e492a 100644 --- a/config/database.yml.example +++ b/config/database.yml.example @@ -1,11 +1,11 @@ default: &default adapter: postgresql encoding: unicode - host: localhost + host: <%= ENV["DB_HOST"].presence || "localhost" %> pool: 5 schema_search_path: "public,shared_extensions" - username: - password: + username: <%= ENV["PGUSER"].presence %> + password: <%= ENV["PGPASSWORD"].presence || ENV["POSTGRES_PASSWORD"].presence %> development: &development <<: *default diff --git a/config/database.yml.gitlab b/config/database.yml.gitlab deleted file mode 100644 index da09aabec..000000000 --- a/config/database.yml.gitlab +++ /dev/null @@ -1,12 +0,0 @@ -default: &default - adapter: postgresql - encoding: unicode - host: postgres - pool: 5 - schema_search_path: "public,shared_extensions" - username: consul - password: password - -test: - <<: *default - database: consul_test diff --git a/docker-compose.yml b/docker-compose.yml index 1ffa2db3f..9fa49af98 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,6 +6,7 @@ services: # see https://hub.docker.com/_/postgres/ image: postgres:13.16 environment: + - POSTGRES_USER=postgres - POSTGRES_PASSWORD=$POSTGRES_PASSWORD # persist the database between containers by storing it in a volume volumes: @@ -34,6 +35,9 @@ services: - bundle:/usr/local/bundle - node_modules:/var/www/consul/node_modules environment: + - DB_HOST=database # Same name as the database service + - PGUSER=postgres + - PGPASSWORD=${PGPASSWORD-$POSTGRES_PASSWORD} - POSTGRES_PASSWORD=$POSTGRES_PASSWORD volumes: db_data: {} diff --git a/docs/en/installation/docker.md b/docs/en/installation/docker.md index c2c23187f..88f42a101 100644 --- a/docs/en/installation/docker.md +++ b/docs/en/installation/docker.md @@ -44,7 +44,7 @@ Then create the secrets and database config files based on the example files: ```bash cp config/secrets.yml.example config/secrets.yml -cp config/database-docker.yml.example config/database.yml +cp config/database.yml.example config/database.yml ``` Then build the image with: diff --git a/docs/es/installation/docker.md b/docs/es/installation/docker.md index 4f7994e38..8db5d62c3 100644 --- a/docs/es/installation/docker.md +++ b/docs/es/installation/docker.md @@ -44,7 +44,7 @@ A continuación, crea los ficheros de `config/secrets.yml` y `config/database.ym ```bash cp config/secrets.yml.example config/secrets.yml -cp config/database-docker.yml.example config/database.yml +cp config/database.yml.example config/database.yml ``` Ahora genera la imagen con: