use of liquibase.structure.DatabaseObject in project liquibase by liquibase.
the class YamlSnapshotSerializer method toMap.
// @Override
// public String serialize(LiquibaseSerializable object, boolean pretty) {
// if (object instanceof DatabaseObject) {
// if (alreadySerializingObject) {
// return ((DatabaseObject) object).getObjectTypeName()+"#"+((DatabaseObject) object).getSnapshotId();
// } else {
// alreadySerializingObject = true;
// String string = super.serialize(object, pretty);
// alreadySerializingObject = false;
// return string;
// }
// }
// return super.serialize(object, pretty);
// }
@Override
protected Object toMap(final LiquibaseSerializable object) {
if (object instanceof DatabaseObject) {
if (object instanceof Column && (BooleanUtil.isTrue(((Column) object).getDescending()) || BooleanUtil.isTrue(((Column) object).getComputed()))) {
// not really a "real" column that has a snapshot to reference, just serialize it
return super.toMap(object);
} else if (alreadySerializingObject) {
String snapshotId = ((DatabaseObject) object).getSnapshotId();
if (snapshotId == null) {
String name = ((DatabaseObject) object).getName();
Object table = ((DatabaseObject) object).getAttribute("table", Object.class);
if (table == null) {
table = ((DatabaseObject) object).getAttribute("relation", Object.class);
}
if (table != null) {
name = table.toString() + "." + name;
}
if (((DatabaseObject) object).getSchema() != null) {
name = ((DatabaseObject) object).getSchema().toString() + "." + name;
}
throw new UnexpectedLiquibaseException("Found a null snapshotId for " + StringUtil.lowerCaseFirst(object.getClass().getSimpleName()) + " " + name);
}
return ((DatabaseObject) object).getClass().getName() + "#" + snapshotId;
} else {
alreadySerializingObject = true;
Object map = super.toMap(object);
alreadySerializingObject = false;
return map;
}
}
if (object instanceof DatabaseObjectCollection) {
SortedMap<String, Object> returnMap = new TreeMap<>();
for (Map.Entry<Class<? extends DatabaseObject>, Set<? extends DatabaseObject>> entry : ((DatabaseObjectCollection) object).toMap().entrySet()) {
ArrayList value = new ArrayList(entry.getValue());
Collections.sort(value, new DatabaseObjectComparator());
returnMap.put(entry.getKey().getName(), value);
}
return returnMap;
}
return super.toMap(object);
}
use of liquibase.structure.DatabaseObject in project liquibase by liquibase.
the class DatabaseObjectFactory method parseTypes.
public Set<Class<? extends DatabaseObject>> parseTypes(String typesString) {
if (StringUtil.trimToNull(typesString) == null) {
return getStandardTypes();
} else {
Set<Class<? extends DatabaseObject>> returnSet = new HashSet<>();
Set<String> typesToInclude = new HashSet<>(Arrays.asList(typesString.toLowerCase().split("\\s*,\\s*")));
Set<String> typesNotFound = new HashSet<>(typesToInclude);
for (DatabaseObject object : Scope.getCurrentScope().getServiceLocator().findInstances(DatabaseObject.class)) {
Class<? extends DatabaseObject> clazz = object.getClass();
if (typesToInclude.contains(clazz.getSimpleName().toLowerCase()) || typesToInclude.contains(clazz.getSimpleName().toLowerCase() + "s") || // like indexes
typesToInclude.contains(clazz.getSimpleName().toLowerCase() + "es")) {
returnSet.add(clazz);
typesNotFound.remove(clazz.getSimpleName().toLowerCase());
typesNotFound.remove(clazz.getSimpleName().toLowerCase() + "s");
typesNotFound.remove(clazz.getSimpleName().toLowerCase() + "es");
}
}
if (!typesNotFound.isEmpty()) {
throw new UnexpectedLiquibaseException("Unknown snapshot type(s) " + StringUtil.join(typesNotFound, ", "));
}
return returnSet;
}
}
use of liquibase.structure.DatabaseObject in project liquibase by liquibase.
the class InternalDiffCommandStep method createReferenceSnapshot.
protected DatabaseSnapshot createReferenceSnapshot(CommandScope commandScope) throws DatabaseException, InvalidExampleException {
CatalogAndSchema[] schemas;
CompareControl compareControl = commandScope.getArgumentValue(COMPARE_CONTROL_ARG);
Database targetDatabase = commandScope.getArgumentValue(TARGET_DATABASE_ARG);
Database referenceDatabase = commandScope.getArgumentValue(REFERENCE_DATABASE_ARG);
SnapshotControl snapshotControl = commandScope.getArgumentValue(REFERENCE_SNAPSHOT_CONTROL_ARG);
Class<? extends DatabaseObject>[] snapshotTypes = commandScope.getArgumentValue(SNAPSHOT_TYPES_ARG);
ObjectChangeFilter objectChangeFilter = commandScope.getArgumentValue(OBJECT_CHANGE_FILTER_ARG);
SnapshotListener snapshotListener = commandScope.getArgumentValue(SNAPSHOT_LISTENER_ARG);
if ((compareControl == null) || (compareControl.getSchemaComparisons() == null)) {
schemas = new CatalogAndSchema[] { targetDatabase.getDefaultSchema() };
} else {
schemas = new CatalogAndSchema[compareControl.getSchemaComparisons().length];
int i = 0;
for (CompareControl.SchemaComparison comparison : compareControl.getSchemaComparisons()) {
CatalogAndSchema schema;
if (referenceDatabase.supportsSchemas()) {
schema = new CatalogAndSchema(referenceDatabase.getDefaultCatalogName(), comparison.getReferenceSchema().getSchemaName());
} else {
schema = new CatalogAndSchema(comparison.getReferenceSchema().getSchemaName(), comparison.getReferenceSchema().getSchemaName());
}
schemas[i++] = schema;
}
}
if (snapshotControl == null) {
snapshotControl = new SnapshotControl(referenceDatabase, objectChangeFilter, snapshotTypes);
}
if (snapshotListener != null) {
snapshotControl.setSnapshotListener(snapshotListener);
}
ObjectQuotingStrategy originalStrategy = referenceDatabase.getObjectQuotingStrategy();
try {
referenceDatabase.setObjectQuotingStrategy(ObjectQuotingStrategy.QUOTE_ALL_OBJECTS);
return SnapshotGeneratorFactory.getInstance().createSnapshot(schemas, referenceDatabase, snapshotControl);
} finally {
referenceDatabase.setObjectQuotingStrategy(originalStrategy);
}
}
use of liquibase.structure.DatabaseObject in project liquibase by liquibase.
the class InternalDiffCommandStep method createTargetSnapshot.
protected DatabaseSnapshot createTargetSnapshot(CommandScope commandScope) throws DatabaseException, InvalidExampleException {
CatalogAndSchema[] schemas;
CompareControl compareControl = commandScope.getArgumentValue(COMPARE_CONTROL_ARG);
Database targetDatabase = commandScope.getArgumentValue(TARGET_DATABASE_ARG);
SnapshotControl snapshotControl = commandScope.getArgumentValue(TARGET_SNAPSHOT_CONTROL_ARG);
Class<? extends DatabaseObject>[] snapshotTypes = commandScope.getArgumentValue(SNAPSHOT_TYPES_ARG);
SnapshotListener snapshotListener = commandScope.getArgumentValue(SNAPSHOT_LISTENER_ARG);
if ((compareControl == null) || (compareControl.getSchemaComparisons() == null)) {
schemas = new CatalogAndSchema[] { targetDatabase.getDefaultSchema() };
} else {
schemas = new CatalogAndSchema[compareControl.getSchemaComparisons().length];
int i = 0;
for (CompareControl.SchemaComparison comparison : compareControl.getSchemaComparisons()) {
CatalogAndSchema schema;
if (targetDatabase.supportsSchemas()) {
schema = new CatalogAndSchema(targetDatabase.getDefaultCatalogName(), comparison.getComparisonSchema().getSchemaName());
} else {
schema = new CatalogAndSchema(comparison.getComparisonSchema().getSchemaName(), comparison.getComparisonSchema().getSchemaName());
}
schemas[i++] = schema;
}
}
if (snapshotControl == null) {
snapshotControl = new SnapshotControl(targetDatabase, snapshotTypes);
}
if (snapshotListener != null) {
snapshotControl.setSnapshotListener(snapshotListener);
}
ObjectQuotingStrategy originalStrategy = targetDatabase.getObjectQuotingStrategy();
try {
targetDatabase.setObjectQuotingStrategy(ObjectQuotingStrategy.QUOTE_ALL_OBJECTS);
return SnapshotGeneratorFactory.getInstance().createSnapshot(schemas, targetDatabase, snapshotControl);
} finally {
targetDatabase.setObjectQuotingStrategy(originalStrategy);
}
}
use of liquibase.structure.DatabaseObject in project liquibase by liquibase.
the class StandardLockService method destroy.
@Override
public void destroy() throws DatabaseException {
try {
//
// This code now uses the ChangeGeneratorFactory to
// allow extension code to be called in order to
// delete the changelog lock table.
//
// To implement the extension, you will need to override:
// DropTableStatement
// DropTableChange
// DropTableGenerator
//
//
DatabaseObject example = new Table().setName(database.getDatabaseChangeLogLockTableName()).setSchema(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName());
if (SnapshotGeneratorFactory.getInstance().has(example, database)) {
DatabaseObject table = SnapshotGeneratorFactory.getInstance().createSnapshot(example, database);
DiffOutputControl diffOutputControl = new DiffOutputControl(true, true, false, null);
Change[] change = ChangeGeneratorFactory.getInstance().fixUnexpected(table, diffOutputControl, database, database);
SqlStatement[] sqlStatement = change[0].generateStatements(database);
Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", database).execute(sqlStatement[0]);
}
reset();
} catch (InvalidExampleException e) {
throw new UnexpectedLiquibaseException(e);
}
}
Aggregations