tada / pljava

PL/Java is a free add-on module that brings Java™ Stored Procedures, Triggers, Functions, Aggregates, Operators, Types, etc., to the PostgreSQL™ backend.
http://tada.github.io/pljava/
Other
238 stars 77 forks source link

wiki update: testcontainers magic #465

Open beargiles opened 9 months ago

beargiles commented 9 months ago

Someone asked how to use this extension with testcontainers a while back and I had a very long-winded answer (but with sample code!)... then realized I was an idiot and there's a much easier answer that can be added to the wiki.

Using a pre-configured Docker image

When I'm using my custom docker images I use

   PostgreSQLContainer getContainer() {
        DockerImageName imageName = DockerImageName.parse("beargiles/postgres-pljava:15.4").asCompatibleSubstituteFor("postgres");
        PostgreSQLContainer db = new PostgreSQLContainer(ImageName);
        // db.withInitScript("/path/to/initScript");
        db.withImagePullPolicy((s) -> false);
        return db;
    }

The key is the asCompatibleSubstituteFor() call.

The initialization script can load the jars you require, perform any required SQL calls, etc.

Note: I strongly recommend that any initialization scripts be limited to preparing the test data. Any custom extensions (UDF, UDT, etc.) should be set up using a SQL Deployment Descriptor bundled in your jar(s). This ensures the SQL extensions and your java implementation remain in sync.

You may not need the withImagePullPolicy() call - I might only need it since I'm developing the docker images locally.

Using an official PostgreSQL image

The short answer is that you can't. The initialization scripts can't install system packages.

The long answer is that you could - but it would be difficult. You would need to install pl/java using the Container#withCommand(String) command once the database container is running. That's a multistep process even if you're taking advantage of the existing Debian packages available on the official PostgreSQL image.

If you're feeling bold look at the first two targets in this Dockerfile.

jcflack commented 9 months ago

Hi,

Where were you thinking this would go in the wiki? A new "Using a pre-configured Docker image" page? Or were you thinking it would belong on some existing page?

jcflack commented 2 months ago

Or would you like to host these instructions somewhere on a site or blog you control, and have me add a link on the "Prebuilt packages" wiki page where your Docker images are linked already?