TypeORM 迁移条目从 DB 丢失,`migration:运行` 重新 运行s 它们,然后失败 "relation already exists"

TypeORM migration entries lost from DB, `migration:run` re-runs them, then fails with "relation already exists"

我有一个NestJS app with TypeORM, dockerized。我关闭了 synchronize,改用迁移。在容器入口点,我做 yarn typeorm migration:run。它第一次运行良好,并根据日志将记录插入 migrations table.

我注意到,当我下次启动该项目时,它经常尝试重新运行 迁移并由于“关系已存在”而失败(正如预期的那样)。此时我可以通过 docker-compose exec db psql -U postgres -c 'SELECT * FROM "migrations" "migrations" 验证 migrations table 中确实缺少条目。数据库架构是最新的。当我手动插入一条新记录时,它会在 缺失的记录之后获得一个增量 ID 。所以记录在某个时候存在。

我不知道是什么导致 migrations table 中的条目消失(被回滚?)。这发生在上面链接的项目上。这是一个简单的示例项目。我没有意外命名为“迁移”的实体。 :)

作为 解决方法 我目前手动插入 migrations table:

docker-compose exec db psql -U postgres -c "INSERT INTO migrations (timestamp, name) VALUES ('1619623728180', 'AddTable1619623728180');"

运行 同步数据库的规范是问题所在。

我有一个 .env.test 来使用不同的数据库,但是 that is not supported by dotenv. There are a 让它工作。我选择 dotenv-flow/config 并将其添加到我的测试脚本中:

jest --collect-coverage --setupFiles dotenv-flow/config