Skip to content

Commit

Permalink
reusable quarkus-flyway logic
Browse files Browse the repository at this point in the history
  • Loading branch information
rmanibus committed Oct 16, 2024
1 parent 49af62c commit 5c8b85a
Show file tree
Hide file tree
Showing 120 changed files with 976 additions and 4,733 deletions.
13 changes: 13 additions & 0 deletions devtools/bom-descriptor-json/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,19 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-flyway-multitenant</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-flyway-mysql</artifactId>
Expand Down
13 changes: 13 additions & 0 deletions docs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,19 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-flyway-multitenant-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-flyway-mysql-deployment</artifactId>
Expand Down
101 changes: 100 additions & 1 deletion docs/src/main/asciidoc/flyway.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ NOTE: Without configuration, Flyway is set up for every datasource using the def

== Customizing Flyway

In cases where Flyway needs to be configured in addition to the configuration options that Quarkus provides, the `io.quarkus.flyway.multitenant.FlywayConfigurationCustomizer` class comes in handy.
In cases where Flyway needs to be configured in addition to the configuration options that Quarkus provides, the `io.quarkus.flyway.FlywayConfigurationCustomizer` class comes in handy.

To customize Flyway for the default datasource, simply add a bean like so:

Expand Down Expand Up @@ -390,6 +390,105 @@ When using Flyway together with Hibernate ORM, you can use the Dev UI to generat
You can find more information about this feature in the xref:hibernate-orm.adoc#flyway[Hibernate ORM guide].

[[reactive-datasources]]

== Flyway and Hibernate Multitenancy

To enable the flyway multitenancy support, you need to add the `quarkus-flyway-multitenant` extension to your project.

[source,xml,role="primary asciidoc-tabs-target-sync-cli asciidoc-tabs-target-sync-maven"]
.pom.xml
----
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-flyway-multitenant</artifactId>
</dependency>
----

you can then use the `@FlywayPersistenceUnit` annotation to inject the Flyway object for a specific tenant.

[source,java]
----
@ApplicationScoped
public class MigrationService {
@Inject
@FlywayPersistenceUnit(tenantId = "tenant1")
Flyway flywayForTenant1; <1>
@Inject
@FlywayPersistenceUnit(value="inventory", tenantId = "tenant1")
Flyway flywayForTenant1OnInventory; <2>
@Inject
@FlywayPersistenceUnit(value="inventory")
FlywayContainer flywayContainer; <3>
public void migrateTenant2() {
flywayContainer.getFlyway("tenant2").migrate();
}
}
----
<1> Inject the Flyway object for a specific tenant
<2> Inject Flyway for named persistence unit using the Quarkus `FlywayPersistenceUnit` qualifier
<3> Inject Flyway container for named persistence unit

Configuration for this extension is similar to the default Flyway extension, with the quarkus.flyway.multitenant prefix.

[source,java]
----
# Flyway config properties
quarkus.flyway.multitenant.migrate-at-start=true
quarkus.flyway.multitenant.named.migrate-at-start=true
----

It is also possible to specify the list of tenants to be migrated at startup when the `quarkus.flyway.multitenant.migrate-at-start` property is enabled by implementing the FlywayTenantSupport interface.

[source,java]
----
// scope is added automatically
@FlywayPersistenceUnit(value="inventory")
public static class TenantToInitialize implements FlywayTenantSupport {
@Override
public List<String> getTenantsToInitialize() {
return List.of("tenant1", "tenant2");
}
}
----

It is also possible to use a decorator to automatically run the Flyway migration when the tenant is resolved.

[source,java]
----
@Decorator
@Priority(10)
public class FlywayTenantResolverDecorator implements TenantResolver {
@Inject
@Delegate
@Any
TenantResolver tenantResolver;
@Inject
@FlywayPersistenceUnit()
FlywayContainer flywayContainer;
@Override
public String getDefaultTenantId() {
return tenantResolver.getDefaultTenantId();
}
@Override
public String resolveTenantId() {
String tenantId = tenantResolver.resolveTenantId();
Flyway flyway = flywayContainer.getFlyway(tenantId);
flyway.migrate();
return tenantId;
}
}
----

== Flyway and Reactive datasources

Flyway internally relies on a JDBC datasource,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
* Supplier that can be used to only run build steps
* if the Flyway extension is enabled.
*/
public class FlywayEnabled implements BooleanSupplier {
public class FlywayMultiTenantEnabled implements BooleanSupplier {

private final FlywayMultiTenantBuildTimeConfig config;

FlywayEnabled(FlywayMultiTenantBuildTimeConfig config) {
FlywayMultiTenantEnabled(FlywayMultiTenantBuildTimeConfig config) {
this.config = config;
}

Expand Down
Loading

0 comments on commit 5c8b85a

Please sign in to comment.