Skip to content

Commit

Permalink
Add tests for pull request #129
Browse files Browse the repository at this point in the history
Refactoring
Update README
  • Loading branch information
avbasov committed Mar 28, 2016
1 parent 96d2377 commit 35ed1d7
Show file tree
Hide file tree
Showing 29 changed files with 432 additions and 288 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ [email protected].
* Added support for CREATE UNLOGGED TABLE (Anatoliy Basov)
* Added support for /**/ comments (yulei)
* Support of triggers for views + clause 'INSTEAD OF' (Sergej Bonich)
* Add support for GRANT and REVOKE on objects: table, view, sequence, column (serge-pouliquen-itf)
* Add support for ALTER TABLE ... OWNER TO (serge-pouliquen-itf)

#### Fixes
* Added hint to use "CREATE TABLE ... CONSTRAINT name PRIMARY KEY/UNIQUE ..."
Expand Down
42 changes: 8 additions & 34 deletions src/main/java/cz/startnet/utils/pgdiff/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,25 @@
*/
package cz.startnet.utils.pgdiff;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

/**
* Compares two PostgreSQL dumps and outputs information about differences in
* the database schemas.
*
*
* @author fordfrog
*/
public class Main {

/**
* APgDiff main method.
*
* @param args
* the command line arguments
*
* @throws UnsupportedEncodingException
* Thrown if unsupported output encoding has been encountered.
*
* @param args the command line arguments
*
* @throws UnsupportedEncodingException Thrown if unsupported output
* encoding has been encountered.
*/
public static void main(final String[] args)
throws UnsupportedEncodingException {
Expand All @@ -34,39 +32,15 @@ public static void main(final String[] args)
final PgDiffArguments arguments = new PgDiffArguments();

if (arguments.parse(writer, args)) {
// localvar in case of print
@SuppressWarnings("UseOfSystemOutOrSystemErr")
final PrintWriter encodedWriter = new PrintWriter(
new OutputStreamWriter(System.out,
arguments.getOutCharsetName()) {
@Override
public void write(int c) throws IOException {
PgDiff.hasPrint = true;
super.write(c);
}

@Override
public void write(char cbuf[], int off, int len)
throws IOException {
PgDiff.hasPrint = true;
super.write(cbuf, off, len);
}

@Override
public void write(String str, int off, int len)
throws IOException {
PgDiff.hasPrint = true;
super.write(str, off, len);
}
});
new OutputStreamWriter(
System.out, arguments.getOutCharsetName()));
PgDiff.createDiff(encodedWriter, arguments);
encodedWriter.close();
}

writer.close();
if (PgDiff.isDifferent) {
System.exit(1);
}
}

