use of liquibase.exception.UnexpectedLiquibaseException in project liquibase by liquibase.
the class ChangeParameterMetaData method setValue.
/**
* Sets the value of this parameter on the given change.
*/
public void setValue(Change change, Object value) {
if (value instanceof String && !dataType.equals("string")) {
try {
if (dataType.equals("bigInteger")) {
value = new BigInteger((String) value);
} else if (dataType.equals("databaseFunction")) {
value = new DatabaseFunction((String) value);
} else {
throw new UnexpectedLiquibaseException("Unknown Data Type: " + dataType);
}
} catch (Throwable e) {
throw new UnexpectedLiquibaseException("Cannot convert string value '" + value + "' to " + dataType + ": " + e.getMessage());
}
}
try {
for (PropertyDescriptor descriptor : PropertyUtils.getInstance().getDescriptors(change.getClass())) {
if (descriptor.getDisplayName().equals(this.parameterName)) {
Method writeMethod = descriptor.getWriteMethod();
if (writeMethod == null) {
throw new UnexpectedLiquibaseException("Could not find writeMethod for " + this.parameterName);
}
Class<?> expectedWriteType = writeMethod.getParameterTypes()[0];
if (value != null && !expectedWriteType.isAssignableFrom(value.getClass())) {
if (expectedWriteType.equals(String.class)) {
value = value.toString();
} else {
throw new UnexpectedLiquibaseException("Could not convert " + value.getClass().getName() + " to " + expectedWriteType.getName());
}
}
writeMethod.invoke(change, value);
}
}
} catch (Exception e) {
throw new UnexpectedLiquibaseException("Error setting " + this.parameterName + " to " + value, e);
}
}
use of liquibase.exception.UnexpectedLiquibaseException in project liquibase by liquibase.
the class Liquibase method generateChangeLog.
public void generateChangeLog(CatalogAndSchema catalogAndSchema, DiffToChangeLog changeLogWriter, PrintStream outputStream, ChangeLogSerializer changeLogSerializer, Class<? extends DatabaseObject>... snapshotTypes) throws DatabaseException, IOException, ParserConfigurationException {
Set<Class<? extends DatabaseObject>> finalCompareTypes = null;
if (snapshotTypes != null && snapshotTypes.length > 0) {
finalCompareTypes = new HashSet<Class<? extends DatabaseObject>>(Arrays.asList(snapshotTypes));
}
SnapshotControl snapshotControl = new SnapshotControl(this.getDatabase(), snapshotTypes);
CompareControl compareControl = new CompareControl(new CompareControl.SchemaComparison[] { new CompareControl.SchemaComparison(catalogAndSchema, catalogAndSchema) }, finalCompareTypes);
// compareControl.addStatusListener(new OutDiffStatusListener());
DatabaseSnapshot originalDatabaseSnapshot = null;
try {
originalDatabaseSnapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(compareControl.getSchemas(CompareControl.DatabaseRole.REFERENCE), getDatabase(), snapshotControl);
DiffResult diffResult = DiffGeneratorFactory.getInstance().compare(originalDatabaseSnapshot, SnapshotGeneratorFactory.getInstance().createSnapshot(compareControl.getSchemas(CompareControl.DatabaseRole.REFERENCE), null, snapshotControl), compareControl);
changeLogWriter.setDiffResult(diffResult);
if (changeLogSerializer != null) {
changeLogWriter.print(outputStream, changeLogSerializer);
} else {
changeLogWriter.print(outputStream);
}
} catch (InvalidExampleException e) {
throw new UnexpectedLiquibaseException(e);
}
}
use of liquibase.exception.UnexpectedLiquibaseException in project liquibase by liquibase.
the class DiffGeneratorFactory method compare.
public DiffResult compare(DatabaseSnapshot referenceSnapshot, DatabaseSnapshot comparisonSnapshot, CompareControl compareControl) throws DatabaseException {
Database referenceDatabase = referenceSnapshot.getDatabase();
Database comparisonDatabase;
if (comparisonSnapshot == null) {
comparisonDatabase = referenceSnapshot.getDatabase();
try {
comparisonSnapshot = new EmptyDatabaseSnapshot(referenceDatabase, referenceSnapshot.getSnapshotControl());
} catch (InvalidExampleException e) {
throw new UnexpectedLiquibaseException(e);
}
} else {
comparisonDatabase = comparisonSnapshot.getDatabase();
}
return getGenerator(referenceDatabase, comparisonDatabase).compare(referenceSnapshot, comparisonSnapshot, compareControl);
}
use of liquibase.exception.UnexpectedLiquibaseException in project liquibase by liquibase.
the class MissingDataChangeGenerator method fixMissing.
@Override
public Change[] fixMissing(DatabaseObject missingObject, DiffOutputControl outputControl, Database referenceDatabase, Database comparisionDatabase, ChangeGeneratorChain chain) {
Statement stmt = null;
ResultSet rs = null;
try {
Data data = (Data) missingObject;
Table table = data.getTable();
if (referenceDatabase.isLiquibaseObject(table)) {
return null;
}
String sql = "SELECT * FROM " + referenceDatabase.escapeTableName(table.getSchema().getCatalogName(), table.getSchema().getName(), table.getName());
stmt = ((JdbcConnection) referenceDatabase.getConnection()).createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
stmt.setFetchSize(1000);
rs = stmt.executeQuery(sql);
List<String> columnNames = new ArrayList<String>();
for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
columnNames.add(rs.getMetaData().getColumnName(i + 1));
}
List<Change> changes = new ArrayList<Change>();
while (rs.next()) {
InsertDataChange change = new InsertDataChange();
if (outputControl.getIncludeCatalog()) {
change.setCatalogName(table.getSchema().getCatalogName());
}
if (outputControl.getIncludeSchema()) {
change.setSchemaName(table.getSchema().getName());
}
change.setTableName(table.getName());
// loop over all columns for this row
for (int i = 0; i < columnNames.size(); i++) {
ColumnConfig column = new ColumnConfig();
column.setName(columnNames.get(i));
Object value = JdbcUtils.getResultSetValue(rs, i + 1);
if (value == null) {
column.setValue(null);
} else if (value instanceof Number) {
column.setValueNumeric((Number) value);
} else if (value instanceof Boolean) {
column.setValueBoolean((Boolean) value);
} else if (value instanceof Date) {
column.setValueDate((Date) value);
} else if (value instanceof byte[]) {
if (referenceDatabase instanceof InformixDatabase) {
column.setValue(new String((byte[]) value, LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding()));
}
column.setValueComputed(new DatabaseFunction("UNSUPPORTED FOR DIFF: BINARY DATA"));
} else {
// fall back to simple string
column.setValue(value.toString().replace("\\", "\\\\"));
}
change.addColumn(column);
}
// for each row, add a new change
// (there will be one group per table)
changes.add(change);
}
return changes.toArray(new Change[changes.size()]);
} catch (Exception e) {
throw new UnexpectedLiquibaseException(e);
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException ignore) {
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException ignore) {
}
}
}
}
use of liquibase.exception.UnexpectedLiquibaseException in project liquibase by liquibase.
the class MissingDataExternalFileChangeGenerator method fixMissing.
@Override
public Change[] fixMissing(DatabaseObject missingObject, DiffOutputControl outputControl, Database referenceDatabase, Database comparisionDatabase, ChangeGeneratorChain chain) {
Statement stmt = null;
ResultSet rs = null;
try {
Data data = (Data) missingObject;
Table table = data.getTable();
if (referenceDatabase.isLiquibaseObject(table)) {
return null;
}
String sql = "SELECT * FROM " + referenceDatabase.escapeTableName(table.getSchema().getCatalogName(), table.getSchema().getName(), table.getName());
stmt = ((JdbcConnection) referenceDatabase.getConnection()).createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
stmt.setFetchSize(100);
rs = stmt.executeQuery(sql);
List<String> columnNames = new ArrayList<String>();
for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
columnNames.add(rs.getMetaData().getColumnName(i + 1));
}
String fileName = table.getName().toLowerCase() + ".csv";
if (dataDir != null) {
fileName = dataDir + "/" + fileName;
File parentDir = new File(dataDir);
if (!parentDir.exists()) {
parentDir.mkdirs();
}
if (!parentDir.isDirectory()) {
throw new RuntimeException(parentDir + " is not a directory");
}
}
CSVWriter outputFile = new CSVWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding())));
String[] dataTypes = new String[columnNames.size()];
String[] line = new String[columnNames.size()];
for (int i = 0; i < columnNames.size(); i++) {
line[i] = columnNames.get(i);
}
outputFile.writeNext(line);
int rowNum = 0;
while (rs.next()) {
line = new String[columnNames.size()];
for (int i = 0; i < columnNames.size(); i++) {
Object value = JdbcUtils.getResultSetValue(rs, i + 1);
if (dataTypes[i] == null && value != null) {
if (value instanceof Number) {
dataTypes[i] = "NUMERIC";
} else if (value instanceof Boolean) {
dataTypes[i] = "BOOLEAN";
} else if (value instanceof Date) {
dataTypes[i] = "DATE";
} else {
dataTypes[i] = "STRING";
}
}
if (value == null) {
line[i] = "NULL";
} else {
if (value instanceof Date) {
line[i] = new ISODateFormat().format(((Date) value));
} else {
line[i] = value.toString();
}
}
}
outputFile.writeNext(line);
rowNum++;
if (rowNum % 5000 == 0) {
outputFile.flush();
}
}
outputFile.flush();
outputFile.close();
LoadDataChange change = new LoadDataChange();
change.setFile(fileName);
change.setEncoding(LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding());
if (outputControl.getIncludeCatalog()) {
change.setCatalogName(table.getSchema().getCatalogName());
}
if (outputControl.getIncludeSchema()) {
change.setSchemaName(table.getSchema().getName());
}
change.setTableName(table.getName());
for (int i = 0; i < columnNames.size(); i++) {
String colName = columnNames.get(i);
LoadDataColumnConfig columnConfig = new LoadDataColumnConfig();
columnConfig.setHeader(colName);
columnConfig.setName(colName);
columnConfig.setType(dataTypes[i]);
change.addColumn(columnConfig);
}
return new Change[] { change };
} catch (Exception e) {
throw new UnexpectedLiquibaseException(e);
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException ignore) {
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException ignore) {
}
}
}
}
Aggregations