Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flyway multitenant extension #43892

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

rmanibus
Copy link
Contributor

@rmanibus rmanibus commented Oct 16, 2024

Quarkus Extension: Flyway Support for Hibernate Multitenancy

Overview

This pull request introduces a new Quarkus extension, quarkus-flyway-multitenant, which enables Flyway integration with Hibernate in multitenant schema-based mode.

Currently, Hibernate handles schema-based multitenancy effectively, but there is no built-in way to manage schema creation or apply migrations lazily for specific tenants. This extension bridges that gap, allowing users to manage migrations in multitenancy scenarios with greater flexibility.

Key Features

Flyway Schema Migration per Tenant:

The extension provides the @FlywayPersistenceUnit annotation, allowing developers to inject Flyway objects for individual tenants or persistence units.

@Inject
@FlywayPersistenceUnit(tenantId = "tenant1")
Flyway flywayForTenant1;

or

@Inject
@FlywayPersistenceUnit()
FlywayContainer flywayContainer;

public void migrateTenant2() {
  flywayContainer.getFlyway("tenant2").migrate();
}

Automatic Migration at Startup:

Support for specifying tenants to be migrated at application startup through the FlywayTenantSupport interface when the quarkus.flyway.multitenant.migrate-at-start property is enabled.

@FlywayPersistenceUnit()
public class TenantToInitialize implements FlywayTenantSupport {
    @Override
    public List<String> getTenantsToInitialize() {
        return List.of("tenant1", "tenant2");
    }
}

On-demand Migration:

Support for lazy migration when a tenant is resolved using a custom FlywayTenantResolverDecorator that runs migrations automatically for the resolved tenant.

This is not included in this PR since the user might want to customize the decorator logic, add caching, but an example implementation is provided in the doc. I am planning to provide a customizable implementation in a subsequent PR.

How to Use

Add the quarkus-flyway-multitenant extension to your project:

<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-flyway-multitenant</artifactId>
</dependency>

Use the @FlywayPersistenceUnit annotation to inject Flyway instances for specific tenants or persistence units.

To automatically run migrations on startup for a set of tenants, implement the FlywayTenantSupport interface.

For automatic tenant-specific migrations during tenant resolution, decorate your TenantResolver.

related to #37080

@rmanibus
Copy link
Contributor Author

cc @geoand

@quarkus-bot

This comment has been minimized.

Copy link

github-actions bot commented Oct 16, 2024

🎊 PR Preview b46c8ef has been successfully built and deployed to https://quarkus-pr-main-43892-preview.surge.sh/version/main/guides/

  • Images of blog posts older than 3 months are not available.
  • Newsletters older than 3 months are not available.

@quarkus-bot

This comment has been minimized.

@geoand
Copy link
Contributor

geoand commented Oct 16, 2024

Nice!

I'll have a close look in the following days, but I see that CI is not happy.
I see this in the logs:

Run ./update-extension-dependencies.sh and add the modified pom.xml files to your commit.

@quarkus-bot quarkus-bot bot added the area/devtools Issues/PR related to maven, gradle, platform and cli tooling/plugins label Oct 16, 2024
@rmanibus rmanibus force-pushed the flyway-multitenant branch 2 times, most recently from 4604dea to 8758112 Compare October 16, 2024 14:06
@quarkus-bot

This comment has been minimized.

@quarkus-bot

This comment has been minimized.

@quarkus-bot

This comment has been minimized.

@quarkus-bot
Copy link

quarkus-bot bot commented Oct 16, 2024

Status for workflow Quarkus Documentation CI

This is the status report for running Quarkus Documentation CI on commit 5c8b85a.

✅ The latest workflow run for the pull request has completed successfully.

It should be safe to merge provided you have a look at the other checks in the summary.

Warning

There are other workflow runs running, you probably need to wait for their status before merging.


import java.util.List;

public interface FlywayTenantSupport {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure about the naming of this one

@quarkus-bot
Copy link

quarkus-bot bot commented Oct 16, 2024

Status for workflow Quarkus CI

This is the status report for running Quarkus CI on commit 5c8b85a.

✅ The latest workflow run for the pull request has completed successfully.

It should be safe to merge provided you have a look at the other checks in the summary.

You can consult the Develocity build scans.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/core area/dependencies Pull requests that update a dependency file area/devtools Issues/PR related to maven, gradle, platform and cli tooling/plugins area/documentation area/flyway
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants