Skip to content

Commit aa513fd

Browse files
committed
Merge branch 'DBTOOLS-1779-remove-only-in-partitioned-tables' into 'master'
DBTOOLS-1779 remove only in partitioned tables See merge request codekeeper/pgcodekeeper-core!96
2 parents 9bea63f + 37e1b81 commit aa513fd

File tree

7 files changed

+103
-16
lines changed

7 files changed

+103
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2222
- Fixed a bug in the behavior of the compiler when the ignore column order setting is enabled.
2323
- Fixed bug in generating the migration script when adding a column to Log family tables in ClickHouse.
2424
- Fixed a false difference between NOT NULL constraints with the default name in PostgreSQL.
25+
- Fixed adding ONLY for columns in partitioned tables in PostgreSQL.
2526

2627
## [12.0.0] - 2025-10-14
2728

CHANGELOG.ru.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
- Исправлена ошибка сравнения таблиц при включённой настройке игнорирования порядка столбцов.
2323
- Исправлена ошибка в генерации скрипта миграции при добавлении колонки в таблицах семейства Log в ClickHouse.
2424
- Исправлено ложное различие ограничений NOT NULL с именем по умолчанию в PostgreSQL.
25+
- Исправлено добавление ONLY для колонок в партиционных таблицах в PostgreSQL.
2526

2627
## [12.0.0] - 2025-10-14
2728

src/main/java/org/pgcodekeeper/core/schema/pg/PartitionGpTable.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@ private void compareTemplates(PartitionGpTable newTable, SQLScript script) {
162162
});
163163
}
164164

