首页 > 解决方案 > 使用种子数据重新填充 Postgres 数据库以进行测试

问题描述

我正在尝试为我的应用程序创建一些集成测试,其中一部分我想在每次测试运行时播种我的应用程序。但是,数据库需要保持连接,因此dropdb/createdb不是一个选项。

我目前正在做:

pg_dump -F custom -O -x -d test-db > ./tests/test-db

测试前,并且:

pg_restore --clean -d test-db ./tests/test-db

在每次测试运行后恢复数据库。

但是,这似乎不起作用,因为该pg_restore命令会引发很多错误:

pg_restore: [archiver (db)] Error while PROCESSING TOC:
pg_restore: [archiver (db)] Error from TOC entry 3771; 2618 175960 RULE skilled_outcome_group _RETURN dan
pg_restore: [archiver (db)] could not execute query: ERROR:  cannot drop rule _RETURN on view public.skilled_outcome_group because view public.skilled_outcome_group requires it
pg_restore: [archiver (db)] Error from TOC entry 184; 1259 174070 TABLE outcome_group_skill dan
pg_restore: [archiver (db)] could not execute query: ERROR:  cannot drop table public.outcome_group_skill because other objects depend on it

它继续这样......我以为--clean是要完全放弃一切,但是pg_restore抱怨它不能删除表/视图?pg_dump我在/中的标志组合有问题pg_restore吗?

标签: postgresqltestingdatabase-restore

解决方案


pg_restore--clean可用于生成导出脚本,在这种情况下可以使用一些标志如。医生说_

此脚本输出等价于 pg_dump 的纯文本输出格式。因此,一些控制输出的选项类似于 pg_dump 选项。

当您运行pg_restore恢复数据库时,它只能应用脚本中存在的命令。

您需要pg_dump使用该--clean选项运行,以便在还原数据库时应用此命令。


推荐阅读