From 22ea8f0b700e72cccacca469e93003703b900b0a Mon Sep 17 00:00:00 2001 From: Justin Vallelonga Date: Wed, 13 Nov 2024 12:35:03 +0700 Subject: [PATCH 1/8] updates database config to work with different options --- config/database.yml | 24 ++++++++++++++++-------- docker-compose.yml | 18 +++++++++++++----- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/config/database.yml b/config/database.yml index b657cbe2b..5ff8667b8 100644 --- a/config/database.yml +++ b/config/database.yml @@ -1,27 +1,35 @@ default: &default adapter: postgresql + username: <%= ENV["HOSTEDGPT_DB_USERNAME"] || "hostedgpt_user" %> + password: <%= ENV["HOSTEDGPT_DB_PASSWORD"] %> + host: <%= ENV["HOSTEDGPT_DB_HOST"] %> + port: <%= ENV["HOSTEDGPT_DB_PORT"] || 5432 %> + # It's best to set DATABASE_URL to configure database details. That overrides this "database" name. + database: <%= ENV["HOSTEDGPT_DB_NAME"] || "hostedgpt" %> encoding: unicode pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> - port: <%= ENV['HOSTEDGPT_DATABASE_PORT'] || 5432 %> <% if RUBY_PLATFORM =~ /darwin/ %> gssencmode: disable <% end %> - url: <%= ENV['DATABASE_URL'] %> - # url format: postgres://username:password@localhost:5432/database_name +# NOTE: DATABASE_URL is a special env var that is automatically used for "url" if it's set +# "url" will be merged into (and take precedence over) the other values for the connection +# url format: postgres://username:password@localhost:5432/database_name +# best practice (https://guides.rubyonrails.org/v7.1/configuring.html#connection-preference) +url: <%= ENV["DATABASE_URL"] %> development: <<: *default - database: <%= ENV['HOSTEDGPT_DEV_DB'] || "hostedgpt_development" %> - # It's best to set DATABASE_URL to configure database details. That overrides this "database" name. Note: Docker sets DATABASE_URL inside docker-compose.yml test: <<: *default - database: <%= ENV['HOSTEDGPT_TEST_DB'] || "hostedgpt_test" %> # It's best to set DATABASE_URL to configure database details. That overrides this "database" name. + database: <%= ENV["HOSTEDGPT_TEST_DB_NAME"] || "hostedgpt_test" %> + <% url_with_db_regex = /(.*\/\/[^\/]+\/)([^?]+)(\??.*)/ %> + url: <%= (ENV["DATABASE_URL"] || "").gsub(url_with_db_regex, '\1\2_test\3') %> # add "_test" to the database name production: <<: *default + # It's best to set DATABASE_URL to configure database details. That overrides this "database" name. database: hostedgpt_production username: hostedgpt - password: <%= ENV["HOSTEDGPT_DATABASE_PASSWORD"] %> - # It's best to set DATABASE_URL to configure database details. That overrides this "database" name. + password: <%= ENV["HOSTEDGPT_DB_PASSWORD"] %> diff --git a/docker-compose.yml b/docker-compose.yml index 012612c85..19cab17a7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,13 +3,15 @@ services: image: postgres:16 restart: always environment: - POSTGRES_USER: app - POSTGRES_DB: app_development - POSTGRES_PASSWORD: secret + # NOTE: If you use DATABASE_URL, you need to make it match these env vars + # so you can either have the url match these default values + # or you can set these env vars to match the url user and password + - POSTGRES_USER=${HOSTEDGPT_DB_USERNAME:-hostedgpt_user} + - POSTGRES_PASSWORD=${HOSTEDGPT_DB_PASSWORD:-secret} volumes: - hostedgpt_pgdata:/var/lib/postgresql/data healthcheck: - test: ["CMD", "pg_isready", "-U", "app", "-d", "app_development"] + test: ["CMD", "pg_isready", "-U", "${HOSTEDGPT_DB_USERNAME:-hostedgpt_user}"] interval: 1s retries: 5 @@ -22,7 +24,13 @@ services: target: development environment: # Be sure to add environment variables to config/options.yml - - DATABASE_URL=postgres://app:secret@postgres/app_development + - HOSTEDGPT_DB_USERNAME=${HOSTEDGPT_DB_USERNAME:-hostedgpt_user} + - HOSTEDGPT_DB_PASSWORD=${HOSTEDGPT_DB_PASSWORD:-secret} + - HOSTEDGPT_DB_HOST=${HOSTEDGPT_DB_HOST:-postgres} + - HOSTEDGPT_DB_PORT=${HOSTEDGPT_DB_PORT:-5432} + - HOSTEDGPT_DB_NAME=${HOSTEDGPT_DB_NAME:-hostedgpt} + - DATABASE_URL + - HOSTEDGPT_TEST_DB_NAME - DEV_HOST=${DEV_HOST:-localhost} # Set if you want to use a different hostname - OVERMIND_COLORS=2,3,5 - ALLOWED_REQUEST_ORIGINS From 09076afc9fdc919e68af65da055cf95d2bc5f179 Mon Sep 17 00:00:00 2001 From: Justin Vallelonga Date: Thu, 14 Nov 2024 01:12:39 +0700 Subject: [PATCH 2/8] simplifying a bit --- config/database.yml | 17 +++++++---------- docker-compose.yml | 10 +++++----- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/config/database.yml b/config/database.yml index 5ff8667b8..ed7b628f7 100644 --- a/config/database.yml +++ b/config/database.yml @@ -1,31 +1,28 @@ default: &default adapter: postgresql + # url format: postgres://username:password@localhost:5432/database_name + url: <%= ENV["DATABASE_URL"] %> + # It's best to set DATABASE_URL to configure database details. That overrides this "database" name. username: <%= ENV["HOSTEDGPT_DB_USERNAME"] || "hostedgpt_user" %> password: <%= ENV["HOSTEDGPT_DB_PASSWORD"] %> host: <%= ENV["HOSTEDGPT_DB_HOST"] %> - port: <%= ENV["HOSTEDGPT_DB_PORT"] || 5432 %> - # It's best to set DATABASE_URL to configure database details. That overrides this "database" name. + port: <%= ENV["HOSTEDGPT_DB_PORT"] %> database: <%= ENV["HOSTEDGPT_DB_NAME"] || "hostedgpt" %> encoding: unicode pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> <% if RUBY_PLATFORM =~ /darwin/ %> gssencmode: disable <% end %> -# NOTE: DATABASE_URL is a special env var that is automatically used for "url" if it's set -# "url" will be merged into (and take precedence over) the other values for the connection -# url format: postgres://username:password@localhost:5432/database_name -# best practice (https://guides.rubyonrails.org/v7.1/configuring.html#connection-preference) -url: <%= ENV["DATABASE_URL"] %> development: <<: *default test: <<: *default - # It's best to set DATABASE_URL to configure database details. That overrides this "database" name. - database: <%= ENV["HOSTEDGPT_TEST_DB_NAME"] || "hostedgpt_test" %> <% url_with_db_regex = /(.*\/\/[^\/]+\/)([^?]+)(\??.*)/ %> - url: <%= (ENV["DATABASE_URL"] || "").gsub(url_with_db_regex, '\1\2_test\3') %> # add "_test" to the database name + url: <%= "#{ENV['DATABASE_URL']}".gsub(url_with_db_regex, '\1\2_test\3') %> # add "_test" to the database name if it's specified + # It's best to set DATABASE_URL to configure database details. That overrides this "database" name. + database: <%= "#{ENV['HOSTEDGPT_DB_NAME'] || 'hostedgpt'}_test" %> production: <<: *default diff --git a/docker-compose.yml b/docker-compose.yml index 19cab17a7..e8a156ec3 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,17 +1,18 @@ services: postgres: image: postgres:16 + container_name: hostedgpt_postgres restart: always environment: - # NOTE: If you use DATABASE_URL, you need to make it match these env vars - # so you can either have the url match these default values - # or you can set these env vars to match the url user and password + # NOTE: If you use DATABASE_URL, it needs to match these env vars + # so you can either have it match the default values for these env vars + # or you can set these env vars to match the user and password from the url - POSTGRES_USER=${HOSTEDGPT_DB_USERNAME:-hostedgpt_user} - POSTGRES_PASSWORD=${HOSTEDGPT_DB_PASSWORD:-secret} volumes: - hostedgpt_pgdata:/var/lib/postgresql/data healthcheck: - test: ["CMD", "pg_isready", "-U", "${HOSTEDGPT_DB_USERNAME:-hostedgpt_user}"] + test: pg_isready -U $$POSTGRES_USER interval: 1s retries: 5 @@ -30,7 +31,6 @@ services: - HOSTEDGPT_DB_PORT=${HOSTEDGPT_DB_PORT:-5432} - HOSTEDGPT_DB_NAME=${HOSTEDGPT_DB_NAME:-hostedgpt} - DATABASE_URL - - HOSTEDGPT_TEST_DB_NAME - DEV_HOST=${DEV_HOST:-localhost} # Set if you want to use a different hostname - OVERMIND_COLORS=2,3,5 - ALLOWED_REQUEST_ORIGINS From 318673b505fb7ef248240560cbb8a06740d21a19 Mon Sep 17 00:00:00 2001 From: Justin Vallelonga Date: Thu, 14 Nov 2024 16:11:21 +0700 Subject: [PATCH 3/8] update comment --- config/database.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/database.yml b/config/database.yml index ed7b628f7..a6d98c4dd 100644 --- a/config/database.yml +++ b/config/database.yml @@ -2,7 +2,7 @@ default: &default adapter: postgresql # url format: postgres://username:password@localhost:5432/database_name url: <%= ENV["DATABASE_URL"] %> - # It's best to set DATABASE_URL to configure database details. That overrides this "database" name. + # It's best to set DATABASE_URL to configure database details. That overrides the following settings username: <%= ENV["HOSTEDGPT_DB_USERNAME"] || "hostedgpt_user" %> password: <%= ENV["HOSTEDGPT_DB_PASSWORD"] %> host: <%= ENV["HOSTEDGPT_DB_HOST"] %> From d7b461d204783a16d37d033cd343ac68ec73e818 Mon Sep 17 00:00:00 2001 From: Justin Vallelonga Date: Thu, 14 Nov 2024 16:33:39 +0700 Subject: [PATCH 4/8] adds docker compose override file to gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index bab408d8f..996384475 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,9 @@ /.tool-versions /node_modules +# Docker Compose override file +**/*compose.override.y*ml + # Ignore all environment files (except templates). /.env* !/.env*.erb From 5e4195086cda31de68772c7c8d505c94431b2efe Mon Sep 17 00:00:00 2001 From: Justin Vallelonga Date: Fri, 15 Nov 2024 07:08:11 +0700 Subject: [PATCH 5/8] removes nesting from ignore for compose override --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 996384475..ed9ff7770 100644 --- a/.gitignore +++ b/.gitignore @@ -9,7 +9,7 @@ /node_modules # Docker Compose override file -**/*compose.override.y*ml +*compose.override.y*ml # Ignore all environment files (except templates). /.env* From 2fea889632f0ef24c727e63a7e81b05a2eb3bae9 Mon Sep 17 00:00:00 2001 From: Justin Vallelonga Date: Fri, 15 Nov 2024 07:14:18 +0700 Subject: [PATCH 6/8] removes variable in favor of better comment --- config/database.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/database.yml b/config/database.yml index a6d98c4dd..c1c150479 100644 --- a/config/database.yml +++ b/config/database.yml @@ -19,8 +19,8 @@ development: test: <<: *default - <% url_with_db_regex = /(.*\/\/[^\/]+\/)([^?]+)(\??.*)/ %> - url: <%= "#{ENV['DATABASE_URL']}".gsub(url_with_db_regex, '\1\2_test\3') %> # add "_test" to the database name if it's specified + # regex to match groups in the url: /(before db name)(db name)(after db name)/ + url: <%= "#{ENV['DATABASE_URL']}".gsub(/(.*\/\/[^\/]+\/)([^?]+)(\??.*)/, '\1\2_test\3') %> # add "_test" to the database name if it's specified # It's best to set DATABASE_URL to configure database details. That overrides this "database" name. database: <%= "#{ENV['HOSTEDGPT_DB_NAME'] || 'hostedgpt'}_test" %> From 9f4bd4c21cb5b221dee33daed1efea7af61ec07c Mon Sep 17 00:00:00 2001 From: Justin Vallelonga Date: Fri, 15 Nov 2024 10:10:46 +0700 Subject: [PATCH 7/8] avoid breaking existing deployments --- config/database.yml | 31 ++++++++++++++++++++++++++----- docker-compose.yml | 4 ++++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/config/database.yml b/config/database.yml index c1c150479..66d205514 100644 --- a/config/database.yml +++ b/config/database.yml @@ -1,13 +1,34 @@ +<% + # first env with a value + def first_of(*envs) + envs.map { |env| ENV[env] }.compact.first + end + + def puts_red(msg) + puts "\e[31m#{msg}\e[0m" + end + + def deprecated(env, message) + puts_red "DEPRECATED: #{env} is deprecated, #{message}" if ENV[env] + end + + + deprecated("HOSTEDGPT_DATABASE_PORT", "use HOSTEDGPT_DB_PORT instead") + deprecated("HOSTEDGPT_DEV_DB", "use HOSTEDGPT_DB_NAME instead") + deprecated("HOSTEDGPT_TEST_DB", "provide HOSTEDGPT_DB_NAME and test database name is generated") + deprecated("HOSTEDGPT_DATABASE_PASSWORD", "use HOSTEDGPT_DB_PASSWORD instead") +%> + default: &default adapter: postgresql # url format: postgres://username:password@localhost:5432/database_name url: <%= ENV["DATABASE_URL"] %> # It's best to set DATABASE_URL to configure database details. That overrides the following settings username: <%= ENV["HOSTEDGPT_DB_USERNAME"] || "hostedgpt_user" %> - password: <%= ENV["HOSTEDGPT_DB_PASSWORD"] %> + password: <%= first_of("HOSTEDGPT_DATABASE_PASSWORD", "HOSTEDGPT_DB_PASSWORD") %> host: <%= ENV["HOSTEDGPT_DB_HOST"] %> - port: <%= ENV["HOSTEDGPT_DB_PORT"] %> - database: <%= ENV["HOSTEDGPT_DB_NAME"] || "hostedgpt" %> + port: <%= first_of("HOSTEDGPT_DATABASE_PORT", "HOSTEDGPT_DB_PORT") || 5432 %> + database: <%= first_of("HOSTEDGPT_DEV_DB", "HOSTEDGPT_DB_NAME") || "hostedgpt" %> encoding: unicode pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> <% if RUBY_PLATFORM =~ /darwin/ %> @@ -22,11 +43,11 @@ test: # regex to match groups in the url: /(before db name)(db name)(after db name)/ url: <%= "#{ENV['DATABASE_URL']}".gsub(/(.*\/\/[^\/]+\/)([^?]+)(\??.*)/, '\1\2_test\3') %> # add "_test" to the database name if it's specified # It's best to set DATABASE_URL to configure database details. That overrides this "database" name. - database: <%= "#{ENV['HOSTEDGPT_DB_NAME'] || 'hostedgpt'}_test" %> + database: <%= ENV["HOSTEDGPT_TEST_DB"] || "#{ENV['HOSTEDGPT_DB_NAME'] || 'hostedgpt'}_test" %> production: <<: *default # It's best to set DATABASE_URL to configure database details. That overrides this "database" name. database: hostedgpt_production username: hostedgpt - password: <%= ENV["HOSTEDGPT_DB_PASSWORD"] %> + password: <%= first_of("HOSTEDGPT_DATABASE_PASSWORD", "HOSTEDGPT_DB_PASSWORD") %> diff --git a/docker-compose.yml b/docker-compose.yml index e8a156ec3..5f724d764 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -31,6 +31,10 @@ services: - HOSTEDGPT_DB_PORT=${HOSTEDGPT_DB_PORT:-5432} - HOSTEDGPT_DB_NAME=${HOSTEDGPT_DB_NAME:-hostedgpt} - DATABASE_URL + - HOSTEDGPT_DATABASE_PORT # DEPRECATED: use HOSTEDGPT_DB_PORT + - HOSTEDGPT_DEV_DB # DEPRECATED: use HOSTEDGPT_DB_NAME + - HOSTEDGPT_TEST_DB # DEPRECATED: provide HOSTEDGPT_DB_NAME and test database name is generated + - HOSTEDGPT_DATABASE_PASSWORD # DEPRECATED: use HOSTEDGPT_DB_PASSWORD - DEV_HOST=${DEV_HOST:-localhost} # Set if you want to use a different hostname - OVERMIND_COLORS=2,3,5 - ALLOWED_REQUEST_ORIGINS From d1ae8fdd9d7d0d7319a744b5c73934b43025f07c Mon Sep 17 00:00:00 2001 From: Justin Vallelonga Date: Fri, 15 Nov 2024 10:29:41 +0700 Subject: [PATCH 8/8] updates actions with env var changes --- .github/workflows/rubyonrails.yml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.github/workflows/rubyonrails.yml b/.github/workflows/rubyonrails.yml index 9b9fe16f0..37733244c 100644 --- a/.github/workflows/rubyonrails.yml +++ b/.github/workflows/rubyonrails.yml @@ -17,7 +17,6 @@ jobs: ports: - "5432:5432" env: - POSTGRES_DB: hostedgpt_test POSTGRES_USER: rails POSTGRES_PASSWORD: password options: >- @@ -27,7 +26,7 @@ jobs: --health-retries 5 env: RAILS_ENV: test - DATABASE_URL: "postgres://rails:password@localhost:5432/hostedgpt_test" + DATABASE_URL: "postgres://rails:password@localhost:5432/hostedgpt" steps: - name: Checkout code uses: actions/checkout@v4 @@ -57,7 +56,6 @@ jobs: ports: - "5432:5432" env: - POSTGRES_DB: hostedgpt_test POSTGRES_USER: rails POSTGRES_PASSWORD: password options: >- @@ -67,7 +65,7 @@ jobs: --health-retries 5 env: RAILS_ENV: test - DATABASE_URL: "postgres://rails:password@localhost:5432/hostedgpt_test" + DATABASE_URL: "postgres://rails:password@localhost:5432/hostedgpt" DISPLAY: "=:99" CHROME_VERSION: "127.0.6533.119"