165+
@Override
166+
public String getPartitionBy() {
167+
return normalizedPartitionGpBounds;
168+
}
169+
165170
@Override
166171
protected void compareTableTypes(AbstractPgTable newTable, SQLScript script) {
167172
// no implements

src/main/java/org/pgcodekeeper/core/schema/pg/PgColumn.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -220,19 +220,8 @@ private String getAlterTableColumn(boolean only, String column, boolean needAlte
220220
@Override
221221
public void getDropSQL(SQLScript script, boolean optionExists) {
222222
if (type != null && getParentCol((AbstractPgTable) parent) == null) {
223-
boolean addOnly = true;
224-
225-
//// Condition for partitioned tables.
226-
// If there are sections, then it is impossible to delete a column
227-
// only from a partitioned table.
228-
// Because of impossible inherit from partitioned tables, this
229-
// condition will also be true for cases when a partitioned table
230-
// does not have sections.
231-
if (parent instanceof AbstractRegularTable regTable) {
232-
addOnly = regTable.getPartitionBy() == null;
233-
}
234223
StringBuilder dropSb = new StringBuilder();
235-
dropSb.append(getAlterTable(addOnly)).append("\n\tDROP COLUMN ");
224+
dropSb.append(getAlterTable(isNeedOnly())).append("\n\tDROP COLUMN ");
236225
if (optionExists) {
237226
dropSb.append(IF_EXISTS);
238227
}
@@ -255,6 +244,16 @@ public void getDropSQL(SQLScript script, boolean optionExists) {
255244
appendComments(script);
256245
}
257246

247+
/**
248+
* @return true if parent table isn't partition table with existing partitions, false otherwise
249+
*/
250+
private boolean isNeedOnly() {
251+
if (parent instanceof AbstractRegularTable regTable) {
252+
return regTable.getPartitionBy() == null;
253+
}
254+
return true;
255+
}
256+
258257
@Override
259258
public ObjectState appendAlterSQL(PgStatement newCondition, SQLScript script) {
260259
int startSize = script.getSize();
@@ -311,7 +310,7 @@ private boolean compareCompressOptions(PgColumn newColumn) {
311310
private void writeOptions(boolean isCreate, SQLScript script) {
312311
if (!options.isEmpty()) {
313312
StringBuilder sb = new StringBuilder();
314-
sb.append(getAlterTableColumn(true, name));
313+
sb.append(getAlterTableColumn(isNeedOnly(), name));
315314
sb.append(isCreate ? " SET (" : " RESET (");
316315
for (Entry<String, String> option : options.entrySet()) {
317316
sb.append(option.getKey());
@@ -492,7 +491,7 @@ private void compareNotNull(PgColumn oldColumn, PgColumn newColumn, SQLScript sc
492491
boolean newNotNull = newColumn != null && newColumn.isNotNull();
493492

494493
if (oldNotNull && !newNotNull) {
495-
script.addStatement(getAlterTableColumn(true, name) + " DROP" + NOT_NULL);
494+
script.addStatement(getAlterTableColumn(isNeedOnly(), name) + " DROP" + NOT_NULL);
496495
return;
497496
}
498497

src/test/resources/org/pgcodekeeper/core/it/diff/pg/modify_partition_table_diff.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ SET search_path = pg_catalog;
22

33
DROP TABLE public.cities_mz;
44

5+
ALTER TABLE public.sales
6+
DROP COLUMN amount;
7+
58
DROP TABLE public.cities_hi;
69

710
ALTER TABLE public.cities_cd
@@ -35,6 +38,9 @@ CREATE TABLE public.tab_of_type OF public.comp (
3538

3639
ALTER TABLE public.tab_of_type OWNER TO galiev_mr;
3740

41+
ALTER TABLE public.measurement
42+
ALTER COLUMN logdate DROP NOT NULL;
43+
3844
ALTER TABLE public.cities_ab
3945
DROP CONSTRAINT constr_check;
4046

src/test/resources/org/pgcodekeeper/core/it/diff/pg/modify_partition_table_new.sql

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,41 @@ CREATE TABLE public.tab_of_type OF public.comp (
8282
CONSTRAINT tab_of_type_f3_check CHECK ((f3 > 1))
8383
);
8484

85-
ALTER TABLE public.tab_of_type OWNER TO galiev_mr;
85+
ALTER TABLE public.tab_of_type OWNER TO galiev_mr;
86+
87+
CREATE TABLE public.measurement (
88+
id integer DEFAULT nextval('public.measurement_id_seq'::regclass) NOT NULL,
89+
logdate date,
90+
temperature real
91+
)
92+
PARTITION BY RANGE (logdate);
93+
94+
ALTER TABLE public.measurement OWNER TO rashit;
95+
96+
CREATE TABLE public.measurement_2023 PARTITION OF public.measurement
97+
FOR VALUES FROM ('2023-01-01') TO ('2024-01-01');
98+
99+
ALTER TABLE ONLY public.measurement_2023 ALTER COLUMN id SET DEFAULT nextval('public.measurement_id_seq'::regclass);
100+
101+
ALTER TABLE public.measurement_2023 OWNER TO rashit;
102+
103+
CREATE TABLE public.measurement_2024 PARTITION OF public.measurement
104+
FOR VALUES FROM ('2024-01-01') TO ('2025-01-01');
105+
106+
ALTER TABLE ONLY public.measurement_2024 ALTER COLUMN id SET DEFAULT nextval('public.measurement_id_seq'::regclass);
107+
108+
ALTER TABLE public.measurement_2024 OWNER TO rashit;
109+
110+
CREATE TABLE public.sales (
111+
id integer,
112+
sale_date date,
113+
rand integer
114+
)
115+
DISTRIBUTED BY (id)
116+
PARTITION BY RANGE(sale_date)
117+
(
118+
START ('2024-01-01'::date) END ('2024-04-01'::date) EVERY ('1 mon'::interval),
119+
DEFAULT PARTITION extra
120+
);
121+
122+
ALTER TABLE public.sales OWNER TO rashit;

src/test/resources/org/pgcodekeeper/core/it/diff/pg/modify_partition_table_original.sql

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,42 @@ CREATE TABLE public.tab_of_type OF public.comp (
9494
)
9595
PARTITION BY LIST ("left"(lower(f2), 1));
9696

97-
ALTER TABLE public.tab_of_type OWNER TO galiev_mr;
97+
ALTER TABLE public.tab_of_type OWNER TO galiev_mr;
98+
99+
CREATE TABLE public.measurement (
100+
id integer DEFAULT nextval('public.measurement_id_seq'::regclass) NOT NULL,
101+
logdate date NOT NULL,
102+
temperature real
103+
)
104+
PARTITION BY RANGE (logdate);
105+
106+
ALTER TABLE public.measurement OWNER TO rashit;
107+
108+
CREATE TABLE public.measurement_2023 PARTITION OF public.measurement
109+
FOR VALUES FROM ('2023-01-01') TO ('2024-01-01');
110+
111+
ALTER TABLE ONLY public.measurement_2023 ALTER COLUMN id SET DEFAULT nextval('public.measurement_id_seq'::regclass);
112+
113+
ALTER TABLE public.measurement_2023 OWNER TO rashit;
114+
115+
CREATE TABLE public.measurement_2024 PARTITION OF public.measurement
116+
FOR VALUES FROM ('2024-01-01') TO ('2025-01-01');
117+
118+
ALTER TABLE ONLY public.measurement_2024 ALTER COLUMN id SET DEFAULT nextval('public.measurement_id_seq'::regclass);
119+
120+
ALTER TABLE public.measurement_2024 OWNER TO rashit;
121+
122+
CREATE TABLE public.sales (
123+
id integer,
124+
sale_date date,
125+
amount numeric(10,2),
126+
rand integer
127+
)
128+
DISTRIBUTED BY (id)
129+
PARTITION BY RANGE(sale_date)
130+
(
131+
START ('2024-01-01'::date) END ('2024-04-01'::date) EVERY ('1 mon'::interval),
132+
DEFAULT PARTITION extra
133+
);
134+
135+
ALTER TABLE public.sales OWNER TO rashit;

0 commit comments

Comments
 (0)