From 6bbef7e75ee84658ef9a28f8a68f969831168142 Mon Sep 17 00:00:00 2001 From: Matchu Date: Thu, 26 Oct 2023 14:20:15 -0700 Subject: [PATCH] Move the `host: db` part of the dev container into its environment vars MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Okay, so I've kept `database.yml` using the new username and password `impress_dev`, cuz I like that it helps clarify that it's dev stuff and is impossible to confuse with prod. (I updated my local setup to match!) But hardcoding `host: db` into here breaks my local setup where the database _isn't_ at the hostname `db`. So I add a way for new optional database URL environment variables to get merged in with these settings, and then configured the dev container to use that—and just in the most limited override possible, to avoid duplicating stuff we don't need to. (I could've just used the same names `DATABASE_URL_{PRIMARY,OPENNEO_ID}` for this, but idk, I think it's confusing to have the same one for both dev and prod, even though Rails _does_ basically do this; see below.) Normally, the environment variable `DATABASE_URL` just _does_ this, and you don't need to include a `url` key at all to get this behavior. But since we've got the legacy two-database thing going on, we do this instead! If we were to merge the `openneo_id.users` table into the primary database, we could simplify this! --- .devcontainer/devcontainer.json | 13 +++++++++---- config/database.yml | 8 ++++++-- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index cd9b5afe..56ca1e0f 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -22,11 +22,16 @@ // Use 'postCreateCommand' to run commands after the container is created. "postCreateCommand": ".devcontainer/post-create.sh", - // HACK: Out of the box, this dev container doesn't allow installation to the - // default GEM_HOME, because of a weird thing going on with RVM. Instead, we - // set a custom GEM_HOME and GEM_PATH in our home directory! - // https://github.com/devcontainers/templates/issues/188 "containerEnv": { + // Because the database is hosted on the local network at the hostname `db`, + // we partially override `config/database.yml` to connect to `db`! + "DATABASE_URL_PRIMARY_DEV": "mysql2://db", + "DATABASE_URL_OPENNEO_ID_DEV": "mysql2://db", + + // HACK: Out of the box, this dev container doesn't allow installation to + // the default GEM_HOME, because of a weird thing going on with RVM. + // Instead, we set a custom GEM_HOME and GEM_PATH in our home directory! + // https://github.com/devcontainers/templates/issues/188 "GEM_HOME": "~/.rubygems", "GEM_PATH": "~/.rubygems" } diff --git a/config/database.yml b/config/database.yml index 2fbc7152..81a389e9 100644 --- a/config/database.yml +++ b/config/database.yml @@ -1,7 +1,9 @@ development: primary: + # You can override these default settings with this environment variable, + # fully or partially. We do this in the .devcontainer setup! + url: <%= ENV['DATABASE_URL_PRIMARY_DEV'] %> adapter: mysql2 - host: db database: openneo_impress username: impress_dev password: impress_dev @@ -10,8 +12,10 @@ development: sql_mode: TRADITIONAL openneo_id: + # You can override these default settings with this environment variable, + # fully or partially. We do this in the .devcontainer setup! + url: <%= ENV['DATABASE_URL_OPENNEO_ID_DEV'] %> adapter: mysql2 - host: db database: openneo_id username: impress_dev password: impress_dev