compile 'io.micronaut.configuration:micronaut-flyway'
Micronaut Flyway
Integration between Micronaut and Flyway
Version: 1.1.0
1 Introduction
To use the Micronaut’s integration with Flyway you must have the micronaut-flyway
dependency on your classpath:
<dependency>
<groupId>io.micronaut.configuration</groupId>
<artifactId>micronaut-flyway</artifactId>
</dependency>
2 What's New
This section will outline any breaking changes between major or milestone releases as well as detail any new features that have been added.
1.1.0
-
Added support for GORM.
-
Upgrade to Micronaut 1.1.3.
1.0.0
-
Underlying implementation has been changed to run the migrations as soon as possible. If you are not extending or overriding the API, there are no functional changes to note.
1.0.0.RC3
-
Upgrade to Micronaut 1.0.4.
-
Upgrade to Flyway 5.2.4.
1.0.0.RC2
-
Add configuration property to clean the schema before running the migrations.
-
Fire events after clean the schema and after run the migrations.
1.0.0.RC1
-
First public release.
3 Configuration
3.1 JPA/Hibernate
You can define Flyway configuration for each datasource. The following example demonstrates using it:
datasources:
default: (3)
url: 'jdbc:h2:mem:flywayDb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE'
username: 'sa'
password: ''
driverClassName: 'org.h2.Driver'
jpa:
default: (3)
packages-to-scan:
- 'example.micronaut'
properties:
hibernate:
hbm2ddl:
auto: none (1)
show_sql: true
flyway:
datasources: (2)
default: (3)
locations: classpath:databasemigrations (4)
1 | Disable schema DDL creation. |
2 | Define flyway configuration under flyway.datasources key. |
3 | Configure flyway configuration for default data source. |
4 | The migrations directory is src/main/resources/databsemigrations . |
In that directory you need to include all your migrations, for example:
create table books (
id int not null primary key,
name varchar(255) not null,
constraint id unique (id),
constraint name unique (name),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Starting with Micronaut 1.1.3 it is not necessary to define the jpa configuration if you only want to run the migrations but not actually use JPA.
|
3.2 GORM
There is also support for running Flyway migrations when using GORM.
1.- Add the dependency:
compile 'io.micronaut.configuration:micronaut-hibernate-gorm'
<dependency>
<groupId>io.micronaut.configuration</groupId>
<artifactId>micronaut-hibernate-gorm</artifactId>
</dependency>
2.- You also need at least one class annotated with @grails.gorm.annotation.Entity
to trigger the migrations.
import grails.gorm.annotation.Entity
@Entity
class Book {
String name
}
3.- Then it is necessary to configure GORM datasource:
dataSource: (1)
pooled: true
jmxExport: true
dbCreate: none (2)
url: 'jdbc:h2:mem:GORMDb'
driverClassName: org.h2.Driver
username: sa
password: ''
flyway:
datasources: (3)
default: (4)
locations: classpath:databasemigrations (5)
1 | Definition of the first data source in GORM. The name it’s default and it can’t be changed. |
2 | Disable schema DDL creation. |
3 | Define flyway configuration under flyway.datasources key. |
4 | Define flyway configuration for the default datasource. |
5 | The migrations directory is src/main/resources/databsemigrations . |
Multiple Data sources
It is also possible to configure Flyway migrations with multiple data sources when using GORM:
dataSource: (1)
pooled: true
jmxExport: true
dbCreate: none
url: 'jdbc:h2:mem:flywayGORMDb'
driverClassName: org.h2.Driver
username: sa
password: ''
dataSources:
books: (2)
pooled: true
jmxExport: true
dbCreate: none
url: 'jdbc:h2:mem:flywayBooksDb'
driverClassName: org.h2.Driver
username: sa
password: ''
flyway:
datasources:
default: (3)
locations: classpath:databasemigrations
books: (4)
locations: classpath:databasemigrations
1 | Definition of the first data source in GORM. The name it’s default and it can’t be changed. |
2 | Name of the additional data source. |
3 | Define flyway configuration for the default datasource. |
4 | Define flyway configuration for the books datasource. |
For more information about how to configure GORM, take a look at the documentation. |
3.3 Additional configuration
There are several options available for configuration:
Property | Type | Description |
---|---|---|
|
org.flywaydb.core.api.configuration.Configuration |
|
|
java.io.OutputStream |
|
|
java.lang.String[] |
|
|
boolean |
|
|
java.lang.String |
|
|
boolean |
|
|
boolean |
|
|
boolean |
|
|
boolean |
|
|
boolean |
|
|
boolean |
|
|
boolean |
|
|
boolean |
|
|
java.lang.String |
|
|
java.lang.String[] |
|
|
java.lang.String |
|
|
org.flywaydb.core.api.MigrationVersion |
|
|
boolean |
|
|
java.util.Map |
|
|
java.lang.String |
|
|
java.lang.String |
|
|
java.lang.String |
|
|
java.lang.String |
|
|
java.lang.String |
|
|
java.lang.String |
|
|
java.lang.String[] |
|
|
javax.sql.DataSource |
|
|
int |
|
|
java.lang.String |
|
|
org.flywaydb.core.api.MigrationVersion |
|
|
java.lang.String |
|
|
boolean |
|
|
boolean |
|
|
org.flywaydb.core.api.callback.Callback[] |
|
|
boolean |
|
|
org.flywaydb.core.api.resolver.MigrationResolver[] |
|
|
boolean |
|
|
boolean |
|
|
boolean |
|
|
boolean |
|
|
java.lang.String |
|
|
boolean |
Set whether this flyway configuration is enabled. Default value (true). |
|
boolean |
Whether flyway migrations should run asynchronously. |
|
boolean |
Set whether Flyway will clean the schema before running the migrations. Default value (false). |
|
java.lang.String |
The JDBC url of the database to migrate |
|
java.lang.String |
The user of the database to migrate |
|
java.lang.String |
The password of the database to migrate |
|
java.lang.String |
The username of the database to migrate |
|
java.lang.String[] |
The locations for the migrations |
By default Micronaut will configure Flyway to use the datasources defined under datasources configuration key. If
you want to use a different datasource you need to define the properties flyway.datasources.*.url , flyway.datasources.*.user
and flyway.datasources.*.password .
|
4 Events
Micronaut fires two different events when:
-
The schema has been cleaned: SchemaCleanedEvent.
-
The migrations have been executed: MigrationFinishedEvent.
5 Endpoint
This configuration also provides a built-in endpoint to expose all the applied migrations in /flyway
.
To enable the endpoint add the following to the configuration:
endpoints:
flyway:
enabled: true (1)
sensitive: false (2)
1 | /flyway endpoint is enabled (this is the default). |
2 | /flyway endpoint is open for unauthenticated access. |
$ curl http://localhost:8080/flyway
[{
"name": "default",
"migrations": [{
"checksum": 1952043475,
"installedOn": "2018-11-12T11:32:52.000+0000",
"executionTime": 96,
"installedRank": 1,
"version": {
"version": "1"
},
"description": "init",
"installedBy": "root",
"state": "SUCCESS",
"type": "SQL",
"script": "V1__init.sql"
}, {
"checksum": -1926058189,
"installedOn": "2018-11-12T11:32:52.000+0000",
"executionTime": 10,
"installedRank": 2,
"version": {
"version": "2"
},
"description": "testdata",
"installedBy": "root",
"state": "SUCCESS",
"type": "SQL",
"script": "V2__testdata.sql"
}]
}]
See the section on Built-in endpoints in the user guide for more information. |