diff --git a/.github/workflows/rubyonrails.yml b/.github/workflows/rubyonrails.yml index 9b9fe16f..37733244 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" diff --git a/.gitignore b/.gitignore index bab408d8..ed9ff777 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 diff --git a/config/database.yml b/config/database.yml index b657cbe2..66d20551 100644 --- a/config/database.yml +++ b/config/database.yml @@ -1,27 +1,53 @@ +<% + # 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: <%= first_of("HOSTEDGPT_DATABASE_PASSWORD", "HOSTEDGPT_DB_PASSWORD") %> + host: <%= ENV["HOSTEDGPT_DB_HOST"] %> + 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 } %> - 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 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" %> + # 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_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_DATABASE_PASSWORD"] %> - # It's best to set DATABASE_URL to configure database details. That overrides this "database" name. + password: <%= first_of("HOSTEDGPT_DATABASE_PASSWORD", "HOSTEDGPT_DB_PASSWORD") %> diff --git a/docker-compose.yml b/docker-compose.yml index 012612c8..5f724d76 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,15 +1,18 @@ services: postgres: image: postgres:16 + container_name: hostedgpt_postgres restart: always environment: - POSTGRES_USER: app - POSTGRES_DB: app_development - POSTGRES_PASSWORD: secret + # 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", "app", "-d", "app_development"] + test: pg_isready -U $$POSTGRES_USER interval: 1s retries: 5 @@ -22,7 +25,16 @@ 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_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