Skip to content

Commit

Permalink
Initial Support to PARTITION BY on Create Table
Browse files Browse the repository at this point in the history
  • Loading branch information
jalisson committed Jun 11, 2020
1 parent a5a412b commit 67e09cd
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/cz/startnet/utils/pgdiff/schema/PgTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ public class PgTable extends PgRelation {
* PgSchema
*/
private final PgSchema schema;

/**
* Partion By
*/

private String partitionBy;

/**
* Creates a new PgTable object.
Expand Down Expand Up @@ -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"));
Expand Down Expand Up @@ -497,4 +508,12 @@ public PgPolicy getPolicy(final String name) {
public List<PgPolicy> getPolicies() {
return Collections.unmodifiableList(policies);
}

public void setPartionBy(String partitionBy){
this.partitionBy = partitionBy;
}

public String getPartionBy(){
return partitionBy;
}
}
2 changes: 2 additions & 0 deletions src/test/java/cz/startnet/utils/pgdiff/PgDiffTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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
--

Original file line number Diff line number Diff line change
@@ -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
--

0 comments on commit 67e09cd

Please sign in to comment.