Search in sources :

Example 1 with SqlScript

use of org.flywaydb.core.internal.sqlscript.SqlScript in project flyway by flyway.

the class SqlMigrationResolver method addMigrations.

private void addMigrations(List<ResolvedMigration> migrations, String prefix, String[] suffixes, boolean repeatable) {
    ResourceNameParser resourceNameParser = new ResourceNameParser(configuration);
    for (LoadableResource resource : resourceProvider.getResources(prefix, suffixes)) {
        String filename = resource.getFilename();
        ResourceName result = resourceNameParser.parse(filename);
        if (!result.isValid() || isSqlCallback(result) || !prefix.equals(result.getPrefix())) {
            continue;
        }
        SqlScript sqlScript = sqlScriptFactory.createSqlScript(resource, configuration.isMixed(), resourceProvider);
        List<LoadableResource> resources = new ArrayList<>();
        resources.add(resource);
        Integer checksum = getChecksumForLoadableResource(repeatable, resources);
        Integer equivalentChecksum = getEquivalentChecksumForLoadableResource(repeatable, resources);
        migrations.add(new ResolvedMigrationImpl(result.getVersion(), result.getDescription(), resource.getRelativePath(), checksum, equivalentChecksum, MigrationType.SQL, resource.getAbsolutePathOnDisk(), new SqlMigrationExecutor(sqlScriptExecutorFactory, sqlScript, false, false)) {

            @Override
            public void validate() {
            }
        });
    }
}
Also used : ResourceName(org.flywaydb.core.internal.resource.ResourceName) ResourceNameParser(org.flywaydb.core.internal.resource.ResourceNameParser) LoadableResource(org.flywaydb.core.api.resource.LoadableResource) ArrayList(java.util.ArrayList) SqlScript(org.flywaydb.core.internal.sqlscript.SqlScript) ResolvedMigrationImpl(org.flywaydb.core.internal.resolver.ResolvedMigrationImpl)

Example 2 with SqlScript

use of org.flywaydb.core.internal.sqlscript.SqlScript 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

ArrayList (java.util.ArrayList)2 SqlScript (org.flywaydb.core.internal.sqlscript.SqlScript)2 List (java.util.List)1 ClassProvider (org.flywaydb.core.api.ClassProvider)1 ResourceProvider (org.flywaydb.core.api.ResourceProvider)1 JavaMigration (org.flywaydb.core.api.migration.JavaMigration)1 LoadableResource (org.flywaydb.core.api.resource.LoadableResource)1 NoopClassProvider (org.flywaydb.core.internal.clazz.NoopClassProvider)1 DatabaseType (org.flywaydb.core.internal.database.DatabaseType)1 Database (org.flywaydb.core.internal.database.base.Database)1 Schema (org.flywaydb.core.internal.database.base.Schema)1 JdbcConnectionFactory (org.flywaydb.core.internal.jdbc.JdbcConnectionFactory)1 StatementInterceptor (org.flywaydb.core.internal.jdbc.StatementInterceptor)1 ParsingContext (org.flywaydb.core.internal.parser.ParsingContext)1 ResolvedMigrationImpl (org.flywaydb.core.internal.resolver.ResolvedMigrationImpl)1 NoopResourceProvider (org.flywaydb.core.internal.resource.NoopResourceProvider)1 ResourceName (org.flywaydb.core.internal.resource.ResourceName)1 ResourceNameParser (org.flywaydb.core.internal.resource.ResourceNameParser)1 StringResource (org.flywaydb.core.internal.resource.StringResource)1 SchemaHistory (org.flywaydb.core.internal.schemahistory.SchemaHistory)1