Skip to content

Commit

Permalink
Liquibase with zero config + flyway classpath:/db/migration (#25)
Browse files Browse the repository at this point in the history
* Liquibase with zero config + flyway classpath:/db/migration

* Versions align
  • Loading branch information
romchellis committed Jul 28, 2023
1 parent 7ee5199 commit 49f404f
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 44 deletions.
42 changes: 28 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ by using [Testcontainers](https://www.testcontainers.org/) and applying database

## Summary

- Plugin migration and code generation might be skipped using `skip` property
- Plugin migration and code generation might be skipped using `skip` property
- If you need to reuse existing database connection - take a look at [Jooq section](#Jooq)

## Database Configuration

To configure a target database, you need to specify at least database `type` property.

#### Properties
Expand Down Expand Up @@ -51,7 +52,7 @@ Currently, the plugin supports all properties existing in Flyway
You can find them by original link
https://flywaydb.org/documentation/configuration/parameters/
<b>Now [config files parameter](https://flywaydb.org/documentation/configuration/parameters/configFiles) is not
implemented yet</b>
implemented yet</b>, but you can use config file at default location ${baseDir}/flyway.conf

#### `flyway` block configuration

Expand Down Expand Up @@ -87,25 +88,35 @@ link https://docs.liquibase.com/tools-integrations/maven/using-liquibase-and-mav

Now supports only the most useful properties

| Property | Required | type |
|--------------------------------|----------|--------|
| changeLogPath | yes | String |
| changeLogDirectory | | String |
| parameters | | Map |
| defaultSchemaName | | String |
| liquibaseSchemaName | | String |
| databaseChangeLogTableName | | String |
| databaseChangeLogLockTableName | | String |
| Property | type | default |
|--------------------------------|--------|------------------------------------------------------------------------------------------------------------------------------|
| changeLogPath | String | if changeLogDirectory is provided - db.changelog-root.xml, otherwise - src/main/resources/db/changelog/db.changelog-root.xml |
| changeLogDirectory | String | projectBaseDir |
| parameters | Map | |
| defaultSchemaName | String | |
| liquibaseSchemaName | String | |
| databaseChangeLogTableName | String | |
| databaseChangeLogLockTableName | String | |

Reference to Liquibase properties - https://docs.liquibase.com/concepts/connections/creating-config-properties.html

#### `liquibase` block configuration

- Zero configuration with defaults

```xml

<liquibase/>
```

- Adding properties

```xml

<liquibase>
<changeLogPath>db.changelog-root.xml</changeLogPath>
<changeLogDirectory>src/main/resources/db/changelog</changeLogPath>
<changeLogPath>db.changelog-root.yml</changeLogPath>
<changeLogDirectory>src/main/resources/db/postgres/changelog</changeLogPath>
<defaultSchemaName>custom</defaultSchemaName>
</liquibase>
```

Expand Down Expand Up @@ -151,8 +162,11 @@ existing database will be used, no container won't be spin up
## Examples

### Complete example

Example with `PostgreSQL` and minimal configuration with `Flyway` and `JOOQ`

```xml

<plugin>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-jooq-codegen-maven-plugin</artifactId>
Expand Down Expand Up @@ -204,7 +218,7 @@ Example with `PostgreSQL` and minimal configuration with `Flyway` and `JOOQ`
[MariaDB + Flyway](examples/mariadb-flyway-example )
[MySQL + Flyway](examples/mysql-flyway-example )
[Postgres + Flyway](examples/postgres-flyway-example )
[Postgres + Liquibase](examples/postgres-liquibase-example )
[Postgres + Liquibase](examples/postgres-liquibase-example )

### Try with example application

Expand Down
2 changes: 1 addition & 1 deletion examples/mariadb-flyway-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<groupId>org.testcontainers.jooqtc</groupId>
<artifactId>mariadb-flyway-example</artifactId>
<version>1.0-SNAPSHOT</version>
<version>0.0.3-SNAPSHOT</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
Expand Down
2 changes: 1 addition & 1 deletion examples/mysql-flyway-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<groupId>org.testcontainers.jooqtc</groupId>
<artifactId>mysql-flyway-example</artifactId>
<version>1.0-SNAPSHOT</version>
<version>0.0.3-SNAPSHOT</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
Expand Down
2 changes: 1 addition & 1 deletion examples/postgres-flyway-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<groupId>org.testcontainers.jooqtc</groupId>
<artifactId>postgres-flyway-example</artifactId>
<version>1.0-SNAPSHOT</version>
<version>0.0.3-SNAPSHOT</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
Expand Down
2 changes: 1 addition & 1 deletion examples/postgres-liquibase-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<groupId>org.testcontainers.jooqtc</groupId>
<artifactId>postgres-liquibase-example</artifactId>
<version>1.0-SNAPSHOT</version>
<version>0.0.3-SNAPSHOT</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
Expand Down
3 changes: 0 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,6 @@
<dependenciesToScan>
<dependency>org.junit.vintage:junit-vintage-engine</dependency>
</dependenciesToScan>
<!-- <includes>-->
<!-- <include>**/JUnit3Runner.java</include>-->
<!-- </includes>-->
</configuration>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ public void run(RunnerProperties runnerProperties) {
}

private void addDefaults(RunnerProperties runnerProperties) {
putIfAbsent(
ConfigUtils.LOCATIONS,
"%s%s/src/main/resources/db/migration"
.formatted(
Location.FILESYSTEM_PREFIX,
runnerProperties.mavenProject().getBasedir().getAbsolutePath()));
var defaultResourceLocation = "%s%s/src/main/resources/db/migration"
.formatted(
Location.FILESYSTEM_PREFIX,
runnerProperties.mavenProject().getBasedir().getAbsolutePath());
var defaultClasspathLocation = "%s%s".formatted("classpath:", "/db/migration");
putIfAbsent(ConfigUtils.LOCATIONS, "%s,%s".formatted(defaultResourceLocation, defaultClasspathLocation));
}

private String addFlywayPrefix(String key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@
public class LiquibaseRunner implements MigrationRunner {

/**
* Required property <br>
* Default: {@link #changeLogDirectory}/{@link #changeLogPath db.changelog-root.xml}<br/>
* or src/main/resources/{@link #changeLogPath db.changelog-root.xml} <br/>
* depending on {@link #changeLogDirectory} presence <br/>
* Example: src/main/resources/db/changelog/db.changelog-root.xml
*/
@Parameter(name = "liquibase.changeLogPath", required = true)
@Parameter(name = "liquibase.changeLogPath")
private String changeLogPath;

/**
* Optional <br>
* Default: project.basedir
*/
@Parameter(name = "liquibase.changeLogDirectory")
Expand All @@ -56,15 +57,16 @@ public class LiquibaseRunner implements MigrationRunner {
private String databaseChangeLogLockTableName;

@Override
public void run(RunnerProperties runnerProperties) throws MojoExecutionException {
public void run(RunnerProperties runnerProps) throws MojoExecutionException {
setDefaultLocations(runnerProps);
try {
Driver driver = runnerProperties.getDriverInstance();
Driver driver = runnerProps.getDriverInstance();
Properties properties = new Properties();
properties.put("user", runnerProperties.getUsername());
properties.put("password", runnerProperties.getPassword());
Connection c = driver.connect(runnerProperties.getUrl(), properties);
properties.put("user", runnerProps.getUsername());
properties.put("password", runnerProps.getPassword());
Connection c = driver.connect(runnerProps.getUrl(), properties);
Database database = createDatabase(c);
ResourceAccessor accessor = getResourceAccessor(runnerProperties);
ResourceAccessor accessor = getResourceAccessor(runnerProps);
Liquibase liquibase = new Liquibase(changeLogPath, accessor, database);
setParameters(liquibase);
liquibase.update();
Expand All @@ -73,6 +75,21 @@ public void run(RunnerProperties runnerProperties) throws MojoExecutionException
}
}

private void setDefaultLocations(RunnerProperties runnerProps) {
var defaultDir = runnerProps.mavenProject().getBasedir();
if (changeLogPath == null) {
changeLogPath = changeLogDirectory == null
? "src/main/resources/db/changelog/db.changelog-root.xml"
: "db.changelog-root.xml";
}
if (changeLogDirectory == null) {
changeLogDirectory = defaultDir.getAbsolutePath();
} else {
changeLogDirectory =
defaultDir.toPath().resolve(changeLogDirectory).toFile().getAbsolutePath();
}
}

private Database createDatabase(Connection c) throws DatabaseException {
Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(c));
if (defaultSchemaName != null) {
Expand Down
9 changes: 1 addition & 8 deletions src/test/resources/pom/postgres-liquibase/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,7 @@
<password>test</password>
<databaseName>test</databaseName>
</database>
<liquibase>
<changeLogDirectory>
${project.basedir}/src/main/resources/db/changelog/postgres
</changeLogDirectory>
<changeLogPath>
db.changelog-root.xml
</changeLogPath>
</liquibase>
<liquibase/>
<jooq>
<generator>
<database>
Expand Down

0 comments on commit 49f404f

Please sign in to comment.