/**
Expand Down
7 changes: 0 additions & 7 deletions src/main/java/cz/startnet/utils/pgdiff/PgDiff.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,6 @@ private static void diffDatabaseSchemas(final PrintWriter writer,
writer.println("START TRANSACTION;");
}

hasPrint = false;
isDifferent = false;

if (oldDatabase.getComment() == null
&& newDatabase.getComment() != null
|| oldDatabase.getComment() != null
Expand All @@ -132,10 +129,6 @@ private static void diffDatabaseSchemas(final PrintWriter writer,
createNewSchemas(writer, oldDatabase, newDatabase);
updateSchemas(writer, arguments, oldDatabase, newDatabase);

if (hasPrint) {
isDifferent = true;
}

if (arguments.isAddTransaction()) {
writer.println();
writer.println("COMMIT TRANSACTION;");
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/cz/startnet/utils/pgdiff/PgDiffTables.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import cz.startnet.utils.pgdiff.schema.PgInheritedColumn;
import cz.startnet.utils.pgdiff.schema.PgSchema;
import cz.startnet.utils.pgdiff.schema.PgTable;
import cz.startnet.utils.pgdiff.schema.PgTablePrivilege;
import cz.startnet.utils.pgdiff.schema.PgRelationPrivilege;
import java.io.PrintWriter;
import java.text.MessageFormat;
import java.util.ArrayList;
Expand Down Expand Up @@ -546,7 +546,7 @@ public static void createTables(final PrintWriter writer,
+ PgDiffUtils.getQuotedName(table.getName())
+ " OWNER TO " + table.getOwnerTo() + ";");
}
for (PgTablePrivilege tablePrivilege : table.getPrivileges()) {
for (PgRelationPrivilege tablePrivilege : table.getPrivileges()) {
writer.println("REVOKE ALL ON TABLE "
+ PgDiffUtils.getQuotedName(table.getName())
+ " FROM " + tablePrivilege.getRoleName() + ";");
Expand Down Expand Up @@ -792,8 +792,8 @@ private static void alterPrivileges(final PrintWriter writer,
final PgTable oldTable, final PgTable newTable,
final SearchPathHelper searchPathHelper) {
boolean emptyLinePrinted = false;
for (PgTablePrivilege oldTablePrivilege : oldTable.getPrivileges()) {
PgTablePrivilege newTablePrivilege = newTable
for (PgRelationPrivilege oldTablePrivilege : oldTable.getPrivileges()) {
PgRelationPrivilege newTablePrivilege = newTable
.getPrivilege(oldTablePrivilege.getRoleName());
if (newTablePrivilege == null) {
if (!emptyLinePrinted) {
Expand Down Expand Up @@ -828,8 +828,8 @@ private static void alterPrivileges(final PrintWriter writer,
}
} // else similar privilege will not be updated
}
for (PgTablePrivilege newTablePrivilege : newTable.getPrivileges()) {
PgTablePrivilege oldTablePrivilege = oldTable
for (PgRelationPrivilege newTablePrivilege : newTable.getPrivileges()) {
PgRelationPrivilege oldTablePrivilege = oldTable
.getPrivilege(newTablePrivilege.getRoleName());
if (oldTablePrivilege == null) {
if (!emptyLinePrinted) {
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/cz/startnet/utils/pgdiff/PgDiffViews.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import cz.startnet.utils.pgdiff.schema.PgColumn;
import cz.startnet.utils.pgdiff.schema.PgSchema;
import cz.startnet.utils.pgdiff.schema.PgView;
import cz.startnet.utils.pgdiff.schema.PgViewPrivilege;
import cz.startnet.utils.pgdiff.schema.PgRelationPrivilege;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -40,7 +40,7 @@ public static void createViews(final PrintWriter writer,
writer.println();
writer.println(newView.getCreationSQL());

for (PgViewPrivilege viewPrivilege : newView.getPrivileges()) {
for (PgRelationPrivilege viewPrivilege : newView.getPrivileges()) {
writer.println("REVOKE ALL ON TABLE "
+ PgDiffUtils.getQuotedName(newView.getName())
+ " FROM " + viewPrivilege.getRoleName() + ";");
Expand Down Expand Up @@ -289,8 +289,8 @@ private static void alterPrivileges(final PrintWriter writer,
final PgView oldView, final PgView newView,
final SearchPathHelper searchPathHelper) {
boolean emptyLinePrinted = false;
for (PgViewPrivilege oldViewPrivilege : oldView.getPrivileges()) {
PgViewPrivilege newViewPrivilege = newView
for (PgRelationPrivilege oldViewPrivilege : oldView.getPrivileges()) {
PgRelationPrivilege newViewPrivilege = newView
.getPrivilege(oldViewPrivilege.getRoleName());
if (newViewPrivilege == null) {
if (!emptyLinePrinted) {
Expand Down Expand Up @@ -323,8 +323,8 @@ private static void alterPrivileges(final PrintWriter writer,
}
} // else similar privilege will not be updated
}
for (PgViewPrivilege newViewPrivilege : newView.getPrivileges()) {
PgViewPrivilege oldViewPrivilege = oldView
for (PgRelationPrivilege newViewPrivilege : newView.getPrivileges()) {
PgRelationPrivilege oldViewPrivilege = oldView
.getPrivilege(newViewPrivilege.getRoleName());
if (oldViewPrivilege == null) {
if (!emptyLinePrinted) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ private static void stripComment(final StringBuilder sbStatement) {

pos = sbStatement.indexOf("--", pos + 1);
}

int endPos = sbStatement.indexOf("*/");
if (endPos >= 0) {
int startPos = sbStatement.indexOf("/*");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
import cz.startnet.utils.pgdiff.schema.PgSequence;
import cz.startnet.utils.pgdiff.schema.PgSequencePrivilege;
import cz.startnet.utils.pgdiff.schema.PgTable;
import cz.startnet.utils.pgdiff.schema.PgTablePrivilege;
import cz.startnet.utils.pgdiff.schema.PgRelationPrivilege;
import cz.startnet.utils.pgdiff.schema.PgView;
import cz.startnet.utils.pgdiff.schema.PgViewPrivilege;

/**
* Parses GRANT statements.
Expand Down Expand Up @@ -198,10 +197,10 @@ public static void parse(final PgDatabase database, final String statement,

if (table != null) {
for (String roleName : roles) {
PgTablePrivilege tablePrivilege = table
PgRelationPrivilege tablePrivilege = table
.getPrivilege(roleName);
if (tablePrivilege == null) {
tablePrivilege = new PgTablePrivilege(roleName);
tablePrivilege = new PgRelationPrivilege(roleName);
table.addPrivilege(tablePrivilege);
}
for (String priv : privileges) {
Expand All @@ -211,10 +210,10 @@ public static void parse(final PgDatabase database, final String statement,
}
} else if (view != null) {
for (String roleName : roles) {
PgViewPrivilege viewPrivilege = view
PgRelationPrivilege viewPrivilege = view
.getPrivilege(roleName);
if (viewPrivilege == null) {
viewPrivilege = new PgViewPrivilege(roleName);
viewPrivilege = new PgRelationPrivilege(roleName);
view.addPrivilege(viewPrivilege);
}
for (String priv : privileges) {
Expand Down
Loading

0 comments on commit 35ed1d7

Please sign in to comment.