use of liquibase.snapshot.SnapshotControl in project dropwizard by dropwizard.
the class DbDumpCommand method generateChangeLog.
private void generateChangeLog(final Database database, final CatalogAndSchema catalogAndSchema, final DiffToChangeLog changeLogWriter, PrintStream outputStream, final Set<Class<? extends DatabaseObject>> compareTypes) throws DatabaseException, IOException, ParserConfigurationException {
@SuppressWarnings({ "unchecked", "rawtypes" }) final SnapshotControl snapshotControl = new SnapshotControl(database, compareTypes.toArray(new Class[compareTypes.size()]));
final CompareControl compareControl = new CompareControl(new CompareControl.SchemaComparison[] { new CompareControl.SchemaComparison(catalogAndSchema, catalogAndSchema) }, compareTypes);
final CatalogAndSchema[] compareControlSchemas = compareControl.getSchemas(CompareControl.DatabaseRole.REFERENCE);
try {
final DatabaseSnapshot referenceSnapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(compareControlSchemas, database, snapshotControl);
final DatabaseSnapshot comparisonSnapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(compareControlSchemas, null, snapshotControl);
final DiffResult diffResult = DiffGeneratorFactory.getInstance().compare(referenceSnapshot, comparisonSnapshot, compareControl);
changeLogWriter.setDiffResult(diffResult);
changeLogWriter.print(outputStream);
} catch (InvalidExampleException e) {
throw new UnexpectedLiquibaseException(e);
}
}
use of liquibase.snapshot.SnapshotControl in project liquibase by liquibase.
the class StandardChangeLogHistoryService method init.
public void init() throws DatabaseException {
if (serviceInitialized) {
return;
}
Database database = getDatabase();
Executor executor = ExecutorService.getInstance().getExecutor(database);
Table changeLogTable = null;
try {
changeLogTable = SnapshotGeneratorFactory.getInstance().getDatabaseChangeLogTable(new SnapshotControl(database, false, Table.class, Column.class), database);
} catch (LiquibaseException e) {
throw new UnexpectedLiquibaseException(e);
}
List<SqlStatement> statementsToExecute = new ArrayList<SqlStatement>();
boolean changeLogCreateAttempted = false;
if (changeLogTable != null) {
boolean hasDescription = changeLogTable.getColumn("DESCRIPTION") != null;
boolean hasComments = changeLogTable.getColumn("COMMENTS") != null;
boolean hasTag = changeLogTable.getColumn("TAG") != null;
boolean hasLiquibase = changeLogTable.getColumn("LIQUIBASE") != null;
boolean hasContexts = changeLogTable.getColumn("CONTEXTS") != null;
boolean hasLabels = changeLogTable.getColumn("LABELS") != null;
boolean liquibaseColumnNotRightSize = false;
if (!(this.getDatabase() instanceof SQLiteDatabase)) {
DataType type = changeLogTable.getColumn("LIQUIBASE").getType();
if (type.getTypeName().toLowerCase().startsWith("varchar")) {
Integer columnSize = type.getColumnSize();
liquibaseColumnNotRightSize = columnSize != null && columnSize < 20;
} else {
liquibaseColumnNotRightSize = false;
}
}
boolean hasOrderExecuted = changeLogTable.getColumn("ORDEREXECUTED") != null;
boolean checksumNotRightSize = false;
if (!(this.getDatabase() instanceof SQLiteDatabase)) {
DataType type = changeLogTable.getColumn("MD5SUM").getType();
if (type.getTypeName().toLowerCase().startsWith("varchar")) {
Integer columnSize = type.getColumnSize();
checksumNotRightSize = columnSize != null && columnSize < 35;
} else {
liquibaseColumnNotRightSize = false;
}
}
boolean hasExecTypeColumn = changeLogTable.getColumn("EXECTYPE") != null;
String charTypeName = getCharTypeName();
boolean hasDeploymentIdColumn = changeLogTable.getColumn("DEPLOYMENT_ID") != null;
if (!hasDescription) {
executor.comment("Adding missing databasechangelog.description column");
statementsToExecute.add(new AddColumnStatement(getLiquibaseCatalogName(), getLiquibaseSchemaName(), getDatabaseChangeLogTableName(), "DESCRIPTION", charTypeName + "(255)", null));
}
if (!hasTag) {
executor.comment("Adding missing databasechangelog.tag column");
statementsToExecute.add(new AddColumnStatement(getLiquibaseCatalogName(), getLiquibaseSchemaName(), getDatabaseChangeLogTableName(), "TAG", charTypeName + "(255)", null));
}
if (!hasComments) {
executor.comment("Adding missing databasechangelog.comments column");
statementsToExecute.add(new AddColumnStatement(getLiquibaseCatalogName(), getLiquibaseSchemaName(), getDatabaseChangeLogTableName(), "COMMENTS", charTypeName + "(255)", null));
}
if (!hasLiquibase) {
executor.comment("Adding missing databasechangelog.liquibase column");
statementsToExecute.add(new AddColumnStatement(getLiquibaseCatalogName(), getLiquibaseSchemaName(), getDatabaseChangeLogTableName(), "LIQUIBASE", charTypeName + "(20)", null));
}
if (!hasOrderExecuted) {
executor.comment("Adding missing databasechangelog.orderexecuted column");
statementsToExecute.add(new AddColumnStatement(getLiquibaseCatalogName(), getLiquibaseSchemaName(), getDatabaseChangeLogTableName(), "ORDEREXECUTED", "int", null));
statementsToExecute.add(new UpdateStatement(getLiquibaseCatalogName(), getLiquibaseSchemaName(), getDatabaseChangeLogTableName()).addNewColumnValue("ORDEREXECUTED", -1));
statementsToExecute.add(new SetNullableStatement(getLiquibaseCatalogName(), getLiquibaseSchemaName(), getDatabaseChangeLogTableName(), "ORDEREXECUTED", "int", false));
}
if (checksumNotRightSize) {
executor.comment("Modifying size of databasechangelog.md5sum column");
statementsToExecute.add(new ModifyDataTypeStatement(getLiquibaseCatalogName(), getLiquibaseSchemaName(), getDatabaseChangeLogTableName(), "MD5SUM", charTypeName + "(35)"));
}
if (liquibaseColumnNotRightSize) {
executor.comment("Modifying size of databasechangelog.liquibase column");
statementsToExecute.add(new ModifyDataTypeStatement(getLiquibaseCatalogName(), getLiquibaseSchemaName(), getDatabaseChangeLogTableName(), "LIQUIBASE", charTypeName + "(20)"));
}
if (!hasExecTypeColumn) {
executor.comment("Adding missing databasechangelog.exectype column");
statementsToExecute.add(new AddColumnStatement(getLiquibaseCatalogName(), getLiquibaseSchemaName(), getDatabaseChangeLogTableName(), "EXECTYPE", charTypeName + "(10)", null));
statementsToExecute.add(new UpdateStatement(getLiquibaseCatalogName(), getLiquibaseSchemaName(), getDatabaseChangeLogTableName()).addNewColumnValue("EXECTYPE", "EXECUTED"));
statementsToExecute.add(new SetNullableStatement(getLiquibaseCatalogName(), getLiquibaseSchemaName(), getDatabaseChangeLogTableName(), "EXECTYPE", charTypeName + "(10)", false));
}
if (hasContexts) {
Integer columnSize = changeLogTable.getColumn("CONTEXTS").getType().getColumnSize();
if (columnSize != null && columnSize < Integer.parseInt(getContextsSize())) {
executor.comment("Modifying size of databasechangelog.contexts column");
statementsToExecute.add(new ModifyDataTypeStatement(getLiquibaseCatalogName(), getLiquibaseSchemaName(), getDatabaseChangeLogTableName(), "CONTEXTS", charTypeName + "(" + getContextsSize() + ")"));
}
} else {
executor.comment("Adding missing databasechangelog.contexts column");
statementsToExecute.add(new AddColumnStatement(getLiquibaseCatalogName(), getLiquibaseSchemaName(), getDatabaseChangeLogTableName(), "CONTEXTS", charTypeName + "(" + getContextsSize() + ")", null));
}
if (hasLabels) {
Integer columnSize = changeLogTable.getColumn("LABELS").getType().getColumnSize();
if (columnSize != null && columnSize < Integer.parseInt(getLabelsSize())) {
executor.comment("Modifying size of databasechangelog.labels column");
statementsToExecute.add(new ModifyDataTypeStatement(getLiquibaseCatalogName(), getLiquibaseSchemaName(), getDatabaseChangeLogTableName(), "LABELS", charTypeName + "(" + getLabelsSize() + ")"));
}
} else {
executor.comment("Adding missing databasechangelog.labels column");
statementsToExecute.add(new AddColumnStatement(getLiquibaseCatalogName(), getLiquibaseSchemaName(), getDatabaseChangeLogTableName(), "LABELS", charTypeName + "(" + getLabelsSize() + ")", null));
}
if (!hasDeploymentIdColumn) {
executor.comment("Adding missing databasechangelog.deployment_id column");
statementsToExecute.add(new AddColumnStatement(getLiquibaseCatalogName(), getLiquibaseSchemaName(), getDatabaseChangeLogTableName(), "DEPLOYMENT_ID", "VARCHAR(10)", null));
if (database instanceof DB2Database) {
statementsToExecute.add(new ReorganizeTableStatement(getLiquibaseCatalogName(), getLiquibaseSchemaName(), getDatabaseChangeLogTableName()));
}
}
List<Map<String, ?>> md5sumRS = ExecutorService.getInstance().getExecutor(database).queryForList(new SelectFromDatabaseChangeLogStatement(new SelectFromDatabaseChangeLogStatement.ByNotNullCheckSum(), new ColumnConfig().setName("MD5SUM")).setLimit(1));
if (md5sumRS.size() > 0) {
String md5sum = md5sumRS.get(0).get("MD5SUM").toString();
if (!md5sum.startsWith(CheckSum.getCurrentVersion() + ":")) {
executor.comment("DatabaseChangeLog checksums are an incompatible version. Setting them to null so they will be updated on next database update");
databaseChecksumsCompatible = false;
statementsToExecute.add(new RawSqlStatement("UPDATE " + getDatabase().escapeTableName(getLiquibaseCatalogName(), getLiquibaseSchemaName(), getDatabaseChangeLogTableName()) + " " + "SET " + getDatabase().escapeObjectName("MD5SUM", Column.class) + " = NULL"));
}
}
} else if (!changeLogCreateAttempted) {
executor.comment("Create Database Change Log Table");
SqlStatement createTableStatement = new CreateDatabaseChangeLogTableStatement();
if (!canCreateChangeLogTable()) {
throw new DatabaseException("Cannot create " + getDatabase().escapeTableName(getLiquibaseCatalogName(), getLiquibaseSchemaName(), getDatabaseChangeLogTableName()) + " table for your getDatabase().\n\n" + "Please construct it manually using the following SQL as a base and re-run Liquibase:\n\n" + createTableStatement);
}
// If there is no table in the database for recording change history create one.
statementsToExecute.add(createTableStatement);
LogFactory.getLogger().info("Creating database history table with name: " + getDatabase().escapeTableName(getLiquibaseCatalogName(), getLiquibaseSchemaName(), getDatabaseChangeLogTableName()));
}
for (SqlStatement sql : statementsToExecute) {
if (SqlGeneratorFactory.getInstance().supports(sql, database)) {
executor.execute(sql);
getDatabase().commit();
} else {
LogFactory.getLogger().info("Cannot run " + sql.getClass().getSimpleName() + " on " + getDatabase().getShortName() + " when checking databasechangelog table");
}
}
serviceInitialized = true;
}
use of liquibase.snapshot.SnapshotControl in project liquibase by liquibase.
the class DBDocVisitor method writeHTML.
public void writeHTML(File rootOutputDir, ResourceAccessor resourceAccessor) throws IOException, LiquibaseException, DatabaseHistoryException {
ChangeLogWriter changeLogWriter = new ChangeLogWriter(resourceAccessor, rootOutputDir);
HTMLWriter authorWriter = new AuthorWriter(rootOutputDir, database);
HTMLWriter tableWriter = new TableWriter(rootOutputDir, database);
HTMLWriter columnWriter = new ColumnWriter(rootOutputDir, database);
HTMLWriter pendingChangesWriter = new PendingChangesWriter(rootOutputDir, database);
HTMLWriter recentChangesWriter = new RecentChangesWriter(rootOutputDir, database);
HTMLWriter pendingSQLWriter = new PendingSQLWriter(rootOutputDir, database, rootChangeLog);
copyFile("liquibase/dbdoc/stylesheet.css", rootOutputDir);
copyFile("liquibase/dbdoc/index.html", rootOutputDir);
copyFile("liquibase/dbdoc/globalnav.html", rootOutputDir);
copyFile("liquibase/dbdoc/overview-summary.html", rootOutputDir);
DatabaseSnapshot snapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(database.getDefaultSchema(), database, new SnapshotControl(database));
new ChangeLogListWriter(rootOutputDir).writeHTML(changeLogs);
SortedSet<Table> tables = new TreeSet<Table>(snapshot.get(Table.class));
Iterator<Table> tableIterator = tables.iterator();
while (tableIterator.hasNext()) {
if (database.isLiquibaseObject(tableIterator.next())) {
tableIterator.remove();
}
}
new TableListWriter(rootOutputDir).writeHTML(tables);
new AuthorListWriter(rootOutputDir).writeHTML(new TreeSet<Object>(changesByAuthor.keySet()));
for (String author : changesByAuthor.keySet()) {
authorWriter.writeHTML(author, changesByAuthor.get(author), changesToRunByAuthor.get(author), rootChangeLogName);
}
for (Table table : tables) {
if (database.isLiquibaseObject(table)) {
continue;
}
tableWriter.writeHTML(table, changesByObject.get(table), changesToRunByObject.get(table), rootChangeLogName);
}
for (Column column : snapshot.get(Column.class)) {
if (database.isLiquibaseObject(column.getRelation())) {
continue;
}
columnWriter.writeHTML(column, changesByObject.get(column), changesToRunByObject.get(column), rootChangeLogName);
}
for (ChangeLogInfo changeLog : changeLogs) {
changeLogWriter.writeChangeLog(changeLog.logicalPath, changeLog.physicalPath);
}
pendingChangesWriter.writeHTML("index", null, changesToRun, rootChangeLogName);
pendingSQLWriter.writeHTML("sql", null, changesToRun, rootChangeLogName);
if (recentChanges.size() > MAX_RECENT_CHANGE) {
recentChanges = recentChanges.subList(0, MAX_RECENT_CHANGE);
}
recentChangesWriter.writeHTML("index", recentChanges, null, rootChangeLogName);
}
use of liquibase.snapshot.SnapshotControl in project liquibase by liquibase.
the class SQLiteDatabase method getAlterTableStatements.
public static List<SqlStatement> getAlterTableStatements(AlterTableVisitor alterTableVisitor, Database database, String catalogName, String schemaName, String tableName) throws DatabaseException {
List<SqlStatement> statements = new ArrayList<SqlStatement>();
Table table = null;
try {
table = SnapshotGeneratorFactory.getInstance().createSnapshot((Table) new Table().setName(tableName).setSchema(new Schema(new Catalog(null), null)), database);
List<ColumnConfig> createColumns = new ArrayList<ColumnConfig>();
List<ColumnConfig> copyColumns = new ArrayList<ColumnConfig>();
if (table != null) {
for (Column column : table.getColumns()) {
ColumnConfig new_column = new ColumnConfig(column);
if (alterTableVisitor.createThisColumn(new_column)) {
createColumns.add(new_column);
}
ColumnConfig copy_column = new ColumnConfig(column);
if (alterTableVisitor.copyThisColumn(copy_column)) {
copyColumns.add(copy_column);
}
}
}
for (ColumnConfig column : alterTableVisitor.getColumnsToAdd()) {
if (alterTableVisitor.createThisColumn(column)) {
createColumns.add(column);
}
if (alterTableVisitor.copyThisColumn(column)) {
copyColumns.add(column);
}
}
List<Index> newIndices = new ArrayList<Index>();
for (Index index : SnapshotGeneratorFactory.getInstance().createSnapshot(new CatalogAndSchema(catalogName, schemaName), database, new SnapshotControl(database, Index.class)).get(Index.class)) {
if (index.getTable().getName().equalsIgnoreCase(tableName)) {
if (alterTableVisitor.createThisIndex(index)) {
newIndices.add(index);
}
}
}
// rename table
String temp_table_name = tableName + "_temporary";
statements.addAll(Arrays.asList(new RenameTableStatement(catalogName, schemaName, tableName, temp_table_name)));
// create temporary table
CreateTableChange ct_change_tmp = new CreateTableChange();
ct_change_tmp.setSchemaName(schemaName);
ct_change_tmp.setTableName(tableName);
for (ColumnConfig column : createColumns) {
ct_change_tmp.addColumn(column);
}
statements.addAll(Arrays.asList(ct_change_tmp.generateStatements(database)));
// copy rows to temporary table
statements.addAll(Arrays.asList(new CopyRowsStatement(temp_table_name, tableName, copyColumns)));
// delete original table
statements.addAll(Arrays.asList(new DropTableStatement(catalogName, schemaName, temp_table_name, false)));
// validate indices
statements.addAll(Arrays.asList(new ReindexStatement(catalogName, schemaName, tableName)));
// add remaining indices
for (Index index_config : newIndices) {
AddColumnConfig[] columns = new AddColumnConfig[index_config.getColumns().size()];
for (int i = 0; i < index_config.getColumns().size(); i++) {
columns[i] = new AddColumnConfig(index_config.getColumns().get(i));
}
statements.addAll(Arrays.asList(new CreateIndexStatement(index_config.getName(), catalogName, schemaName, tableName, index_config.isUnique(), index_config.getAssociatedWithAsString(), columns)));
}
return statements;
} catch (InvalidExampleException e) {
throw new UnexpectedLiquibaseException(e);
}
}
use of liquibase.snapshot.SnapshotControl in project liquibase by liquibase.
the class StringSnapshotSerializerReadable method serialize.
@Override
public String serialize(LiquibaseSerializable object, boolean pretty) {
try {
StringBuilder buffer = new StringBuilder();
DatabaseSnapshot snapshot = ((DatabaseSnapshot) object);
Database database = snapshot.getDatabase();
buffer.append("Database snapshot for ").append(database.getConnection().getURL()).append("\n");
addDivider(buffer);
buffer.append("Database type: ").append(database.getDatabaseProductName()).append("\n");
buffer.append("Database version: ").append(database.getDatabaseProductVersion()).append("\n");
buffer.append("Database user: ").append(database.getConnection().getConnectionUserName()).append("\n");
SnapshotControl snapshotControl = snapshot.getSnapshotControl();
List<Class> includedTypes = sort(snapshotControl.getTypesToInclude());
buffer.append("Included types:\n").append(StringUtils.indent(StringUtils.join(includedTypes, "\n", new StringUtils.StringUtilsFormatter<Class>() {
@Override
public String toString(Class obj) {
return obj.getName();
}
}))).append("\n");
List<Schema> schemas = sort(snapshot.get(Schema.class), new Comparator<Schema>() {
@Override
public int compare(Schema o1, Schema o2) {
return o1.toString().compareTo(o2.toString());
}
});
for (Schema schema : schemas) {
if (database.supportsSchemas()) {
buffer.append("\nCatalog & Schema: ").append(schema.getCatalogName()).append(" / ").append(schema.getName()).append("\n");
} else {
buffer.append("\nCatalog: ").append(schema.getCatalogName()).append("\n");
}
StringBuilder catalogBuffer = new StringBuilder();
for (Class type : includedTypes) {
if (type.equals(Schema.class) || type.equals(Catalog.class) || type.equals(Column.class)) {
continue;
}
List<DatabaseObject> objects = new ArrayList<DatabaseObject>(snapshot.get(type));
ListIterator<DatabaseObject> iterator = objects.listIterator();
while (iterator.hasNext()) {
DatabaseObject next = iterator.next();
if (next instanceof DatabaseLevelObject) {
continue;
}
Schema objectSchema = next.getSchema();
if (objectSchema == null) {
if (!(next instanceof CatalogLevelObject) || !((CatalogLevelObject) next).getCatalog().equals(schema.getCatalog())) {
iterator.remove();
}
} else if (!objectSchema.equals(schema)) {
iterator.remove();
}
}
outputObjects(objects, type, catalogBuffer);
}
buffer.append(StringUtils.indent(catalogBuffer.toString(), INDENT_LENGTH));
}
//standardize all newline chars
return buffer.toString().replace("\r\n", "\n").replace("\r", "\n");
} catch (Exception e) {
throw new UnexpectedLiquibaseException(e);
}
}
Aggregations