postgresql - 使用种子数据重新填充 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
吗?
解决方案
pg_restore
--clean
可用于生成导出脚本,在这种情况下可以使用一些标志如。医生说_
此脚本输出等价于 pg_dump 的纯文本输出格式。因此,一些控制输出的选项类似于 pg_dump 选项。
当您运行pg_restore
恢复数据库时,它只能应用脚本中存在的命令。
您需要pg_dump
使用该--clean
选项运行,以便在还原数据库时应用此命令。
推荐阅读
- wpf - 应用自定义样式时,DatGridCell 会重置
- javascript - 为什么 newList 在 this.setState({ filteredStudents: newList }); 处是一个空数组?
- python - 从图像为自定义神经网络创建 AI Platform 的 json 实例
- resharper - Resharper:将左括号放在新行上
- react-native - 我正在尝试将我的 BottomTabNavigator 集成到我的 StackNavigator
- sql - 如何将 abcd/FR/efgh 替换为 xxxx/FR/xxxx
- spring-boot - Spring Boot MyBatis @MapperScan 不再用作 WAR
- sql - 我需要一个线索如何从这个查询开始
- solidity - 如何可靠地修复错误“此声明遮蔽了现有声明”?
- javascript - 试图计算通过麦克风拾取的声音之间的时间间隔