All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.flywaydb.core.internal.resolver.sql.SqlMigrationResolver Maven / Gradle / Ivy
/*
* Copyright (C) Red Gate Software Ltd 2010-2024
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.flywaydb.core.internal.resolver.sql;
import lombok.CustomLog;
import org.flywaydb.core.api.CoreMigrationType;
import org.flywaydb.core.api.ResourceProvider;
import org.flywaydb.core.api.callback.Event;
import org.flywaydb.core.api.configuration.Configuration;
import org.flywaydb.core.api.resolver.MigrationResolver;
import org.flywaydb.core.api.resolver.ResolvedMigration;
import org.flywaydb.core.api.resource.LoadableResource;
import org.flywaydb.core.api.resource.Resource;
import org.flywaydb.core.extensibility.LicenseGuard;
import org.flywaydb.core.extensibility.Tier;
import org.flywaydb.core.internal.parser.ParsingContext;
import org.flywaydb.core.internal.parser.PlaceholderReplacingReader;
import org.flywaydb.core.internal.resolver.ChecksumCalculator;
import org.flywaydb.core.internal.resolver.ResolvedMigrationComparator;
import org.flywaydb.core.internal.resolver.ResolvedMigrationImpl;
import org.flywaydb.core.internal.resource.ResourceName;
import org.flywaydb.core.internal.resource.ResourceNameParser;
import org.flywaydb.core.internal.sqlscript.SqlScript;
import org.flywaydb.core.internal.sqlscript.SqlScriptExecutorFactory;
import org.flywaydb.core.internal.sqlscript.SqlScriptFactory;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.stream.Collectors;
/**
* Migration resolver for SQL files on the classpath. The SQL files must have names like
* V1__Description.sql, V1_1__Description.sql, or R__description.sql.
*/
@CustomLog
public class SqlMigrationResolver implements MigrationResolver {
private final SqlScriptExecutorFactory sqlScriptExecutorFactory;
private final ResourceProvider resourceProvider;
private final SqlScriptFactory sqlScriptFactory;
private final Configuration configuration;
private final ParsingContext parsingContext;
public SqlMigrationResolver(ResourceProvider resourceProvider, SqlScriptExecutorFactory sqlScriptExecutorFactory,
SqlScriptFactory sqlScriptFactory, Configuration configuration, ParsingContext parsingContext) {
this.sqlScriptExecutorFactory = sqlScriptExecutorFactory;
this.resourceProvider = resourceProvider;
this.sqlScriptFactory = sqlScriptFactory;
this.configuration = configuration;
this.parsingContext = parsingContext;
}
@Override
public List resolveMigrations(Context context) {
List migrations = new ArrayList<>();
String[] suffixes = configuration.getSqlMigrationSuffixes();
addMigrations(migrations, configuration.getSqlMigrationPrefix(), suffixes, false);
addMigrations(migrations, configuration.getRepeatableSqlMigrationPrefix(), suffixes, true);
migrations.sort(new ResolvedMigrationComparator());
return migrations;
}
private LoadableResource[] createPlaceholderReplacingLoadableResources(List loadableResources) {
List list = new ArrayList<>();
for (final LoadableResource loadableResource : loadableResources) {
LoadableResource placeholderReplacingLoadableResource = new LoadableResource() {
@Override
public Reader read() {
return PlaceholderReplacingReader.create(configuration, parsingContext, loadableResource.read());
}
@Override
public String getAbsolutePath() {return loadableResource.getAbsolutePath();}
@Override
public String getAbsolutePathOnDisk() {return loadableResource.getAbsolutePathOnDisk();}
@Override
public String getFilename() {return loadableResource.getFilename();}
@Override
public String getRelativePath() {return loadableResource.getRelativePath();}
};
list.add(placeholderReplacingLoadableResource);
}
return list.toArray(new LoadableResource[0]);
}
private Integer getChecksumForLoadableResource(boolean repeatable, List loadableResources, ResourceName resourceName) {
if (repeatable && configuration.isPlaceholderReplacement()) {
parsingContext.updateFilenamePlaceholder(resourceName, configuration);
return ChecksumCalculator.calculate(createPlaceholderReplacingLoadableResources(loadableResources));
}
return ChecksumCalculator.calculate(loadableResources.toArray(new LoadableResource[0]));
}
private Integer getEquivalentChecksumForLoadableResource(boolean repeatable, List loadableResources) {
if (repeatable) {
return ChecksumCalculator.calculate(loadableResources.toArray(new LoadableResource[0]));
}
return null;
}
private void addMigrations(List migrations, String prefix, String[] suffixes, boolean repeatable) {
ResourceNameParser resourceNameParser = new ResourceNameParser(configuration);
for (LoadableResource resource : resourceProvider.getResources(prefix, suffixes)) {
String filename = resource.getFilename();
ResourceName resourceName = resourceNameParser.parse(filename);
if (!resourceName.isValid() || isSqlCallback(resourceName) || !prefix.equals(resourceName.getPrefix())) {
continue;
}
SqlScript sqlScript = sqlScriptFactory.createSqlScript(resource, configuration.isMixed(), resourceProvider);
List resources = new ArrayList<>();
resources.add(resource);
Integer checksum = getChecksumForLoadableResource(repeatable, resources, resourceName);
Integer equivalentChecksum = getEquivalentChecksumForLoadableResource(repeatable, resources);
migrations.add(new ResolvedMigrationImpl(
resourceName.getVersion(),
resourceName.getDescription(),
resource.getRelativePath(),
checksum,
equivalentChecksum,
CoreMigrationType.SQL,
resource.getAbsolutePathOnDisk(),
new SqlMigrationExecutor(sqlScriptExecutorFactory, sqlScript, false,
configuration.isBatch())));
}
}
/**
* Checks whether this filename is actually a sql-based callback instead of a regular migration.
*
* @param result The parsing result to check.
*/
protected static boolean isSqlCallback(ResourceName result) {
return Event.fromId(result.getPrefix()) != null;
}
}