Skip to content

Commit

Permalink
--schema-qualify-objects
Browse files Browse the repository at this point in the history
instead of adding 'SET search_path' statements prior to diff output (the
default behaviour), all statements will be schema-qualified.

This commit touches quite a few files in the project, and also fixes a
few privilege related bugs in passing. 

A thorough test case is also added.
  • Loading branch information
marcus committed Jan 31, 2017
1 parent f6e199b commit b37c9f9
Show file tree
Hide file tree
Showing 25 changed files with 1,297 additions and 269 deletions.
11 changes: 3 additions & 8 deletions src/main/java/cz/startnet/utils/pgdiff/PgDiff.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,9 @@ private static void updateSchemas(final PrintWriter writer,
for (final PgSchema newSchema : newDatabase.getSchemas()) {
final SearchPathHelper searchPathHelper;

if (setSearchPath) {
searchPathHelper = new SearchPathHelper("SET search_path = "
+ PgDiffUtils.getQuotedName(newSchema.getName(), true)
+ ", pg_catalog;");
} else {
searchPathHelper = new SearchPathHelper(null);
}

searchPathHelper = new SearchPathHelper(newSchema.getName(),
arguments.isSchemaQualifyObjects(), setSearchPath);

final PgSchema oldSchema =
oldDatabase.getSchema(newSchema.getName());

Expand Down
26 changes: 26 additions & 0 deletions src/main/java/cz/startnet/utils/pgdiff/PgDiffArguments.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ public class PgDiffArguments {
* Whether Slony triggers should be ignored.
*/
private boolean ignoreSlonyTriggers;
/**
* Whether statements should be schema qualified.
*/
private boolean schemaQualifyObjects;

/**
* Setter for {@link #addDefaults}.
Expand Down Expand Up @@ -253,6 +257,8 @@ public boolean parse(final PrintWriter writer, final String[] args) {
i++;
} else if ("--output-ignored-statements".equals(args[i])) {
setOutputIgnoredStatements(true);
} else if ("--schema-qualify-objects".equals(args[i])) {
setSchemaQualifyObjects(true);
} else if ("--version".equals(args[i])) {
setVersion(true);
} else {
Expand Down Expand Up @@ -387,4 +393,24 @@ public boolean isIgnoreSlonyTriggers() {
public void setIgnoreSlonyTriggers(final boolean ignoreSlonyTriggers) {
this.ignoreSlonyTriggers = ignoreSlonyTriggers;
}

/**
* Getter for {@link #schemaQualifyObjects}.
*
* @return {@link #schemaQualifyObjects}
*/
public boolean isSchemaQualifyObjects() {
return schemaQualifyObjects;
}

/**
* Setter for {@link #schemaQualifyObjects}.
*
* @param schemaQualifyObjects {@link #schemaQualifyObjects}
*/
public void setSchemaQualifyObjects(final boolean schemaQualifyObjects) {
this.schemaQualifyObjects = schemaQualifyObjects;
}


}
12 changes: 6 additions & 6 deletions src/main/java/cz/startnet/utils/pgdiff/PgDiffConstraints.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static void createConstraints(final PrintWriter writer,
getNewConstraints(oldTable, newTable, primaryKey)) {
searchPathHelper.outputSearchPath(writer);
writer.println();
writer.println(constraint.getCreationSQL());
writer.println(constraint.getCreationSQL(searchPathHelper));
}
}
}
Expand Down Expand Up @@ -80,7 +80,7 @@ public static void dropConstraints(final PrintWriter writer,
getDropConstraints(oldTable, newTable, primaryKey)) {
searchPathHelper.outputSearchPath(writer);
writer.println();
writer.println(constraint.getDropSQL());
writer.println(constraint.getDropSQL(searchPathHelper));
}
}
}
Expand Down Expand Up @@ -199,14 +199,14 @@ public static void alterComments(final PrintWriter writer,

if (newConstraint.isPrimaryKeyConstraint()) {
writer.print("INDEX ");
writer.print(PgDiffUtils.getQuotedName(
writer.print(searchPathHelper.getQuotedName(
newConstraint.getName()));
} else {
writer.print("CONSTRAINT ");
writer.print(PgDiffUtils.getQuotedName(
newConstraint.getName()));
writer.print(" ON ");
writer.print(PgDiffUtils.getQuotedName(
writer.print(searchPathHelper.getQuotedName(
newConstraint.getTableName()));
}

Expand All @@ -221,14 +221,14 @@ public static void alterComments(final PrintWriter writer,

if (newConstraint.isPrimaryKeyConstraint()) {
writer.print("INDEX ");
writer.print(PgDiffUtils.getQuotedName(
writer.print(searchPathHelper.getQuotedName(
newConstraint.getName()));
} else {
writer.print("CONSTRAINT ");
writer.print(PgDiffUtils.getQuotedName(
newConstraint.getName()));
writer.print(" ON ");
writer.print(PgDiffUtils.getQuotedName(
writer.print(searchPathHelper.getQuotedName(
newConstraint.getTableName()));
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/cz/startnet/utils/pgdiff/PgDiffFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static void createFunctions(final PrintWriter writer,
oldFunction, arguments.isIgnoreFunctionWhitespace())) {
searchPathHelper.outputSearchPath(writer);
writer.println();
writer.println(newFunction.getCreationSQL());
writer.println(newFunction.getCreationSQL(searchPathHelper));
}
}
}
Expand All @@ -68,7 +68,7 @@ public static void dropFunctions(final PrintWriter writer,
if (!newSchema.containsFunction(oldFunction.getSignature())) {
searchPathHelper.outputSearchPath(writer);
writer.println();
writer.println(oldFunction.getDropSQL());
writer.println(oldFunction.getDropSQL(searchPathHelper));
}
}
}
Expand Down Expand Up @@ -105,7 +105,7 @@ public static void alterComments(final PrintWriter writer,
searchPathHelper.outputSearchPath(writer);
writer.println();
writer.print("COMMENT ON FUNCTION ");
writer.print(PgDiffUtils.getQuotedName(newFunction.getName()));
writer.print(searchPathHelper.getQuotedName(newFunction.getName()));
writer.print('(');

boolean addComma = false;
Expand All @@ -129,7 +129,7 @@ public static void alterComments(final PrintWriter writer,
searchPathHelper.outputSearchPath(writer);
writer.println();
writer.print("COMMENT ON FUNCTION ");
writer.print(PgDiffUtils.getQuotedName(newFunction.getName()));
writer.print(searchPathHelper.getQuotedName(newFunction.getName()));
writer.print('(');

boolean addComma = false;
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/cz/startnet/utils/pgdiff/PgDiffIndexes.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ public static void createIndexes(final PrintWriter writer,
for (PgIndex index : newTable.getIndexes()) {
searchPathHelper.outputSearchPath(writer);
writer.println();
writer.println(index.getCreationSQL());
writer.println(index.getCreationSQL(searchPathHelper));
}
} else {
for (PgIndex index : getNewIndexes(
oldSchema.getTable(newTableName), newTable)) {
searchPathHelper.outputSearchPath(writer);
writer.println();
writer.println(index.getCreationSQL());
writer.println(index.getCreationSQL(searchPathHelper));
}
}
}
Expand Down Expand Up @@ -76,7 +76,7 @@ public static void dropIndexes(final PrintWriter writer,
for (final PgIndex index : getDropIndexes(oldTable, newTable)) {
searchPathHelper.outputSearchPath(writer);
writer.println();
writer.println(index.getDropSQL());
writer.println(index.getDropSQL(searchPathHelper));
}
}
}
Expand Down Expand Up @@ -173,7 +173,7 @@ public static void alterComments(final PrintWriter writer,
writer.println();
writer.print("COMMENT ON INDEX ");
writer.print(
PgDiffUtils.getQuotedName(newIndex.getName()));
searchPathHelper.getQuotedName(newIndex.getName()));
writer.print(" IS ");
writer.print(newIndex.getComment());
writer.println(';');
Expand All @@ -183,7 +183,7 @@ public static void alterComments(final PrintWriter writer,
writer.println();
writer.print("COMMENT ON INDEX ");
writer.print(
PgDiffUtils.getQuotedName(newIndex.getName()));
searchPathHelper.getQuotedName(newIndex.getName()));
writer.println(" IS NULL;");
}
}
Expand Down
38 changes: 19 additions & 19 deletions src/main/java/cz/startnet/utils/pgdiff/PgDiffSequences.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,26 @@ public static void createSequences(final PrintWriter writer,
|| !oldSchema.containsSequence(sequence.getName())) {
searchPathHelper.outputSearchPath(writer);
writer.println();
writer.println(sequence.getCreationSQL());
writer.println(sequence.getCreationSQL(searchPathHelper));

for (PgSequencePrivilege sequencePrivilege : sequence
.getPrivileges()) {
writer.println("REVOKE ALL ON TABLE "
+ PgDiffUtils.getQuotedName(sequence.getName())
writer.println("REVOKE ALL ON SEQUENCE "
+ searchPathHelper.getQuotedName(sequence.getName())
+ " FROM " + sequencePrivilege.getRoleName() + ";");
if (!"".equals(sequencePrivilege.getPrivilegesSQL(true))) {
writer.println("GRANT "
+ sequencePrivilege.getPrivilegesSQL(true)
+ " ON TABLE "
+ PgDiffUtils.getQuotedName(sequence.getName())
+ " ON SEQUENCE "
+ searchPathHelper.getQuotedName(sequence.getName())
+ " TO " + sequencePrivilege.getRoleName()
+ " WITH GRANT OPTION;");
}
if (!"".equals(sequencePrivilege.getPrivilegesSQL(false))) {
writer.println("GRANT "
+ sequencePrivilege.getPrivilegesSQL(false)
+ " ON TABLE "
+ PgDiffUtils.getQuotedName(sequence.getName())
+ " ON SEQUENCE "
+ searchPathHelper.getQuotedName(sequence.getName())
+ " TO " + sequencePrivilege.getRoleName()
+ ";");
}
Expand Down Expand Up @@ -83,7 +83,7 @@ public static void alterCreatedSequences(final PrintWriter writer,
&& !sequence.getOwnedBy().isEmpty()) {
searchPathHelper.outputSearchPath(writer);
writer.println();
writer.println(sequence.getOwnedBySQL());
writer.println(sequence.getOwnedBySQL(searchPathHelper));
}
}
}
Expand All @@ -108,7 +108,7 @@ public static void dropSequences(final PrintWriter writer,
if (!newSchema.containsSequence(sequence.getName())) {
searchPathHelper.outputSearchPath(writer);
writer.println();
writer.println(sequence.getDropSQL());
writer.println(sequence.getDropSQL(searchPathHelper));
}
}
}
Expand Down Expand Up @@ -221,7 +221,7 @@ public static void alterSequences(final PrintWriter writer,
searchPathHelper.outputSearchPath(writer);
writer.println();
writer.print("ALTER SEQUENCE "
+ PgDiffUtils.getQuotedName(newSequence.getName()));
+ searchPathHelper.getQuotedName(newSequence.getName()));
writer.print(sbSQL.toString());
writer.println(';');
}
Expand All @@ -235,7 +235,7 @@ public static void alterSequences(final PrintWriter writer,
searchPathHelper.outputSearchPath(writer);
writer.println();
writer.print("COMMENT ON SEQUENCE ");
writer.print(PgDiffUtils.getQuotedName(newSequence.getName()));
writer.print(searchPathHelper.getQuotedName(newSequence.getName()));
writer.print(" IS ");
writer.print(newSequence.getComment());
writer.println(';');
Expand All @@ -244,7 +244,7 @@ public static void alterSequences(final PrintWriter writer,
searchPathHelper.outputSearchPath(writer);
writer.println();
writer.print("COMMENT ON SEQUENCE ");
writer.print(newSequence.getName());
writer.print(searchPathHelper.getQuotedName(newSequence.getName()));
writer.println(" IS NULL;");
}

Expand All @@ -266,29 +266,29 @@ private static void alterPrivileges(final PrintWriter writer,
writer.println();
}
writer.println("REVOKE ALL ON SEQUENCE "
+ PgDiffUtils.getQuotedName(oldSequence.getName())
+ searchPathHelper.getQuotedName(oldSequence.getName())
+ " FROM " + oldSequencePrivilege.getRoleName() + ";");
} else if (!oldSequencePrivilege.isSimilar(newSequencePrivilege)) {
if (!emptyLinePrinted) {
searchPathHelper.outputSearchPath(writer);
writer.println();
}
writer.println("REVOKE ALL ON SEQUENCE "
+ PgDiffUtils.getQuotedName(newSequence.getName())
+ searchPathHelper.getQuotedName(newSequence.getName())
+ " FROM " + newSequencePrivilege.getRoleName() + ";");
if (!"".equals(newSequencePrivilege.getPrivilegesSQL(true))) {
writer.println("GRANT "
+ newSequencePrivilege.getPrivilegesSQL(true)
+ " ON SEQUENCE "
+ PgDiffUtils.getQuotedName(newSequence.getName())
+ searchPathHelper.getQuotedName(newSequence.getName())
+ " TO " + newSequencePrivilege.getRoleName()
+ " WITH GRANT OPTION;");
}
if (!"".equals(newSequencePrivilege.getPrivilegesSQL(false))) {
writer.println("GRANT "
+ newSequencePrivilege.getPrivilegesSQL(false)
+ " ON SEQUENCE "
+ PgDiffUtils.getQuotedName(newSequence.getName())
+ searchPathHelper.getQuotedName(newSequence.getName())
+ " TO " + newSequencePrivilege.getRoleName() + ";");
}
} // else similar privilege will not be updated
Expand All @@ -303,21 +303,21 @@ private static void alterPrivileges(final PrintWriter writer,
writer.println();
}
writer.println("REVOKE ALL ON SEQUENCE "
+ PgDiffUtils.getQuotedName(newSequence.getName())
+ searchPathHelper.getQuotedName(newSequence.getName())
+ " FROM " + newSequencePrivilege.getRoleName() + ";");
if (!"".equals(newSequencePrivilege.getPrivilegesSQL(true))) {
writer.println("GRANT "
+ newSequencePrivilege.getPrivilegesSQL(true)
+ " ON SEQUENCE "
+ PgDiffUtils.getQuotedName(newSequence.getName())
+ searchPathHelper.getQuotedName(newSequence.getName())
+ " TO " + newSequencePrivilege.getRoleName()
+ " WITH GRANT OPTION;");
}
if (!"".equals(newSequencePrivilege.getPrivilegesSQL(false))) {
writer.println("GRANT "
+ newSequencePrivilege.getPrivilegesSQL(false)
+ " ON SEQUENCE "
+ PgDiffUtils.getQuotedName(newSequence.getName())
+ searchPathHelper.getQuotedName(newSequence.getName())
+ " TO " + newSequencePrivilege.getRoleName() + ";");
}
}
Expand Down
Loading

0 comments on commit b37c9f9

Please sign in to comment.