Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To integrate a Quarkus bean into a Flyway-based Java migration, follow these steps:

  1. Create a Quarkus application with the required dependencies, including Flyway and the database driver for the target database.
  2. Create a bean class that encapsulates the required functionality for the migration task. The bean class should be annotated with @ApplicationScoped to make it available for injection.
  3. Define a Flyway migration script in the appropriate directory for the database version. The script should use JDBC calls and invoke the methods of the injected bean instance to execute the migration task.
  4. Execute the Flyway migration command (using the command line or a tool like Maven or Gradle) to run the migration script and apply the changes to the target database.

Here's an example of a Quarkus bean that performs a migration task:

import javax.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class MyMigrationBean {

    public void migrate() {
        // perform migration task here
    }

}

To use this bean in a Flyway migration script, create a script file (e.g. V1_mymigration.sql) with the following contents:

-- Migration script using Quarkus bean
-- Invoke the migrate() method of MyMigrationBean

CREATE OR REPLACE FUNCTION migrate_my_data ()
RETURNS void AS $$
BEGIN
    perform migrate();
END;
$$ LANGUAGE plpgsql;

SELECT migrate_my_data();

This script creates a PL/pgSQL function that invokes the migrate() method of the Quarkus bean and executes it. The script can then be executed through Flyway to apply the migration changes to the target database:

mvn flyway:migrate

Or alternatively:

./gradlew flywayMigrate