Search in sources :

Example 1 with FluentConfiguration

use of org.flywaydb.core.api.configuration.FluentConfiguration in project ninja by ninjaframework.

the class MigrationEngineFlywayTest method testModeDefault.

@Test
public void testModeDefault() {
    FluentConfiguration mock = testMockedMigrate(NinjaMode.test);
    InOrder inOrder = Mockito.inOrder(mock, mockedFlyway);
    inOrder.verify(mock).dataSource("testurl", "testuser", "testpassword");
    inOrder.verify(mock).load();
    inOrder.verify(mockedFlyway).clean();
    inOrder.verify(mockedFlyway).migrate();
}
Also used : FluentConfiguration(org.flywaydb.core.api.configuration.FluentConfiguration) InOrder(org.mockito.InOrder) Test(org.junit.Test)

Example 2 with FluentConfiguration

use of org.flywaydb.core.api.configuration.FluentConfiguration in project ninja by ninjaframework.

the class MigrationEngineFlywayTest method testModeNoDrop.

@Test
public void testModeNoDrop() {
    FluentConfiguration mock = testMockedMigrate(NinjaMode.test, ImmutableMap.of(NinjaConstant.NINJA_MIGRATION_DROP_SCHEMA, String.valueOf(false)));
    InOrder inOrder = Mockito.inOrder(mock, mockedFlyway);
    inOrder.verify(mock).dataSource("testurl", "testuser", "testpassword");
    inOrder.verify(mock).load();
    inOrder.verify(mockedFlyway).migrate();
    verify(mockedFlyway, never()).clean();
}
Also used : FluentConfiguration(org.flywaydb.core.api.configuration.FluentConfiguration) InOrder(org.mockito.InOrder) Test(org.junit.Test)

Example 3 with FluentConfiguration

use of org.flywaydb.core.api.configuration.FluentConfiguration in project flyway by flyway.

the class Main method main.

public static void main(String[] args) {
    CommandLineArguments commandLineArguments = new CommandLineArguments(args);
    initLogging(commandLineArguments);
    try {
        commandLineArguments.validate();
        if (commandLineArguments.hasOperation("help") || commandLineArguments.shouldPrintUsage()) {
            StringBuilder helpText = new StringBuilder();
            boolean helpAsVerbWithOperation = commandLineArguments.hasOperation("help") && commandLineArguments.getOperations().size() > 1;
            boolean helpAsFlagWithOperation = commandLineArguments.shouldPrintUsage() && commandLineArguments.getOperations().size() > 0;
            if (helpAsVerbWithOperation || helpAsFlagWithOperation) {
                for (String operation : commandLineArguments.getOperations()) {
                    String helpTextForOperation = PluginRegister.getPlugins(CommandExtension.class).stream().filter(e -> e.handlesCommand(operation)).map(CommandExtension::getHelpText).collect(Collectors.joining("\n\n"));
                    if (StringUtils.hasText(helpTextForOperation)) {
                        helpText.append(helpTextForOperation).append("\n\n");
                    }
                }
            }
            if (!StringUtils.hasText(helpText.toString())) {
                printUsage();
            } else {
                LOG.info(helpText.toString());
            }
            return;
        }
        Map<String, String> envVars = ConfigUtils.environmentVariablesToPropertyMap();
        Map<String, String> config = new HashMap<>();
        initializeDefaults(config, commandLineArguments);
        loadConfigurationFromConfigFiles(config, commandLineArguments, envVars);
        if (commandLineArguments.isWorkingDirectorySet()) {
            makeRelativeLocationsBasedOnWorkingDirectory(commandLineArguments, config);
        }
        config.putAll(envVars);
        config = overrideConfiguration(config, commandLineArguments.getConfiguration());
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        List<File> jarFiles = new ArrayList<>();
        jarFiles.addAll(getJdbcDriverJarFiles());
        jarFiles.addAll(getJavaMigrationJarFiles(config));
        if (!jarFiles.isEmpty()) {
            classLoader = ClassUtils.addJarsOrDirectoriesToClasspath(classLoader, jarFiles);
        }
        if (!commandLineArguments.shouldSuppressPrompt()) {
            promptForCredentialsIfMissing(config);
        }
        ConfigUtils.dumpConfiguration(config);
        filterProperties(config);
        Configuration configuration = new FluentConfiguration(classLoader).configuration(config);
        if (!commandLineArguments.skipCheckForUpdate()) {
            if (RedgateUpdateChecker.isEnabled() && configuration.getDataSource() != null) {
                JdbcConnectionFactory jdbcConnectionFactory = new JdbcConnectionFactory(configuration.getDataSource(), configuration, null);
                Database database = jdbcConnectionFactory.getDatabaseType().createDatabase(configuration, false, jdbcConnectionFactory, null);
                RedgateUpdateChecker.Context context = new RedgateUpdateChecker.Context(config.get(ConfigUtils.URL), commandLineArguments.getOperations(), database.getDatabaseType().getName(), database.getVersion().getVersion());
                RedgateUpdateChecker.checkForVersionUpdates(context);
            } else {
                MavenVersionChecker.checkForVersionUpdates();
            }
        }
        Flyway flyway = Flyway.configure(classLoader).configuration(configuration).load();
        OperationResultBase result;
        if (commandLineArguments.getOperations().size() == 1) {
            String operation = commandLineArguments.getOperations().get(0);
            result = executeOperation(flyway, operation, config, commandLineArguments);
        } else {
            result = new CompositeResult();
            for (String operation : commandLineArguments.getOperations()) {
                OperationResultBase individualResult = executeOperation(flyway, operation, config, commandLineArguments);
                ((CompositeResult) result).individualResults.add(individualResult);
            }
        }
        if (commandLineArguments.shouldOutputJson()) {
            printJson(commandLineArguments, result);
        }
    } catch (DbMigrate.FlywayMigrateException e) {
        MigrateErrorResult errorResult = ErrorOutput.fromMigrateException(e);
        printError(commandLineArguments, e, errorResult);
        System.exit(1);
    } catch (Exception e) {
        ErrorOutput errorOutput = ErrorOutput.fromException(e);
        printError(commandLineArguments, e, errorOutput);
        System.exit(1);
    } finally {
        flushLog(commandLineArguments);
    }
}
Also used : DbMigrate(org.flywaydb.core.internal.command.DbMigrate) Configuration(org.flywaydb.core.api.configuration.Configuration) FluentConfiguration(org.flywaydb.core.api.configuration.FluentConfiguration) Database(org.flywaydb.core.internal.database.base.Database) JdbcConnectionFactory(org.flywaydb.core.internal.jdbc.JdbcConnectionFactory) Flyway(org.flywaydb.core.Flyway) FlywayTrialExpiredException(org.flywaydb.core.internal.license.FlywayTrialExpiredException) IOException(java.io.IOException) FluentConfiguration(org.flywaydb.core.api.configuration.FluentConfiguration) File(java.io.File)

