com.tyro.oss:database-evolution-test-utils

A simple library for testing liquibase database migrations.

License

License

Categories

Categories

Data
GroupId

GroupId

com.tyro.oss
ArtifactId

ArtifactId

database-evolution-test-utils
Last Version

Last Version

2.0
Release Date

Release Date

Type

Type

jar
Description

Description

com.tyro.oss:database-evolution-test-utils
A simple library for testing liquibase database migrations.
Project URL

Project URL

https://github.com/tyro/database-evolution-test-utils
Source Code Management

Source Code Management

https://github.com/tyro/database-evolution-test-utils

Download database-evolution-test-utils

How to add to project

<!-- https://jarcasting.com/artifacts/com.tyro.oss/database-evolution-test-utils/ -->
<dependency>
    <groupId>com.tyro.oss</groupId>
    <artifactId>database-evolution-test-utils</artifactId>
    <version>2.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.tyro.oss/database-evolution-test-utils/
implementation 'com.tyro.oss:database-evolution-test-utils:2.0'
// https://jarcasting.com/artifacts/com.tyro.oss/database-evolution-test-utils/
implementation ("com.tyro.oss:database-evolution-test-utils:2.0")
'com.tyro.oss:database-evolution-test-utils:jar:2.0'
<dependency org="com.tyro.oss" name="database-evolution-test-utils" rev="2.0">
  <artifact name="database-evolution-test-utils" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.tyro.oss', module='database-evolution-test-utils', version='2.0')
)
libraryDependencies += "com.tyro.oss" % "database-evolution-test-utils" % "2.0"
[com.tyro.oss/database-evolution-test-utils "2.0"]

Dependencies

compile (10)

Group / Artifact Type Version
org.liquibase : liquibase-core jar 3.6.3
org.apache.ddlutils : ddlutils jar 1.0
org.apache.commons : commons-lang3 jar 3.9
commons-io : commons-io jar 2.6
commons-logging : commons-logging jar 1.1.1
org.springframework : spring-tx Optional jar
org.springframework : spring-test Optional jar
org.mybatis : mybatis jar 3.5.2
org.junit.jupiter : junit-jupiter-api jar 5.5.2
org.junit.jupiter : junit-jupiter-engine jar 5.5.2

runtime (2)

Group / Artifact Type Version
mysql : mysql-connector-java Optional jar 8.0.15
xerces : xercesImpl jar 2.12.0

test (4)

Group / Artifact Type Version
junit : junit jar 4.12
org.testcontainers : junit-jupiter jar 1.12.2
org.testcontainers : testcontainers jar 1.12.1
org.testcontainers : mysql jar 1.12.1

Project Modules

There are no modules declared in this project.

database-evolution-test-utils

Download Build Status License

A simple library for testing liquibase database migrations.

Getting Started

database-evolution-test-utils is available on Maven Central.

<dependency>
    <groupId>com.tyro.oss</groupId>
    <artifactId>database-evolution-test-utils</artifactId>
    <version>2.0</version>
    <scope>test</scope>
</dependency>

Example

This example shows how we can use database-evolution-test-utils to test liquibase scripts for database migrations.

Folder structure overview

├── src
│   ├── main
│   │   ├── java
│   │   └── resources
│   │       ├── application.properties
│   │       ├── migration-scripts.xml
│   │       └── dbevolution
│   │           └── CreateCustomerTable.xml
│   └── test
│       ├── java
│       │   └── com
│       │       └── tyro
│       │           └── oss
│       │               └── dbevolution
│       │                   └── CreateCustomerTable.java
│       │                   ├── LiquibaseScriptsTest.java
│       └── resources
│           └── schema.sql

The following in an example Changeset to create a table called CustomerTable with multiple columns.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
   http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.6.xsd">

    <changeSet id='20191114' author='author'>
        <preConditions>
            <not>
                <tableExists tableName="CustomerTable"/>
            </not>
        </preConditions>

        <createTable tableName="CustomerTable">
            <column name="id" type="bigint" autoIncrement="true">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="name" type="varchar(255)">
                <constraints nullable="false"/>
            </column>
        </createTable>


    </changeSet>
</databaseChangeLog>

Here's the associate liquibase Changelog file with the create table Changeset:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
   http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.6.xsd">

    <include file="dbevolution/CreateCustomerTable.xml" />

</databaseChangeLog>

Testing

Here's how we test whether the Changesets are created correctly.

Define LiquibaseMigrationTestDefinition for each Changeset.

NOTE: It needs to be the same name as the one defined in migration-scripts.xml, and also should be under the same directory, i.e. dbevolution/CreateCustomerTable.java

public class CreateCustomerTable extends LiquibaseMigrationTestDefinition {

    @Override
    protected void assertPreMigrationSchema(Database schema, Connection connection) {
        assertThatSchema(schema, connection)
                .doesNotHaveTable("CustomerTable");
    }

    @Override
    protected void assertPostMigrationSchema(Database schema, Connection connection) {
        assertThatSchema(schema, connection)
                .hasTable("CustomerTable")
                .enterNewTableAssertionMode()
                    .hasColumn("id")
                        .isPrimaryKeyIdColumn()
                    .hasColumn("name")
                        .supportsType(String.class)
                        .isNotNullable();
    }
}

Creates test class that extends the LiquibaseMigrationScriptTestBase class, and include all the LiquibaseMigrationTestDefinitions in it.

@Testcontainers
@SchemaDetails(
        migrationUser = "username",
        migrationPassword = "password",
        url = "jdbc:tc:mysql://localhost/test?serverTimezone=UTC",
        snapshotScript = "schema.sql")
@MigrationScript(filename = "migration-scripts.xml")
public class LiquibaseScriptsTest extends LiquibaseMigrationScriptTestBase {

    @Container
    private final MySQLContainer mysql = new MySQLContainer();

    @Override
    protected List<LiquibaseMigrationTestDefinition> testDefinitions() {
        return asList(new CreateCustomerTable());
    }
}

That's it!!! Happy migrating!

Copyright and Licensing

Copyright (C) 2019 Tyro Payments Pty Ltd

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Contributing

See CONTRIBUTING for details.

com.tyro.oss

Tyro. Better business banking.

Versions

Version
2.0
1.0