testcontainers - 在初始化脚本之前在 postgres 测试容器中创建一个文件夹
问题描述
我有以下代码:
public static PostgreSQLContainer<?> postgreDBContainer = new PostgreSQLContainer<>("postgres:12")
.withInitScript("init-database-test.sql")
.withUsername("dba")
.withPassword("dba");
在初始化脚本中,我正在创建一些表空间并关联文件夹:
CREATE TABLESPACE tsd01 OWNER dba LOCATION '/tsd01';
CREATE TABLESPACE tsi01 OWNER dba LOCATION '/tsi01';
CREATE TABLESPACE tsisecurity01 OWNER dba LOCATION '/tsisecurity01';
这些表空间文件夹应该在初始化脚本运行之前创建。我怎么能做到这一点?
解决方案
我能够通过扩展默认值PostgreSQLContainer
并更改containerIsStarted
方法来解决此问题:
public class CustomPostgreSQLContainer<SELF extends CustomPostgreSQLContainer<SELF>> extends PostgreSQLContainer<SELF> {
private static final Logger log = LoggerFactory.getLogger(CustomPostgreSQLContainer.class);
public CustomPostgreSQLContainer() {
super("postgres:12");
}
@Override
protected void containerIsStarted(InspectContainerResponse containerInfo) {
try {
log.debug("M=containerIsStarted, creating database namespace folders and setting permissions");
execInContainer("mkdir", "/tsd01");
execInContainer("chown", "-R", "postgres.postgres", "/tsd01/");
execInContainer("mkdir", "/tsi01");
execInContainer("chown", "-R", "postgres.postgres", "/tsi01/");
execInContainer("mkdir", "/tsisecurity01");
execInContainer("chown", "-R", "postgres.postgres", "/tsisecurity01/");
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
super.containerIsStarted(containerInfo);
}
}
推荐阅读
- python - 如何修复“Nginx 被禁止”的错误?
- javascript - 在 Ajax 中实现这个精确的 PHP foreach 功能?
- java - 如何为 ResultSet 应用 MapReduce?
- java - 从服务器 url 在 android 应用程序中显示 pdf 文件,而无需授予下载该文件的权限
- c++ - C++ 二维向量导致缓冲区溢出
- javascript - React Native 无法构建
- r - 如何获得每周。来自一年的每日数据集的季度和月度计数
- javascript - 为什么要用IFFE做构造函数
- python - AttributeError:“模块”对象没有属性“已发送”
- jquery - Kendo UI jQuery Grid 服务器端过滤