首页 > 解决方案 > Heroku CI,Phoenix,Elixir:'角色“postgres”不存在''psql:致命:数据库“u13792”不存在'

问题描述

尝试将 Heroku CI 与 Phoenix 和 heroku buildpack elixir 一起使用。

测试正在运行,但收到​​错误消息 psql: FATAL: database "u13792" does not existrole "postgres" does not exist.

应用程序.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;'"
      }
    }
  }
}

测试控制台:

  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
...

配置/test.ex

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

...

当我psql -c 'CREATE ROLE postgres;从 app.json 中删除命令时,它会出错:

没有 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

谢谢你。

标签: elixirphoenix-frameworkheroku-ci

解决方案


好的,显然,config.test.ex 需要一个 DATABASE_URL 变量。
对于任何追随我的人来说,在 dyno 中的东西是不必要的。
所以 app.json 看起来像这样:

应用程序.json

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

最重要的是,配置测试数据库配置需要 DATABASE_URL。

config.test.ex

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

这些资源有所帮助:
博客文章
Heroku 示例应用程序


推荐阅读