testcontainers / testcontainers/testcontainers-java

[Enhancement]: Support running init script against a specific database

Open
#8,634 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

type/enhancement
Dominant language
Java
Stars
8.7k
Forks
1.9k
Avg merge
2d 17h
Merged PRs (30d)
9

Description

Module

PostgreSQL

Proposal
  new PostgreSQLContainer<>("postgres").withInitScript("init.sql");

init.sql

CREATE DATABASE example;

\c example

CREATE SCHEMA example;

does not work because the Postgres JDBC driver does not support the \c psql meta-command to switch databases.

This is similar to #7680 and its predecessors #2578, #1997, and #2232.

But in this use case one would would need to specify the database in addition to the init script path.

Something along the lines of:

      new PostgreSQLContainer<>("postgres")
          .withInitScript("createDatabase.sql") // CREATE DATABASE example;
          .withInitScriptForDatabase("example", "createSchema.sql"); // CREATE SCHEMA example;

      new PostgreSQLContainer<>("postgres")
          .withInitScript("createDatabases.sql") // CREATE DATABASE example1; CREATE DATABASE example2;
          .withInitScriptForDatabase("example1", "createExample1Schema.sql"); // CREATE SCHEMA example1;
          .withInitScriptForDatabase("example2", "createExample2Schema.sql"); // CREATE SCHEMA example2;

Note:

new PostgreSQLContainer<>("postgres")
          .withDatabaseName("example")
          ...;

will auto-create the example database but the DDL above would be in reality more complex like:

CREATE DATABASE example WITH OWNER example_admin TEMPLATE template0
  ENCODING UTF8 LC_COLLATE 'de_DE.UTF8' LC_CTYPE 'de_DE.UTF8';

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the PostgreSQL container's existing init-script behavior and the related issues #7680, #2578, #1997, and #2232. Define how scripts target named databases despite the JDBC driver's lack of psql meta-command support; done means the proposed API can create databases and run each schema script against its specified database.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, postgresql
Domain
databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.