use of liquibase.snapshot.DatabaseSnapshot in project liquibase by liquibase.
the class AbstractIntegrationTest method testOutputChangeLog.
@Test
public void testOutputChangeLog() throws Exception {
if (database == null) {
return;
}
StringWriter output = new StringWriter();
Liquibase liquibase = createLiquibase(completeChangeLog);
clearDatabase(liquibase);
liquibase = createLiquibase(completeChangeLog);
liquibase.update(this.contexts, output);
String outputResult = output.getBuffer().toString();
assertNotNull(outputResult);
//should be pretty big
assertTrue(outputResult.length() > 100);
System.out.println(outputResult);
assertTrue("create databasechangelog command not found in: \n" + outputResult, outputResult.contains("CREATE TABLE " + database.escapeTableName(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogTableName())));
assertTrue("create databasechangeloglock command not found in: \n" + outputResult, outputResult.contains("CREATE TABLE " + database.escapeTableName(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogLockTableName())));
assertTrue(outputResult.contains("€"));
assertTrue(outputResult.contains("€"));
DatabaseSnapshot snapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(database.getDefaultSchema(), database, new SnapshotControl(database));
assertEquals(0, snapshot.get(Schema.class).iterator().next().getDatabaseObjects(Table.class).size());
}
use of liquibase.snapshot.DatabaseSnapshot 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.DatabaseSnapshot 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.DatabaseSnapshot 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);
}
}
use of liquibase.snapshot.DatabaseSnapshot in project liquibase by liquibase.
the class YamlSnapshotParser method parse.
@Override
public DatabaseSnapshot parse(String path, ResourceAccessor resourceAccessor) throws LiquibaseParseException {
Yaml yaml = new Yaml();
try {
InputStream stream = StreamUtil.singleInputStream(path, resourceAccessor);
if (stream == null) {
throw new LiquibaseParseException(path + " does not exist");
}
Map parsedYaml;
try {
parsedYaml = yaml.loadAs(new InputStreamReader(stream, LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding()), Map.class);
} catch (Exception e) {
throw new LiquibaseParseException("Syntax error in " + getSupportedFileExtensions()[0] + ": " + e.getMessage(), e);
} finally {
try {
stream.close();
} catch (IOException ioe) {
}
}
Map rootList = (Map) parsedYaml.get("snapshot");
if (rootList == null) {
throw new LiquibaseParseException("Could not find root snapshot node");
}
String shortName = (String) ((Map) rootList.get("database")).get("shortName");
Database database = DatabaseFactory.getInstance().getDatabase(shortName).getClass().newInstance();
DatabaseSnapshot snapshot = new RestoredDatabaseSnapshot(database);
ParsedNode snapshotNode = new ParsedNode(null, "snapshot");
snapshotNode.setValue(rootList);
Map metadata = (Map) rootList.get("metadata");
if (metadata != null) {
snapshot.getMetadata().putAll(metadata);
}
snapshot.load(snapshotNode, resourceAccessor);
return snapshot;
} catch (Throwable e) {
if (e instanceof LiquibaseParseException) {
throw (LiquibaseParseException) e;
}
throw new LiquibaseParseException(e);
}
}
Aggregations