Permalink
Browse files

CORE-1784 CORE-1784 GenerateChangeLog with objects in multiple schema…

…s returns objects from multiple schemas and empty createTable statements

Only generate changeSets for objects in configured schemas. Always include FK catalog/schema if different than base table
  • Loading branch information...
1 parent 97ee2da commit d172cc784536c96c3e5af0af490638af021a8b0c @nvoxland nvoxland committed Feb 24, 2014
@@ -21,7 +21,10 @@ public String getSchemaName() {
}
public boolean equals(CatalogAndSchema catalogAndSchema, Database database) {
- return this.equals(catalogAndSchema); //todo: use database param or remove method
+ catalogAndSchema = database.correctSchema(catalogAndSchema);
+ CatalogAndSchema thisCatalogAndSchema = database.correctSchema(this);
+
+ return catalogAndSchema.toString().equals(thisCatalogAndSchema.toString());
}
@Override
@@ -51,7 +51,6 @@ public void setSerializerFormat(String serializerFormat) {
@Override
protected Object run() throws Exception {
- System.out.println("Snapshot!");
SnapshotControl snapshotControl = new SnapshotControl(database);
DatabaseSnapshot snapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(catalogs.toArray(new CatalogAndSchema[catalogs.size()]), database, snapshotControl);
@@ -754,7 +754,7 @@ public void dropDatabaseObjects(final CatalogAndSchema schemaToDrop) throws Liqu
final long changeSetStarted = System.currentTimeMillis();
DiffResult diffResult = DiffGeneratorFactory.getInstance().compare(new EmptyDatabaseSnapshot(this), snapshot, new CompareControl(snapshot.getSnapshotControl().getTypesToInclude()));
- List<ChangeSet> changeSets = new DiffToChangeLog(diffResult, new DiffOutputControl(true, true, false)).generateChangeSets();
+ List<ChangeSet> changeSets = new DiffToChangeLog(diffResult, new DiffOutputControl(true, true, false).addIncludedSchema(schemaToDrop)).generateChangeSets();
LogFactory.getLogger().debug(String.format("ChangeSet to Remove Database Objects generated in %d ms.", System.currentTimeMillis() - changeSetStarted));
final boolean reEnableFK = supportsForeignKeyDisable() && disableForeignKeyChecks();
@@ -1,14 +1,22 @@
package liquibase.diff.output;
+import liquibase.CatalogAndSchema;
import liquibase.database.Database;
import liquibase.database.InternalDatabase;
import liquibase.database.core.H2Database;
import liquibase.diff.output.changelog.ChangeGeneratorFactory;
import liquibase.diff.output.changelog.core.MissingDataExternalFileChangeGenerator;
import liquibase.structure.DatabaseObject;
import liquibase.structure.DatabaseObjectCollection;
+import liquibase.structure.core.Schema;
+
+import java.util.HashSet;
+import java.util.Set;
public class DiffOutputControl {
+
+ private Set<CatalogAndSchema> includeSchemas = new HashSet<CatalogAndSchema>();
+
private boolean includeSchema;
private boolean includeCatalog;
private boolean includeTablespace;
@@ -84,7 +92,36 @@ public void setAlreadyHandledChanged(DatabaseObject changedObject) {
}
public boolean alreadyHandledChanged(DatabaseObject changedObject, Database accordingTo) {
- return alreadyHandledChanged.contains(changedObject); }
+ return alreadyHandledChanged.contains(changedObject);
+ }
+
+ public DiffOutputControl addIncludedSchema(Schema schema) {
+ this.includeSchemas.add(schema.toCatalogAndSchema());
+ return this;
+ }
+
+ public DiffOutputControl addIncludedSchema(CatalogAndSchema schema) {
+ this.includeSchemas.add(schema);
+ return this;
+ }
+
+ public boolean shouldOutput(DatabaseObject object, Database accordingTo) {
+ if (includeSchemas.size() > 0) {
+ Schema schema = object.getSchema();
+ if (schema == null) {
+ return true;
+ }
+ CatalogAndSchema objectCatalogAndSchema = schema.toCatalogAndSchema();
+ for (CatalogAndSchema catalogAndSchema : includeSchemas) {
+ if (objectCatalogAndSchema.equals(catalogAndSchema, accordingTo)) {
+ return true;
+ }
+ }
+ return false;
+ } else {
+ return true;
+ }
+ }
private static class DatabaseForHash extends H2Database implements InternalDatabase {
@Override
@@ -88,6 +88,10 @@ private ChangeGeneratorChain createGeneratorChain(Class<? extends ChangeGenerato
}
public Change[] fixMissing(DatabaseObject missingObject, DiffOutputControl control, Database referenceDatabase, Database comparisionDatabase) {
+ if (!control.shouldOutput(missingObject, comparisionDatabase)) {
+ return null;
+ }
+
ChangeGeneratorChain chain = createGeneratorChain(MissingObjectChangeGenerator.class, missingObject.getClass(), referenceDatabase);
if (chain == null) {
return null;
@@ -96,6 +100,9 @@ private ChangeGeneratorChain createGeneratorChain(Class<? extends ChangeGenerato
}
public Change[] fixUnexpected(DatabaseObject unexpectedObject, DiffOutputControl control, Database referenceDatabase, Database comparisionDatabase) {
+ if (!control.shouldOutput(unexpectedObject, comparisionDatabase)) {
+ return null;
+ }
ChangeGeneratorChain chain = createGeneratorChain(UnexpectedObjectChangeGenerator.class, unexpectedObject.getClass(), referenceDatabase);
if (chain == null) {
return null;
@@ -104,6 +111,10 @@ private ChangeGeneratorChain createGeneratorChain(Class<? extends ChangeGenerato
}
public Change[] fixChanged(DatabaseObject changedObject, ObjectDifferences differences, DiffOutputControl control, Database referenceDatabase, Database comparisionDatabase) {
+ if (!control.shouldOutput(changedObject, comparisionDatabase)) {
+ return null;
+ }
+
ChangeGeneratorChain chain = createGeneratorChain(ChangedObjectChangeGenerator.class, changedObject.getClass(), referenceDatabase);
if (chain == null) {
return null;
@@ -40,10 +40,10 @@ public int getPriority(Class<? extends DatabaseObject> objectType, Database data
change.setConstraintName(fk.getName());
change.setReferencedTableName(fk.getPrimaryKeyTable().getName());
- if (control.isIncludeCatalog()) {
+ if (!((ForeignKey) missingObject).getPrimaryKeyTable().getSchema().equals(((ForeignKey) missingObject).getForeignKeyTable().getSchema()) || control.isIncludeCatalog()) {
change.setReferencedTableCatalogName(fk.getPrimaryKeyTable().getSchema().getCatalogName());
}
- if (control.isIncludeSchema()) {
+ if (!((ForeignKey) missingObject).getPrimaryKeyTable().getSchema().equals(((ForeignKey) missingObject).getForeignKeyTable().getSchema()) || control.isIncludeSchema()) {
change.setReferencedTableSchemaName(fk.getPrimaryKeyTable().getSchema().getName());
}
change.setReferencedColumnNames(fk.getPrimaryKeyColumns());
@@ -1,5 +1,6 @@
package liquibase.integration.ant;
+import liquibase.CatalogAndSchema;
import liquibase.database.Database;
import liquibase.diff.DiffResult;
import liquibase.diff.output.DiffOutputControl;
@@ -10,7 +11,7 @@
public class DiffDatabaseToChangeLogTask extends DiffDatabaseTask {
@Override
protected void outputDiff(PrintStream writer, DiffResult diffResult, Database targetDatabase) throws Exception {
- DiffOutputControl diffOutputControl = new DiffOutputControl(getIncludeCatalog(), getIncludeSchema(), getIncludeTablespace());
+ DiffOutputControl diffOutputControl = new DiffOutputControl(getIncludeCatalog(), getIncludeSchema(), getIncludeTablespace()).addIncludedSchema(new CatalogAndSchema(getDefaultCatalogName(), getDefaultSchemaName()));
if (getChangeLogFile() == null) {
new DiffToChangeLog(diffResult, diffOutputControl).print(writer);
} else {
@@ -92,7 +92,7 @@ public void executeWithLiquibaseClassloader() throws BuildException {
DiffResult diffResult = DiffGeneratorFactory.getInstance().compare(referenceSnapshot, null, new CompareControl(new CompareControl.SchemaComparison[] {new CompareControl.SchemaComparison(new CatalogAndSchema(getDefaultCatalogName(), getDefaultSchemaName()), new CatalogAndSchema(getDefaultCatalogName(), getDefaultSchemaName()))}, getDiffTypes() ));
// diff.addStatusListener(new OutDiffStatusListener());
- DiffOutputControl diffOutputConfig = new DiffOutputControl(getIncludeCatalog(), getIncludeSchema(), getIncludeTablespace());
+ DiffOutputControl diffOutputConfig = new DiffOutputControl(getIncludeCatalog(), getIncludeSchema(), getIncludeTablespace()).addIncludedSchema(new CatalogAndSchema(getDefaultCatalogName(), getDefaultSchemaName()));
diffOutputConfig.setDataDir(getDataDir());
if (getChangeLogFile() == null) {
new DiffToChangeLog(diffResult, diffOutputConfig).print(writer);
@@ -539,6 +539,12 @@ protected void printHelp(PrintStream stream) {
stream.println(" --defaultSchemaName=<name> Default database schema to use");
stream.println(" --referenceDefaultCatalogName=<name> Reference database catalog to use");
stream.println(" --referenceDefaultSchemaName=<name> Reference database schema to use");
+ stream.println(" --includeCatalog=<true|false> If true, the catalog will be");
+ stream.println(" included in generated changeSets");
+ stream.println(" Defaults to false");
+ stream.println(" --includeSchema=<true|false> If true, the schema will be");
+ stream.println(" included in generated changeSets");
+ stream.println(" Defaults to false");
stream.println(" --referenceDriver=<jdbc.driver.ClassName> Reference database driver class name");
stream.println(" --dataOutputDirectory=DIR Output data as CSV in the given ");
stream.println(" directory");
@@ -801,7 +807,7 @@ protected void doMigration() throws Exception {
boolean includeCatalog = Boolean.parseBoolean(getCommandParam("includeCatalog", "false"));
boolean includeSchema = Boolean.parseBoolean(getCommandParam("includeSchema", "false"));
boolean includeTablespace = Boolean.parseBoolean(getCommandParam("includeTablespace", "false"));
- DiffOutputControl diffOutputControl = new DiffOutputControl(includeCatalog, includeSchema, includeTablespace);
+ DiffOutputControl diffOutputControl = new DiffOutputControl(includeCatalog, includeSchema, includeTablespace).addIncludedSchema(new CatalogAndSchema(defaultCatalogName, defaultSchemaName));
if ("diff".equalsIgnoreCase(command)) {
CommandLineUtils.doDiff(createReferenceDatabaseFromCommandParams(commandParams), database, StringUtils.trimToNull(diffTypes));
@@ -6,6 +6,7 @@
import javax.xml.parsers.ParserConfigurationException;
+import liquibase.CatalogAndSchema;
import liquibase.Liquibase;
import liquibase.database.Database;
import liquibase.diff.output.DiffOutputControl;
@@ -145,7 +146,7 @@ protected void performLiquibaseTask(Liquibase liquibase) throws LiquibaseExcepti
getLog().info("Performing Diff on database " + db.toString());
if (diffChangeLogFile != null) {
try {
- CommandLineUtils.doDiffToChangeLog(diffChangeLogFile, referenceDatabase, db, new DiffOutputControl(diffIncludeCatalog, diffIncludeSchema, diffIncludeTablespace), StringUtils.trimToNull(diffTypes));
+ CommandLineUtils.doDiffToChangeLog(diffChangeLogFile, referenceDatabase, db, new DiffOutputControl(diffIncludeCatalog, diffIncludeSchema, diffIncludeTablespace).addIncludedSchema(new CatalogAndSchema(referenceDefaultCatalogName, referenceDefaultSchemaName)), StringUtils.trimToNull(diffTypes));
getLog().info("Differences written to Change Log File, " + diffChangeLogFile);
}
catch (IOException e) {

0 comments on commit d172cc7

Please sign in to comment.