elixirphoenix-frameworkheroku-ci

Heroku CI, Phoenix, Elixir: 'role "postgres" does not exist' 'psql: FATAL: database "u13792" does not exist'


Trying to use Heroku CI with Phoenix and heroku buildpack elixir.

Tests are running, but getting error message psql: FATAL: database "u13792" does not exist or role "postgres" does not exist.

app.json

{
  "addons": [
    "heroku-postgresql"
  ],
  "buildpacks": [
    {
      "url": "https://github.com/HashNuke/heroku-buildpack-elixir.git"
    }
  ],
  "env": {
    "ACCEPT_ORIGIN": {
      "required": true
    },
    "POOL_SIZE": {
      "required": true
    },
    "SECRET_KEY_BASE": {
      "required": true
    }
  },
  "formation": {
    "web": {
      "quantity": 1
    }
  },
  "name": "valius-api",
  "scripts": {
  },
  "stack": "heroku-18",
  "environments": {
    "test": {
      "env": {
        "MIX_ENV": "test"
      },
      "addons": ["heroku-postgresql:in-dyno"],
      "scripts": {
        "test-setup": "psql -c 'CREATE ROLE postgres;'"
      }
    }
  }
}

Test Console:

  lib/mix/tasks/ja_serializer.gen.phoenix_api.ex:57

  lib/mix/tasks/ja_serializer.gen.phoenix_api.ex:60

warning: function Mix.Phoenix.params/1 is undefined or private

  lib/mix/tasks/ja_serializer.gen.phoenix_api.ex:44

Generated ja_serializer app

==> phoenix_ecto

Compiling 6 files (.ex)

Generated phoenix_ecto app

==> api

Compiling 21 files (.ex)

Generated api app

-----> Creating .profile.d with env vars

-----> Writing export for multi-buildpack support

-----> Running test-setup command `psql -c 'CREATE ROLE postgres;'`...

psql: FATAL:  database "u13792" does not exist

-----> test-setup command `psql -c 'CREATE ROLE postgres;'` failed with exit status 2

mix.exs

...
  defp aliases do
    [
      "ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"],
      "ecto.reset": ["ecto.drop", "ecto.setup"],
      test: ["ecto.migrate", "test"],
      "test.local": ["ecto.create --quiet", "ecto.migrate", "test"]
    ]
  end
...

config/test.ex

...
# Configure your database
config :api, Api.Repo,
  username: "postgres",
  password: "postgres",
  database: "api_test",
  hostname: "localhost",
  pool: Ecto.Adapters.SQL.Sandbox

...

When I remove the psql -c 'CREATE ROLE postgres; command from app.json, it errors with:

Test Console without CREATE ROLE

-----> The postgresql buildpack does not run tests. Skipping.

-----> Running Elixir buildpack tests...

21:24:33.873 [error] Postgrex.Protocol (#PID<0.245.0>) failed to connect: ** (Postgrex.Error) FATAL 28000 (invalid_authorization_specification) role "postgres" does not exist

21:24:33.873 [error] Postgrex.Protocol (#PID<0.246.0>) failed to connect: ** (Postgrex.Error) FATAL 28000 (invalid_authorization_specification) role "postgres" does not exist

21:24:35.752 [error] Postgrex.Protocol (#PID<0.245.0>) failed to connect: ** (Postgrex.Error) FATAL 28000 (invalid_authorization_specification) role "postgres" does not exist

21:24:36.124 [error] Postgrex.Protocol (#PID<0.246.0>) failed to connect: ** (Postgrex.Error) FATAL 28000 (invalid_authorization_specification) role "postgres" does not exist

** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2984ms. You can configure how long requests wait in the queue using :queue_target and :queue_interval. See DBConnection.start_link/2 for more information

    (db_connection) lib/db_connection/ownership.ex:81: DBConnection.Ownership.ownership_checkout/2

    (ecto_sql) lib/ecto/adapters/sql/sandbox.ex:431: Ecto.Adapters.SQL.Sandbox.checkout/2

    (ecto_sql) lib/ecto/adapters/sql/sandbox.ex:478: Ecto.Adapters.SQL.Sandbox.unboxed_run/2

    (ecto_sql) lib/mix/tasks/ecto.migrate.ex:108: anonymous fn/4 in Mix.Tasks.Ecto.Migrate.run/2

    (elixir) lib/enum.ex:765: Enum."-each/2-lists^foreach/1-0-"/2

    (elixir) lib/enum.ex:765: Enum.each/2

    (mix) lib/mix/task.ex:316: Mix.Task.run_task/3

    (mix) lib/mix/task.ex:350: Mix.Task.run_alias/3

-----> Elixir buildpack tests failed with exit status 1

Thank you.


Solution

  • OK, apparently, the config.test.ex needed a DATABASE_URL var.
    For anyone coming after me, the in-dyno stuff wasn't necessary.
    So the app.json can look like this:

    app.json

    ...
      },
      "stack": "heroku-18",
      "environments": {
        "test": {
          "env": {
            "MIX_ENV": "test"
          }
        }
      }
    }
    

    And most importantly, the config test database config needs the DATABASE_URL.

    config.test.ex

    ...
    config :api, Api.Repo,
      adapter: Ecto.Adapters.Postgres,
      url: System.get_env("DATABASE_URL"),
      pool: Ecto.Adapters.SQL.Sandbox
    ...
    

    These resources helped:
    Blog Post
    Heroku Example App