Example 4 with FluentConfiguration

use of org.flywaydb.core.api.configuration.FluentConfiguration in project spring-boot by spring-projects.

the class FlywayPropertiesTests method defaultValuesAreConsistent.

@SuppressWarnings("deprecation")
@Test
void defaultValuesAreConsistent() {
    FlywayProperties properties = new FlywayProperties();
    Configuration configuration = new FluentConfiguration();
    assertThat(configuration.isFailOnMissingLocations()).isEqualTo(properties.isFailOnMissingLocations());
    assertThat(properties.getLocations().stream().map(Location::new).toArray(Location[]::new)).isEqualTo(configuration.getLocations());
    assertThat(properties.getEncoding()).isEqualTo(configuration.getEncoding());
    assertThat(properties.getConnectRetries()).isEqualTo(configuration.getConnectRetries());
    // Can't assert connect retries interval as it is new in Flyway 7.15
    // Asserting hard-coded value in the metadata instead
    assertThat(configuration.getConnectRetriesInterval()).isEqualTo(120);
    // Can't assert lock retry count default as it is new in Flyway 7.1
    // Asserting hard-coded value in the metadata instead
    assertThat(configuration.getLockRetryCount()).isEqualTo(50);
    assertThat(properties.getDefaultSchema()).isEqualTo(configuration.getDefaultSchema());
    assertThat(properties.getSchemas()).isEqualTo(Arrays.asList(configuration.getSchemas()));
    assertThat(properties.isCreateSchemas()).isEqualTo(configuration.isCreateSchemas());
    assertThat(properties.getTable()).isEqualTo(configuration.getTable());
    assertThat(properties.getBaselineDescription()).isEqualTo(configuration.getBaselineDescription());
    assertThat(MigrationVersion.fromVersion(properties.getBaselineVersion())).isEqualTo(configuration.getBaselineVersion());
    assertThat(properties.getInstalledBy()).isEqualTo(configuration.getInstalledBy());
    assertThat(properties.getPlaceholders()).isEqualTo(configuration.getPlaceholders());
    assertThat(properties.getPlaceholderPrefix()).isEqualToIgnoringWhitespace(configuration.getPlaceholderPrefix());
    assertThat(properties.getPlaceholderSuffix()).isEqualTo(configuration.getPlaceholderSuffix());
    assertThat(properties.isPlaceholderReplacement()).isEqualTo(configuration.isPlaceholderReplacement());
    assertThat(properties.getSqlMigrationPrefix()).isEqualTo(configuration.getSqlMigrationPrefix());
    assertThat(properties.getSqlMigrationSuffixes()).isEqualTo(Arrays.asList(configuration.getSqlMigrationSuffixes()));
    assertThat(properties.getSqlMigrationSeparator()).isEqualTo(properties.getSqlMigrationSeparator());
    assertThat(properties.getRepeatableSqlMigrationPrefix()).isEqualTo(properties.getRepeatableSqlMigrationPrefix());
    assertThat(properties.getTarget()).isNull();
    assertThat(configuration.getTarget()).isNull();
    assertThat(configuration.getInitSql()).isNull();
    assertThat(properties.getInitSqls()).isEmpty();
    assertThat(configuration.isBaselineOnMigrate()).isEqualTo(properties.isBaselineOnMigrate());
    assertThat(configuration.isCleanDisabled()).isEqualTo(properties.isCleanDisabled());
    assertThat(configuration.isCleanOnValidationError()).isEqualTo(properties.isCleanOnValidationError());
    assertThat(configuration.isGroup()).isEqualTo(properties.isGroup());
    assertThat(configuration.isIgnoreMissingMigrations()).isEqualTo(properties.isIgnoreMissingMigrations());
    assertThat(configuration.isIgnoreIgnoredMigrations()).isEqualTo(properties.isIgnoreIgnoredMigrations());
    assertThat(configuration.isIgnorePendingMigrations()).isEqualTo(properties.isIgnorePendingMigrations());
    assertThat(configuration.isIgnoreFutureMigrations()).isEqualTo(properties.isIgnoreFutureMigrations());
    assertThat(configuration.isMixed()).isEqualTo(properties.isMixed());
    assertThat(configuration.isOutOfOrder()).isEqualTo(properties.isOutOfOrder());
    assertThat(configuration.isSkipDefaultCallbacks()).isEqualTo(properties.isSkipDefaultCallbacks());
    assertThat(configuration.isSkipDefaultResolvers()).isEqualTo(properties.isSkipDefaultResolvers());
    assertThat(configuration.isValidateMigrationNaming()).isEqualTo(properties.isValidateMigrationNaming());
    assertThat(configuration.isValidateOnMigrate()).isEqualTo(properties.isValidateOnMigrate());
    assertThat(properties.getDetectEncoding()).isNull();
    assertThat(configuration.getScriptPlaceholderPrefix()).isEqualTo("FP__");
    assertThat(configuration.getScriptPlaceholderSuffix()).isEqualTo("__");
}
Also used : FluentConfiguration(org.flywaydb.core.api.configuration.FluentConfiguration) ClassicConfiguration(org.flywaydb.core.api.configuration.ClassicConfiguration) Configuration(org.flywaydb.core.api.configuration.Configuration) FluentConfiguration(org.flywaydb.core.api.configuration.FluentConfiguration) Test(org.junit.jupiter.api.Test)

