Search in sources :

Example 1 with V11__MigrateUsersToDB

use of com.helger.phoss.smp.backend.sql.migration.V11__MigrateUsersToDB in project phoss-smp by phax.

the class FlywayMigrator method runFlyway.

void runFlyway(@Nonnull final EDatabaseType eDBType) {
    ValueEnforcer.notNull(eDBType, "DBType");
    LOGGER.info("Starting to run Flyway for DB type " + eDBType);
    final ConfigFile aCF = SMPServerConfiguration.getConfigFile();
    final Callback aCallbackLogging = new BaseCallback() {

        public void handle(@Nonnull final Event aEvent, @Nonnull final Context aContext) {
            if (LOGGER.isInfoEnabled())
                LOGGER.info("Flyway: Event " + aEvent.getId());
            if (aEvent == Event.AFTER_EACH_MIGRATE && aContext != null) {
                final MigrationInfo aMI = aContext.getMigrationInfo();
                if (aMI instanceof MigrationInfoImpl) {
                    final ResolvedMigration aRM = ((MigrationInfoImpl) aMI).getResolvedMigration();
                    if (aRM != null)
                        if (LOGGER.isInfoEnabled())
                            LOGGER.info("  Performed migration: " + aRM);
                }
            }
        }
    };
    final Callback aCallbackAudit = new BaseCallback() {

        public void handle(@Nonnull final Event aEvent, @Nonnull final Context aContext) {
            if (aEvent == Event.AFTER_EACH_MIGRATE && aContext != null) {
                final MigrationInfo aMI = aContext.getMigrationInfo();
                if (aMI instanceof MigrationInfoImpl) {
                    final ResolvedMigration aRM = ((MigrationInfoImpl) aMI).getResolvedMigration();
                    // before that version
                    if (aRM != null && aRM.getVersion().isAtLeast("7"))
                        AuditHelper.onAuditExecuteSuccess("sql-migration-success", aRM.getVersion().toString(), aRM.getDescription(), aRM.getScript(), aRM.getType().name(), aRM.getPhysicalLocation());
                }
            }
        }
    };
    final FluentConfiguration aConfig = Flyway.configure().dataSource(new DriverDataSource(FlywayMigrator.class.getClassLoader(), aCF.getAsString(SMPJDBCConfiguration.CONFIG_JDBC_DRIVER), aCF.getAsString(SMPJDBCConfiguration.CONFIG_JDBC_URL), aCF.getAsString(SMPJDBCConfiguration.CONFIG_JDBC_USER), aCF.getAsString(SMPJDBCConfiguration.CONFIG_JDBC_PASSWORD))).baselineOnMigrate(true).validateOnMigrate(false).baselineVersion("1").baselineDescription("SMP 5.2.x database layout, MySQL only").locations("db/migrate-" + eDBType.getID()).javaMigrations(new V2__MigrateDBUsersToPhotonUsers(), new V5__MigrateTransportProfilesToDB(), new V10__MigrateRolesToDB(), new V11__MigrateUsersToDB(), new V12__MigrateUserGroupsToDB(), new V14__MigrateSettingsToDB(), new V15__MigrateDBUsersToPhotonUsers()).callbacks(aCallbackLogging, aCallbackAudit);
    // Flyway to handle the DB schema?
    final String sSchema = aCF.getAsString(SMPJDBCConfiguration.CONFIG_JDBC_SCHEMA);
    if (StringHelper.hasText(sSchema)) {
        // Use the schema only, if it is explicitly configured
        // The default schema name is ["$user", public] and as such unusable
        aConfig.schemas(sSchema);
    }
    // If no schema is specified, schema create should also be disabled
    final boolean bCreateSchema = aCF.getAsBoolean(SMPJDBCConfiguration.CONFIG_JDBC_SCHEMA_CREATE, false);
    aConfig.createSchemas(bCreateSchema);
    final Flyway aFlyway = aConfig.load();
    if (false)
        aFlyway.validate();
    aFlyway.migrate();
    LOGGER.info("Finished running Flyway");
}
Also used : Context(org.flywaydb.core.api.callback.Context) MigrationInfo(org.flywaydb.core.api.MigrationInfo) Flyway(org.flywaydb.core.Flyway) DriverDataSource(org.flywaydb.core.internal.jdbc.DriverDataSource) V15__MigrateDBUsersToPhotonUsers(com.helger.phoss.smp.backend.sql.migration.V15__MigrateDBUsersToPhotonUsers) ConfigFile(com.helger.settings.exchange.configfile.ConfigFile) Nonnull(javax.annotation.Nonnull) MigrationInfoImpl(org.flywaydb.core.internal.info.MigrationInfoImpl) V12__MigrateUserGroupsToDB(com.helger.phoss.smp.backend.sql.migration.V12__MigrateUserGroupsToDB) FluentConfiguration(org.flywaydb.core.api.configuration.FluentConfiguration) V2__MigrateDBUsersToPhotonUsers(com.helger.phoss.smp.backend.sql.migration.V2__MigrateDBUsersToPhotonUsers) V11__MigrateUsersToDB(com.helger.phoss.smp.backend.sql.migration.V11__MigrateUsersToDB) Callback(org.flywaydb.core.api.callback.Callback) BaseCallback(org.flywaydb.core.api.callback.BaseCallback) V5__MigrateTransportProfilesToDB(com.helger.phoss.smp.backend.sql.migration.V5__MigrateTransportProfilesToDB) V14__MigrateSettingsToDB(com.helger.phoss.smp.backend.sql.migration.V14__MigrateSettingsToDB) Event(org.flywaydb.core.api.callback.Event) ResolvedMigration(org.flywaydb.core.api.resolver.ResolvedMigration) V10__MigrateRolesToDB(com.helger.phoss.smp.backend.sql.migration.V10__MigrateRolesToDB) BaseCallback(org.flywaydb.core.api.callback.BaseCallback)

