Skip to content
This repository has been archived by the owner on Mar 19, 2024. It is now read-only.

Latest commit

 

History

History
171 lines (132 loc) · 5.74 KB

README.md

File metadata and controls

171 lines (132 loc) · 5.74 KB

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.