Example 5 with FluentConfiguration

use of org.flywaydb.core.api.configuration.FluentConfiguration in project ninja by ninjaframework.

the class MigrationEngineFlyway method migrate.

@Override
public void migrate() {
    // Get the connection credentials from application.conf
    String connectionUrl = ninjaProperties.getOrDie(NinjaConstant.DB_CONNECTION_URL);
    String connectionUsername = ninjaProperties.getOrDie(NinjaConstant.DB_CONNECTION_USERNAME);
    String connectionPassword = ninjaProperties.getOrDie(NinjaConstant.DB_CONNECTION_PASSWORD);
    String locations = ninjaProperties.get(NinjaConstant.NINJA_MIGRATION_LOCATIONS);
    String schemas = ninjaProperties.get(NinjaConstant.NINJA_MIGRATION_SCHEMAS);
    // We migrate automatically => if you do not want that (eg in production)
    // set ninja.migration.run=false in application.conf
    FluentConfiguration fluentConfiguration = fluentConfigurationProvider.get();
    if (schemas != null) {
        fluentConfiguration.schemas(schemas);
    }
    if (locations != null) {
        fluentConfiguration.locations(locations);
    }
    Flyway flyway = fluentConfiguration.dataSource(connectionUrl, connectionUsername, connectionPassword).load();
    // get a fresh database.
    if (ninjaProperties.getBooleanWithDefault(NinjaConstant.NINJA_MIGRATION_DROP_SCHEMA, ninjaProperties.isTest())) {
        flyway.clean();
    }
    flyway.migrate();
}
Also used : FluentConfiguration(org.flywaydb.core.api.configuration.FluentConfiguration) Flyway(org.flywaydb.core.Flyway)

Aggregations

FluentConfiguration (org.flywaydb.core.api.configuration.FluentConfiguration)9 Test (org.junit.Test)5 InOrder (org.mockito.InOrder)5 Flyway (org.flywaydb.core.Flyway)3 Configuration (org.flywaydb.core.api.configuration.Configuration)2 File (java.io.File)1 IOException (java.io.IOException)1 MigrationEngineFlyway (ninja.migrations.flyway.MigrationEngineFlyway)1 ClassicConfiguration (org.flywaydb.core.api.configuration.ClassicConfiguration)1 DbMigrate (org.flywaydb.core.internal.command.DbMigrate)1 Database (org.flywaydb.core.internal.database.base.Database)1 JdbcConnectionFactory (org.flywaydb.core.internal.jdbc.JdbcConnectionFactory)1 FlywayTrialExpiredException (org.flywaydb.core.internal.license.FlywayTrialExpiredException)1 Test (org.junit.jupiter.api.Test)1