Search in sources :

Example 1 with JdbcConnectionFactory

use of org.flywaydb.core.internal.jdbc.JdbcConnectionFactory 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 2 with JdbcConnectionFactory

use of org.flywaydb.core.internal.jdbc.JdbcConnectionFactory in project flyway by flyway.

the class FlywayExecutor method execute.

/**
 * Executes this command with proper resource handling and cleanup.
 *
 * @param command The command to execute.
 * @param <T> The type of the result.
 * @return The result of the command.
 */
public <T> T execute(Command<T> command, boolean scannerRequired) {
    T result;
    configurationValidator.validate(configuration);
    StatementInterceptor statementInterceptor = null;
    final Pair<ResourceProvider, ClassProvider<JavaMigration>> resourceProviderClassProviderPair = createResourceAndClassProviders(scannerRequired);
    final ResourceProvider resourceProvider = resourceProviderClassProviderPair.getLeft();
    final ClassProvider<JavaMigration> classProvider = resourceProviderClassProviderPair.getRight();
    final ParsingContext parsingContext = new ParsingContext();
    resourceNameValidator.validateSQLMigrationNaming(resourceProvider, configuration);
    JdbcConnectionFactory jdbcConnectionFactory = new JdbcConnectionFactory(configuration.getDataSource(), configuration, statementInterceptor);
    final DatabaseType databaseType = jdbcConnectionFactory.getDatabaseType();
    final SqlScriptFactory sqlScriptFactory = databaseType.createSqlScriptFactory(configuration, parsingContext);
    RetryStrategy.setNumberOfRetries(configuration.getLockRetryCount());
    final SqlScriptExecutorFactory noCallbackSqlScriptExecutorFactory = databaseType.createSqlScriptExecutorFactory(jdbcConnectionFactory, NoopCallbackExecutor.INSTANCE, null);
    jdbcConnectionFactory.setConnectionInitializer((jdbcConnectionFactory1, connection) -> {
        if (configuration.getInitSql() == null) {
            return;
        }
        StringResource resource = new StringResource(configuration.getInitSql());
        SqlScript sqlScript = sqlScriptFactory.createSqlScript(resource, true, resourceProvider);
        boolean outputQueryResults = false;
        noCallbackSqlScriptExecutorFactory.createSqlScriptExecutor(connection, false, false, outputQueryResults).execute(sqlScript);
    });
    Database database = null;
    try {
        VersionPrinter.printVersion();
        database = databaseType.createDatabase(configuration, !dbConnectionInfoPrinted, jdbcConnectionFactory, statementInterceptor);
        databaseType.printMessages();
        dbConnectionInfoPrinted = true;
        LOG.debug("DDL Transactions Supported: " + database.supportsDdlTransactions());
        Pair<Schema, List<Schema>> schemas = SchemaHistoryFactory.prepareSchemas(configuration, database);
        Schema defaultSchema = schemas.getLeft();
        parsingContext.populate(database, configuration);
        database.ensureSupported();
        DefaultCallbackExecutor callbackExecutor = new DefaultCallbackExecutor(configuration, database, defaultSchema, prepareCallbacks(database, resourceProvider, jdbcConnectionFactory, sqlScriptFactory, statementInterceptor, defaultSchema, parsingContext));
        SqlScriptExecutorFactory sqlScriptExecutorFactory = databaseType.createSqlScriptExecutorFactory(jdbcConnectionFactory, callbackExecutor, statementInterceptor);
        SchemaHistory schemaHistory = SchemaHistoryFactory.getSchemaHistory(configuration, noCallbackSqlScriptExecutorFactory, sqlScriptFactory, database, defaultSchema, statementInterceptor);
        result = command.execute(createMigrationResolver(resourceProvider, classProvider, sqlScriptExecutorFactory, sqlScriptFactory, parsingContext), schemaHistory, database, defaultSchema, schemas.getRight().toArray(new Schema[0]), callbackExecutor, statementInterceptor);
    } finally {
        IOUtils.close(database);
        showMemoryUsage();
    }
    return result;
}
Also used : JdbcConnectionFactory(org.flywaydb.core.internal.jdbc.JdbcConnectionFactory) SqlScriptExecutorFactory(org.flywaydb.core.internal.sqlscript.SqlScriptExecutorFactory) ParsingContext(org.flywaydb.core.internal.parser.ParsingContext) DatabaseType(org.flywaydb.core.internal.database.DatabaseType) SqlScriptFactory(org.flywaydb.core.internal.sqlscript.SqlScriptFactory) Schema(org.flywaydb.core.internal.database.base.Schema) SchemaHistory(org.flywaydb.core.internal.schemahistory.SchemaHistory) StringResource(org.flywaydb.core.internal.resource.StringResource) ResourceProvider(org.flywaydb.core.api.ResourceProvider) NoopResourceProvider(org.flywaydb.core.internal.resource.NoopResourceProvider) ClassProvider(org.flywaydb.core.api.ClassProvider) NoopClassProvider(org.flywaydb.core.internal.clazz.NoopClassProvider) Database(org.flywaydb.core.internal.database.base.Database) SqlScript(org.flywaydb.core.internal.sqlscript.SqlScript) JavaMigration(org.flywaydb.core.api.migration.JavaMigration) ArrayList(java.util.ArrayList) List(java.util.List) StatementInterceptor(org.flywaydb.core.internal.jdbc.StatementInterceptor)

Aggregations

Database (org.flywaydb.core.internal.database.base.Database)2 JdbcConnectionFactory (org.flywaydb.core.internal.jdbc.JdbcConnectionFactory)2 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Flyway (org.flywaydb.core.Flyway)1 ClassProvider (org.flywaydb.core.api.ClassProvider)1 ResourceProvider (org.flywaydb.core.api.ResourceProvider)1 Configuration (org.flywaydb.core.api.configuration.Configuration)1 FluentConfiguration (org.flywaydb.core.api.configuration.FluentConfiguration)1 JavaMigration (org.flywaydb.core.api.migration.JavaMigration)1 NoopClassProvider (org.flywaydb.core.internal.clazz.NoopClassProvider)1 DbMigrate (org.flywaydb.core.internal.command.DbMigrate)1 DatabaseType (org.flywaydb.core.internal.database.DatabaseType)1 Schema (org.flywaydb.core.internal.database.base.Schema)1 StatementInterceptor (org.flywaydb.core.internal.jdbc.StatementInterceptor)1 FlywayTrialExpiredException (org.flywaydb.core.internal.license.FlywayTrialExpiredException)1 ParsingContext (org.flywaydb.core.internal.parser.ParsingContext)1 NoopResourceProvider (org.flywaydb.core.internal.resource.NoopResourceProvider)1