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.
package liquibase.snapshot;
import liquibase.CatalogAndSchema;
import liquibase.GlobalConfiguration;
import liquibase.Scope;
import liquibase.SupportsMethodValidationLevelsEnum;
import liquibase.database.*;
import liquibase.database.core.MariaDBDatabase;
import liquibase.database.core.MockDatabase;
import liquibase.database.core.PostgresDatabase;
import liquibase.diff.compare.DatabaseObjectComparatorFactory;
import liquibase.exception.DatabaseException;
import liquibase.exception.UnexpectedLiquibaseException;
import liquibase.executor.ExecutorService;
import liquibase.statement.core.RawParameterizedSqlStatement;
import liquibase.structure.DatabaseObject;
import liquibase.structure.core.Schema;
import liquibase.structure.core.Table;
import liquibase.util.LiquibaseUtil;
import java.util.*;
import static liquibase.snapshot.SnapshotGenerator.PRIORITY_NONE;
public class SnapshotGeneratorFactory {
private static SnapshotGeneratorFactory instance;
private final List generators = new ArrayList<>();
protected static final String SUPPORTS_METHOD_REQUIRED_MESSAGE = "%s class does not properly implement the 'getPriority(Class extends DatabaseObject>, Database)' method and may incorrectly override other snapshot generators causing unexpected behavior. Please report this to the Liquibase developers or if you are developing this change please fix it ;)";
protected SnapshotGeneratorFactory() {
try {
for (SnapshotGenerator generator : Scope.getCurrentScope().getServiceLocator().findInstances(SnapshotGenerator.class)) {
verifyPriorityMethodImplementedCorrectly(generator);
register(generator);
}
} catch (Exception e) {
throw new UnexpectedLiquibaseException(e);
}
}
/**
* Ensure that the getPriority method returns PRIORITY_NONE when an unexpected database is supplied to the method.
* This ensures that no snapshot generator accidentally returns PRIORITY_SPECIALIZED for all databases, instead of
* only for the database that it is supposed to work with.
* @param generator
*/
private void verifyPriorityMethodImplementedCorrectly(SnapshotGenerator generator) {
if (GlobalConfiguration.SUPPORTS_METHOD_VALIDATION_LEVEL.getCurrentValue().equals(SupportsMethodValidationLevelsEnum.OFF)) {
return;
}
try {
int priority = generator.getPriority(null, new MockDatabase());
if (priority != PRIORITY_NONE) {
if (LiquibaseUtil.isDevVersion()) {
throw new UnexpectedLiquibaseException(String.format(SUPPORTS_METHOD_REQUIRED_MESSAGE, generator.getClass().getName()));
}
switch (GlobalConfiguration.SUPPORTS_METHOD_VALIDATION_LEVEL.getCurrentValue()) {
case WARN:
Scope.getCurrentScope().getLog(getClass()).warning(String.format(SUPPORTS_METHOD_REQUIRED_MESSAGE, generator.getClass().getName()));
break;
case FAIL:
throw new UnexpectedLiquibaseException(String.format(SUPPORTS_METHOD_REQUIRED_MESSAGE, generator.getClass().getName()));
default:
break;
}
}
} catch (UnexpectedLiquibaseException ue) {
throw ue;
} catch (Exception e) {
Scope.getCurrentScope().getLog(getClass()).fine("Failed to check validity of getPriority method in " + generator.getClass().getSimpleName() + " snapshot generator.", e);
}
}
/**
* Return singleton SnapshotGeneratorFactory
*/
public static synchronized SnapshotGeneratorFactory getInstance() {
if (instance == null) {
instance = new SnapshotGeneratorFactory();
}
return instance;
}
public static synchronized void reset() {
instance = new SnapshotGeneratorFactory();
}
public static synchronized void resetAll() {
instance = null;
}
public void register(SnapshotGenerator generator) {
generators.add(generator);
}
public void unregister(SnapshotGenerator generator) {
generators.remove(generator);
}
public void unregister(Class generatorClass) {
SnapshotGenerator toRemove = null;
for (SnapshotGenerator existingGenerator : generators) {
if (existingGenerator.getClass().equals(generatorClass)) {
toRemove = existingGenerator;
}
}
unregister(toRemove);
}
protected SortedSet getGenerators(Class extends DatabaseObject> generatorClass, Database database) {
SortedSet validGenerators = new TreeSet<>(new SnapshotGeneratorComparator(generatorClass, database));
/*
* Query all SnapshotGenerators if they consider themselves applicable for the generatorClass (e.g. a Table)
* for a specific Database (e.g. MSSQL, Oracle, Postgres...)
*/
for (SnapshotGenerator generator : generators) {
if (generator.getPriority(generatorClass, database) > 0) {
validGenerators.add(generator);
}
}
return validGenerators;
}
/**
* Initialize the types to use for the snapshot
*
* @param example the example object to search for
* @param database the database to snapshot
* @return the Database Object types to search for
*/
private Set> initializeSnapshotTypes(DatabaseObject example, Database database) {
Set> types = new HashSet<>(getContainerTypes(example.getClass(), database));
types.add(example.getClass());
return types;
}
/**
* Check if the example object is a liquibase managed table
*
* @param example the database object to search for
* @param database the database to search
* @param liquibaseTableNames the list of liquibase table names
* @return true if the table exists, false otherwise
* @throws DatabaseException if there was an exception encountered when rolling back the postgres database
*/
private boolean checkLiquibaseTablesExistence(DatabaseObject example, Database database, List liquibaseTableNames) throws DatabaseException {
if (example instanceof Table && liquibaseTableNames.contains(example.getName())) {
try {
if (database instanceof MariaDBDatabase) {
// Handle MariaDB differently to avoid the
// Error: 1146-42S02: Table 'intuser_db.DATABASECHANGELOGLOCK' doesn't exist
String sql = "select table_name from information_schema.TABLES where TABLE_SCHEMA = ? and TABLE_NAME = ?;";
List