Running PostgreSQL database tests on DEV@CloudBees

In our project, we drop and recreate the database for doing our tests. This is controlled by our maven build and can happen for any module in the build. This is done using the following maven configuration:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>sql-maven-plugin</artifactId>
    <version>1.5</version>

    <dependencies>
        <!-- specify the dependent jdbc driver here -->
        <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>${postgresql-version}</version>
        </dependency>
    </dependencies>

    <!-- common configuration shared by all executions -->
    <configuration>
        <driver>org.postgresql.Driver</driver>
        <url>jdbc:postgresql://${dbHostPort}/postgres</url>
        <username>${dbAdminUser}</username>
        <password>${dbAdminPassword}</password>
    </configuration>

    <executions>
        <execution>
            <id>drop and create</id>
            <goals>
                <goal>execute</goal>
            </goals>
            <phase>generate-test-resources</phase>
            <configuration>
                <!-- need another database to drop/create the targeted one -->
                <url>jdbc:postgresql://${dbHostPort}/postgres</url>
                <autocommit>true</autocommit>
                <sqlCommand>
                    drop database myproject_test;
                    create database myproject_test encoding 'UTF8';
                    CREATE ROLE myproject LOGIN ENCRYPTED PASSWORD 'myproject'
                        SUPERUSER INHERIT CREATEDB NOCREATEROLE;
                    alter database myproject_test OWNER TO myproject;
                    grant all on database myproject_test to myproject;
                </sqlCommand>
                <onError>continue</onError>
            </configuration>
        </execution>
    </executions>
</plugin>

When trying to run this build on CloudBees, there were some problems. Our project uses PostgreSQL. This may not be immediately clear, but it is supported as documented on their site.

You basically include a pre-build step to assure that PostgreSQL is installed and (according to their page) that your database is up. However, my build script created the database itself and need the postgres superuser for access.

So I adapted the script to assure that the postgres users can be used from the maven db plugin:

######### POSTGRES INSTAL(first time) AND LAUNCH #########
curl -s -o use-postgresql https://repository-cloudbees.forge.cloudbees.com/distributions/ci-addons/use-postgresql
POSTGRESQL_VERSION=9.0.4 \
source ./use-postgresql

### create "postgres" user
psql -d postgres -c "DROP USER IF EXISTS postgres;"
psql -d postgres -c "CREATE ROLE postgres LOGIN ENCRYPTED PASSWORD 'postgres' SUPERUSER INHERIT CREATEDB;"

######### THIS CODE NOW EXITS. #########
## If you have other 'M2 PreBuild' steps following this 
## in your configuration, they will now execute
##
## Eventually, control is given to the main build and
## the PostgreSQL server will still be running, ready
## to use!

One Comment

  1. Andreas says:

    Hi!

    I am currently trying to setup my postgreSQL database for unit tests using maven. However, I don’t know how and where I can create the schema. Can maven create this schema for me? Or do i have to create the schema before executing the unit tests? And how do I create the tables and insert the test data, that I am going to use during the tests.

    I would be very grateful if I get some hints.

    Best regards

    Andreas

Leave a Reply

Your email address will not be published. Required fields are marked *

question razz sad evil exclaim smile redface biggrin surprised eek confused cool lol mad twisted rolleyes wink idea arrow neutral cry mrgreen

*