When I run mix ecto.migrate --prefix private
, the command fails if no schema_migrations
table exists in the public
prefix.
- Elixir: 1.12.2
- Erlang/OTP: 24
- Postgres: 13-alpine running in docker container
- OS: MacOS 11.4
- Dependencies:
- ecto: 3.7.0
- ecto_sql: 3.7.0
- postgrex: 0.15.10
- Others: see
mix.lock
file
mix deps.get
docker compose up -d
mix ecto.create
- Create schema
private
. You can use your favorite DB client or psql with:psql -h localhost -U postgres ecto_migration
- Enter
postgres
as password CREATE SCHEMA private;
mix ecto.migrate --prefix private
schema_migrations
andsample_table
are created in theprivate
schemaschema_migrations
contains the timestamp of the migration that createdsample_table
schema_migrations
is created in theprivate
schemasample_table
is not being createdschema_migrations
is emptymix ecto.migrate
fails with the error:
** (MatchError) no match of right hand side value: {:error, %Postgrex.Error{connection_id: 5373, message: nil, postgres: %{code: :undefined_table, file: "namespace.c", line: "431", message: "relation \"schema_migrations\" does not exist", pg_code: "42P01", routine: "RangeVarGetRelidExtended", severity: "ERROR", unknown: "ERROR"}, query: nil}}
(ecto_sql 3.7.0) lib/ecto/adapters/postgres.ex:220: anonymous fn/3 in Ecto.Adapters.Postgres.lock_for_migrations/3
(ecto_sql 3.7.0) lib/ecto/adapters/sql.ex:1013: anonymous fn/3 in Ecto.Adapters.SQL.checkout_or_transaction/4
(db_connection 2.4.0) lib/db_connection.ex:1512: DBConnection.run_transaction/4
(ecto_sql 3.7.0) lib/ecto/adapters/postgres.ex:215: Ecto.Adapters.Postgres.lock_for_migrations/3
(ecto_sql 3.7.0) lib/ecto/migrator.ex:493: Ecto.Migrator.lock_for_migrations/4
(ecto_sql 3.7.0) lib/ecto/migrator.ex:388: Ecto.Migrator.run/4
(ecto_sql 3.7.0) lib/ecto/migrator.ex:146: Ecto.Migrator.with_repo/3
(ecto_sql 3.7.0) lib/mix/tasks/ecto.migrate.ex:133: anonymous fn/5 in Mix.Tasks.Ecto.Migrate.run/2
mix ecto.migrate --prefix private
only works if there already is a schema_migrations
table in the public
prefix.
The error suggests that Ecto is trying to lock the schema_migrations
table in the public
prefix. Since there is none there, Ecto is raises an error.
In fact, If a schema_migrations
table exists in the public
schema, the migration command with prefix succeeds.
Note that the structure or content of this table is irrelevant, it can just be a dummy table created with create table schema_migrations ();
This suggests that ecto is simply trying to lock the schema_migrations
table in the wrong schema (public
) before running the migration.