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

Commit

Permalink
Merge pull request #3 from yonggzhou/add-usage-to-readme
Browse files Browse the repository at this point in the history
Add usage to readme
  • Loading branch information
steveorourke authored Nov 21, 2019
2 parents b5ddd6a + a60c0a7 commit 0877ede
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 4 deletions.
132 changes: 132 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,138 @@ database-evolution-test-utils is available on Maven Central.
</dependency>
```

### Example

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

#### Folder structure overview
```text
├── 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 <b>CustomerTable</b> with multiple columns.

```xml
<?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
<?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 <b>LiquibaseMigrationTestDefinition</b> for each Changeset.

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

```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 <b>LiquibaseMigrationScriptTestBase</b> class, and include all the
<b>LiquibaseMigrationTestDefinitions</b> in it.

```java
@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ protected void assertPostMigrationSchema(Database schema, Connection connection)
.hasTable("ExampleTable")
.enterNewTableAssertionMode()
.hasColumn("id")
.supportsIdType()
.isPrimaryKey()
.isNotNullable()
.isAutoIncrementing()
.isPrimaryKeyIdColumn()
.hasColumn("column1")
.supportsType(String.class)
.isNullable()
Expand Down

0 comments on commit 0877ede

Please sign in to comment.