Aggregations

V10__MigrateRolesToDB (com.helger.phoss.smp.backend.sql.migration.V10__MigrateRolesToDB)1 V11__MigrateUsersToDB (com.helger.phoss.smp.backend.sql.migration.V11__MigrateUsersToDB)1 V12__MigrateUserGroupsToDB (com.helger.phoss.smp.backend.sql.migration.V12__MigrateUserGroupsToDB)1 V14__MigrateSettingsToDB (com.helger.phoss.smp.backend.sql.migration.V14__MigrateSettingsToDB)1 V15__MigrateDBUsersToPhotonUsers (com.helger.phoss.smp.backend.sql.migration.V15__MigrateDBUsersToPhotonUsers)1 V2__MigrateDBUsersToPhotonUsers (com.helger.phoss.smp.backend.sql.migration.V2__MigrateDBUsersToPhotonUsers)1 V5__MigrateTransportProfilesToDB (com.helger.phoss.smp.backend.sql.migration.V5__MigrateTransportProfilesToDB)1 ConfigFile (com.helger.settings.exchange.configfile.ConfigFile)1 Nonnull (javax.annotation.Nonnull)1 Flyway (org.flywaydb.core.Flyway)1 MigrationInfo (org.flywaydb.core.api.MigrationInfo)1 BaseCallback (org.flywaydb.core.api.callback.BaseCallback)1 Callback (org.flywaydb.core.api.callback.Callback)1 Context (org.flywaydb.core.api.callback.Context)1 Event (org.flywaydb.core.api.callback.Event)1 FluentConfiguration (org.flywaydb.core.api.configuration.FluentConfiguration)1 ResolvedMigration (org.flywaydb.core.api.resolver.ResolvedMigration)1 MigrationInfoImpl (org.flywaydb.core.internal.info.MigrationInfoImpl)1 DriverDataSource (org.flywaydb.core.internal.jdbc.DriverDataSource)1