Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--force-drop-function command line parameter added. #104

Open
wants to merge 2 commits into
base: next_release
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/cz/startnet/utils/pgdiff/PgDiff.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
48 changes: 48 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,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}.
Expand Down Expand Up @@ -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(": ");
Expand Down Expand Up @@ -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;
}
}
3 changes: 3 additions & 0 deletions src/main/java/cz/startnet/utils/pgdiff/PgDiffFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Expand Down
24 changes: 19 additions & 5 deletions src/main/java/cz/startnet/utils/pgdiff/PgDiffSequences.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<PgTable> droppedTables = PgDiffTables.getDropTableCandidates(oldSchema, newSchema);
List<String> droppedTableNames = new ArrayList<String>();
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());
}
}
}
}
Expand Down
33 changes: 26 additions & 7 deletions src/main/java/cz/startnet/utils/pgdiff/PgDiffTables.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<PgTable> getDropTableCandidates(final PgSchema oldSchema, final PgSchema newSchema) {
List<PgTable> tables = new ArrayList<PgTable>();

if (oldSchema != null) {
for (final PgTable table : oldSchema.getTables()) {
if (!newSchema.containsTable(table.getName())) {
tables.add(table);
}
}
}

return tables;
}

/**
* Outputs statements for dropping tables.
Expand All @@ -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());
}
}

Expand Down
22 changes: 22 additions & 0 deletions src/main/java/cz/startnet/utils/pgdiff/schema/PgSequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand Down
10 changes: 8 additions & 2 deletions src/main/resources/cz/startnet/utils/pgdiff/Resources.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down