如何解决在 init 脚本之前在 postgres testcontainer 中创建一个文件夹
我有以下一段代码:
public static PostgreSQLContainer<?> postgreDBContainer = new PostgreSQLContainer<>("postgres:12")
.withInitScript("init-database-test.sql")
.withUsername("dba")
.withPassword("dba");
在 init 脚本中,我正在创建一些表空间并关联文件夹:
CREATE TABLESPACE tsd01 OWNER dba LOCATION '/tsd01';
CREATE TABLESPACE tsi01 OWNER dba LOCATION '/tsi01';
CREATE TABLESPACE tsisecurity01 OWNER dba LOCATION '/tsisecurity01';
表空间的这些文件夹应该在 init 脚本运行之前创建。我怎样才能做到这一点?
解决方法
通过扩展默认 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","/tsi01/");
execInContainer("mkdir","/tsisecurity01");
execInContainer("chown","/tsisecurity01/");
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
super.containerIsStarted(containerInfo);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。