From 67e09cddfde3f158b54b29bbc3d1a8ac4443869d Mon Sep 17 00:00:00 2001 From: jalisson Date: Wed, 10 Jun 2020 21:27:37 -0300 Subject: [PATCH] Initial Support to PARTITION BY on Create Table --- .../pgdiff/parsers/CreateTableParser.java | 4 +- .../startnet/utils/pgdiff/schema/PgTable.java | 19 +++++ .../cz/startnet/utils/pgdiff/PgDiffTest.java | 2 + .../pgdiff/add_table_partition_by_diff.sql | 10 +++ .../pgdiff/add_table_partition_by_new.sql | 71 +++++++++++++++++++ .../add_table_partition_by_original.sql | 57 +++++++++++++++ 6 files changed, 162 insertions(+), 1 deletion(-) create mode 100644 src/test/resources/cz/startnet/utils/pgdiff/add_table_partition_by_diff.sql create mode 100644 src/test/resources/cz/startnet/utils/pgdiff/add_table_partition_by_new.sql create mode 100644 src/test/resources/cz/startnet/utils/pgdiff/add_table_partition_by_original.sql diff --git a/src/main/java/cz/startnet/utils/pgdiff/parsers/CreateTableParser.java b/src/main/java/cz/startnet/utils/pgdiff/parsers/CreateTableParser.java index 3dc39e3d..92dbe2b6 100644 --- a/src/main/java/cz/startnet/utils/pgdiff/parsers/CreateTableParser.java +++ b/src/main/java/cz/startnet/utils/pgdiff/parsers/CreateTableParser.java @@ -82,7 +82,9 @@ public static void parse(final PgDatabase database, } while (!parser.expectOptional(";")) { - if (parser.expectOptional("INHERITS")) { + if (parser.expectOptional("PARTITION","BY")) { + table.setPartionBy("PARTITION BY "+parser.getExpression()); + } else if (parser.expectOptional("INHERITS")) { parseInherits(database, parser, table); } else if (parser.expectOptional("WITHOUT")) { table.setWith("OIDS=false"); 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 ee2069b8..4ebc7462 100755 --- a/src/main/java/cz/startnet/utils/pgdiff/schema/PgTable.java +++ b/src/main/java/cz/startnet/utils/pgdiff/schema/PgTable.java @@ -72,6 +72,12 @@ public class PgTable extends PgRelation { * PgSchema */ private final PgSchema schema; + + /** + * Partion By + */ + + private String partitionBy; /** * Creates a new PgTable object. @@ -163,6 +169,11 @@ public String getCreationSQL(final PgSchema schema) { sbSQL.append(System.getProperty("line.separator")); sbSQL.append(")"); } + + if(partitionBy!=null && !partitionBy.isEmpty()){ + sbSQL.append(" "); + sbSQL.append(partitionBy); + } if (inherits != null && !inherits.isEmpty()) { sbSQL.append(System.getProperty("line.separator")); @@ -497,4 +508,12 @@ public PgPolicy getPolicy(final String name) { public List getPolicies() { return Collections.unmodifiableList(policies); } + + public void setPartionBy(String partitionBy){ + this.partitionBy = partitionBy; + } + + public String getPartionBy(){ + return partitionBy; + } } diff --git a/src/test/java/cz/startnet/utils/pgdiff/PgDiffTest.java b/src/test/java/cz/startnet/utils/pgdiff/PgDiffTest.java index f6d36d88..91f1274c 100755 --- a/src/test/java/cz/startnet/utils/pgdiff/PgDiffTest.java +++ b/src/test/java/cz/startnet/utils/pgdiff/PgDiffTest.java @@ -230,6 +230,8 @@ public static Collection parameters() { {"view_colnames", false, false, false, false}, // Tests objects with the $ sign in the name {"add_table_bug102", false, false, false, false}, + // Tests objects with the PARTITION BY + {"add_table_partition_by", false, false, false, false}, // Tests scenario where new UNLOGGED TABLE is added. {"add_unlogged_table", false, false, false, false}, // Tests scenario where UNLOGGED TABLE is dropped. diff --git a/src/test/resources/cz/startnet/utils/pgdiff/add_table_partition_by_diff.sql b/src/test/resources/cz/startnet/utils/pgdiff/add_table_partition_by_diff.sql new file mode 100644 index 00000000..e1d73662 --- /dev/null +++ b/src/test/resources/cz/startnet/utils/pgdiff/add_table_partition_by_diff.sql @@ -0,0 +1,10 @@ +CREATE TABLE IF NOT EXISTS "procedureresult$Operation" ( + id bigint NOT NULL, + name character varying(255), + result_id bigint +) PARTITION BY RANGE(result_id); + +ALTER TABLE "procedureresult$Operation" OWNER TO fordfrog; + +ALTER TABLE IF EXISTS "procedureresult$Operation" + ADD CONSTRAINT IF NOT EXISTS $1 FOREIGN KEY (result_id) REFERENCES testtable(field1) ON UPDATE RESTRICT ON DELETE RESTRICT; diff --git a/src/test/resources/cz/startnet/utils/pgdiff/add_table_partition_by_new.sql b/src/test/resources/cz/startnet/utils/pgdiff/add_table_partition_by_new.sql new file mode 100644 index 00000000..6ec3e31f --- /dev/null +++ b/src/test/resources/cz/startnet/utils/pgdiff/add_table_partition_by_new.sql @@ -0,0 +1,71 @@ +-- +-- PostgreSQL database dump +-- + +SET client_encoding = 'UTF8'; +SET check_function_bodies = false; +SET client_min_messages = warning; + +-- +-- Name: SCHEMA public; Type: COMMENT; Schema: -; Owner: postgres +-- + +COMMENT ON SCHEMA public IS 'Standard public schema'; + + +SET search_path = public, pg_catalog; + +SET default_tablespace = ''; + +SET default_with_oids = false; + +-- +-- Name: testtable; Type: TABLE; Schema: public; Owner: fordfrog; Tablespace: +-- + +CREATE TABLE testtable ( + field1 integer, + field2 integer, + field3 character varying(150) DEFAULT 'none'::character varying, + field4 double precision +); + + +ALTER TABLE public.testtable OWNER TO fordfrog; + +-- +-- Name: procedureresult$operation; Type: TABLE; Schema: public; Owner: fordfrog; Tablespace: +-- + +CREATE TABLE "procedureresult$Operation" ( + id bigint NOT NULL, + name character varying(255), + result_id bigint +) PARTITION BY RANGE(result_id); + +ALTER TABLE public."procedureresult$Operation" OWNER TO fordfrog; + +ALTER TABLE ONLY "procedureresult$Operation" +ADD CONSTRAINT "$1" FOREIGN KEY (result_id) REFERENCES testtable(field1) ON UPDATE RESTRICT ON DELETE RESTRICT; + +-- +-- Name: testindex; Type: INDEX; Schema: public; Owner: fordfrog; Tablespace: +-- + +CREATE INDEX testindex ON testtable USING btree (field3); + + +-- +-- 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 +-- + diff --git a/src/test/resources/cz/startnet/utils/pgdiff/add_table_partition_by_original.sql b/src/test/resources/cz/startnet/utils/pgdiff/add_table_partition_by_original.sql new file mode 100644 index 00000000..c7349f2a --- /dev/null +++ b/src/test/resources/cz/startnet/utils/pgdiff/add_table_partition_by_original.sql @@ -0,0 +1,57 @@ + +-- +-- PostgreSQL database dump +-- + +SET client_encoding = 'UTF8'; +SET check_function_bodies = false; +SET client_min_messages = warning; + +-- +-- Name: SCHEMA public; Type: COMMENT; Schema: -; Owner: postgres +-- + +COMMENT ON SCHEMA public IS 'Standard public schema'; + + +SET search_path = public, pg_catalog; + +SET default_tablespace = ''; + +SET default_with_oids = false; + +-- +-- Name: testtable; Type: TABLE; Schema: public; Owner: fordfrog; Tablespace: +-- + +CREATE TABLE testtable ( + field1 integer, + field2 integer, + field3 character varying(150) DEFAULT 'none'::character varying, + field4 double precision +); + + +ALTER TABLE public.testtable OWNER TO fordfrog; + +-- +-- Name: testindex; Type: INDEX; Schema: public; Owner: fordfrog; Tablespace: +-- + +CREATE INDEX testindex ON testtable USING btree (field3); + + +-- +-- 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 +-- +