From 916847a7434655a3e14eae606583d5aeb173bce6 Mon Sep 17 00:00:00 2001 From: Marcus Kempe Date: Fri, 20 Jan 2017 23:41:31 +0100 Subject: [PATCH 1/3] Previously, parsing of column access privileges wasn't working. The parser couldn't handle the way Postgres collates the column-list to the privilege: GRANT ALL(col1) ON TABLE table1 TO ellmkay; This is now fixed in the parser, and a test-case is added. --- .../startnet/utils/pgdiff/parsers/Parser.java | 1 + .../cz/startnet/utils/pgdiff/PgDiffTest.java | 3 +- .../pgdiff/revoke_on_table_column_diff.sql | 11 ++ .../pgdiff/revoke_on_table_column_new.sql | 98 ++++++++++++++++ .../revoke_on_table_column_original.sql | 106 ++++++++++++++++++ 5 files changed, 218 insertions(+), 1 deletion(-) create mode 100644 src/test/resources/cz/startnet/utils/pgdiff/revoke_on_table_column_diff.sql create mode 100644 src/test/resources/cz/startnet/utils/pgdiff/revoke_on_table_column_new.sql create mode 100644 src/test/resources/cz/startnet/utils/pgdiff/revoke_on_table_column_original.sql diff --git a/src/main/java/cz/startnet/utils/pgdiff/parsers/Parser.java b/src/main/java/cz/startnet/utils/pgdiff/parsers/Parser.java index dc33b43d..0dbb32c7 100644 --- a/src/main/java/cz/startnet/utils/pgdiff/parsers/Parser.java +++ b/src/main/java/cz/startnet/utils/pgdiff/parsers/Parser.java @@ -69,6 +69,7 @@ public boolean expect(final String word, final boolean optional) { || Character.isWhitespace(string.charAt(wordEnd)) || string.charAt(wordEnd) == ';' || string.charAt(wordEnd) == ')' + || string.charAt(wordEnd) == '(' || string.charAt(wordEnd) == ',' || string.charAt(wordEnd) == '[' || "(".equals(word) || ",".equals(word) || "[".equals(word) diff --git a/src/test/java/cz/startnet/utils/pgdiff/PgDiffTest.java b/src/test/java/cz/startnet/utils/pgdiff/PgDiffTest.java index fc98edbb..2ed50155 100644 --- a/src/test/java/cz/startnet/utils/pgdiff/PgDiffTest.java +++ b/src/test/java/cz/startnet/utils/pgdiff/PgDiffTest.java @@ -242,7 +242,8 @@ public static Collection parameters() { {"grant_on_table_sequence", false, false, false, false}, {"revoke_on_table_sequence", false, false, false, false}, {"grant_on_view", false, false, false, false}, - {"revoke_on_view", false, false, false, false} + {"revoke_on_view", false, false, false, false}, + {"revoke_on_table_column", false, false, false, false} // Tests types , {"add_type", false, false, false, false} , {"drop_type", false, false, false, false} diff --git a/src/test/resources/cz/startnet/utils/pgdiff/revoke_on_table_column_diff.sql b/src/test/resources/cz/startnet/utils/pgdiff/revoke_on_table_column_diff.sql new file mode 100644 index 00000000..fcc9dc3f --- /dev/null +++ b/src/test/resources/cz/startnet/utils/pgdiff/revoke_on_table_column_diff.sql @@ -0,0 +1,11 @@ + +REVOKE ALL ON TABLE table1 FROM ellmkay; +GRANT SELECT ON TABLE table1 TO ellmkay; + +REVOKE ALL (col1) ON TABLE table1 FROM ellmkay; +GRANT ALL (col1) ON TABLE table1 TO ellmkay; +REVOKE ALL (col2) ON TABLE table1 FROM public; +REVOKE ALL (col2) ON TABLE table1 FROM postgres; +REVOKE ALL (col2) ON TABLE table1 FROM ellmkay; +REVOKE ALL (col3) ON TABLE table1 FROM ellmkay; +GRANT UPDATE (col3) ON TABLE table1 TO ellmkay; diff --git a/src/test/resources/cz/startnet/utils/pgdiff/revoke_on_table_column_new.sql b/src/test/resources/cz/startnet/utils/pgdiff/revoke_on_table_column_new.sql new file mode 100644 index 00000000..73cc18b2 --- /dev/null +++ b/src/test/resources/cz/startnet/utils/pgdiff/revoke_on_table_column_new.sql @@ -0,0 +1,98 @@ +-- +-- PostgreSQL database dump +-- + +-- Dumped from database version 9.5.5 +-- Dumped by pg_dump version 9.5.5 + +SET statement_timeout = 0; +SET lock_timeout = 0; +SET client_encoding = 'UTF8'; +SET standard_conforming_strings = on; +SET check_function_bodies = false; +SET client_min_messages = warning; +SET row_security = off; + +-- +-- Name: plpgsql; Type: EXTENSION; Schema: -; Owner: +-- + +CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog; + + +-- +-- Name: EXTENSION plpgsql; Type: COMMENT; Schema: -; Owner: +-- + +COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language'; + + +SET search_path = public, pg_catalog; + +SET default_tablespace = ''; + +SET default_with_oids = false; + +-- +-- Name: table1; Type: TABLE; Schema: public; Owner: postgres +-- + +CREATE TABLE table1 ( + col1 integer, + col2 integer, + col3 integer +); + + +ALTER TABLE table1 OWNER TO postgres; + +-- +-- Data for Name: table1; Type: TABLE DATA; Schema: public; Owner: postgres +-- + +COPY table1 (col1, col2, col3) FROM stdin; +\. + + +-- +-- Name: public; Type: ACL; Schema: -; Owner: postgres +-- + +REVOKE ALL ON SCHEMA public FROM PUBLIC; +REVOKE ALL ON SCHEMA public FROM postgres; +GRANT ALL ON SCHEMA public TO postgres; +GRANT ALL ON SCHEMA public TO PUBLIC; + + +-- +-- Name: table1; Type: ACL; Schema: public; Owner: postgres +-- + +REVOKE ALL ON TABLE table1 FROM PUBLIC; +REVOKE ALL ON TABLE table1 FROM postgres; +GRANT ALL ON TABLE table1 TO postgres; +GRANT SELECT ON TABLE table1 TO ellmkay; + + +-- +-- Name: table1.col1; Type: ACL; Schema: public; Owner: postgres +-- + +REVOKE ALL(col1) ON TABLE table1 FROM PUBLIC; +REVOKE ALL(col1) ON TABLE table1 FROM postgres; +GRANT ALL(col1) ON TABLE table1 TO ellmkay; + + +-- +-- Name: table1.col3; Type: ACL; Schema: public; Owner: postgres +-- + +REVOKE ALL(col3) ON TABLE table1 FROM PUBLIC; +REVOKE ALL(col3) ON TABLE table1 FROM postgres; +GRANT UPDATE(col3) ON TABLE table1 TO ellmkay; + + +-- +-- PostgreSQL database dump complete +-- + diff --git a/src/test/resources/cz/startnet/utils/pgdiff/revoke_on_table_column_original.sql b/src/test/resources/cz/startnet/utils/pgdiff/revoke_on_table_column_original.sql new file mode 100644 index 00000000..4a5bbf3d --- /dev/null +++ b/src/test/resources/cz/startnet/utils/pgdiff/revoke_on_table_column_original.sql @@ -0,0 +1,106 @@ +-- +-- PostgreSQL database dump +-- + +-- Dumped from database version 9.5.5 +-- Dumped by pg_dump version 9.5.5 + +SET statement_timeout = 0; +SET lock_timeout = 0; +SET client_encoding = 'UTF8'; +SET standard_conforming_strings = on; +SET check_function_bodies = false; +SET client_min_messages = warning; +SET row_security = off; + +-- +-- Name: plpgsql; Type: EXTENSION; Schema: -; Owner: +-- + +CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog; + + +-- +-- Name: EXTENSION plpgsql; Type: COMMENT; Schema: -; Owner: +-- + +COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language'; + + +SET search_path = public, pg_catalog; + +SET default_tablespace = ''; + +SET default_with_oids = false; + +-- +-- Name: table1; Type: TABLE; Schema: public; Owner: postgres +-- + +CREATE TABLE table1 ( + col1 integer, + col2 integer, + col3 integer +); + + +ALTER TABLE table1 OWNER TO postgres; + +-- +-- Data for Name: table1; Type: TABLE DATA; Schema: public; Owner: postgres +-- + +COPY table1 (col1, col2, col3) FROM stdin; +\. + + +-- +-- Name: public; Type: ACL; Schema: -; Owner: postgres +-- + +REVOKE ALL ON SCHEMA public FROM PUBLIC; +REVOKE ALL ON SCHEMA public FROM postgres; +GRANT ALL ON SCHEMA public TO postgres; +GRANT ALL ON SCHEMA public TO PUBLIC; + + +-- +-- Name: table1; Type: ACL; Schema: public; Owner: postgres +-- + +REVOKE ALL ON TABLE table1 FROM PUBLIC; +REVOKE ALL ON TABLE table1 FROM postgres; +GRANT ALL ON TABLE table1 TO postgres; + + +-- +-- Name: table1.col1; Type: ACL; Schema: public; Owner: postgres +-- + +REVOKE ALL(col1) ON TABLE table1 FROM PUBLIC; +REVOKE ALL(col1) ON TABLE table1 FROM postgres; +GRANT SELECT(col1) ON TABLE table1 TO ellmkay; + + +-- +-- Name: table1.col2; Type: ACL; Schema: public; Owner: postgres +-- + +REVOKE ALL(col2) ON TABLE table1 FROM PUBLIC; +REVOKE ALL(col2) ON TABLE table1 FROM postgres; +GRANT ALL(col2) ON TABLE table1 TO ellmkay; + + +-- +-- Name: table1.col3; Type: ACL; Schema: public; Owner: postgres +-- + +REVOKE ALL(col3) ON TABLE table1 FROM PUBLIC; +REVOKE ALL(col3) ON TABLE table1 FROM postgres; +GRANT ALL(col3) ON TABLE table1 TO ellmkay; + + +-- +-- PostgreSQL database dump complete +-- + From f6e199bb67c243540ca634587e36f9955b13c3a8 Mon Sep 17 00:00:00 2001 From: Marcus Kempe Date: Mon, 30 Jan 2017 09:57:11 +0100 Subject: [PATCH 2/3] Fix search-path output which wasn't working in cases where privileges were diffing in a multi-schema setup. --- .../utils/pgdiff/PgDiffSequences.java | 3 + .../startnet/utils/pgdiff/PgDiffTables.java | 9 +- .../cz/startnet/utils/pgdiff/PgDiffViews.java | 3 + .../cz/startnet/utils/pgdiff/PgDiffTest.java | 3 +- .../multiple_schema_privileges_diff.sql | 20 ++ .../pgdiff/multiple_schema_privileges_new.sql | 230 ++++++++++++++++++ .../multiple_schema_privileges_original.sql | 228 +++++++++++++++++ 7 files changed, 494 insertions(+), 2 deletions(-) create mode 100644 src/test/resources/cz/startnet/utils/pgdiff/multiple_schema_privileges_diff.sql create mode 100644 src/test/resources/cz/startnet/utils/pgdiff/multiple_schema_privileges_new.sql create mode 100644 src/test/resources/cz/startnet/utils/pgdiff/multiple_schema_privileges_original.sql diff --git a/src/main/java/cz/startnet/utils/pgdiff/PgDiffSequences.java b/src/main/java/cz/startnet/utils/pgdiff/PgDiffSequences.java index 882cfd43..0ee21261 100644 --- a/src/main/java/cz/startnet/utils/pgdiff/PgDiffSequences.java +++ b/src/main/java/cz/startnet/utils/pgdiff/PgDiffSequences.java @@ -262,6 +262,7 @@ private static void alterPrivileges(final PrintWriter writer, .getPrivilege(oldSequencePrivilege.getRoleName()); if (newSequencePrivilege == null) { if (!emptyLinePrinted) { + searchPathHelper.outputSearchPath(writer); writer.println(); } writer.println("REVOKE ALL ON SEQUENCE " @@ -269,6 +270,7 @@ private static void alterPrivileges(final PrintWriter writer, + " FROM " + oldSequencePrivilege.getRoleName() + ";"); } else if (!oldSequencePrivilege.isSimilar(newSequencePrivilege)) { if (!emptyLinePrinted) { + searchPathHelper.outputSearchPath(writer); writer.println(); } writer.println("REVOKE ALL ON SEQUENCE " @@ -297,6 +299,7 @@ private static void alterPrivileges(final PrintWriter writer, .getPrivilege(newSequencePrivilege.getRoleName()); if (oldSequencePrivilege == null) { if (!emptyLinePrinted) { + searchPathHelper.outputSearchPath(writer); writer.println(); } writer.println("REVOKE ALL ON SEQUENCE " diff --git a/src/main/java/cz/startnet/utils/pgdiff/PgDiffTables.java b/src/main/java/cz/startnet/utils/pgdiff/PgDiffTables.java index 36e652d3..a1bb925e 100644 --- a/src/main/java/cz/startnet/utils/pgdiff/PgDiffTables.java +++ b/src/main/java/cz/startnet/utils/pgdiff/PgDiffTables.java @@ -547,7 +547,8 @@ public static void createTables(final PrintWriter writer, + " OWNER TO " + table.getOwnerTo() + ";"); } for (PgRelationPrivilege tablePrivilege : table.getPrivileges()) { - writer.println("REVOKE ALL ON TABLE " + searchPathHelper.outputSearchPath(writer); + writer.println("REVOKE ALL ON TABLE " + PgDiffUtils.getQuotedName(table.getName()) + " FROM " + tablePrivilege.getRoleName() + ";"); if (!"".equals(tablePrivilege.getPrivilegesSQL(true))) { @@ -660,6 +661,7 @@ private static void alterPrivilegesColumns(final PrintWriter writer, .getPrivilege(oldColumnPrivilege.getRoleName()); if (newColumnPrivilege == null) { if (!emptyLinePrinted) { + searchPathHelper.outputSearchPath(writer); emptyLinePrinted = true; writer.println(); } @@ -682,6 +684,7 @@ private static void alterPrivilegesColumns(final PrintWriter writer, } if (!newColumnPrivilege.isSimilar(oldColumnPrivilege)) { if (!emptyLinePrinted) { + searchPathHelper.outputSearchPath(writer); emptyLinePrinted = true; writer.println(); } @@ -797,6 +800,7 @@ private static void alterPrivileges(final PrintWriter writer, .getPrivilege(oldTablePrivilege.getRoleName()); if (newTablePrivilege == null) { if (!emptyLinePrinted) { + searchPathHelper.outputSearchPath(writer); emptyLinePrinted = true; writer.println(); } @@ -805,6 +809,7 @@ private static void alterPrivileges(final PrintWriter writer, + " FROM " + oldTablePrivilege.getRoleName() + ";"); } else if (!oldTablePrivilege.isSimilar(newTablePrivilege)) { if (!emptyLinePrinted) { + searchPathHelper.outputSearchPath(writer); emptyLinePrinted = true; writer.println(); } @@ -833,6 +838,7 @@ private static void alterPrivileges(final PrintWriter writer, .getPrivilege(newTablePrivilege.getRoleName()); if (oldTablePrivilege == null) { if (!emptyLinePrinted) { + searchPathHelper.outputSearchPath(writer); writer.println(); } writer.println("REVOKE ALL ON TABLE " @@ -864,6 +870,7 @@ private static void alterOwnerTo(final PrintWriter writer, final String newOwnerTo = newTable.getOwnerTo(); if (newOwnerTo != null && !newOwnerTo.equals(oldOwnerTo)) { + searchPathHelper.outputSearchPath(writer); writer.println(); writer.println("ALTER " + ((newTable.isForeign()) ? "FOREIGN ":"") + "TABLE " + PgDiffUtils.getQuotedName(newTable.getName()) diff --git a/src/main/java/cz/startnet/utils/pgdiff/PgDiffViews.java b/src/main/java/cz/startnet/utils/pgdiff/PgDiffViews.java index 8ef101c2..63422fa7 100644 --- a/src/main/java/cz/startnet/utils/pgdiff/PgDiffViews.java +++ b/src/main/java/cz/startnet/utils/pgdiff/PgDiffViews.java @@ -294,6 +294,7 @@ private static void alterPrivileges(final PrintWriter writer, .getPrivilege(oldViewPrivilege.getRoleName()); if (newViewPrivilege == null) { if (!emptyLinePrinted) { + searchPathHelper.outputSearchPath(writer); writer.println(); } writer.println("REVOKE ALL ON TABLE " @@ -301,6 +302,7 @@ private static void alterPrivileges(final PrintWriter writer, + " FROM " + oldViewPrivilege.getRoleName() + ";"); } else if (!oldViewPrivilege.isSimilar(newViewPrivilege)) { if (!emptyLinePrinted) { + searchPathHelper.outputSearchPath(writer); writer.println(); } writer.println("REVOKE ALL ON TABLE " @@ -328,6 +330,7 @@ private static void alterPrivileges(final PrintWriter writer, .getPrivilege(newViewPrivilege.getRoleName()); if (oldViewPrivilege == null) { if (!emptyLinePrinted) { + searchPathHelper.outputSearchPath(writer); writer.println(); } writer.println("REVOKE ALL ON TABLE " diff --git a/src/test/java/cz/startnet/utils/pgdiff/PgDiffTest.java b/src/test/java/cz/startnet/utils/pgdiff/PgDiffTest.java index 2ed50155..fdd9c72d 100644 --- a/src/test/java/cz/startnet/utils/pgdiff/PgDiffTest.java +++ b/src/test/java/cz/startnet/utils/pgdiff/PgDiffTest.java @@ -243,7 +243,8 @@ public static Collection parameters() { {"revoke_on_table_sequence", false, false, false, false}, {"grant_on_view", false, false, false, false}, {"revoke_on_view", false, false, false, false}, - {"revoke_on_table_column", false, false, false, false} + {"revoke_on_table_column", false, false, false, false}, + {"multiple_schema_privileges", false, false, false, false} // Tests types , {"add_type", false, false, false, false} , {"drop_type", false, false, false, false} diff --git a/src/test/resources/cz/startnet/utils/pgdiff/multiple_schema_privileges_diff.sql b/src/test/resources/cz/startnet/utils/pgdiff/multiple_schema_privileges_diff.sql new file mode 100644 index 00000000..3bd2a779 --- /dev/null +++ b/src/test/resources/cz/startnet/utils/pgdiff/multiple_schema_privileges_diff.sql @@ -0,0 +1,20 @@ + +SET search_path = schema1, pg_catalog; + +REVOKE ALL ON TABLE table1 FROM ellmkay; +GRANT ALL ON TABLE table1 TO ellmkay; + +SET search_path = schema2, pg_catalog; + +REVOKE ALL ON TABLE table2 FROM postgres; +GRANT UPDATE ON TABLE table2 TO postgres; + +SET search_path = schema3, pg_catalog; + +REVOKE ALL ON SEQUENCE sequence3 FROM ellmkay; +GRANT SELECT ON SEQUENCE sequence3 TO ellmkay; + +REVOKE ALL ON SEQUENCE sequence3 FROM postgres; +GRANT ALL ON SEQUENCE sequence3 TO postgres; + +REVOKE ALL ON TABLE table3 FROM ellmkay; diff --git a/src/test/resources/cz/startnet/utils/pgdiff/multiple_schema_privileges_new.sql b/src/test/resources/cz/startnet/utils/pgdiff/multiple_schema_privileges_new.sql new file mode 100644 index 00000000..c9138ec1 --- /dev/null +++ b/src/test/resources/cz/startnet/utils/pgdiff/multiple_schema_privileges_new.sql @@ -0,0 +1,230 @@ +-- +-- PostgreSQL database dump +-- + +-- Dumped from database version 9.5.5 +-- Dumped by pg_dump version 9.5.5 + +SET statement_timeout = 0; +SET lock_timeout = 0; +SET client_encoding = 'UTF8'; +SET standard_conforming_strings = on; +SET check_function_bodies = false; +SET client_min_messages = warning; +SET row_security = off; + +-- +-- Name: schema1; Type: SCHEMA; Schema: -; Owner: postgres +-- + +CREATE SCHEMA schema1; + + +ALTER SCHEMA schema1 OWNER TO postgres; + +-- +-- Name: schema2; Type: SCHEMA; Schema: -; Owner: ellmkay +-- + +CREATE SCHEMA schema2; + + +ALTER SCHEMA schema2 OWNER TO ellmkay; + +-- +-- Name: schema3; Type: SCHEMA; Schema: -; Owner: ellmkay +-- + +CREATE SCHEMA schema3; + + +ALTER SCHEMA schema3 OWNER TO ellmkay; + +-- +-- Name: plpgsql; Type: EXTENSION; Schema: -; Owner: +-- + +CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog; + + +-- +-- Name: EXTENSION plpgsql; Type: COMMENT; Schema: -; Owner: +-- + +COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language'; + + +SET search_path = schema1, pg_catalog; + +SET default_tablespace = ''; + +SET default_with_oids = false; + +-- +-- Name: table1; Type: TABLE; Schema: schema1; Owner: marcus +-- + +CREATE TABLE table1 ( + x integer +); + + +ALTER TABLE table1 OWNER TO marcus; + +SET search_path = schema2, pg_catalog; + +-- +-- Name: table2; Type: TABLE; Schema: schema2; Owner: marcus +-- + +CREATE TABLE table2 ( + x integer +); + + +ALTER TABLE table2 OWNER TO marcus; + +SET search_path = schema3, pg_catalog; + +-- +-- Name: sequence3; Type: SEQUENCE; Schema: schema3; Owner: postgres +-- + +CREATE SEQUENCE sequence3 + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +ALTER TABLE sequence3 OWNER TO postgres; + +-- +-- Name: table3; Type: TABLE; Schema: schema3; Owner: marcus +-- + +CREATE TABLE table3 ( + x integer +); + + +ALTER TABLE table3 OWNER TO marcus; + +SET search_path = schema1, pg_catalog; + +-- +-- Data for Name: table1; Type: TABLE DATA; Schema: schema1; Owner: marcus +-- + +COPY table1 (x) FROM stdin; +\. + + +SET search_path = schema2, pg_catalog; + +-- +-- Data for Name: table2; Type: TABLE DATA; Schema: schema2; Owner: marcus +-- + +COPY table2 (x) FROM stdin; +\. + + +SET search_path = schema3, pg_catalog; + +-- +-- Name: sequence3; Type: SEQUENCE SET; Schema: schema3; Owner: postgres +-- + +SELECT pg_catalog.setval('sequence3', 1, false); + + +-- +-- Data for Name: table3; Type: TABLE DATA; Schema: schema3; Owner: marcus +-- + +COPY table3 (x) FROM stdin; +\. + + +-- +-- Name: public; Type: ACL; Schema: -; Owner: postgres +-- + +REVOKE ALL ON SCHEMA public FROM PUBLIC; +REVOKE ALL ON SCHEMA public FROM postgres; +GRANT ALL ON SCHEMA public TO postgres; +GRANT ALL ON SCHEMA public TO PUBLIC; + + +-- +-- Name: schema1; Type: ACL; Schema: -; Owner: postgres +-- + +REVOKE ALL ON SCHEMA schema1 FROM PUBLIC; +REVOKE ALL ON SCHEMA schema1 FROM postgres; +GRANT ALL ON SCHEMA schema1 TO postgres; +GRANT USAGE ON SCHEMA schema1 TO ellmkay; + + +-- +-- Name: schema2; Type: ACL; Schema: -; Owner: ellmkay +-- + +REVOKE ALL ON SCHEMA schema2 FROM PUBLIC; +REVOKE ALL ON SCHEMA schema2 FROM ellmkay; +GRANT ALL ON SCHEMA schema2 TO ellmkay; +GRANT USAGE ON SCHEMA schema2 TO postgres; + + +SET search_path = schema1, pg_catalog; + +-- +-- Name: table1; Type: ACL; Schema: schema1; Owner: marcus +-- + +REVOKE ALL ON TABLE table1 FROM PUBLIC; +REVOKE ALL ON TABLE table1 FROM marcus; +GRANT ALL ON TABLE table1 TO marcus; +GRANT SELECT ON TABLE table1 TO postgres; +GRANT ALL ON TABLE table1 TO ellmkay; + + +SET search_path = schema2, pg_catalog; + +-- +-- Name: table2; Type: ACL; Schema: schema2; Owner: marcus +-- + +REVOKE ALL ON TABLE table2 FROM PUBLIC; +REVOKE ALL ON TABLE table2 FROM marcus; +GRANT ALL ON TABLE table2 TO marcus; +GRANT UPDATE ON TABLE table2 TO postgres; + + +SET search_path = schema3, pg_catalog; + +-- +-- Name: sequence3; Type: ACL; Schema: schema3; Owner: postgres +-- + +REVOKE ALL ON SEQUENCE sequence3 FROM PUBLIC; +REVOKE ALL ON SEQUENCE sequence3 FROM postgres; +GRANT ALL ON SEQUENCE sequence3 TO postgres; +GRANT SELECT ON SEQUENCE sequence3 TO ellmkay; + + +-- +-- Name: table3; Type: ACL; Schema: schema3; Owner: marcus +-- + +REVOKE ALL ON TABLE table3 FROM PUBLIC; +REVOKE ALL ON TABLE table3 FROM marcus; +GRANT ALL ON TABLE table3 TO marcus; + + +-- +-- PostgreSQL database dump complete +-- + diff --git a/src/test/resources/cz/startnet/utils/pgdiff/multiple_schema_privileges_original.sql b/src/test/resources/cz/startnet/utils/pgdiff/multiple_schema_privileges_original.sql new file mode 100644 index 00000000..ee488acb --- /dev/null +++ b/src/test/resources/cz/startnet/utils/pgdiff/multiple_schema_privileges_original.sql @@ -0,0 +1,228 @@ +-- +-- PostgreSQL database dump +-- + +-- Dumped from database version 9.5.5 +-- Dumped by pg_dump version 9.5.5 + +SET statement_timeout = 0; +SET lock_timeout = 0; +SET client_encoding = 'UTF8'; +SET standard_conforming_strings = on; +SET check_function_bodies = false; +SET client_min_messages = warning; +SET row_security = off; + +-- +-- Name: schema1; Type: SCHEMA; Schema: -; Owner: ellmkay +-- + +CREATE SCHEMA schema1; + + +ALTER SCHEMA schema1 OWNER TO ellmkay; + +-- +-- Name: schema2; Type: SCHEMA; Schema: -; Owner: ellmkay +-- + +CREATE SCHEMA schema2; + + +ALTER SCHEMA schema2 OWNER TO ellmkay; + +-- +-- Name: schema3; Type: SCHEMA; Schema: -; Owner: postgres +-- + +CREATE SCHEMA schema3; + + +ALTER SCHEMA schema3 OWNER TO postgres; + +-- +-- Name: plpgsql; Type: EXTENSION; Schema: -; Owner: +-- + +CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog; + + +-- +-- Name: EXTENSION plpgsql; Type: COMMENT; Schema: -; Owner: +-- + +COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language'; + + +SET search_path = schema1, pg_catalog; + +SET default_tablespace = ''; + +SET default_with_oids = false; + +-- +-- Name: table1; Type: TABLE; Schema: schema1; Owner: marcus +-- + +CREATE TABLE table1 ( + x integer +); + + +ALTER TABLE table1 OWNER TO marcus; + +SET search_path = schema2, pg_catalog; + +-- +-- Name: table2; Type: TABLE; Schema: schema2; Owner: marcus +-- + +CREATE TABLE table2 ( + x integer +); + + +ALTER TABLE table2 OWNER TO marcus; + +SET search_path = schema3, pg_catalog; + +-- +-- Name: sequence3; Type: SEQUENCE; Schema: schema3; Owner: ellmkay +-- + +CREATE SEQUENCE sequence3 + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +ALTER TABLE sequence3 OWNER TO ellmkay; + +-- +-- Name: table3; Type: TABLE; Schema: schema3; Owner: marcus +-- + +CREATE TABLE table3 ( + x integer +); + + +ALTER TABLE table3 OWNER TO marcus; + +SET search_path = schema1, pg_catalog; + +-- +-- Data for Name: table1; Type: TABLE DATA; Schema: schema1; Owner: marcus +-- + +COPY table1 (x) FROM stdin; +\. + + +SET search_path = schema2, pg_catalog; + +-- +-- Data for Name: table2; Type: TABLE DATA; Schema: schema2; Owner: marcus +-- + +COPY table2 (x) FROM stdin; +\. + + +SET search_path = schema3, pg_catalog; + +-- +-- Name: sequence3; Type: SEQUENCE SET; Schema: schema3; Owner: ellmkay +-- + +SELECT pg_catalog.setval('sequence3', 1, false); + + +-- +-- Data for Name: table3; Type: TABLE DATA; Schema: schema3; Owner: marcus +-- + +COPY table3 (x) FROM stdin; +\. + + +-- +-- Name: public; Type: ACL; Schema: -; Owner: postgres +-- + +REVOKE ALL ON SCHEMA public FROM PUBLIC; +REVOKE ALL ON SCHEMA public FROM postgres; +GRANT ALL ON SCHEMA public TO postgres; +GRANT ALL ON SCHEMA public TO PUBLIC; + + +-- +-- Name: schema1; Type: ACL; Schema: -; Owner: ellmkay +-- + +REVOKE ALL ON SCHEMA schema1 FROM PUBLIC; +REVOKE ALL ON SCHEMA schema1 FROM ellmkay; +GRANT ALL ON SCHEMA schema1 TO ellmkay; +GRANT USAGE ON SCHEMA schema1 TO postgres; + + +-- +-- Name: schema2; Type: ACL; Schema: -; Owner: ellmkay +-- + +REVOKE ALL ON SCHEMA schema2 FROM PUBLIC; +REVOKE ALL ON SCHEMA schema2 FROM ellmkay; +GRANT ALL ON SCHEMA schema2 TO ellmkay; + + +SET search_path = schema1, pg_catalog; + +-- +-- Name: table1; Type: ACL; Schema: schema1; Owner: marcus +-- + +REVOKE ALL ON TABLE table1 FROM PUBLIC; +REVOKE ALL ON TABLE table1 FROM marcus; +GRANT ALL ON TABLE table1 TO marcus; +GRANT SELECT ON TABLE table1 TO postgres; + + +SET search_path = schema2, pg_catalog; + +-- +-- Name: table2; Type: ACL; Schema: schema2; Owner: marcus +-- + +REVOKE ALL ON TABLE table2 FROM PUBLIC; +REVOKE ALL ON TABLE table2 FROM marcus; +GRANT ALL ON TABLE table2 TO marcus; + + +SET search_path = schema3, pg_catalog; + +-- +-- Name: sequence3; Type: ACL; Schema: schema3; Owner: ellmkay +-- + +REVOKE ALL ON SEQUENCE sequence3 FROM PUBLIC; +REVOKE ALL ON SEQUENCE sequence3 FROM ellmkay; +GRANT ALL ON SEQUENCE sequence3 TO ellmkay; +GRANT SELECT ON SEQUENCE sequence3 TO postgres; + + +-- +-- Name: table3; Type: ACL; Schema: schema3; Owner: marcus +-- + +REVOKE ALL ON TABLE table3 FROM PUBLIC; +REVOKE ALL ON TABLE table3 FROM marcus; +GRANT ALL ON TABLE table3 TO marcus; +GRANT SELECT ON TABLE table3 TO ellmkay; + + +-- +-- PostgreSQL database dump complete +-- + From b37c9f902a8a8baf56aadac8f0ccfd84cd86aa6f Mon Sep 17 00:00:00 2001 From: marcus Date: Tue, 31 Jan 2017 16:13:37 +0100 Subject: [PATCH 3/3] --schema-qualify-objects 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. --- .../java/cz/startnet/utils/pgdiff/PgDiff.java | 11 +- .../utils/pgdiff/PgDiffArguments.java | 26 + .../utils/pgdiff/PgDiffConstraints.java | 12 +- .../utils/pgdiff/PgDiffFunctions.java | 8 +- .../startnet/utils/pgdiff/PgDiffIndexes.java | 10 +- .../utils/pgdiff/PgDiffSequences.java | 38 +- .../startnet/utils/pgdiff/PgDiffTables.java | 64 +-- .../startnet/utils/pgdiff/PgDiffTriggers.java | 8 +- .../cz/startnet/utils/pgdiff/PgDiffTypes.java | 6 +- .../cz/startnet/utils/pgdiff/PgDiffViews.java | 38 +- .../utils/pgdiff/SearchPathHelper.java | 42 +- .../utils/pgdiff/schema/PgConstraint.java | 12 +- .../utils/pgdiff/schema/PgFunction.java | 12 +- .../startnet/utils/pgdiff/schema/PgIndex.java | 11 +- .../utils/pgdiff/schema/PgRelation.java | 11 +- .../utils/pgdiff/schema/PgSequence.java | 15 +- .../startnet/utils/pgdiff/schema/PgTable.java | 16 +- .../utils/pgdiff/schema/PgTrigger.java | 12 +- .../startnet/utils/pgdiff/schema/PgType.java | 12 +- .../startnet/utils/pgdiff/schema/PgView.java | 8 +- .../utils/pgdiff/Resources.properties | 4 + .../cz/startnet/utils/pgdiff/PgDiffTest.java | 238 ++++----- .../utils/pgdiff/schema_qualified_diff.sql | 51 ++ .../utils/pgdiff/schema_qualified_new.sql | 476 ++++++++++++++++++ .../pgdiff/schema_qualified_original.sql | 425 ++++++++++++++++ 25 files changed, 1297 insertions(+), 269 deletions(-) create mode 100644 src/test/resources/cz/startnet/utils/pgdiff/schema_qualified_diff.sql create mode 100644 src/test/resources/cz/startnet/utils/pgdiff/schema_qualified_new.sql create mode 100644 src/test/resources/cz/startnet/utils/pgdiff/schema_qualified_original.sql diff --git a/src/main/java/cz/startnet/utils/pgdiff/PgDiff.java b/src/main/java/cz/startnet/utils/pgdiff/PgDiff.java index 2ac157fc..dd46f2a5 100644 --- a/src/main/java/cz/startnet/utils/pgdiff/PgDiff.java +++ b/src/main/java/cz/startnet/utils/pgdiff/PgDiff.java @@ -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()); diff --git a/src/main/java/cz/startnet/utils/pgdiff/PgDiffArguments.java b/src/main/java/cz/startnet/utils/pgdiff/PgDiffArguments.java index 66230c24..ca924120 100644 --- a/src/main/java/cz/startnet/utils/pgdiff/PgDiffArguments.java +++ b/src/main/java/cz/startnet/utils/pgdiff/PgDiffArguments.java @@ -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}. @@ -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 { @@ -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; + } + + } diff --git a/src/main/java/cz/startnet/utils/pgdiff/PgDiffConstraints.java b/src/main/java/cz/startnet/utils/pgdiff/PgDiffConstraints.java index 26acc141..b2d48bc2 100644 --- a/src/main/java/cz/startnet/utils/pgdiff/PgDiffConstraints.java +++ b/src/main/java/cz/startnet/utils/pgdiff/PgDiffConstraints.java @@ -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)); } } } @@ -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)); } } } @@ -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())); } @@ -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())); } diff --git a/src/main/java/cz/startnet/utils/pgdiff/PgDiffFunctions.java b/src/main/java/cz/startnet/utils/pgdiff/PgDiffFunctions.java index ffbc75b0..f3553a3f 100644 --- a/src/main/java/cz/startnet/utils/pgdiff/PgDiffFunctions.java +++ b/src/main/java/cz/startnet/utils/pgdiff/PgDiffFunctions.java @@ -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)); } } } @@ -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)); } } } @@ -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; @@ -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; diff --git a/src/main/java/cz/startnet/utils/pgdiff/PgDiffIndexes.java b/src/main/java/cz/startnet/utils/pgdiff/PgDiffIndexes.java index 05b5dfec..dc14f8ea 100644 --- a/src/main/java/cz/startnet/utils/pgdiff/PgDiffIndexes.java +++ b/src/main/java/cz/startnet/utils/pgdiff/PgDiffIndexes.java @@ -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)); } } } @@ -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)); } } } @@ -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(';'); @@ -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;"); } } diff --git a/src/main/java/cz/startnet/utils/pgdiff/PgDiffSequences.java b/src/main/java/cz/startnet/utils/pgdiff/PgDiffSequences.java index 0ee21261..590882b9 100644 --- a/src/main/java/cz/startnet/utils/pgdiff/PgDiffSequences.java +++ b/src/main/java/cz/startnet/utils/pgdiff/PgDiffSequences.java @@ -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() + ";"); } @@ -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)); } } } @@ -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)); } } } @@ -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(';'); } @@ -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(';'); @@ -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;"); } @@ -266,7 +266,7 @@ 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) { @@ -274,13 +274,13 @@ 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;"); } @@ -288,7 +288,7 @@ private static void alterPrivileges(final PrintWriter writer, 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 @@ -303,13 +303,13 @@ 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;"); } @@ -317,7 +317,7 @@ private static void alterPrivileges(final PrintWriter writer, writer.println("GRANT " + newSequencePrivilege.getPrivilegesSQL(false) + " ON SEQUENCE " - + PgDiffUtils.getQuotedName(newSequence.getName()) + + searchPathHelper.getQuotedName(newSequence.getName()) + " TO " + newSequencePrivilege.getRoleName() + ";"); } } diff --git a/src/main/java/cz/startnet/utils/pgdiff/PgDiffTables.java b/src/main/java/cz/startnet/utils/pgdiff/PgDiffTables.java index a1bb925e..251c5b4b 100644 --- a/src/main/java/cz/startnet/utils/pgdiff/PgDiffTables.java +++ b/src/main/java/cz/startnet/utils/pgdiff/PgDiffTables.java @@ -61,7 +61,7 @@ public static void dropClusters(final PrintWriter writer, searchPathHelper.outputSearchPath(writer); writer.println(); writer.print("ALTER TABLE "); - writer.print(PgDiffUtils.getQuotedName(newTable.getName())); + writer.print(searchPathHelper.getQuotedName(newTable.getName())); writer.println(" SET WITHOUT CLUSTER;"); } } @@ -103,7 +103,7 @@ public static void createClusters(final PrintWriter writer, searchPathHelper.outputSearchPath(writer); writer.println(); writer.print("ALTER TABLE "); - writer.print(PgDiffUtils.getQuotedName(newTable.getName())); + writer.print(searchPathHelper.getQuotedName(newTable.getName())); writer.print(" CLUSTER ON "); writer.print(PgDiffUtils.getQuotedName(newCluster)); writer.println(';'); @@ -184,7 +184,7 @@ private static void addAlterStatistics(final PrintWriter writer, searchPathHelper.outputSearchPath(writer); writer.println(); writer.print("ALTER TABLE ONLY "); - writer.print(PgDiffUtils.getQuotedName(newTable.getName())); + writer.print(searchPathHelper.getQuotedName(newTable.getName())); writer.print(" ALTER COLUMN "); writer.print(PgDiffUtils.getQuotedName(entry.getKey())); writer.print(" SET STATISTICS "); @@ -231,7 +231,7 @@ private static void addAlterStorage(final PrintWriter writer, searchPathHelper.outputSearchPath(writer); writer.println(); writer.print("ALTER TABLE ONLY "); - writer.print(PgDiffUtils.getQuotedName(newTable.getName())); + writer.print(searchPathHelper.getQuotedName(newTable.getName())); writer.print(" ALTER COLUMN "); writer.print(PgDiffUtils.getQuotedName(newColumn.getName())); writer.print(" SET STORAGE "); @@ -388,7 +388,7 @@ private static void checkInherits(final PrintWriter writer, searchPathHelper.outputSearchPath(writer); writer.println(); writer.println("ALTER TABLE " - + PgDiffUtils.getQuotedName(newTable.getName())); + + searchPathHelper.getQuotedName(newTable.getName())); writer.println("\tNO INHERIT " + inheritTableName + ';'); } @@ -414,7 +414,7 @@ private static void checkInherits(final PrintWriter writer, searchPathHelper.outputSearchPath(writer); writer.println(); writer.println("ALTER TABLE " - + PgDiffUtils.getQuotedName(newTable.getName())); + + searchPathHelper.getQuotedName(newTable.getName())); writer.println("\tINHERIT " + inheritTableName + ';'); } @@ -449,7 +449,7 @@ private static void addInheritedColumnDefaults(final PrintWriter writer, if (!oldDefault.equals(newDefault)) { writer.println(); writer.print("ALTER TABLE ONLY "); - writer.println(PgDiffUtils.getQuotedName(newTable.getName())); + writer.println(searchPathHelper.getQuotedName(newTable.getName())); writer.print("\tALTER COLUMN "); writer.print(PgDiffUtils.getQuotedName(newColumn.getInheritedColumn().getName())); if (newDefault.length() == 0) { @@ -486,7 +486,7 @@ private static void checkWithOIDS(final PrintWriter writer, searchPathHelper.outputSearchPath(writer); writer.println(); writer.println("ALTER TABLE " - + PgDiffUtils.getQuotedName(newTable.getName())); + + searchPathHelper.getQuotedName(newTable.getName())); if (newTable.getWith() == null || "OIDS=false".equalsIgnoreCase(newTable.getWith())) { @@ -519,7 +519,7 @@ private static void checkTablespace(final PrintWriter writer, searchPathHelper.outputSearchPath(writer); writer.println(); writer.println("ALTER TABLE " - + PgDiffUtils.getQuotedName(newTable.getName())); + + searchPathHelper.getQuotedName(newTable.getName())); writer.println("\tTABLESPACE " + newTable.getTablespace() + ';'); } @@ -539,23 +539,23 @@ public static void createTables(final PrintWriter writer, || !oldSchema.containsTable(table.getName())) { searchPathHelper.outputSearchPath(writer); writer.println(); - writer.println(table.getCreationSQL(newSchema)); + writer.println(table.getCreationSQL(newSchema, searchPathHelper)); writer.println(); if (table.getOwnerTo() != null) { writer.println("ALTER TABLE " - + PgDiffUtils.getQuotedName(table.getName()) + + searchPathHelper.getQuotedName(table.getName()) + " OWNER TO " + table.getOwnerTo() + ";"); } for (PgRelationPrivilege tablePrivilege : table.getPrivileges()) { searchPathHelper.outputSearchPath(writer); writer.println("REVOKE ALL ON TABLE " - + PgDiffUtils.getQuotedName(table.getName()) + + searchPathHelper.getQuotedName(table.getName()) + " FROM " + tablePrivilege.getRoleName() + ";"); if (!"".equals(tablePrivilege.getPrivilegesSQL(true))) { writer.println("GRANT " + tablePrivilege.getPrivilegesSQL(true) + " ON TABLE " - + PgDiffUtils.getQuotedName(table.getName()) + + searchPathHelper.getQuotedName(table.getName()) + " TO " + tablePrivilege.getRoleName() + " WITH GRANT OPTION;"); } @@ -563,7 +563,7 @@ public static void createTables(final PrintWriter writer, writer.println("GRANT " + tablePrivilege.getPrivilegesSQL(false) + " ON TABLE " - + PgDiffUtils.getQuotedName(table.getName()) + + searchPathHelper.getQuotedName(table.getName()) + " TO " + tablePrivilege.getRoleName() + ";"); } } @@ -591,7 +591,7 @@ public static void dropTables(final PrintWriter writer, if (!newSchema.containsTable(table.getName())) { searchPathHelper.outputSearchPath(writer); writer.println(); - writer.println(table.getDropSQL()); + writer.println(table.getDropSQL(searchPathHelper)); } } } @@ -621,7 +621,7 @@ private static void updateTableColumns(final PrintWriter writer, if (!statements.isEmpty()) { final String quotedTableName = - PgDiffUtils.getQuotedName(newTable.getName()); + searchPathHelper.getQuotedName(newTable.getName()); searchPathHelper.outputSearchPath(writer); writer.println(); writer.println("ALTER " + ((newTable.isForeign()) ? "FOREIGN ":"") + "TABLE " + quotedTableName); @@ -668,7 +668,7 @@ private static void alterPrivilegesColumns(final PrintWriter writer, writer.println("REVOKE ALL (" + PgDiffUtils.getQuotedName(newColumn.getName()) + ") ON TABLE " - + PgDiffUtils.getQuotedName(newTable.getName()) + + searchPathHelper.getQuotedName(newTable.getName()) + " FROM " + oldColumnPrivilege.getRoleName() + ";"); } @@ -691,7 +691,7 @@ private static void alterPrivilegesColumns(final PrintWriter writer, writer.println("REVOKE ALL (" + PgDiffUtils.getQuotedName(newColumn.getName()) + ") ON TABLE " - + PgDiffUtils.getQuotedName(newTable.getName()) + + searchPathHelper.getQuotedName(newTable.getName()) + " FROM " + newColumnPrivilege.getRoleName() + ";"); if (!"".equals(newColumnPrivilege.getPrivilegesSQL( @@ -702,7 +702,7 @@ private static void alterPrivilegesColumns(final PrintWriter writer, PgDiffUtils.getQuotedName(newColumn .getName())) + " ON TABLE " - + PgDiffUtils.getQuotedName(newTable + + searchPathHelper.getQuotedName(newTable .getName()) + " TO " + newColumnPrivilege.getRoleName() + " WITH GRANT OPTION;"); @@ -716,7 +716,7 @@ private static void alterPrivilegesColumns(final PrintWriter writer, .getQuotedName(newColumn .getName())) + " ON TABLE " - + PgDiffUtils.getQuotedName(newTable + + searchPathHelper.getQuotedName(newTable .getName()) + " TO " + newColumnPrivilege.getRoleName() + ";"); } @@ -749,7 +749,7 @@ private static void alterComments(final PrintWriter writer, searchPathHelper.outputSearchPath(writer); writer.println(); writer.print("COMMENT ON TABLE "); - writer.print(PgDiffUtils.getQuotedName(newTable.getName())); + writer.print(searchPathHelper.getQuotedName(newTable.getName())); writer.print(" IS "); writer.print(newTable.getComment()); writer.println(';'); @@ -758,7 +758,7 @@ private static void alterComments(final PrintWriter writer, searchPathHelper.outputSearchPath(writer); writer.println(); writer.print("COMMENT ON TABLE "); - writer.print(PgDiffUtils.getQuotedName(newTable.getName())); + writer.print(searchPathHelper.getQuotedName(newTable.getName())); writer.println(" IS NULL;"); } @@ -773,7 +773,7 @@ private static void alterComments(final PrintWriter writer, searchPathHelper.outputSearchPath(writer); writer.println(); writer.print("COMMENT ON COLUMN "); - writer.print(PgDiffUtils.getQuotedName(newTable.getName())); + writer.print(searchPathHelper.getQuotedName(newTable.getName())); writer.print('.'); writer.print(PgDiffUtils.getQuotedName(newColumn.getName())); writer.print(" IS "); @@ -783,7 +783,7 @@ private static void alterComments(final PrintWriter writer, searchPathHelper.outputSearchPath(writer); writer.println(); writer.print("COMMENT ON COLUMN "); - writer.print(PgDiffUtils.getQuotedName(newTable.getName())); + writer.print(searchPathHelper.getQuotedName(newTable.getName())); writer.print('.'); writer.print(PgDiffUtils.getQuotedName(newColumn.getName())); writer.println(" IS NULL;"); @@ -805,7 +805,7 @@ private static void alterPrivileges(final PrintWriter writer, writer.println(); } writer.println("REVOKE ALL ON TABLE " - + PgDiffUtils.getQuotedName(oldTable.getName()) + + searchPathHelper.getQuotedName(oldTable.getName()) + " FROM " + oldTablePrivilege.getRoleName() + ";"); } else if (!oldTablePrivilege.isSimilar(newTablePrivilege)) { if (!emptyLinePrinted) { @@ -814,13 +814,13 @@ private static void alterPrivileges(final PrintWriter writer, writer.println(); } writer.println("REVOKE ALL ON TABLE " - + PgDiffUtils.getQuotedName(newTable.getName()) + + searchPathHelper.getQuotedName(newTable.getName()) + " FROM " + newTablePrivilege.getRoleName() + ";"); if (!"".equals(newTablePrivilege.getPrivilegesSQL(true))) { writer.println("GRANT " + newTablePrivilege.getPrivilegesSQL(true) + " ON TABLE " - + PgDiffUtils.getQuotedName(newTable.getName()) + + searchPathHelper.getQuotedName(newTable.getName()) + " TO " + newTablePrivilege.getRoleName() + " WITH GRANT OPTION;"); } @@ -828,7 +828,7 @@ private static void alterPrivileges(final PrintWriter writer, writer.println("GRANT " + newTablePrivilege.getPrivilegesSQL(false) + " ON TABLE " - + PgDiffUtils.getQuotedName(newTable.getName()) + + searchPathHelper.getQuotedName(newTable.getName()) + " TO " + newTablePrivilege.getRoleName() + ";"); } } // else similar privilege will not be updated @@ -842,13 +842,13 @@ private static void alterPrivileges(final PrintWriter writer, writer.println(); } writer.println("REVOKE ALL ON TABLE " - + PgDiffUtils.getQuotedName(newTable.getName()) + + searchPathHelper.getQuotedName(newTable.getName()) + " FROM " + newTablePrivilege.getRoleName() + ";"); if (!"".equals(newTablePrivilege.getPrivilegesSQL(true))) { writer.println("GRANT " + newTablePrivilege.getPrivilegesSQL(true) + " ON TABLE " - + PgDiffUtils.getQuotedName(newTable.getName()) + + searchPathHelper.getQuotedName(newTable.getName()) + " TO " + newTablePrivilege.getRoleName() + " WITH GRANT OPTION;"); } @@ -856,7 +856,7 @@ private static void alterPrivileges(final PrintWriter writer, writer.println("GRANT " + newTablePrivilege.getPrivilegesSQL(false) + " ON TABLE " - + PgDiffUtils.getQuotedName(newTable.getName()) + + searchPathHelper.getQuotedName(newTable.getName()) + " TO " + newTablePrivilege.getRoleName() + ";"); } } @@ -873,7 +873,7 @@ private static void alterOwnerTo(final PrintWriter writer, searchPathHelper.outputSearchPath(writer); writer.println(); writer.println("ALTER " + ((newTable.isForeign()) ? "FOREIGN ":"") + "TABLE " - + PgDiffUtils.getQuotedName(newTable.getName()) + + searchPathHelper.getQuotedName(newTable.getName()) + " OWNER TO " + newTable.getOwnerTo() + ";"); } } diff --git a/src/main/java/cz/startnet/utils/pgdiff/PgDiffTriggers.java b/src/main/java/cz/startnet/utils/pgdiff/PgDiffTriggers.java index 5a0e038a..6c86355a 100644 --- a/src/main/java/cz/startnet/utils/pgdiff/PgDiffTriggers.java +++ b/src/main/java/cz/startnet/utils/pgdiff/PgDiffTriggers.java @@ -43,7 +43,7 @@ public static void createTriggers(final PrintWriter writer, for (final PgTrigger trigger : getNewTriggers(oldRelation, newRelation)) { searchPathHelper.outputSearchPath(writer); writer.println(); - writer.println(trigger.getCreationSQL()); + writer.println(trigger.getCreationSQL(searchPathHelper)); } } } @@ -73,7 +73,7 @@ public static void dropTriggers(final PrintWriter writer, getDropTriggers(oldRelation, newRelation)) { searchPathHelper.outputSearchPath(writer); writer.println(); - writer.println(trigger.getDropSQL()); + writer.println(trigger.getDropSQL(searchPathHelper)); } } } @@ -174,7 +174,7 @@ public static void alterComments(final PrintWriter writer, writer.print( PgDiffUtils.getQuotedName(newTrigger.getName())); writer.print(" ON "); - writer.print(PgDiffUtils.getQuotedName( + writer.print(searchPathHelper.getQuotedName( newTrigger.getRelationName())); writer.print(" IS "); writer.print(newTrigger.getComment()); @@ -187,7 +187,7 @@ public static void alterComments(final PrintWriter writer, writer.print( PgDiffUtils.getQuotedName(newTrigger.getName())); writer.print(" ON "); - writer.print(PgDiffUtils.getQuotedName( + writer.print(searchPathHelper.getQuotedName( newTrigger.getRelationName())); writer.println(" IS NULL;"); } diff --git a/src/main/java/cz/startnet/utils/pgdiff/PgDiffTypes.java b/src/main/java/cz/startnet/utils/pgdiff/PgDiffTypes.java index 29b8c1f7..a2be1879 100644 --- a/src/main/java/cz/startnet/utils/pgdiff/PgDiffTypes.java +++ b/src/main/java/cz/startnet/utils/pgdiff/PgDiffTypes.java @@ -179,7 +179,7 @@ public static void createTypes(final PrintWriter writer, || !oldSchema.containsType(type.getName())) { searchPathHelper.outputSearchPath(writer); writer.println(); - writer.println(type.getCreationSQL()); + writer.println(type.getCreationSQL(searchPathHelper)); } } } @@ -203,7 +203,7 @@ public static void dropTypes(final PrintWriter writer, if (!newSchema.containsType(type.getName())) { searchPathHelper.outputSearchPath(writer); writer.println(); - writer.println(type.getDropSQL()); + writer.println(type.getDropSQL(searchPathHelper)); } } } @@ -233,7 +233,7 @@ private static void updateTypeColumns(final PrintWriter writer, if (!statements.isEmpty()) { final String quotedTypeName = - PgDiffUtils.getQuotedName(newType.getName()); + searchPathHelper.getQuotedName(newType.getName()); searchPathHelper.outputSearchPath(writer); writer.println(); writer.println("ALTER TYPE " + quotedTypeName); diff --git a/src/main/java/cz/startnet/utils/pgdiff/PgDiffViews.java b/src/main/java/cz/startnet/utils/pgdiff/PgDiffViews.java index 63422fa7..1f1d8692 100644 --- a/src/main/java/cz/startnet/utils/pgdiff/PgDiffViews.java +++ b/src/main/java/cz/startnet/utils/pgdiff/PgDiffViews.java @@ -38,17 +38,17 @@ public static void createViews(final PrintWriter writer, oldSchema.getView(newView.getName()), newView)) { searchPathHelper.outputSearchPath(writer); writer.println(); - writer.println(newView.getCreationSQL()); + writer.println(newView.getCreationSQL(searchPathHelper)); for (PgRelationPrivilege viewPrivilege : newView.getPrivileges()) { writer.println("REVOKE ALL ON TABLE " - + PgDiffUtils.getQuotedName(newView.getName()) + + searchPathHelper.getQuotedName(newView.getName()) + " FROM " + viewPrivilege.getRoleName() + ";"); if (!"".equals(viewPrivilege.getPrivilegesSQL(true))) { writer.println("GRANT " + viewPrivilege.getPrivilegesSQL(true) + " ON TABLE " - + PgDiffUtils.getQuotedName(newView.getName()) + + searchPathHelper.getQuotedName(newView.getName()) + " TO " + viewPrivilege.getRoleName() + " WITH GRANT OPTION;"); } @@ -56,7 +56,7 @@ public static void createViews(final PrintWriter writer, writer.println("GRANT " + viewPrivilege.getPrivilegesSQL(false) + " ON TABLE " - + PgDiffUtils.getQuotedName(newView.getName()) + + searchPathHelper.getQuotedName(newView.getName()) + " TO " + viewPrivilege.getRoleName() + ";"); } } @@ -86,7 +86,7 @@ public static void dropViews(final PrintWriter writer, if (newView == null || isViewModified(oldView, newView)) { searchPathHelper.outputSearchPath(writer); writer.println(); - writer.println(oldView.getDropSQL()); + writer.println(oldView.getDropSQL(searchPathHelper)); } } } @@ -155,7 +155,7 @@ public static void alterViews(final PrintWriter writer, writer.println(); writer.print("COMMENT ON VIEW "); writer.print( - PgDiffUtils.getQuotedName(newView.getName())); + searchPathHelper.getQuotedName(newView.getName())); writer.print(" IS "); writer.print(newView.getComment()); writer.println(';'); @@ -164,7 +164,7 @@ public static void alterViews(final PrintWriter writer, searchPathHelper.outputSearchPath(writer); writer.println(); writer.print("COMMENT ON VIEW "); - writer.print(PgDiffUtils.getQuotedName(newView.getName())); + writer.print(searchPathHelper.getQuotedName(newView.getName())); writer.println(" IS NULL;"); } @@ -198,7 +198,7 @@ public static void alterViews(final PrintWriter writer, searchPathHelper.outputSearchPath(writer); writer.println(); writer.print("COMMENT ON COLUMN "); - writer.print(PgDiffUtils.getQuotedName(newView.getName())); + writer.print(searchPathHelper.getQuotedName(newView.getName())); writer.print('.'); writer.print(PgDiffUtils.getQuotedName(newCol.getName())); writer.print(" IS "); @@ -209,7 +209,7 @@ public static void alterViews(final PrintWriter writer, searchPathHelper.outputSearchPath(writer); writer.println(); writer.print("COMMENT ON COLUMN "); - writer.print(PgDiffUtils.getQuotedName(newView.getName())); + writer.print(searchPathHelper.getQuotedName(newView.getName())); writer.print('.'); writer.print(PgDiffUtils.getQuotedName(oldCol.getName())); writer.println(" IS NULL;"); @@ -246,7 +246,7 @@ private static void diffDefaultValues(final PrintWriter writer, writer.println(); writer.print("ALTER TABLE "); writer.print( - PgDiffUtils.getQuotedName(newView.getName())); + searchPathHelper.getQuotedName(newView.getName())); writer.print(" ALTER COLUMN "); writer.print(PgDiffUtils.getQuotedName(newCol.getName())); writer.print(" SET DEFAULT "); @@ -257,7 +257,7 @@ private static void diffDefaultValues(final PrintWriter writer, searchPathHelper.outputSearchPath(writer); writer.println(); writer.print("ALTER TABLE "); - writer.print(PgDiffUtils.getQuotedName(newView.getName())); + writer.print(searchPathHelper.getQuotedName(newView.getName())); writer.print(" ALTER COLUMN "); writer.print(PgDiffUtils.getQuotedName(oldCol.getName())); writer.println(" DROP DEFAULT;"); @@ -276,7 +276,7 @@ private static void diffDefaultValues(final PrintWriter writer, searchPathHelper.outputSearchPath(writer); writer.println(); writer.print("ALTER TABLE "); - writer.print(PgDiffUtils.getQuotedName(newView.getName())); + writer.print(searchPathHelper.getQuotedName(newView.getName())); writer.print(" ALTER COLUMN "); writer.print(PgDiffUtils.getQuotedName(newCol.getName())); writer.print(" SET DEFAULT "); @@ -298,7 +298,7 @@ private static void alterPrivileges(final PrintWriter writer, writer.println(); } writer.println("REVOKE ALL ON TABLE " - + PgDiffUtils.getQuotedName(oldView.getName()) + + searchPathHelper.getQuotedName(oldView.getName()) + " FROM " + oldViewPrivilege.getRoleName() + ";"); } else if (!oldViewPrivilege.isSimilar(newViewPrivilege)) { if (!emptyLinePrinted) { @@ -306,13 +306,13 @@ private static void alterPrivileges(final PrintWriter writer, writer.println(); } writer.println("REVOKE ALL ON TABLE " - + PgDiffUtils.getQuotedName(newView.getName()) + + searchPathHelper.getQuotedName(newView.getName()) + " FROM " + newViewPrivilege.getRoleName() + ";"); if (!"".equals(newViewPrivilege.getPrivilegesSQL(true))) { writer.println("GRANT " + newViewPrivilege.getPrivilegesSQL(true) + " ON TABLE " - + PgDiffUtils.getQuotedName(newView.getName()) + + searchPathHelper.getQuotedName(newView.getName()) + " TO " + newViewPrivilege.getRoleName() + " WITH GRANT OPTION;"); } @@ -320,7 +320,7 @@ private static void alterPrivileges(final PrintWriter writer, writer.println("GRANT " + newViewPrivilege.getPrivilegesSQL(false) + " ON TABLE " - + PgDiffUtils.getQuotedName(newView.getName()) + + searchPathHelper.getQuotedName(newView.getName()) + " TO " + newViewPrivilege.getRoleName() + ";"); } } // else similar privilege will not be updated @@ -334,13 +334,13 @@ private static void alterPrivileges(final PrintWriter writer, writer.println(); } writer.println("REVOKE ALL ON TABLE " - + PgDiffUtils.getQuotedName(newView.getName()) + + searchPathHelper.getQuotedName(newView.getName()) + " FROM " + newViewPrivilege.getRoleName() + ";"); if (!"".equals(newViewPrivilege.getPrivilegesSQL(true))) { writer.println("GRANT " + newViewPrivilege.getPrivilegesSQL(true) + " ON TABLE " - + PgDiffUtils.getQuotedName(newView.getName()) + + searchPathHelper.getQuotedName(newView.getName()) + " TO " + newViewPrivilege.getRoleName() + " WITH GRANT OPTION;"); } @@ -348,7 +348,7 @@ private static void alterPrivileges(final PrintWriter writer, writer.println("GRANT " + newViewPrivilege.getPrivilegesSQL(false) + " ON TABLE " - + PgDiffUtils.getQuotedName(newView.getName()) + + searchPathHelper.getQuotedName(newView.getName()) + " TO " + newViewPrivilege.getRoleName() + ";"); } } diff --git a/src/main/java/cz/startnet/utils/pgdiff/SearchPathHelper.java b/src/main/java/cz/startnet/utils/pgdiff/SearchPathHelper.java index c5891232..5d4348bb 100644 --- a/src/main/java/cz/startnet/utils/pgdiff/SearchPathHelper.java +++ b/src/main/java/cz/startnet/utils/pgdiff/SearchPathHelper.java @@ -16,18 +16,40 @@ public class SearchPathHelper { * Statement to output. */ private final String searchPath; + /** + * Name of current schema. + */ + private final String schema; /** * Flag determining whether the statement was already output. */ private boolean wasOutput; + /** + * Flag determining whether statements should be schema-qualified. + */ + private boolean schemaQualify; + /** + * Flag determining whether anything should be output at all. + */ + private boolean setSearchPath; /** * Creates new instance of SearchPathHelper. * - * @param searchPath {@link #searchPath} + * @param schema {@link #schema} + * @param schemaQualify {@link #schemaQualify} + * @param setSearchPath {@link #setSearchPath} */ - public SearchPathHelper(final String searchPath) { - this.searchPath = searchPath; + public SearchPathHelper(final String schema, final boolean schemaQualify, final boolean setSearchPath) { + this.schema = PgDiffUtils.getQuotedName(schema, true); + this.schemaQualify = schemaQualify; + this.searchPath = "SET search_path = " + PgDiffUtils.getQuotedName(schema, true) + + ", pg_catalog;"; + this.setSearchPath = setSearchPath; + + if (schemaQualify) { + wasOutput = true; + } } /** @@ -36,10 +58,22 @@ public SearchPathHelper(final String searchPath) { * @param writer writer */ public void outputSearchPath(final PrintWriter writer) { - if (!wasOutput && searchPath != null && !searchPath.isEmpty()) { + if (!wasOutput && setSearchPath) { writer.println(); writer.println(searchPath); wasOutput = true; } } + + + public String getQuotedName(String objectName) { + if (schemaQualify) { + return schema + "." + PgDiffUtils.getQuotedName(objectName); + } + else { + return PgDiffUtils.getQuotedName(objectName); + } + } + + } diff --git a/src/main/java/cz/startnet/utils/pgdiff/schema/PgConstraint.java b/src/main/java/cz/startnet/utils/pgdiff/schema/PgConstraint.java index 281389e9..d3865217 100644 --- a/src/main/java/cz/startnet/utils/pgdiff/schema/PgConstraint.java +++ b/src/main/java/cz/startnet/utils/pgdiff/schema/PgConstraint.java @@ -6,6 +6,8 @@ package cz.startnet.utils.pgdiff.schema; import cz.startnet.utils.pgdiff.PgDiffUtils; +import cz.startnet.utils.pgdiff.SearchPathHelper; + import java.util.regex.Pattern; /** @@ -51,10 +53,10 @@ public PgConstraint(String name) { * * @return created SQL */ - public String getCreationSQL() { + public String getCreationSQL(SearchPathHelper searchPathHelper) { final StringBuilder sbSQL = new StringBuilder(100); sbSQL.append("ALTER TABLE "); - sbSQL.append(PgDiffUtils.getQuotedName(getTableName())); + sbSQL.append(searchPathHelper.getQuotedName(getTableName())); sbSQL.append(System.getProperty("line.separator")); sbSQL.append("\tADD CONSTRAINT "); sbSQL.append(PgDiffUtils.getQuotedName(getName())); @@ -68,7 +70,7 @@ public String getCreationSQL() { sbSQL.append("COMMENT ON CONSTRAINT "); sbSQL.append(PgDiffUtils.getQuotedName(name)); sbSQL.append(" ON "); - sbSQL.append(PgDiffUtils.getQuotedName(tableName)); + sbSQL.append(searchPathHelper.getQuotedName(tableName)); sbSQL.append(" IS "); sbSQL.append(comment); sbSQL.append(';'); @@ -118,10 +120,10 @@ public String getDefinition() { * * @return created SQL */ - public String getDropSQL() { + public String getDropSQL(SearchPathHelper searchPathHelper) { final StringBuilder sbSQL = new StringBuilder(100); sbSQL.append("ALTER TABLE "); - sbSQL.append(PgDiffUtils.getQuotedName(getTableName())); + sbSQL.append(searchPathHelper.getQuotedName(getTableName())); sbSQL.append(System.getProperty("line.separator")); sbSQL.append("\tDROP CONSTRAINT "); sbSQL.append(PgDiffUtils.getQuotedName(getName())); diff --git a/src/main/java/cz/startnet/utils/pgdiff/schema/PgFunction.java b/src/main/java/cz/startnet/utils/pgdiff/schema/PgFunction.java index 794fc418..86560e5d 100644 --- a/src/main/java/cz/startnet/utils/pgdiff/schema/PgFunction.java +++ b/src/main/java/cz/startnet/utils/pgdiff/schema/PgFunction.java @@ -6,6 +6,8 @@ package cz.startnet.utils.pgdiff.schema; import cz.startnet.utils.pgdiff.PgDiffUtils; +import cz.startnet.utils.pgdiff.SearchPathHelper; + import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -59,10 +61,10 @@ public void setComment(final String comment) { * * @return creation SQL */ - public String getCreationSQL() { + public String getCreationSQL(SearchPathHelper searchPathHelper) { final StringBuilder sbSQL = new StringBuilder(500); sbSQL.append("CREATE OR REPLACE FUNCTION "); - sbSQL.append(PgDiffUtils.getQuotedName(name)); + sbSQL.append(searchPathHelper.getQuotedName(name)); sbSQL.append('('); boolean addComma = false; @@ -85,7 +87,7 @@ public String getCreationSQL() { sbSQL.append(System.getProperty("line.separator")); sbSQL.append(System.getProperty("line.separator")); sbSQL.append("COMMENT ON FUNCTION "); - sbSQL.append(PgDiffUtils.getQuotedName(name)); + sbSQL.append(searchPathHelper.getQuotedName(name)); sbSQL.append('('); addComma = false; @@ -131,10 +133,10 @@ public String getBody() { * * @return created SQL */ - public String getDropSQL() { + public String getDropSQL(SearchPathHelper searchPathHelper) { final StringBuilder sbString = new StringBuilder(100); sbString.append("DROP FUNCTION "); - sbString.append(name); + sbString.append(searchPathHelper.getQuotedName(name)); sbString.append('('); boolean addComma = false; diff --git a/src/main/java/cz/startnet/utils/pgdiff/schema/PgIndex.java b/src/main/java/cz/startnet/utils/pgdiff/schema/PgIndex.java index 661f4b38..42034569 100644 --- a/src/main/java/cz/startnet/utils/pgdiff/schema/PgIndex.java +++ b/src/main/java/cz/startnet/utils/pgdiff/schema/PgIndex.java @@ -6,6 +6,7 @@ package cz.startnet.utils.pgdiff.schema; import cz.startnet.utils.pgdiff.PgDiffUtils; +import cz.startnet.utils.pgdiff.SearchPathHelper; /** * Stores table index information. @@ -67,7 +68,7 @@ public void setComment(final String comment) { * * @return created SQL */ - public String getCreationSQL() { + public String getCreationSQL(SearchPathHelper searchPathHelper) { final StringBuilder sbSQL = new StringBuilder(100); sbSQL.append("CREATE "); @@ -78,7 +79,7 @@ public String getCreationSQL() { sbSQL.append("INDEX "); sbSQL.append(PgDiffUtils.getQuotedName(getName())); sbSQL.append(" ON "); - sbSQL.append(PgDiffUtils.getQuotedName(getTableName())); + sbSQL.append(searchPathHelper.getQuotedName(getTableName())); sbSQL.append(' '); sbSQL.append(getDefinition()); sbSQL.append(';'); @@ -87,7 +88,7 @@ public String getCreationSQL() { sbSQL.append(System.getProperty("line.separator")); sbSQL.append(System.getProperty("line.separator")); sbSQL.append("COMMENT ON INDEX "); - sbSQL.append(PgDiffUtils.getQuotedName(name)); + sbSQL.append(searchPathHelper.getQuotedName(name)); sbSQL.append(" IS "); sbSQL.append(comment); sbSQL.append(';'); @@ -119,8 +120,8 @@ public String getDefinition() { * * @return created SQL statement */ - public String getDropSQL() { - return "DROP INDEX " + PgDiffUtils.getQuotedName(getName()) + ";"; + public String getDropSQL(SearchPathHelper searchPathHelper) { + return "DROP INDEX " + searchPathHelper.getQuotedName(getName()) + ";"; } /** diff --git a/src/main/java/cz/startnet/utils/pgdiff/schema/PgRelation.java b/src/main/java/cz/startnet/utils/pgdiff/schema/PgRelation.java index eaa29c00..f2871629 100644 --- a/src/main/java/cz/startnet/utils/pgdiff/schema/PgRelation.java +++ b/src/main/java/cz/startnet/utils/pgdiff/schema/PgRelation.java @@ -6,6 +6,7 @@ package cz.startnet.utils.pgdiff.schema; import cz.startnet.utils.pgdiff.PgDiffUtils; +import cz.startnet.utils.pgdiff.SearchPathHelper; import java.util.ArrayList; import java.util.Collections; @@ -109,7 +110,7 @@ public List getColumns() { * * @return SQL code for declaring relation and column comments */ - protected String getCommentDefinitionSQL() { + protected String getCommentDefinitionSQL(SearchPathHelper searchPathHelper) { final StringBuilder sbSQL = new StringBuilder(100); if (comment != null && !comment.isEmpty()) { @@ -118,7 +119,7 @@ protected String getCommentDefinitionSQL() { sbSQL.append("COMMENT ON "); sbSQL.append(getRelationKind()); sbSQL.append(' '); - sbSQL.append(PgDiffUtils.getQuotedName(name)); + sbSQL.append(searchPathHelper.getQuotedName(name)); sbSQL.append(" IS "); sbSQL.append(comment); sbSQL.append(';'); @@ -129,7 +130,7 @@ protected String getCommentDefinitionSQL() { sbSQL.append(System.getProperty("line.separator")); sbSQL.append(System.getProperty("line.separator")); sbSQL.append("COMMENT ON COLUMN "); - sbSQL.append(PgDiffUtils.getQuotedName(name)); + sbSQL.append(searchPathHelper.getQuotedName(name)); sbSQL.append('.'); sbSQL.append(PgDiffUtils.getQuotedName(column.getName())); sbSQL.append(" IS "); @@ -286,9 +287,9 @@ public void addTrigger(final PgTrigger trigger) { * * @return created SQL statement */ - public String getDropSQL() { + public String getDropSQL(SearchPathHelper searchPathHelper) { return "DROP " + getRelationKind() + " " + - PgDiffUtils.getQuotedName(getName()) + ";"; + searchPathHelper.getQuotedName(getName()) + ";"; } /** 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 0a26e121..01315466 100644 --- a/src/main/java/cz/startnet/utils/pgdiff/schema/PgSequence.java +++ b/src/main/java/cz/startnet/utils/pgdiff/schema/PgSequence.java @@ -10,6 +10,7 @@ import java.util.List; import cz.startnet.utils.pgdiff.PgDiffUtils; +import cz.startnet.utils.pgdiff.SearchPathHelper; /** * Stores sequence information. @@ -110,10 +111,10 @@ public void setComment(final String comment) { * * @return created SQL statement */ - public String getCreationSQL() { + public String getCreationSQL(SearchPathHelper searchPathHelper) { final StringBuilder sbSQL = new StringBuilder(100); sbSQL.append("CREATE SEQUENCE "); - sbSQL.append(PgDiffUtils.getQuotedName(name)); + sbSQL.append(searchPathHelper.getQuotedName(name)); if (startWith != null) { sbSQL.append(System.getProperty("line.separator")); @@ -164,7 +165,7 @@ public String getCreationSQL() { sbSQL.append(System.getProperty("line.separator")); sbSQL.append(System.getProperty("line.separator")); sbSQL.append("COMMENT ON SEQUENCE "); - sbSQL.append(PgDiffUtils.getQuotedName(name)); + sbSQL.append(searchPathHelper.getQuotedName(name)); sbSQL.append(" IS "); sbSQL.append(comment); sbSQL.append(';'); @@ -178,11 +179,11 @@ public String getCreationSQL() { * * @return created SQL statement */ - public String getOwnedBySQL() { + public String getOwnedBySQL(SearchPathHelper searchPathHelper) { final StringBuilder sbSQL = new StringBuilder(100); sbSQL.append("ALTER SEQUENCE "); - sbSQL.append(PgDiffUtils.getQuotedName(name)); + sbSQL.append(searchPathHelper.getQuotedName(name)); if (ownedBy != null && !ownedBy.isEmpty()) { sbSQL.append(System.getProperty("line.separator")); @@ -218,8 +219,8 @@ public boolean isCycle() { * * @return created SQL */ - public String getDropSQL() { - return "DROP SEQUENCE " + PgDiffUtils.getQuotedName(getName()) + ";"; + public String getDropSQL(SearchPathHelper searchPathHelper) { + return "DROP SEQUENCE " + searchPathHelper.getQuotedName(getName()) + ";"; } /** diff --git a/src/main/java/cz/startnet/utils/pgdiff/schema/PgTable.java b/src/main/java/cz/startnet/utils/pgdiff/schema/PgTable.java index 93778e48..f6e607b4 100644 --- a/src/main/java/cz/startnet/utils/pgdiff/schema/PgTable.java +++ b/src/main/java/cz/startnet/utils/pgdiff/schema/PgTable.java @@ -7,6 +7,8 @@ import cz.startnet.utils.pgdiff.Pair; import cz.startnet.utils.pgdiff.PgDiffUtils; +import cz.startnet.utils.pgdiff.SearchPathHelper; + import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -112,7 +114,7 @@ public String getRelationKind() { * * @return created SQL statement */ - public String getCreationSQL(final PgSchema schema) { + public String getCreationSQL(final PgSchema schema, SearchPathHelper searchPathHelper) { final StringBuilder sbSQL = new StringBuilder(1000); sbSQL.append("CREATE "); if (isUnlogged()) { @@ -122,7 +124,7 @@ public String getCreationSQL(final PgSchema schema) { sbSQL.append("FOREIGN "); } sbSQL.append("TABLE "); - sbSQL.append(PgDiffUtils.getQuotedName(name)); + sbSQL.append(searchPathHelper.getQuotedName(name)); sbSQL.append(" ("); sbSQL.append(System.getProperty("line.separator")); @@ -207,7 +209,7 @@ public String getCreationSQL(final PgSchema schema) { sbSQL.append(System.getProperty("line.separator")); sbSQL.append(System.getProperty("line.separator")); sbSQL.append("ALTER TABLE ONLY "); - sbSQL.append(PgDiffUtils.getQuotedName(name)); + sbSQL.append(searchPathHelper.getQuotedName(name)); sbSQL.append(System.getProperty("line.separator")); sbSQL.append("\tALTER COLUMN "); sbSQL.append( @@ -221,7 +223,7 @@ public String getCreationSQL(final PgSchema schema) { for (PgColumn column : getColumnsWithStatistics()) { sbSQL.append(System.getProperty("line.separator")); sbSQL.append("ALTER TABLE ONLY "); - sbSQL.append(PgDiffUtils.getQuotedName(name)); + sbSQL.append(searchPathHelper.getQuotedName(name)); sbSQL.append(" ALTER COLUMN "); sbSQL.append( PgDiffUtils.getQuotedName(column.getName())); @@ -230,7 +232,7 @@ public String getCreationSQL(final PgSchema schema) { sbSQL.append(';'); } - sbSQL.append(getCommentDefinitionSQL()); + sbSQL.append(getCommentDefinitionSQL(searchPathHelper)); return sbSQL.toString(); } @@ -419,9 +421,9 @@ public void setUnlogged(boolean unlogged) { */ @Override - public String getDropSQL() { + public String getDropSQL(SearchPathHelper searchPathHelper) { return "DROP " + ((isForeign()) ? "FOREIGN ":"") + getRelationKind() + " " + - PgDiffUtils.getQuotedName(getName()) + ";"; + searchPathHelper.getQuotedName(getName()) + ";"; } public boolean isForeign() { diff --git a/src/main/java/cz/startnet/utils/pgdiff/schema/PgTrigger.java b/src/main/java/cz/startnet/utils/pgdiff/schema/PgTrigger.java index 007d67b0..e64712fb 100644 --- a/src/main/java/cz/startnet/utils/pgdiff/schema/PgTrigger.java +++ b/src/main/java/cz/startnet/utils/pgdiff/schema/PgTrigger.java @@ -6,6 +6,8 @@ package cz.startnet.utils.pgdiff.schema; import cz.startnet.utils.pgdiff.PgDiffUtils; +import cz.startnet.utils.pgdiff.SearchPathHelper; + import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -137,7 +139,7 @@ public void setComment(final String comment) { * * @return created SQL */ - public String getCreationSQL() { + public String getCreationSQL(SearchPathHelper searchPathHelper) { final StringBuilder sbSQL = new StringBuilder(100); sbSQL.append("CREATE TRIGGER "); sbSQL.append(PgDiffUtils.getQuotedName(getName())); @@ -196,7 +198,7 @@ public String getCreationSQL() { } sbSQL.append(" ON "); - sbSQL.append(PgDiffUtils.getQuotedName(getRelationName())); + sbSQL.append(searchPathHelper.getQuotedName(getRelationName())); sbSQL.append(System.getProperty("line.separator")); sbSQL.append("\tFOR EACH "); sbSQL.append(isForEachRow() ? "ROW" : "STATEMENT"); @@ -219,7 +221,7 @@ public String getCreationSQL() { sbSQL.append("COMMENT ON TRIGGER "); sbSQL.append(PgDiffUtils.getQuotedName(name)); sbSQL.append(" ON "); - sbSQL.append(PgDiffUtils.getQuotedName(relationName)); + sbSQL.append(searchPathHelper.getQuotedName(relationName)); sbSQL.append(" IS "); sbSQL.append(comment); sbSQL.append(';'); @@ -233,9 +235,9 @@ public String getCreationSQL() { * * @return created SQL */ - public String getDropSQL() { + public String getDropSQL(SearchPathHelper searchPathHelper) { return "DROP TRIGGER " + PgDiffUtils.getQuotedName(getName()) + " ON " - + PgDiffUtils.getQuotedName(getRelationName()) + ";"; + + searchPathHelper.getQuotedName(getRelationName()) + ";"; } /** diff --git a/src/main/java/cz/startnet/utils/pgdiff/schema/PgType.java b/src/main/java/cz/startnet/utils/pgdiff/schema/PgType.java index 0953de1c..f7878ac3 100644 --- a/src/main/java/cz/startnet/utils/pgdiff/schema/PgType.java +++ b/src/main/java/cz/startnet/utils/pgdiff/schema/PgType.java @@ -6,6 +6,8 @@ package cz.startnet.utils.pgdiff.schema; import cz.startnet.utils.pgdiff.PgDiffUtils; +import cz.startnet.utils.pgdiff.SearchPathHelper; + import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -71,10 +73,10 @@ public List getColumns() { * * @return created SQL statement */ - public String getCreationSQL() { + public String getCreationSQL(SearchPathHelper searchPathHelper) { final StringBuilder sbSQL = new StringBuilder(1000); sbSQL.append("CREATE TYPE "); - sbSQL.append(PgDiffUtils.getQuotedName(name)); + sbSQL.append(searchPathHelper.getQuotedName(name)); if (isEnum) { sbSQL.append(" AS ENUM ("); } else { @@ -121,7 +123,7 @@ public String getCreationSQL() { for (PgColumn column : getColumnsWithStatistics()) { sbSQL.append(System.getProperty("line.separator")); sbSQL.append("ALTER TABLE ONLY "); - sbSQL.append(PgDiffUtils.getQuotedName(name)); + sbSQL.append(searchPathHelper.getQuotedName(name)); sbSQL.append(" ALTER COLUMN "); sbSQL.append( PgDiffUtils.getQuotedName(column.getName())); @@ -136,8 +138,8 @@ public String getCreationSQL() { * * @return created SQL statement */ - public String getDropSQL() { - return "DROP TYPE " + PgDiffUtils.getQuotedName(getName()) + ";"; + public String getDropSQL(SearchPathHelper searchPathHelper) { + return "DROP TYPE " + searchPathHelper.getQuotedName(getName()) + ";"; } /** diff --git a/src/main/java/cz/startnet/utils/pgdiff/schema/PgView.java b/src/main/java/cz/startnet/utils/pgdiff/schema/PgView.java index 043ee1b5..0630e141 100644 --- a/src/main/java/cz/startnet/utils/pgdiff/schema/PgView.java +++ b/src/main/java/cz/startnet/utils/pgdiff/schema/PgView.java @@ -6,6 +6,8 @@ package cz.startnet.utils.pgdiff.schema; import cz.startnet.utils.pgdiff.PgDiffUtils; +import cz.startnet.utils.pgdiff.SearchPathHelper; + import java.util.ArrayList; import java.util.List; @@ -94,12 +96,12 @@ public String getRelationKind() { * * @return created SQL statement */ - public String getCreationSQL() { + public String getCreationSQL(SearchPathHelper searchPathHelper) { final StringBuilder sbSQL = new StringBuilder(query.length() * 2); sbSQL.append("CREATE "); sbSQL.append(getRelationKind()); sbSQL.append(' '); - sbSQL.append(PgDiffUtils.getQuotedName(name)); + sbSQL.append(searchPathHelper.getQuotedName(name)); if (declareColumnNames) { assert columns != null && !columns.isEmpty(); @@ -141,7 +143,7 @@ public String getCreationSQL() { } } - sbSQL.append(getCommentDefinitionSQL()); + sbSQL.append(getCommentDefinitionSQL(searchPathHelper)); return sbSQL.toString(); } diff --git a/src/main/resources/cz/startnet/utils/pgdiff/Resources.properties b/src/main/resources/cz/startnet/utils/pgdiff/Resources.properties index 25b73511..8f734354 100644 --- a/src/main/resources/cz/startnet/utils/pgdiff/Resources.properties +++ b/src/main/resources/cz/startnet/utils/pgdiff/Resources.properties @@ -41,6 +41,10 @@ ${tab}statements).\n\ ${tab}when parsing SQL statements, ignores Slony triggers named\n\ ${tab}_slony_logtrigger and _slony_denyaccess.\n\ \n\ +--schema-qualify-objects:\n\ +${tab}instead of adding 'SET search_path' statements prior to diff output\n\ +${tab}(the default behaviour), all statements will be schema-qualified.\n\ +\n\ --list-charsets\n\ ${tab}lists all supported charsets Version=Version diff --git a/src/test/java/cz/startnet/utils/pgdiff/PgDiffTest.java b/src/test/java/cz/startnet/utils/pgdiff/PgDiffTest.java index fdd9c72d..4e9159e1 100644 --- a/src/test/java/cz/startnet/utils/pgdiff/PgDiffTest.java +++ b/src/test/java/cz/startnet/utils/pgdiff/PgDiffTest.java @@ -38,222 +38,216 @@ public static Collection parameters() { return Arrays.asList( new Object[][]{ // Tests scenario where COLUMN type is modified. - {"modify_column_type", false, false, false, false}, + {"modify_column_type", false, false, false, false, false}, // Tests scenario where CLUSTER is added to TABLE. - {"add_cluster", false, false, false, false}, + {"add_cluster", false, false, false, false, false}, // Tests scenario where CLUSTER is dropped from TABLE. - {"drop_cluster", false, false, false, false}, + {"drop_cluster", false, false, false, false, false}, // Tests scenario where CLUSTER is changed on TABLE. - {"modify_cluster", false, false, false, false}, + {"modify_cluster", false, false, false, false, false}, // Tests scenario where EXTENSION is added to TABLE. - {"add_extension", false, false, false, false}, + {"add_extension", false, false, false, false, false}, // Tests scenario where EXTENSION is dropped from TABLE. - {"drop_extension", false, false, false, false}, + {"drop_extension", false, false, false, false, false}, // Tests scenario where WITH OIDS is dropped from TABLE. - {"drop_with_oids", false, false, false, false}, + {"drop_with_oids", false, false, false, false, false}, // Tests scenario where INDEX is added. - {"add_index", false, false, false, false}, + {"add_index", false, false, false, false, false}, // Tests scenario where INDEX is dropped. - {"drop_index", false, false, false, false}, + {"drop_index", false, false, false, false, false}, // Tests scenario where INDEX that TABLE CLUSTER is based // on is dropped. - {"drop_index_with_cluster", false, false, false, false}, + {"drop_index_with_cluster", false, false, false, false, false}, // Tests scenario where INDEX definition is modified. - {"modify_index", false, false, false, false}, + {"modify_index", false, false, false, false, false}, // Tests scenario where STATISTICS information is added // to COLUMN. - {"add_statistics", false, false, false, false}, + {"add_statistics", false, false, false, false, false}, // Tests scenario where STATISTICS information is modified. - {"modify_statistics", false, false, false, false}, + {"modify_statistics", false, false, false, false, false}, // Tests scenario where STATISTICS information is dropped. - {"drop_statistics", false, false, false, false}, + {"drop_statistics", false, false, false, false, false}, // Tests scenario where DEFAULT value is set on COLUMN. - {"add_default_value", false, false, false, false}, + {"add_default_value", false, false, false, false, false}, // Tests scenario where DEFAULT value is modified. - {"modify_default_value", false, false, false, false}, + {"modify_default_value", false, false, false, false, false}, // Tests scenario where DEFAULT value is dropped from COLUMN. - {"drop_default_value", false, false, false, false}, + {"drop_default_value", false, false, false, false, false}, // Tests scenario where NOT NULL constraint is set on COLUMN. - {"add_not_null", false, false, false, false}, + {"add_not_null", false, false, false, false, false}, // Tests scenario where NOT NULL constraint is dropped // from COLUMN.MATERIALIZED - {"drop_not_null", false, false, false, false}, + {"drop_not_null", false, false, false, false, false}, // Tests scenario where COLUMN is added to TABLE definition. - {"add_column", false, false, false, false}, + {"add_column", false, false, false, false, false}, // Tests scenario where COLUMN is dropped from TABLE. - {"drop_column", false, false, false, false}, + {"drop_column", false, false, false, false, false}, // Tests scenario where new TABLE is added. - {"add_table", false, false, false, false}, + {"add_table", false, false, false, false, false}, // Tests scenario where TABLE is dropped. - {"drop_table", false, false, false, false}, + {"drop_table", false, false, false, false, false}, // Tests scenario where TABLE CONSTRAINT is added. - {"add_constraint", false, false, false, false}, + {"add_constraint", false, false, false, false, false}, // Tests scenario where TABLE CONSTRAINT is modified. - {"modify_constraint", false, false, false, false}, + {"modify_constraint", false, false, false, false, false}, // Tests scenario where TABLE CONSTRAINT is dropped. - {"drop_constraint", false, false, false, false}, + {"drop_constraint", false, false, false, false, false}, // Tests scenario where UNIQUE TABLE CONSTRAINT is added. - {"add_unique_constraint", false, false, false, true}, + {"add_unique_constraint", false, false, false, true, false}, // Tests reading of TABLE with INHERITS. - {"read_inherits", false, false, false, false}, + {"read_inherits", false, false, false, false, false}, // Tests scenario where TABLE with INHERITS is added. - {"add_inherits", false, false, false, false}, + {"add_inherits", false, false, false, false, false}, // Tests scenario where original and new TABLE contain //different INHERITS. - {"modify_inherits", false, false, false, false}, + {"modify_inherits", false, false, false, false, false}, // Add a table with a default value for a column that belongs to an inherited table - {"add_inherits_default_column", false, false, false, false}, + {"add_inherits_default_column", false, false, false, false, false}, // Add a table with a default value for a column that belongs to an inherited table in a different schema - {"add_inherits_schema_default_column", false, false, false, false}, + {"add_inherits_schema_default_column", false, false, false, false, false}, // Tests when a default value is changed for a column that belongs to an inherited table - {"modify_default_value_inherited_column", false, false, false, false}, + {"modify_default_value_inherited_column", false, false, false, false, false}, // Tests scenario where SEQUENCE is added. - {"add_sequence", false, false, false, false}, + {"add_sequence", false, false, false, false, false}, // Tests scenario where SEQUENCE is dropped. - {"drop_sequence", false, false, false, false}, + {"drop_sequence", false, false, false, false, false}, // Tests scenario where INCREMENT BY is modified on SEQUENCE. - {"modify_sequence_increment", false, false, false, false}, + {"modify_sequence_increment", false, false, false, false, false}, // Tests scenario where START WITH is modified on SEQUENCE // (both with --ignore-start-with turned off and on). - {"modify_sequence_start_ignore_off", false, false, false, - false - }, - {"modify_sequence_start_ignore_on", false, false, false, - true - }, + {"modify_sequence_start_ignore_off", false, false, false, false, false}, + {"modify_sequence_start_ignore_on", false, false, false, true, false}, // Tests scenario where MINVALUE is modified on SEQUENCE // (both setting and unsetting the value). - {"modify_sequence_minvalue_set", false, false, false, false}, - {"modify_sequence_minvalue_unset", false, false, false, - false - }, + {"modify_sequence_minvalue_set", false, false, false, false, false}, + {"modify_sequence_minvalue_unset", false, false, false, false, false}, // Tests scenario where MAXVALUE is modified on SEQUENCE // (both setting and unsetting the value). - {"modify_sequence_maxvalue_set", false, false, false, false}, - {"modify_sequence_maxvalue_unset", false, false, false, - false - }, + {"modify_sequence_maxvalue_set", false, false, false, false, false}, + {"modify_sequence_maxvalue_unset", false, false, false, false, false}, // Tests scenario where CACHE is modified on SEQUENCE. - {"modify_sequence_cache", false, false, false, false}, + {"modify_sequence_cache", false, false, false, false, false}, // Tests scenario where CYCLE is modified on SEQUENCE. - {"modify_sequence_cycle_on", false, false, false, false}, - {"modify_sequence_cycle_off", false, false, false, false}, + {"modify_sequence_cycle_on", false, false, false, false, false}, + {"modify_sequence_cycle_off", false, false, false, false, false}, // Tests correct finding of function end. - {"modify_function_end_detection", false, false, false, false}, + {"modify_function_end_detection", false, false, false, false, false}, // Tests scenario where new FUNCTION without args is added. - {"add_function_noargs", false, false, false, false}, + {"add_function_noargs", false, false, false, false, false}, // Tests scenario where FUNCTION without args is dropped. - {"drop_function_noargs", false, false, false, false}, + {"drop_function_noargs", false, false, false, false, false}, // Tests scenario where FUNCTION without args is modified. - {"modify_function_noargs", false, false, false, false}, + {"modify_function_noargs", false, false, false, false, false}, // Tests scenario where new FUNCTION with args is added. - {"add_function_args", false, false, false, false}, + {"add_function_args", false, false, false, false, false}, // Tests scenario where FUNCTION with args is dropped. - {"drop_function_args", false, false, false, false}, + {"drop_function_args", false, false, false, false, false}, // Tests scenario where FUNCTION with args is modified. - {"modify_function_args", false, false, false, false}, + {"modify_function_args", false, false, false, false, false}, // Tests scenario where new FUNCTION with args is added. - {"add_function_args2", false, false, false, false}, + {"add_function_args2", false, false, false, false, false}, // Tests scenario where FUNCTION with args is dropped. - {"drop_function_args2", false, false, false, false}, + {"drop_function_args2", false, false, false, false, false}, // Tests scenario where FUNCTION with args is modified. - {"modify_function_args2", false, false, false, false}, + {"modify_function_args2", false, false, false, false, false}, // Tests scenario where FUNCTION with same name but // different args is added. - {"add_function_similar", false, false, false, false}, + {"add_function_similar", false, false, false, false, false}, // Tests scenario where FUNCTION with same name but // different args is dropped. - {"drop_function_similar", false, false, false, false}, + {"drop_function_similar", false, false, false, false, false}, // Tests scenario where FUNCTION with same name but // different args is modified. - {"modify_function_similar", false, false, false, false}, + {"modify_function_similar", false, false, false, false, false}, // Tests different whitespace formatting in functions - {"function_equal_whitespace", false, false, true, false}, + {"function_equal_whitespace", false, false, true, false, false}, // Tests scenario where TRIGGER is added. - {"add_trigger", false, false, false, false}, + {"add_trigger", false, false, false, false, false}, // Tests scenario where TRIGGER is dropped. - {"drop_trigger", false, false, false, false}, + {"drop_trigger", false, false, false, false, false}, // Tests scenario where TRIGGER is modified. - {"modify_trigger", false, false, false, false}, + {"modify_trigger", false, false, false, false, false}, // Tests scenario where VIEW is added. - {"add_view", false, false, false, false}, + {"add_view", false, false, false, false, false}, // Tests scenario where VIEW is dropped. - {"drop_view", false, false, false, false}, + {"drop_view", false, false, false, false, false}, // Tests scenario where VIEW is modified. - {"modify_view", false, false, false, false}, + {"modify_view", false, false, false, false, false}, // Tests scenario where MATERIALIZED VIEW is added. - {"add_materialized_view", false, false, false, false}, + {"add_materialized_view", false, false, false, false, false}, // Tests scenario where MATERIALIZED VIEW is dropped. - {"drop_materialized_view", false, false, false, false}, + {"drop_materialized_view", false, false, false, false, false}, // Tests scenario where MATERIALIZED VIEW is modified. - {"modify_materialized_view", false, false, false, false}, + {"modify_materialized_view", false, false, false, false, false}, // Tests scenario where --add-defaults is specified. - {"add_defaults", true, false, false, false}, + {"add_defaults", true, false, false, false, false}, // Tests scenario where multiple schemas are in the dumps. - {"multiple_schemas", false, false, false, false}, + {"multiple_schemas", false, false, false, false, false}, // Tests scenario where --add-transaction is specified. - {"multiple_schemas", false, true, false, false}, + {"multiple_schemas", false, true, false, false, false}, // Tests dropping view default value - {"alter_view_drop_default", false, true, false, false}, + {"alter_view_drop_default", false, true, false, false, false}, // Tests adding view default value - {"alter_view_add_default", false, true, false, false}, + {"alter_view_add_default", false, true, false, false, false}, // Tests adding of comments - {"add_comments", false, true, false, false}, + {"add_comments", false, true, false, false, false}, // Tests dropping of comments - {"drop_comments", false, true, false, false}, + {"drop_comments", false, true, false, false, false}, // Tests altering of comments - {"alter_comments", false, true, false, false}, + {"alter_comments", false, true, false, false, false}, // Tests changing view default value - {"alter_view_change_default", false, true, false, false}, + {"alter_view_change_default", false, true, false, false, false}, // Tests creation of sequence with bug in MINVALUE value - {"add_sequence_bug2100013", false, true, false, false}, + {"add_sequence_bug2100013", false, true, false, false, false}, // Tests view with default value - {"view_bug3080388", false, true, false, false}, + {"view_bug3080388", false, true, false, false, false}, // Tests function arguments beginning with in_ - {"function_bug3084274", false, true, false, false}, + {"function_bug3084274", false, true, false, false, false}, // Tests addition of comment when new column has been added - {"add_comment_new_column", false, true, false, false}, + {"add_comment_new_column", false, true, false, false, false}, // Tests handling of quoted schemas in search_path - {"quoted_schema", false, true, false, false}, + {"quoted_schema", false, true, false, false, false}, // Tests adding new column with add defaults turned on - {"add_column_add_defaults", true, true, false, false}, + {"add_column_add_defaults", true, true, false, false, false}, // Tests adding new sequence that is owned by table - {"add_owned_sequence", false, true, false, false}, + {"add_owned_sequence", false, true, false, false, false}, // Tests adding empty table - {"add_empty_table", false, false, false, false}, + {"add_empty_table", false, false, false, false, false}, // Tests view with column names whose query changes - {"view_colnames", false, false, false, false}, + {"view_colnames", false, false, false, false, false}, // Tests objects with the $ sign in the name - {"add_table_bug102", false, false, false, false}, + {"add_table_bug102", false, false, false, false, false}, // Tests scenario where new UNLOGGED TABLE is added. - {"add_unlogged_table", false, false, false, false}, + {"add_unlogged_table", false, false, false, false, false}, // Tests scenario where UNLOGGED TABLE is dropped. - {"drop_unlogged_table", false, false, false, false}, + {"drop_unlogged_table", false, false, false, false, false}, // Test scenarios where /**/ comments. - {"add_table_issue115", false, false, false, false}, - {"add_column_issue134", false, false, false, false}, - {"add_column_issue188", false, false, false, false}, - {"add_column_issue188", false, false, false, false}, - {"view_alias_with_quote", false, false, false, false}, + {"add_table_issue115", false, false, false, false, false}, + {"add_column_issue134", false, false, false, false, false}, + {"add_column_issue188", false, false, false, false, false}, + {"add_column_issue188", false, false, false, false, false}, + {"view_alias_with_quote", false, false, false, false, false}, // Tests view triggers (support for 'INSTEAD OF') //90 - {"view_triggers", false, false, false, false}, + {"view_triggers", false, false, false, false, false}, // Tests privileges - {"grant_on_table_sequence", false, false, false, false}, - {"revoke_on_table_sequence", false, false, false, false}, - {"grant_on_view", false, false, false, false}, - {"revoke_on_view", false, false, false, false}, - {"revoke_on_table_column", false, false, false, false}, - {"multiple_schema_privileges", false, false, false, false} + {"grant_on_table_sequence", false, false, false, false, false}, + {"revoke_on_table_sequence", false, false, false, false, false}, + {"grant_on_view", false, false, false, false, false}, + {"revoke_on_view", false, false, false, false, false}, + {"revoke_on_table_column", false, false, false, false, false}, + {"multiple_schema_privileges", false, false, false, false, false} // Tests types - , {"add_type", false, false, false, false} - , {"drop_type", false, false, false, false} - , {"alter_type", false, false, false, false} + , {"add_type", false, false, false, false, false} + , {"drop_type", false, false, false, false, false} + , {"alter_type", false, false, false, false, false} // Test Foreign Tables - , {"foreign_create_table", false, false, false, false} - , {"foreign_drop_table", false, false, false, false} + , {"foreign_create_table", false, false, false, false, false} + , {"foreign_drop_table", false, false, false, false, false} //100 - , {"foreign_alter_type", false, false, false, false} + , {"foreign_alter_type", false, false, false, false, false} + //Test schema qualified option. + , {"schema_qualified", false, false, false, false, true} }); } /** @@ -278,6 +272,10 @@ public static Collection parameters() { * Value for the same named command line argument. */ private final boolean ignoreStartWith; + /** + * Value for the same named command line argument. + */ + private final boolean schemaQualifyObjects; /** * Creates a new PgDiffTest object. @@ -287,17 +285,20 @@ public static Collection parameters() { * @param addTransaction {@link #addTransaction} * @param ignoreFunctionWhitespace {@link #ignoreFunctionWhitespace} * @param ignoreStartWith {@link #ignoreStartWith} + * @param schemaQualifyObjects {@link #schemaQualifyObjects} */ public PgDiffTest(final String fileNameTemplate, final boolean addDefaults, final boolean addTransaction, final boolean ignoreFunctionWhitespace, - final boolean ignoreStartWith) { + final boolean ignoreStartWith, + final boolean schemaQualifyObjects) { super(); this.fileNameTemplate = fileNameTemplate; this.addDefaults = addDefaults; this.addTransaction = addTransaction; this.ignoreFunctionWhitespace = ignoreFunctionWhitespace; this.ignoreStartWith = ignoreStartWith; + this.schemaQualifyObjects = schemaQualifyObjects; Locale.setDefault(Locale.ENGLISH); } @@ -362,6 +363,7 @@ public void runDiff() throws FileNotFoundException, IOException { arguments.setAddDefaults(addDefaults); arguments.setIgnoreFunctionWhitespace(ignoreFunctionWhitespace); arguments.setIgnoreStartWith(ignoreStartWith); + arguments.setSchemaQualifyObjects(schemaQualifyObjects); PgDiff.createDiff(writer, arguments, PgDiffTest.class.getResourceAsStream( fileNameTemplate + "_original.sql"), @@ -372,8 +374,8 @@ public void runDiff() throws FileNotFoundException, IOException { final BufferedReader reader = new BufferedReader(new InputStreamReader( PgDiffTest.class.getResourceAsStream( fileNameTemplate + "_diff.sql"))); - final char[] part = new char[1024]; - final StringBuilder sbExpDiff = new StringBuilder(1024); + final char[] part = new char[2048]; + final StringBuilder sbExpDiff = new StringBuilder(2048); while (reader.read(part) != -1) { sbExpDiff.append(part); diff --git a/src/test/resources/cz/startnet/utils/pgdiff/schema_qualified_diff.sql b/src/test/resources/cz/startnet/utils/pgdiff/schema_qualified_diff.sql new file mode 100644 index 00000000..8491d914 --- /dev/null +++ b/src/test/resources/cz/startnet/utils/pgdiff/schema_qualified_diff.sql @@ -0,0 +1,51 @@ + +CREATE OR REPLACE FUNCTION public.function2() RETURNS integer + LANGUAGE sql + AS $$ select 2; $$; + +CREATE VIEW public.view2 AS + SELECT table1.col1, + table1.col2 + FROM schema1.table1 + WHERE (table1.col2 < 10); + +ALTER SEQUENCE schema1.table1_col1_seq + INCREMENT BY 5; + +ALTER TABLE schema1.table1 OWNER TO postgres; + +ALTER TABLE schema2.table2 + DROP CONSTRAINT table2_col2_fkey; + +DROP INDEX schema2.index2; + +ALTER TABLE schema2.table2 + ADD COLUMN added character varying; + +ALTER TABLE schema2.table2 OWNER TO postgres; + +ALTER TABLE schema2.table2 + ADD CONSTRAINT table1_col2_key UNIQUE (col2); + +ALTER TABLE schema2.table2 + ADD CONSTRAINT table2_col2_fkey FOREIGN KEY (col2) REFERENCES schema3.table3(col2); + +DROP FUNCTION schema3.function3(); + +ALTER TABLE schema3.table3 + DROP CONSTRAINT table3_pkey; + +DROP INDEX schema3.index3; + +ALTER TABLE schema3.table3 + ADD CONSTRAINT table3_pkey_new PRIMARY KEY (col1); + +ALTER TABLE schema3.table3 + ADD CONSTRAINT table3_col2_key UNIQUE (col2); + +CREATE INDEX index3 ON schema3.table3 USING btree (col2 DESC); + +CREATE TRIGGER trigger3 + AFTER INSERT ON schema3.table3 + FOR EACH STATEMENT + EXECUTE PROCEDURE triggfunc(); diff --git a/src/test/resources/cz/startnet/utils/pgdiff/schema_qualified_new.sql b/src/test/resources/cz/startnet/utils/pgdiff/schema_qualified_new.sql new file mode 100644 index 00000000..c005e4d0 --- /dev/null +++ b/src/test/resources/cz/startnet/utils/pgdiff/schema_qualified_new.sql @@ -0,0 +1,476 @@ +-- +-- PostgreSQL database dump +-- + +-- Dumped from database version 9.5.5 +-- Dumped by pg_dump version 9.5.5 + +SET statement_timeout = 0; +SET lock_timeout = 0; +SET client_encoding = 'UTF8'; +SET standard_conforming_strings = on; +SET check_function_bodies = false; +SET client_min_messages = warning; +SET row_security = off; + +-- +-- Name: schema1; Type: SCHEMA; Schema: -; Owner: postgres +-- + +CREATE SCHEMA schema1; + + +ALTER SCHEMA schema1 OWNER TO postgres; + +-- +-- Name: schema2; Type: SCHEMA; Schema: -; Owner: postgres +-- + +CREATE SCHEMA schema2; + + +ALTER SCHEMA schema2 OWNER TO postgres; + +-- +-- Name: schema3; Type: SCHEMA; Schema: -; Owner: ellmkay +-- + +CREATE SCHEMA schema3; + + +ALTER SCHEMA schema3 OWNER TO ellmkay; + +-- +-- Name: plpgsql; Type: EXTENSION; Schema: -; Owner: +-- + +CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog; + + +-- +-- Name: EXTENSION plpgsql; Type: COMMENT; Schema: -; Owner: +-- + +COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language'; + + +SET search_path = schema1, pg_catalog; + +-- +-- Name: type1; Type: TYPE; Schema: schema1; Owner: postgres +-- + +CREATE TYPE type1 AS ( + col integer, + other text +); + + +ALTER TYPE type1 OWNER TO postgres; + +SET search_path = public, pg_catalog; + +-- +-- Name: function2(); Type: FUNCTION; Schema: public; Owner: marcus +-- + +CREATE FUNCTION function2() RETURNS integer + LANGUAGE sql + AS $$ select 2; $$; + + +ALTER FUNCTION public.function2() OWNER TO marcus; + +SET search_path = schema1, pg_catalog; + +-- +-- Name: function1(); Type: FUNCTION; Schema: schema1; Owner: ellmkay +-- + +CREATE FUNCTION function1() RETURNS integer + LANGUAGE sql + AS $$ select 1; $$; + + +ALTER FUNCTION schema1.function1() OWNER TO ellmkay; + +SET search_path = schema2, pg_catalog; + +-- +-- Name: function2(); Type: FUNCTION; Schema: schema2; Owner: ellmkay +-- + +CREATE FUNCTION function2() RETURNS integer + LANGUAGE sql + AS $$ select 1; $$; + + +ALTER FUNCTION schema2.function2() OWNER TO ellmkay; + +SET search_path = schema3, pg_catalog; + +-- +-- Name: triggfunc(); Type: FUNCTION; Schema: schema3; Owner: ellmkay +-- + +CREATE FUNCTION triggfunc() RETURNS trigger + LANGUAGE plpgsql + AS $$ begin return null; END; $$; + + +ALTER FUNCTION schema3.triggfunc() OWNER TO ellmkay; + +SET search_path = schema1, pg_catalog; + +SET default_tablespace = ''; + +SET default_with_oids = false; + +-- +-- Name: table1; Type: TABLE; Schema: schema1; Owner: postgres +-- + +CREATE TABLE table1 ( + col1 integer NOT NULL, + col2 integer NOT NULL +); + + +ALTER TABLE table1 OWNER TO postgres; + +SET search_path = public, pg_catalog; + +-- +-- Name: view2; Type: VIEW; Schema: public; Owner: postgres +-- + +CREATE VIEW view2 AS + SELECT table1.col1, + table1.col2 + FROM schema1.table1 + WHERE (table1.col2 < 10); + + +ALTER TABLE view2 OWNER TO postgres; + +SET search_path = schema1, pg_catalog; + +-- +-- Name: table1_col1_seq; Type: SEQUENCE; Schema: schema1; Owner: postgres +-- + +CREATE SEQUENCE table1_col1_seq + START WITH 1 + INCREMENT BY 5 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +ALTER TABLE table1_col1_seq OWNER TO postgres; + +-- +-- Name: table1_col1_seq; Type: SEQUENCE OWNED BY; Schema: schema1; Owner: postgres +-- + +ALTER SEQUENCE table1_col1_seq OWNED BY table1.col1; + + +SET search_path = schema2, pg_catalog; + +-- +-- Name: table2; Type: TABLE; Schema: schema2; Owner: postgres +-- + +CREATE TABLE table2 ( + col1 integer NOT NULL, + col2 integer NOT NULL, + added character varying +); + + +ALTER TABLE table2 OWNER TO postgres; + +-- +-- Name: table2_col1_seq; Type: SEQUENCE; Schema: schema2; Owner: postgres +-- + +CREATE SEQUENCE table2_col1_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +ALTER TABLE table2_col1_seq OWNER TO postgres; + +-- +-- Name: table2_col1_seq; Type: SEQUENCE OWNED BY; Schema: schema2; Owner: postgres +-- + +ALTER SEQUENCE table2_col1_seq OWNED BY table2.col1; + + +-- +-- Name: view2; Type: VIEW; Schema: schema2; Owner: ellmkay +-- + +CREATE VIEW view2 AS + SELECT table1.col1, + table1.col2 + FROM schema1.table1; + + +ALTER TABLE view2 OWNER TO ellmkay; + +SET search_path = schema3, pg_catalog; + +-- +-- Name: table3; Type: TABLE; Schema: schema3; Owner: ellmkay +-- + +CREATE TABLE table3 ( + col1 integer NOT NULL, + col2 integer NOT NULL +); + + +ALTER TABLE table3 OWNER TO ellmkay; + +-- +-- Name: table3_col1_seq; Type: SEQUENCE; Schema: schema3; Owner: ellmkay +-- + +CREATE SEQUENCE table3_col1_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +ALTER TABLE table3_col1_seq OWNER TO ellmkay; + +-- +-- Name: table3_col1_seq; Type: SEQUENCE OWNED BY; Schema: schema3; Owner: ellmkay +-- + +ALTER SEQUENCE table3_col1_seq OWNED BY table3.col1; + + +SET search_path = schema1, pg_catalog; + +-- +-- Name: col1; Type: DEFAULT; Schema: schema1; Owner: postgres +-- + +ALTER TABLE ONLY table1 ALTER COLUMN col1 SET DEFAULT nextval('table1_col1_seq'::regclass); + + +SET search_path = schema2, pg_catalog; + +-- +-- Name: col1; Type: DEFAULT; Schema: schema2; Owner: postgres +-- + +ALTER TABLE ONLY table2 ALTER COLUMN col1 SET DEFAULT nextval('table2_col1_seq'::regclass); + + +SET search_path = schema3, pg_catalog; + +-- +-- Name: col1; Type: DEFAULT; Schema: schema3; Owner: ellmkay +-- + +ALTER TABLE ONLY table3 ALTER COLUMN col1 SET DEFAULT nextval('table3_col1_seq'::regclass); + + +SET search_path = schema1, pg_catalog; + +-- +-- Data for Name: table1; Type: TABLE DATA; Schema: schema1; Owner: postgres +-- + +COPY table1 (col1, col2) FROM stdin; +\. + + +-- +-- Name: table1_col1_seq; Type: SEQUENCE SET; Schema: schema1; Owner: postgres +-- + +SELECT pg_catalog.setval('table1_col1_seq', 100, false); + + +SET search_path = schema2, pg_catalog; + +-- +-- Data for Name: table2; Type: TABLE DATA; Schema: schema2; Owner: postgres +-- + +COPY table2 (col1, col2, added) FROM stdin; +\. + + +-- +-- Name: table2_col1_seq; Type: SEQUENCE SET; Schema: schema2; Owner: postgres +-- + +SELECT pg_catalog.setval('table2_col1_seq', 1, false); + + +SET search_path = schema3, pg_catalog; + +-- +-- Data for Name: table3; Type: TABLE DATA; Schema: schema3; Owner: ellmkay +-- + +COPY table3 (col1, col2) FROM stdin; +\. + + +-- +-- Name: table3_col1_seq; Type: SEQUENCE SET; Schema: schema3; Owner: ellmkay +-- + +SELECT pg_catalog.setval('table3_col1_seq', 1, false); + + +SET search_path = schema1, pg_catalog; + +-- +-- Name: table1_col2_key; Type: CONSTRAINT; Schema: schema1; Owner: postgres +-- + +ALTER TABLE ONLY table1 + ADD CONSTRAINT table1_col2_key UNIQUE (col2); + + +-- +-- Name: table1_pkey; Type: CONSTRAINT; Schema: schema1; Owner: postgres +-- + +ALTER TABLE ONLY table1 + ADD CONSTRAINT table1_pkey PRIMARY KEY (col1); + + +SET search_path = schema2, pg_catalog; + +-- +-- Name: table1_col2_key; Type: CONSTRAINT; Schema: schema2; Owner: postgres +-- + +ALTER TABLE ONLY table2 + ADD CONSTRAINT table1_col2_key UNIQUE (col2); + + +-- +-- Name: table2_pkey; Type: CONSTRAINT; Schema: schema2; Owner: postgres +-- + +ALTER TABLE ONLY table2 + ADD CONSTRAINT table2_pkey PRIMARY KEY (col1); + + +SET search_path = schema3, pg_catalog; + +-- +-- Name: table3_col2_key; Type: CONSTRAINT; Schema: schema3; Owner: ellmkay +-- + +ALTER TABLE ONLY table3 + ADD CONSTRAINT table3_col2_key UNIQUE (col2); + + +-- +-- Name: table3_pkey_new; Type: CONSTRAINT; Schema: schema3; Owner: ellmkay +-- + +ALTER TABLE ONLY table3 + ADD CONSTRAINT table3_pkey_new PRIMARY KEY (col1); + + +SET search_path = schema1, pg_catalog; + +-- +-- Name: index1; Type: INDEX; Schema: schema1; Owner: postgres +-- + +CREATE INDEX index1 ON table1 USING btree (col2); + + +SET search_path = schema3, pg_catalog; + +-- +-- Name: index3; Type: INDEX; Schema: schema3; Owner: ellmkay +-- + +CREATE INDEX index3 ON table3 USING btree (col2 DESC); + + +SET search_path = schema1, pg_catalog; + +-- +-- Name: trigger1; Type: TRIGGER; Schema: schema1; Owner: postgres +-- + +CREATE TRIGGER trigger1 AFTER INSERT ON table1 FOR EACH STATEMENT EXECUTE PROCEDURE schema3.triggfunc(); + + +SET search_path = schema3, pg_catalog; + +-- +-- Name: trigger3; Type: TRIGGER; Schema: schema3; Owner: ellmkay +-- + +CREATE TRIGGER trigger3 AFTER INSERT ON table3 FOR EACH STATEMENT EXECUTE PROCEDURE triggfunc(); + + +SET search_path = schema2, pg_catalog; + +-- +-- Name: table2_col2_fkey; Type: FK CONSTRAINT; Schema: schema2; Owner: postgres +-- + +ALTER TABLE ONLY table2 + ADD CONSTRAINT table2_col2_fkey FOREIGN KEY (col2) REFERENCES schema3.table3(col2); + + +SET search_path = schema3, pg_catalog; + +-- +-- Name: table3_col2_fkey; Type: FK CONSTRAINT; Schema: schema3; Owner: ellmkay +-- + +ALTER TABLE ONLY table3 + ADD CONSTRAINT table3_col2_fkey FOREIGN KEY (col2) REFERENCES schema1.table1(col2); + + +-- +-- Name: public; Type: ACL; Schema: -; Owner: postgres +-- + +REVOKE ALL ON SCHEMA public FROM PUBLIC; +REVOKE ALL ON SCHEMA public FROM postgres; +GRANT ALL ON SCHEMA public TO postgres; +GRANT ALL ON SCHEMA public TO PUBLIC; + + +SET search_path = schema1, pg_catalog; + +-- +-- Name: function1(); Type: ACL; Schema: schema1; Owner: ellmkay +-- + +REVOKE ALL ON FUNCTION function1() FROM PUBLIC; +REVOKE ALL ON FUNCTION function1() FROM ellmkay; +GRANT ALL ON FUNCTION function1() TO ellmkay; +GRANT ALL ON FUNCTION function1() TO PUBLIC; +GRANT ALL ON FUNCTION function1() TO postgres; + + +-- +-- PostgreSQL database dump complete +-- + diff --git a/src/test/resources/cz/startnet/utils/pgdiff/schema_qualified_original.sql b/src/test/resources/cz/startnet/utils/pgdiff/schema_qualified_original.sql new file mode 100644 index 00000000..a8dccad6 --- /dev/null +++ b/src/test/resources/cz/startnet/utils/pgdiff/schema_qualified_original.sql @@ -0,0 +1,425 @@ +-- +-- PostgreSQL database dump +-- + +-- Dumped from database version 9.5.5 +-- Dumped by pg_dump version 9.5.5 + +SET statement_timeout = 0; +SET lock_timeout = 0; +SET client_encoding = 'UTF8'; +SET standard_conforming_strings = on; +SET check_function_bodies = false; +SET client_min_messages = warning; +SET row_security = off; + +-- +-- Name: schema1; Type: SCHEMA; Schema: -; Owner: ellmkay +-- + +CREATE SCHEMA schema1; + + +ALTER SCHEMA schema1 OWNER TO ellmkay; + +-- +-- Name: schema2; Type: SCHEMA; Schema: -; Owner: ellmkay +-- + +CREATE SCHEMA schema2; + + +ALTER SCHEMA schema2 OWNER TO ellmkay; + +-- +-- Name: schema3; Type: SCHEMA; Schema: -; Owner: ellmkay +-- + +CREATE SCHEMA schema3; + + +ALTER SCHEMA schema3 OWNER TO ellmkay; + +-- +-- Name: plpgsql; Type: EXTENSION; Schema: -; Owner: +-- + +CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog; + + +-- +-- Name: EXTENSION plpgsql; Type: COMMENT; Schema: -; Owner: +-- + +COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language'; + + +SET search_path = schema1, pg_catalog; + +-- +-- Name: type1; Type: TYPE; Schema: schema1; Owner: ellmkay +-- + +CREATE TYPE type1 AS ( + col integer, + other text +); + + +ALTER TYPE type1 OWNER TO ellmkay; + +-- +-- Name: function1(); Type: FUNCTION; Schema: schema1; Owner: ellmkay +-- + +CREATE FUNCTION function1() RETURNS integer + LANGUAGE sql + AS $$ select 1; $$; + + +ALTER FUNCTION schema1.function1() OWNER TO ellmkay; + +SET search_path = schema2, pg_catalog; + +-- +-- Name: function2(); Type: FUNCTION; Schema: schema2; Owner: ellmkay +-- + +CREATE FUNCTION function2() RETURNS integer + LANGUAGE sql + AS $$ select 1; $$; + + +ALTER FUNCTION schema2.function2() OWNER TO ellmkay; + +SET search_path = schema3, pg_catalog; + +-- +-- Name: function3(); Type: FUNCTION; Schema: schema3; Owner: ellmkay +-- + +CREATE FUNCTION function3() RETURNS integer + LANGUAGE sql + AS $$ select 1; $$; + + +ALTER FUNCTION schema3.function3() OWNER TO ellmkay; + +-- +-- Name: triggfunc(); Type: FUNCTION; Schema: schema3; Owner: ellmkay +-- + +CREATE FUNCTION triggfunc() RETURNS trigger + LANGUAGE plpgsql + AS $$ begin return null; END; $$; + + +ALTER FUNCTION schema3.triggfunc() OWNER TO ellmkay; + +SET search_path = schema1, pg_catalog; + +SET default_tablespace = ''; + +SET default_with_oids = false; + +-- +-- Name: table1; Type: TABLE; Schema: schema1; Owner: ellmkay +-- + +CREATE TABLE table1 ( + col1 integer NOT NULL, + col2 integer NOT NULL +); + + +ALTER TABLE table1 OWNER TO ellmkay; + +-- +-- Name: table1_col1_seq; Type: SEQUENCE; Schema: schema1; Owner: ellmkay +-- + +CREATE SEQUENCE table1_col1_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +ALTER TABLE table1_col1_seq OWNER TO ellmkay; + +-- +-- Name: table1_col1_seq; Type: SEQUENCE OWNED BY; Schema: schema1; Owner: ellmkay +-- + +ALTER SEQUENCE table1_col1_seq OWNED BY table1.col1; + + +SET search_path = schema2, pg_catalog; + +-- +-- Name: table2; Type: TABLE; Schema: schema2; Owner: ellmkay +-- + +CREATE TABLE table2 ( + col1 integer NOT NULL, + col2 integer NOT NULL +); + + +ALTER TABLE table2 OWNER TO ellmkay; + +-- +-- Name: table2_col1_seq; Type: SEQUENCE; Schema: schema2; Owner: ellmkay +-- + +CREATE SEQUENCE table2_col1_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +ALTER TABLE table2_col1_seq OWNER TO ellmkay; + +-- +-- Name: table2_col1_seq; Type: SEQUENCE OWNED BY; Schema: schema2; Owner: ellmkay +-- + +ALTER SEQUENCE table2_col1_seq OWNED BY table2.col1; + + +-- +-- Name: view2; Type: VIEW; Schema: schema2; Owner: ellmkay +-- + +CREATE VIEW view2 AS + SELECT table1.col1, + table1.col2 + FROM schema1.table1; + + +ALTER TABLE view2 OWNER TO ellmkay; + +SET search_path = schema3, pg_catalog; + +-- +-- Name: table3; Type: TABLE; Schema: schema3; Owner: ellmkay +-- + +CREATE TABLE table3 ( + col1 integer NOT NULL, + col2 integer NOT NULL +); + + +ALTER TABLE table3 OWNER TO ellmkay; + +-- +-- Name: table3_col1_seq; Type: SEQUENCE; Schema: schema3; Owner: ellmkay +-- + +CREATE SEQUENCE table3_col1_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +ALTER TABLE table3_col1_seq OWNER TO ellmkay; + +-- +-- Name: table3_col1_seq; Type: SEQUENCE OWNED BY; Schema: schema3; Owner: ellmkay +-- + +ALTER SEQUENCE table3_col1_seq OWNED BY table3.col1; + + +SET search_path = schema1, pg_catalog; + +-- +-- Name: col1; Type: DEFAULT; Schema: schema1; Owner: ellmkay +-- + +ALTER TABLE ONLY table1 ALTER COLUMN col1 SET DEFAULT nextval('table1_col1_seq'::regclass); + + +SET search_path = schema2, pg_catalog; + +-- +-- Name: col1; Type: DEFAULT; Schema: schema2; Owner: ellmkay +-- + +ALTER TABLE ONLY table2 ALTER COLUMN col1 SET DEFAULT nextval('table2_col1_seq'::regclass); + + +SET search_path = schema3, pg_catalog; + +-- +-- Name: col1; Type: DEFAULT; Schema: schema3; Owner: ellmkay +-- + +ALTER TABLE ONLY table3 ALTER COLUMN col1 SET DEFAULT nextval('table3_col1_seq'::regclass); + + +SET search_path = schema1, pg_catalog; + +-- +-- Data for Name: table1; Type: TABLE DATA; Schema: schema1; Owner: ellmkay +-- + +COPY table1 (col1, col2) FROM stdin; +\. + + +-- +-- Name: table1_col1_seq; Type: SEQUENCE SET; Schema: schema1; Owner: ellmkay +-- + +SELECT pg_catalog.setval('table1_col1_seq', 1, false); + + +SET search_path = schema2, pg_catalog; + +-- +-- Data for Name: table2; Type: TABLE DATA; Schema: schema2; Owner: ellmkay +-- + +COPY table2 (col1, col2) FROM stdin; +\. + + +-- +-- Name: table2_col1_seq; Type: SEQUENCE SET; Schema: schema2; Owner: ellmkay +-- + +SELECT pg_catalog.setval('table2_col1_seq', 1, false); + + +SET search_path = schema3, pg_catalog; + +-- +-- Data for Name: table3; Type: TABLE DATA; Schema: schema3; Owner: ellmkay +-- + +COPY table3 (col1, col2) FROM stdin; +\. + + +-- +-- Name: table3_col1_seq; Type: SEQUENCE SET; Schema: schema3; Owner: ellmkay +-- + +SELECT pg_catalog.setval('table3_col1_seq', 1, false); + + +SET search_path = schema1, pg_catalog; + +-- +-- Name: table1_col2_key; Type: CONSTRAINT; Schema: schema1; Owner: ellmkay +-- + +ALTER TABLE ONLY table1 + ADD CONSTRAINT table1_col2_key UNIQUE (col2); + + +-- +-- Name: table1_pkey; Type: CONSTRAINT; Schema: schema1; Owner: ellmkay +-- + +ALTER TABLE ONLY table1 + ADD CONSTRAINT table1_pkey PRIMARY KEY (col1); + + +SET search_path = schema2, pg_catalog; + +-- +-- Name: table2_pkey; Type: CONSTRAINT; Schema: schema2; Owner: ellmkay +-- + +ALTER TABLE ONLY table2 + ADD CONSTRAINT table2_pkey PRIMARY KEY (col1); + + +SET search_path = schema3, pg_catalog; + +-- +-- Name: table3_pkey; Type: CONSTRAINT; Schema: schema3; Owner: ellmkay +-- + +ALTER TABLE ONLY table3 + ADD CONSTRAINT table3_pkey PRIMARY KEY (col1); + + +SET search_path = schema1, pg_catalog; + +-- +-- Name: index1; Type: INDEX; Schema: schema1; Owner: ellmkay +-- + +CREATE INDEX index1 ON table1 USING btree (col2); + + +SET search_path = schema2, pg_catalog; + +-- +-- Name: index2; Type: INDEX; Schema: schema2; Owner: ellmkay +-- + +CREATE INDEX index2 ON table2 USING btree (col2); + + +SET search_path = schema3, pg_catalog; + +-- +-- Name: index3; Type: INDEX; Schema: schema3; Owner: ellmkay +-- + +CREATE INDEX index3 ON table3 USING btree (col2); + + +SET search_path = schema1, pg_catalog; + +-- +-- Name: trigger1; Type: TRIGGER; Schema: schema1; Owner: ellmkay +-- + +CREATE TRIGGER trigger1 AFTER INSERT ON table1 FOR EACH STATEMENT EXECUTE PROCEDURE schema3.triggfunc(); + + +SET search_path = schema2, pg_catalog; + +-- +-- Name: table2_col2_fkey; Type: FK CONSTRAINT; Schema: schema2; Owner: ellmkay +-- + +ALTER TABLE ONLY table2 + ADD CONSTRAINT table2_col2_fkey FOREIGN KEY (col2) REFERENCES schema1.table1(col2); + + +SET search_path = schema3, pg_catalog; + +-- +-- Name: table3_col2_fkey; Type: FK CONSTRAINT; Schema: schema3; Owner: ellmkay +-- + +ALTER TABLE ONLY table3 + ADD CONSTRAINT table3_col2_fkey FOREIGN KEY (col2) REFERENCES schema1.table1(col2); + + +-- +-- Name: public; Type: ACL; Schema: -; Owner: postgres +-- + +REVOKE ALL ON SCHEMA public FROM PUBLIC; +REVOKE ALL ON SCHEMA public FROM postgres; +GRANT ALL ON SCHEMA public TO postgres; +GRANT ALL ON SCHEMA public TO PUBLIC; + + +-- +-- PostgreSQL database dump complete +-- +