diff --git a/src/main/java/cz/startnet/utils/pgdiff/PgDiff.java b/src/main/java/cz/startnet/utils/pgdiff/PgDiff.java index 36bd59bf..0458b19d 100644 --- a/src/main/java/cz/startnet/utils/pgdiff/PgDiff.java +++ b/src/main/java/cz/startnet/utils/pgdiff/PgDiff.java @@ -240,7 +240,7 @@ private static void updateSchemas(final PrintWriter writer, PgDiffTables.dropTables( writer, oldSchema, newSchema, searchPathHelper); PgDiffSequences.dropSequences( - writer, oldSchema, newSchema, searchPathHelper); + writer, arguments, oldSchema, newSchema, searchPathHelper); PgDiffSequences.createSequences( writer, oldSchema, newSchema, searchPathHelper); diff --git a/src/main/java/cz/startnet/utils/pgdiff/PgDiffArguments.java b/src/main/java/cz/startnet/utils/pgdiff/PgDiffArguments.java index 66230c24..17320c0a 100644 --- a/src/main/java/cz/startnet/utils/pgdiff/PgDiffArguments.java +++ b/src/main/java/cz/startnet/utils/pgdiff/PgDiffArguments.java @@ -65,6 +65,14 @@ public class PgDiffArguments { * Whether Slony triggers should be ignored. */ private boolean ignoreSlonyTriggers; + /** + * Force DROP FUNCTION statement before any CREATE FUNCTION one for existing function. + */ + private boolean forceDropFunction; + /** + * Do not generate DROP SEQUENCE statement for DROPPED tables. + */ + private boolean tableDropsSequence; /** * Setter for {@link #addDefaults}. @@ -255,6 +263,10 @@ public boolean parse(final PrintWriter writer, final String[] args) { setOutputIgnoredStatements(true); } else if ("--version".equals(args[i])) { setVersion(true); + } else if ("--force-drop-function".equals(args[i])) { + setForceDropFunction(true); + } else if ("--table-drops-sequence".equals(args[i])) { + setTableDropsSequence(true); } else { writer.print(Resources.getString("ErrorUnknownOption")); writer.print(": "); @@ -387,4 +399,40 @@ public boolean isIgnoreSlonyTriggers() { public void setIgnoreSlonyTriggers(final boolean ignoreSlonyTriggers) { this.ignoreSlonyTriggers = ignoreSlonyTriggers; } + + /** + * Getter for {@link #forceDropFunction}. + * + * @return {@link #forceDropFunction} + */ + public boolean isForceDropFunction() { + return forceDropFunction; + } + + /** + * Setter for {@link #forceDropFunction}. + * + * @param forceDropFunction {@link #forceDropFunction} + */ + public void setForceDropFunction(final boolean forceDropFunction) { + this.forceDropFunction = forceDropFunction; + } + + /** + * Getter for {@link #tableDropsSequence}. + * + * @return {@link #tableDropsSequence} + */ + public boolean isTableDropsSequence() { + return tableDropsSequence; + } + + /** + * Setter for {@link #tableDropsSequence}. + * + * @param tableDropsSequence {@link #tableDropsSequence} + */ + public void setTableDropsSequence(boolean tableDropsSequence) { + this.tableDropsSequence = tableDropsSequence; + } } diff --git a/src/main/java/cz/startnet/utils/pgdiff/PgDiffFunctions.java b/src/main/java/cz/startnet/utils/pgdiff/PgDiffFunctions.java index ffbc75b0..46f8cf44 100644 --- a/src/main/java/cz/startnet/utils/pgdiff/PgDiffFunctions.java +++ b/src/main/java/cz/startnet/utils/pgdiff/PgDiffFunctions.java @@ -42,6 +42,9 @@ public static void createFunctions(final PrintWriter writer, oldFunction, arguments.isIgnoreFunctionWhitespace())) { searchPathHelper.outputSearchPath(writer); writer.println(); + if (arguments.isForceDropFunction() && oldFunction != null) { + writer.println(oldFunction.getDropSQL()); + } writer.println(newFunction.getCreationSQL()); } } diff --git a/src/main/java/cz/startnet/utils/pgdiff/PgDiffSequences.java b/src/main/java/cz/startnet/utils/pgdiff/PgDiffSequences.java index 19ac3b28..8e105e15 100644 --- a/src/main/java/cz/startnet/utils/pgdiff/PgDiffSequences.java +++ b/src/main/java/cz/startnet/utils/pgdiff/PgDiffSequences.java @@ -7,7 +7,10 @@ import cz.startnet.utils.pgdiff.schema.PgSchema; import cz.startnet.utils.pgdiff.schema.PgSequence; +import cz.startnet.utils.pgdiff.schema.PgTable; import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.List; /** * Diffs sequences. @@ -71,18 +74,29 @@ public static void alterCreatedSequences(final PrintWriter writer, * @param searchPathHelper search path helper */ public static void dropSequences(final PrintWriter writer, - final PgSchema oldSchema, final PgSchema newSchema, - final SearchPathHelper searchPathHelper) { + final PgDiffArguments arguments, final PgSchema oldSchema, + final PgSchema newSchema, final SearchPathHelper searchPathHelper) { if (oldSchema == null) { return; } + + List droppedTables = PgDiffTables.getDropTableCandidates(oldSchema, newSchema); + List droppedTableNames = new ArrayList(); + for (PgTable table : droppedTables) { + droppedTableNames.add(table.getName()); + } // Drop sequences that do not exist in new schema for (final PgSequence sequence : oldSchema.getSequences()) { if (!newSchema.containsSequence(sequence.getName())) { - searchPathHelper.outputSearchPath(writer); - writer.println(); - writer.println(sequence.getDropSQL()); + if (arguments.isTableDropsSequence() && -1 != droppedTableNames.indexOf(sequence.getOwnedByTable())) { + // Don't drop sequences for dropped tables on new Postgresql servers, + // it will do it authomatically. + } else { + searchPathHelper.outputSearchPath(writer); + writer.println(); + writer.println(sequence.getDropSQL()); + } } } } diff --git a/src/main/java/cz/startnet/utils/pgdiff/PgDiffTables.java b/src/main/java/cz/startnet/utils/pgdiff/PgDiffTables.java index 3fc5f8ac..09dd531d 100644 --- a/src/main/java/cz/startnet/utils/pgdiff/PgDiffTables.java +++ b/src/main/java/cz/startnet/utils/pgdiff/PgDiffTables.java @@ -461,6 +461,27 @@ public static void createTables(final PrintWriter writer, } } } + + /** + * Returns all tables which will be dropped by dropTables call. + * + * @param oldSchema origin schema + * @param newSchema new schema + * @return + */ + public static List getDropTableCandidates(final PgSchema oldSchema, final PgSchema newSchema) { + List tables = new ArrayList(); + + if (oldSchema != null) { + for (final PgTable table : oldSchema.getTables()) { + if (!newSchema.containsTable(table.getName())) { + tables.add(table); + } + } + } + + return tables; + } /** * Outputs statements for dropping tables. @@ -476,13 +497,11 @@ public static void dropTables(final PrintWriter writer, if (oldSchema == null) { return; } - - for (final PgTable table : oldSchema.getTables()) { - if (!newSchema.containsTable(table.getName())) { - searchPathHelper.outputSearchPath(writer); - writer.println(); - writer.println(table.getDropSQL()); - } + + for (PgTable table : getDropTableCandidates(oldSchema, newSchema)) { + searchPathHelper.outputSearchPath(writer); + writer.println(); + writer.println(table.getDropSQL()); } } diff --git a/src/main/java/cz/startnet/utils/pgdiff/schema/PgSequence.java b/src/main/java/cz/startnet/utils/pgdiff/schema/PgSequence.java index b62f411d..c05f9271 100644 --- a/src/main/java/cz/startnet/utils/pgdiff/schema/PgSequence.java +++ b/src/main/java/cz/startnet/utils/pgdiff/schema/PgSequence.java @@ -302,6 +302,28 @@ public String getStartWith() { public String getOwnedBy() { return ownedBy; } + + /** + * Returns owner table name. + * + * @return Table name. + */ + public String getOwnedByTable() { + String table; + + if (ownedBy == null) { + table = null; + } else { + int i = ownedBy.indexOf("."); + if (i == -1) { + throw new RuntimeException("Can't find sequence owner table name."); + } + + table = ownedBy.substring(0, i); + } + + return table; + } /** * Setter for {@link #ownedBy}. diff --git a/src/main/resources/cz/startnet/utils/pgdiff/Resources.properties b/src/main/resources/cz/startnet/utils/pgdiff/Resources.properties index fd42d429..227ed2f0 100644 --- a/src/main/resources/cz/startnet/utils/pgdiff/Resources.properties +++ b/src/main/resources/cz/startnet/utils/pgdiff/Resources.properties @@ -40,8 +40,14 @@ ${tab}statements)\n\ ${tab}when parsing SQL statements, ignores Slony triggers named _slony_logtrigger\n\ ${tab}and _slony_denyaccess\n\ \n\ ---list-charsets\n\ -${tab}lists all supported charsets +--list-charsets:\n\ +${tab}lists all supported charsets\n\ +\n\ +--force-drop-function:\n\ +${tab}force DROP FUNCTION statement before any CREATE FUNCTION one for existing function\n\ +\n\ +--table-drops-sequence:\n\ +${tab}do not generate DROP SEQUENCE statement for DROPPED tables Version=Version OriginalDatabaseIgnoredStatements=Original database ignored statements NewDatabaseIgnoredStatements=New database ignored statements