Skip to content

Instantly share code, notes, and snippets.

@ealebed
Created February 5, 2021 13:27
Show Gist options
  • Save ealebed/494ad37566c0db949da9370939bd6282 to your computer and use it in GitHub Desktop.
Save ealebed/494ad37566c0db949da9370939bd6282 to your computer and use it in GitHub Desktop.
For flyway-validation-example
plugins {
java
}
repositories {
jcenter()
}
dependencies {
val testcontainersVersion = "1.15.1"
testImplementation("org.junit.jupiter:junit-jupiter:5.7.1")
testImplementation("org.flywaydb:flyway-core:7.5.2")
testImplementation("org.postgresql:postgresql:42.2.18")
testImplementation("org.slf4j:slf4j-simple:1.7.30")
testImplementation("org.testcontainers:junit-jupiter:${testcontainersVersion}")
testImplementation("org.testcontainers:postgresql:${testcontainersVersion}")
testImplementation("org.testcontainers:testcontainers:${testcontainersVersion}")
}
tasks.test {
useJUnitPlatform()
}
package com.loopme.db;
import org.flywaydb.core.Flyway;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
@Testcontainers
public class FlywayMigrationTest {
private static final String DOCKER_IMAGE = "postgres:13";
private static final String DB_NAME = "postgres";
private static final String USERNAME = "postgres";
private static final String PASSWORD = "postgres";
private static final String INIT_SCRIPT_PATH = "initialize/initdb.sql";
@Container
private static PostgreSQLContainer container = new PostgreSQLContainer<>(DOCKER_IMAGE)
.withDatabaseName(DB_NAME)
.withUsername(USERNAME)
.withPassword(PASSWORD)
.withInitScript(INIT_SCRIPT_PATH);
@Test
public void runDB1Migrations() {
container.withDatabaseName("database_01");
var flyway = Flyway.configure()
.locations("migrations/database_01/")
.schemas("public")
.dataSource(container.getJdbcUrl(), container.getUsername(), container.getPassword())
.load();
flyway.info();
flyway.migrate();
}
@Test
public void runDB2Migrations() {
container.withDatabaseName("database_02");
var flyway = Flyway.configure()
.locations("migrations/database_02/")
.schemas("public")
.dataSource(container.getJdbcUrl(), container.getUsername(), container.getPassword())
.load();
flyway.info();
flyway.migrate();
}
}
CREATE USER ro_user;
CREATE USER test;
CREATE DATABASE database_01;
CREATE DATABASE database_02;
FlywayMigrationTest STANDARD_ERROR
[Test worker] INFO org.testcontainers.dockerclient.DockerClientProviderStrategy - Loaded org.testcontainers.dockerclient.UnixSocketClientProviderStrategy from ~/.testcontainers.properties, will try it first
[Test worker] INFO org.testcontainers.dockerclient.DockerClientProviderStrategy - Found Docker environment with local Unix socket (unix:///var/run/docker.sock)
[Test worker] INFO org.testcontainers.DockerClientFactory - Docker host IP address is localhost
[Test worker] INFO org.testcontainers.DockerClientFactory - Connected to docker:
Server Version: 20.10.2
API Version: 1.41
Operating System: Docker Desktop
Total Memory: 16016 MB
[Test worker] INFO org.testcontainers.utility.ImageNameSubstitutor - Image name substitution will be performed by: DefaultImageNameSubstitutor (composite of 'ConfigurationFileImageNameSubstitutor' and 'PrefixingImageNameSubstitutor')
[Test worker] INFO org.testcontainers.DockerClientFactory - Ryuk started - will monitor and terminate Testcontainers containers on JVM exit
[Test worker] INFO org.testcontainers.DockerClientFactory - Checking the system...
[Test worker] INFO org.testcontainers.DockerClientFactory - ✔︎ Docker server version should be at least 1.6.0
[Test worker] INFO org.testcontainers.DockerClientFactory - ✔︎ Docker environment should have more than 2GB free disk space
[Test worker] INFO 🐳 [postgres:13] - Creating container for image: postgres:13
[Test worker] INFO 🐳 [postgres:13] - Starting container with ID: e8943be2813c2177546a7b2ba1ab3b7942017645ebdcfdee1501fa555a7ac4cf
[Test worker] INFO 🐳 [postgres:13] - Container postgres:13 is starting: e8943be2813c2177546a7b2ba1ab3b7942017645ebdcfdee1501fa555a7ac4cf
[Test worker] INFO 🐳 [postgres:13] - Container postgres:13 started in PT1.609842S
[Test worker] INFO org.testcontainers.ext.ScriptUtils - Executing database script from initialize/initdb.sql
[Test worker] INFO org.testcontainers.ext.ScriptUtils - Executed database script from initialize/initdb.sql in 103 ms.
FlywayMigrationTest > runDB1Migrations() STANDARD_ERROR
[Test worker] INFO org.flywaydb.core.internal.license.VersionPrinter - Flyway Community Edition 7.5.0 by Redgate
[Test worker] INFO org.flywaydb.core.internal.database.base.DatabaseType - Database: jdbc:postgresql://localhost:55046/database_01 (PostgreSQL 13.1)
[Test worker] INFO org.flywaydb.core.internal.license.VersionPrinter - Flyway Community Edition 7.5.0 by Redgate
[Test worker] INFO org.flywaydb.core.internal.command.DbValidate - Successfully validated 5 migrations (execution time 00:00.006s)
[Test worker] INFO org.flywaydb.core.internal.schemahistory.JdbcTableSchemaHistory - Creating Schema History table "public"."flyway_schema_history" ...
[Test worker] INFO org.flywaydb.core.internal.command.DbMigrate - Current version of schema "public": << Empty Schema >>
[Test worker] INFO org.flywaydb.core.internal.command.DbMigrate - Migrating schema "public" to version "20210203000000 - Base version"
[Test worker] INFO org.flywaydb.core.internal.command.DbMigrate - Migrating schema "public" to version "20210203150726 - create traces table"
[Test worker] INFO org.flywaydb.core.internal.command.DbMigrate - Migrating schema "public" to version "20210205103852 - grants for ro user"
[Test worker] INFO org.flywaydb.core.internal.command.DbMigrate - Migrating schema "public" to version "20210205110053 - create invoices table"
[Test worker] INFO org.flywaydb.core.internal.command.DbMigrate - Migrating schema "public" to version "20210205142305 - add key to bundles"
[Test worker] INFO org.flywaydb.core.internal.command.DbMigrate - Successfully applied 5 migrations to schema "public" (execution time 00:00.175s)
FlywayMigrationTest > runDB2Migrations() STANDARD_ERROR
[Test worker] INFO org.flywaydb.core.internal.license.VersionPrinter - Flyway Community Edition 7.5.0 by Redgate
[Test worker] INFO org.flywaydb.core.internal.database.base.DatabaseType - Database: jdbc:postgresql://localhost:55046/database_02 (PostgreSQL 13.1)
[Test worker] INFO org.flywaydb.core.internal.license.VersionPrinter - Flyway Community Edition 7.5.0 by Redgate
[Test worker] INFO org.flywaydb.core.internal.command.DbValidate - Successfully validated 2 migrations (execution time 00:00.006s)
[Test worker] INFO org.flywaydb.core.internal.schemahistory.JdbcTableSchemaHistory - Creating Schema History table "public"."flyway_schema_history" ...
[Test worker] INFO org.flywaydb.core.internal.command.DbMigrate - Current version of schema "public": << Empty Schema >>
[Test worker] INFO org.flywaydb.core.internal.command.DbMigrate - Migrating schema "public" to version "20210201000000 - Base version"
[Test worker] INFO org.flywaydb.core.internal.command.DbMigrate - Migrating schema "public" to version "20210201102112 - added filters column to scheduled reports table"
[Test worker] INFO org.flywaydb.core.internal.command.DbMigrate - Successfully applied 2 migrations to schema "public" (execution time 00:00.065s)
Gradle Test Executor 5 finished executing tests.
> Task :test
BUILD SUCCESSFUL in 4s
3 actionable tasks: 2 executed, 1 up-to-date
CREATE TABLE public.bundles (
id integer NOT NULL,
created_at timestamp without time zone DEFAULT now(),
updated_at timestamp without time zone DEFAULT now(),
json_data jsonb DEFAULT '{}'::jsonb,
modified_by text DEFAULT ''::text
);
ALTER TABLE public.bundles OWNER TO test;
grant select on bundles to ro_user;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment