use of liquibase.exception.UnexpectedLiquibaseException in project liquibase by liquibase.
the class DatabaseSnapshot method include.
/**
* Include the object described by the passed example object in this snapshot. Returns the object snapshot or null if the object does not exist in the database.
* If the same object was returned by an earlier include() call, the same object instance will be returned.
*/
protected <T extends DatabaseObject> T include(T example) throws DatabaseException, InvalidExampleException {
if (example == null) {
return null;
}
if (database.isSystemObject(example)) {
return null;
}
if (example instanceof Schema && example.getName() == null && (((Schema) example).getCatalog() == null || ((Schema) example).getCatalogName() == null)) {
CatalogAndSchema catalogAndSchema = ((Schema) example).toCatalogAndSchema().customize(database);
example = (T) new Schema(catalogAndSchema.getCatalogName(), catalogAndSchema.getSchemaName());
}
if (!snapshotControl.shouldInclude(example.getClass())) {
return example;
}
T existing = get(example);
if (existing != null) {
return existing;
}
if (isKnownNull(example)) {
return null;
}
SnapshotListener snapshotListener = snapshotControl.getSnapshotListener();
SnapshotGeneratorChain chain = createGeneratorChain(example.getClass(), database);
if (snapshotListener != null) {
snapshotListener.willSnapshot(example, database);
}
T object = chain.snapshot(example, this);
if (object == null) {
Set<DatabaseObject> collection = knownNull.get(example.getClass());
if (collection == null) {
collection = new HashSet<DatabaseObject>();
knownNull.put(example.getClass(), collection);
}
collection.add(example);
if (example instanceof Schema) {
LogFactory.getInstance().getLog().warning("Did not find schema '" + example + "' to snapshot");
}
if (example instanceof Catalog) {
LogFactory.getInstance().getLog().warning("Did not find catalog '" + example + "' to snapshot");
}
} else {
allFound.add(object);
try {
includeNestedObjects(object);
} catch (InstantiationException e) {
throw new UnexpectedLiquibaseException(e);
} catch (IllegalAccessException e) {
throw new UnexpectedLiquibaseException(e);
}
}
if (snapshotListener != null) {
snapshotListener.finishedSnapshot(example, object, database);
}
return object;
}
use of liquibase.exception.UnexpectedLiquibaseException in project liquibase by liquibase.
the class ReflectionSerializer method getValue.
public Object getValue(Object object, String field) {
if (!reflectionCache.containsKey(object.getClass())) {
//fills cache
getFields(object);
}
Map<String, Field> fieldsByName = reflectionCache.get(object.getClass());
Field foundField = fieldsByName.get(field);
try {
if (foundField == null) {
foundField = findField(object, field);
foundField.setAccessible(true);
fieldsByName.put(field, foundField);
}
return foundField.get(object);
} catch (Exception e) {
throw new UnexpectedLiquibaseException(e);
}
}
use of liquibase.exception.UnexpectedLiquibaseException in project liquibase by liquibase.
the class ReflectionSerializer method getDataTypeClass.
public Class getDataTypeClass(Object object, String field) {
try {
Field foundField = findField(object, field);
Type dataType = foundField.getGenericType();
if (dataType instanceof Class) {
return (Class) dataType;
}
if (dataType instanceof ParameterizedType) {
return (Class) ((ParameterizedType) dataType).getRawType();
}
return Object.class;
} catch (Exception e) {
throw new UnexpectedLiquibaseException(e);
}
}
use of liquibase.exception.UnexpectedLiquibaseException in project liquibase by liquibase.
the class FormattedSqlChangeLogSerializer method getTargetDatabase.
protected Database getTargetDatabase(ChangeSet changeSet) {
String filePath = changeSet.getFilePath();
if (filePath == null) {
throw new UnexpectedLiquibaseException("You must specify the changelog file name as filename.DB_TYPE.sql. Example: changelog.mysql.sql");
}
Matcher matcher = fileNamePatter.matcher(filePath);
if (!matcher.matches()) {
throw new UnexpectedLiquibaseException("Serializing changelog as sql requires a file name in the format *.databaseType.sql. Example: changelog.h2.sql. Passed: " + filePath);
}
String shortName = matcher.replaceFirst("$1");
Database database = DatabaseFactory.getInstance().getDatabase(shortName);
if (database == null) {
throw new UnexpectedLiquibaseException("Serializing changelog as sql requires a file name in the format *.databaseType.sql. Example: changelog.h2.sql. Unknown databaes type: " + shortName);
}
return database;
}
use of liquibase.exception.UnexpectedLiquibaseException in project liquibase by liquibase.
the class FormattedSqlChangeLogSerializer method serialize.
@Override
public String serialize(LiquibaseSerializable object, boolean pretty) {
if (object instanceof ChangeSet) {
StringBuilder builder = new StringBuilder();
ChangeSet changeSet = (ChangeSet) object;
Database database = getTargetDatabase(changeSet);
String author = (changeSet.getAuthor()).replaceAll("\\s+", "_");
author = author.replace("_(generated)", "");
builder.append("--changeset ").append(author).append(":").append(changeSet.getId()).append("\n");
for (Change change : changeSet.getChanges()) {
Sql[] sqls = SqlGeneratorFactory.getInstance().generateSql(change.generateStatements(database), database);
if (sqls != null) {
for (Sql sql : sqls) {
builder.append(sql.toSql()).append(sql.getEndDelimiter()).append("\n");
}
}
}
return builder.toString();
} else {
throw new UnexpectedLiquibaseException("Cannot serialize object type: " + object.getClass().getName());
}
}
Aggregations