Skip to content

Commit

Permalink
Merge branch '17130_field_type_partitioned_table' into 'master'
Browse files Browse the repository at this point in the history
17130 field type partitioned table

See merge request codekeeper/codekeeper!319
  • Loading branch information
Endeavourl committed Jul 1, 2019
2 parents 6572366 + 3693b44 commit e49ad8a
Show file tree
Hide file tree
Showing 9 changed files with 271 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ public static Collection<?> parameters() {
{"add_column_null"},
// Tests scenario where COLUMN with index dependency is dropped from TABLE.
{"drop_column"},
// Tests scenario where COLUMNs are dropped from partitioned TABLE.
{"drop_col_from_partitioned_tbl"},
// Tests scenario where new TABLE is added.
{"add_table"},
// Tests scenario where new FOREIGN TABLE is added.
Expand Down Expand Up @@ -438,6 +440,8 @@ public static Collection<?> parameters() {
{"modify_column_type_and_default"},
// Tests scenario where COLUMN type is modified and default is deleted.
{"modify_column_type_and_drop_default"},
// Tests scenario where type in COLUMN of partitioned TABLE is changed.
{"modify_col_type_in_partitioned_tbl"},
// Tests scenario where new OPERATOR is added.
{"add_operator"},
// Tests scenario where new overloaded OPERATOR is added.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
SET search_path = pg_catalog;

ALTER TABLE public.cities
DROP COLUMN bstat;

-- DEPCY: This COLUMN depends on the COLUMN: public.cities.qwe

ALTER TABLE ONLY public.cities_ab_10000_to_100000
ALTER COLUMN qwe DROP DEFAULT;

-- DEPCY: This COLUMN depends on the COLUMN: public.cities.qwe

ALTER TABLE ONLY public.cities_ab
ALTER COLUMN qwe DROP DEFAULT;

-- DEPCY: This COLUMN depends on the COLUMN: public.cities.qwe

ALTER TABLE ONLY public.cities_partdef
ALTER COLUMN qwe DROP DEFAULT;

ALTER TABLE public.cities
DROP COLUMN qwe;
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
CREATE SEQUENCE public.cities_city_id_seq
START WITH 1
INCREMENT BY 1
NO MAXVALUE
NO MINVALUE
CACHE 1;

--------------------------------------------------------------------------------

CREATE TABLE public.cities (
city_id bigint DEFAULT nextval('public.cities_city_id_seq'::regclass) NOT NULL,
name text NOT NULL,
population bigint
)
PARTITION BY LIST ("left"(lower(name), 1));

--------------------------------------------------------------------------------

CREATE TABLE public.cities_partdef PARTITION OF public.cities
DEFAULT;

ALTER TABLE ONLY public.cities_partdef ALTER COLUMN city_id SET DEFAULT nextval('public.cities_city_id_seq'::regclass);

--------------------------------------------------------------------------------

CREATE TABLE public.cities_ab PARTITION OF public.cities
FOR VALUES IN ('a', 'b')
PARTITION BY RANGE (population);

ALTER TABLE ONLY public.cities_ab ALTER COLUMN city_id SET DEFAULT nextval('public.cities_city_id_seq'::regclass);

ALTER TABLE public.cities_ab
ADD CONSTRAINT city_id_nonzero CHECK ((city_id <> 0));

--------------------------------------------------------------------------------

CREATE TABLE public.cities_ab_10000_to_100000 PARTITION OF public.cities_ab
FOR VALUES FROM ('10000') TO ('100000');

ALTER TABLE ONLY public.cities_ab_10000_to_100000 ALTER COLUMN city_id SET DEFAULT nextval('public.cities_city_id_seq'::regclass);
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
CREATE SEQUENCE public.cities_city_id_seq
START WITH 1
INCREMENT BY 1
NO MAXVALUE
NO MINVALUE
CACHE 1;

--------------------------------------------------------------------------------

CREATE TABLE public.cities (
city_id bigint DEFAULT nextval('public.cities_city_id_seq'::regclass) NOT NULL,
name text NOT NULL,
population bigint,
bstat integer,
qwe text DEFAULT USER
)
PARTITION BY LIST ("left"(lower(name), 1));

--------------------------------------------------------------------------------

CREATE TABLE public.cities_partdef PARTITION OF public.cities
DEFAULT;

ALTER TABLE ONLY public.cities_partdef ALTER COLUMN city_id SET DEFAULT nextval('public.cities_city_id_seq'::regclass);

ALTER TABLE ONLY public.cities_partdef ALTER COLUMN qwe SET DEFAULT USER;

--------------------------------------------------------------------------------

CREATE TABLE public.cities_ab PARTITION OF public.cities
FOR VALUES IN ('a', 'b')
PARTITION BY RANGE (population);

ALTER TABLE ONLY public.cities_ab ALTER COLUMN city_id SET DEFAULT nextval('public.cities_city_id_seq'::regclass);

ALTER TABLE ONLY public.cities_ab ALTER COLUMN qwe SET DEFAULT USER;

ALTER TABLE public.cities_ab
ADD CONSTRAINT city_id_nonzero CHECK ((city_id <> 0));

--------------------------------------------------------------------------------

CREATE TABLE public.cities_ab_10000_to_100000 PARTITION OF public.cities_ab
FOR VALUES FROM ('10000') TO ('100000');

ALTER TABLE ONLY public.cities_ab_10000_to_100000 ALTER COLUMN city_id SET DEFAULT nextval('public.cities_city_id_seq'::regclass);

ALTER TABLE ONLY public.cities_ab_10000_to_100000 ALTER COLUMN qwe SET DEFAULT USER;
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
SET search_path = pg_catalog;

-- DEPCY: This COLUMN depends on the COLUMN: public.measurement_year_month.unitsales

ALTER TABLE ONLY public.measurement_ym_y2017m01
ALTER COLUMN unitsales DROP DEFAULT;

-- DEPCY: This COLUMN depends on the COLUMN: public.measurement_year_month.unitsales

ALTER TABLE ONLY public.measurement_ym_y2016m12
ALTER COLUMN unitsales DROP DEFAULT;

-- DEPCY: This COLUMN depends on the COLUMN: public.measurement_year_month.unitsales

ALTER TABLE ONLY public.measurement_ym_y2016m11
ALTER COLUMN unitsales DROP DEFAULT;

-- DEPCY: This COLUMN depends on the COLUMN: public.measurement_year_month.unitsales

ALTER TABLE ONLY public.measurement_ym_older
ALTER COLUMN unitsales DROP DEFAULT;

ALTER TABLE ONLY public.measurement_year_month
ALTER COLUMN unitsales DROP DEFAULT;

ALTER TABLE public.measurement_year_month
ALTER COLUMN unitsales TYPE text USING unitsales::text; /* TYPE change - table: public.measurement_year_month original: integer new: text */

ALTER TABLE ONLY public.measurement_year_month
ALTER COLUMN unitsales SET DEFAULT USER;

ALTER TABLE ONLY public.measurement_ym_y2017m01
ALTER COLUMN unitsales SET DEFAULT USER;

ALTER TABLE ONLY public.measurement_ym_y2016m12
ALTER COLUMN unitsales SET DEFAULT USER;

ALTER TABLE ONLY public.measurement_ym_y2016m11
ALTER COLUMN unitsales SET DEFAULT USER;

ALTER TABLE ONLY public.measurement_ym_older
ALTER COLUMN unitsales SET DEFAULT USER;
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
CREATE TABLE public.measurement_year_month (
logdate date NOT NULL,
peaktemp integer,
unitsales text DEFAULT USER
)
PARTITION BY RANGE (date_part('year'::text, logdate), date_part('month'::text, logdate));


CREATE TABLE public.measurement_ym_older PARTITION OF public.measurement_year_month
FOR VALUES FROM (MINVALUE, MINVALUE) TO ('2016', '11');

ALTER TABLE ONLY public.measurement_ym_older ALTER COLUMN unitsales SET DEFAULT USER;


CREATE TABLE public.measurement_ym_y2016m11 PARTITION OF public.measurement_year_month
FOR VALUES FROM ('2016', '11') TO ('2016', '12');

ALTER TABLE ONLY public.measurement_ym_y2016m11 ALTER COLUMN unitsales SET DEFAULT USER;


CREATE TABLE public.measurement_ym_y2016m12 PARTITION OF public.measurement_year_month
FOR VALUES FROM ('2016', '12') TO ('2017', '1');

ALTER TABLE ONLY public.measurement_ym_y2016m12 ALTER COLUMN unitsales SET DEFAULT USER;


CREATE TABLE public.measurement_ym_y2017m01 PARTITION OF public.measurement_year_month
FOR VALUES FROM ('2017', '1') TO ('2017', '2');

ALTER TABLE ONLY public.measurement_ym_y2017m01 ALTER COLUMN unitsales SET DEFAULT USER;
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
CREATE TABLE public.measurement_year_month (
logdate date NOT NULL,
peaktemp integer,
unitsales integer DEFAULT 5234
)
PARTITION BY RANGE (date_part('year'::text, logdate), date_part('month'::text, logdate));


CREATE TABLE public.measurement_ym_older PARTITION OF public.measurement_year_month
FOR VALUES FROM (MINVALUE, MINVALUE) TO ('2016', '11');

ALTER TABLE ONLY public.measurement_ym_older ALTER COLUMN unitsales SET DEFAULT 5234;


CREATE TABLE public.measurement_ym_y2016m11 PARTITION OF public.measurement_year_month
FOR VALUES FROM ('2016', '11') TO ('2016', '12');

ALTER TABLE ONLY public.measurement_ym_y2016m11 ALTER COLUMN unitsales SET DEFAULT 5234;


CREATE TABLE public.measurement_ym_y2016m12 PARTITION OF public.measurement_year_month
FOR VALUES FROM ('2016', '12') TO ('2017', '1');

ALTER TABLE ONLY public.measurement_ym_y2016m12 ALTER COLUMN unitsales SET DEFAULT 5234;


CREATE TABLE public.measurement_ym_y2017m01 PARTITION OF public.measurement_year_month
FOR VALUES FROM ('2017', '1') TO ('2017', '2');

ALTER TABLE ONLY public.measurement_ym_y2017m01 ALTER COLUMN unitsales SET DEFAULT 5234;
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,20 @@ private String getAlterColumn(boolean newLine, boolean only, String column) {
@Override
public String getDropSQL() {
if (getType() != null) {
return getAlterTable(false, true) + "\n\tDROP COLUMN "
+ PgDiffUtils.getQuotedName(getName()) + ';';
boolean addOnly = true;

//// Condition for partitioned tables.
// If there are sections, then it is impossible to delete a column
// only from a partitioned table.
// Because of impossible inherit from partitioned tables, this
// condition will also be true for cases when a partitioned table
// does not have sections.
if (getParent() instanceof AbstractRegularTable) {
addOnly = ((AbstractRegularTable) getParent()).getPartitionBy() == null;
}

return getAlterTable(false, addOnly) + "\n\tDROP COLUMN "
+ PgDiffUtils.getQuotedName(getName()) + ';';
}

StringBuilder sb = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@
import org.jgrapht.graph.EdgeReversedGraph;
import org.jgrapht.graph.SimpleDirectedGraph;

import cz.startnet.utils.pgdiff.schema.AbstractColumn;
import cz.startnet.utils.pgdiff.schema.AbstractConstraint;
import cz.startnet.utils.pgdiff.schema.AbstractIndex;
import cz.startnet.utils.pgdiff.schema.AbstractPgTable.Inherits;
import cz.startnet.utils.pgdiff.schema.AbstractTable;
import cz.startnet.utils.pgdiff.schema.GenericColumn;
import cz.startnet.utils.pgdiff.schema.IStatementContainer;
import cz.startnet.utils.pgdiff.schema.PartitionPgTable;
import cz.startnet.utils.pgdiff.schema.PgColumn;
import cz.startnet.utils.pgdiff.schema.PgDatabase;
import cz.startnet.utils.pgdiff.schema.PgStatement;
import ru.taximaxim.codekeeper.apgdiff.Log;
import ru.taximaxim.codekeeper.apgdiff.model.difftree.DbObjType;

public class DepcyGraph {
Expand Down Expand Up @@ -71,6 +76,11 @@ private void create() {
if (st.getStatementType() == DbObjType.CONSTRAINT) {
createFkeyToUnique((AbstractConstraint)st);
}
if (st.getStatementType() == DbObjType.COLUMN
&& st.getParent() instanceof PartitionPgTable) {
createChildColToPartTblCol((PartitionPgTable) st.getParent(),
(PgColumn) st);
}
});
}

Expand Down Expand Up @@ -110,6 +120,37 @@ private void createFkeyToUnique(AbstractConstraint con) {
}
}

/**
* Creates the connection between the column of a partitioned table and the
* columns of its sections (child tables).
*/
private void createChildColToPartTblCol(PartitionPgTable pTbl, PgColumn pCol) {
String colName = pCol.getName();
for (Inherits in : pTbl.getInherits()) {
PgStatement parentTbl = new GenericColumn(in.getKey(), in.getValue(),
DbObjType.TABLE).getStatement(db);
if (parentTbl == null) {
Log.log(Log.LOG_ERROR, "There is no such object of inheritance"
+ " as table: " + in.getQualifiedName());
continue;
}

if (parentTbl instanceof PartitionPgTable) {
createChildColToPartTblCol((PartitionPgTable) parentTbl, pCol);
} else {
AbstractColumn parentCol = ((AbstractTable) parentTbl).getColumn(colName);
if (parentCol != null) {
graph.addEdge(pCol, parentCol);
} else {
Log.log(Log.LOG_ERROR, "The parent '" + in.getQualifiedName()
+ '.' + colName + "' column for '" + pCol.getSchemaName()
+ '.' + pCol.getParent().getName()
+ '.' + pCol.getName() + "' column is missed.");
}
}
}
}

public void addCustomDepcies(List<Entry<PgStatement, PgStatement>> depcies) {
for (Entry<PgStatement, PgStatement> depcy : depcies) {
graph.addEdge(depcy.getKey(), depcy.getValue());
Expand Down

0 comments on commit e49ad8a

Please sign in to comment.