Skip to content

Commit 9bea63f

Browse files
committed
Merge branch 'DBTOOLS-1804-fix-pg-18-constraint-names' into 'master'
DBTOOLS-1804 fixed pg 18 constraint names diff See merge request codekeeper/pgcodekeeper-core!99
2 parents e978e66 + 79c86c4 commit 9bea63f

File tree

8 files changed

+33
-23
lines changed

8 files changed

+33
-23
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2121
- Fixed CTE formatting in ClickHouse.
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.
24+
- Fixed a false difference between NOT NULL constraints with the default name in PostgreSQL.
2425

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

CHANGELOG.ru.md

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

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

src/main/java/org/pgcodekeeper/core/loader/jdbc/pg/TablesReader.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -231,21 +231,21 @@ private void readColumns(ResultSet res, AbstractPgTable t, long ofTypeOid,
231231
colNotNullNoInherit = getColArray(res, "col_notnull_no_inherit");
232232
}
233233

234-
Integer[] colStatictics;
234+
Integer[] colStatistics;
235235
if (SupportedPgVersion.VERSION_16.isLE(loader.getVersion())) {
236236
// in PG 16 type changed from int4 to int2
237-
Short[] tmpStatictics = getColArray(res, "col_statictics");
238-
colStatictics = new Integer[tmpStatictics.length];
239-
for (int i = 0; i < tmpStatictics.length; i++) {
237+
Short[] tmpStatistics = getColArray(res, "col_statistics");
238+
colStatistics = new Integer[tmpStatistics.length];
239+
for (int i = 0; i < tmpStatistics.length; i++) {
240240
//in PG 17 we can have null instead -1
241-
if (tmpStatictics[i] == null) {
242-
colStatictics[i] = -1;
241+
if (tmpStatistics[i] == null) {
242+
colStatistics[i] = -1;
243243
} else {
244-
colStatictics[i] = (int) tmpStatictics[i];
244+
colStatistics[i] = (int) tmpStatistics[i];
245245
}
246246
}
247247
} else {
248-
colStatictics = getColArray(res, "col_statictics");
248+
colStatistics = getColArray(res, "col_statistics");
249249
}
250250

251251
Boolean[] colIsLocal = getColArray(res, "col_local");
@@ -338,11 +338,11 @@ private void readColumns(ResultSet res, AbstractPgTable t, long ofTypeOid,
338338
column.setNotNull(true);
339339
if (SupportedPgVersion.VERSION_18.isLE(loader.getVersion())) {
340340
column.setNotNullNoInherit(colNotNullNoInherit[i]);
341-
column.setNotNullConName(colNotNullConName[i]);
341+
column.setNotNullConName(t.getName(), colNotNullConName[i]);
342342
}
343343
}
344344

345-
int statistics = colStatictics[i];
345+
int statistics = colStatistics[i];
346346
// if the attstattarget entry for this column is
347347
// non-negative (i.e. it's not the default value)
348348
if (statistics > -1) {
@@ -587,7 +587,7 @@ ELSE NOT EXISTS(SELECT 1 FROM inherits inh WHERE inh.inhrelid = a.attrelid AND i
587587
END
588588
) ORDER BY a.attnum
589589
) AS col_notnull""")
590-
.column("pg_catalog.array_agg(a.attstattarget ORDER BY a.attnum) AS col_statictics")
590+
.column("pg_catalog.array_agg(a.attstattarget ORDER BY a.attnum) AS col_statistics")
591591
.column("pg_catalog.array_agg(a.attislocal ORDER BY a.attnum) AS col_local")
592592
.column("pg_catalog.array_agg(a.attacl::text ORDER BY a.attnum) AS col_acl")
593593
.column("pg_catalog.array_agg(a.attcollation::bigint ORDER BY a.attnum) AS col_collation")
@@ -643,7 +643,7 @@ ELSE NOT EXISTS(SELECT 1 FROM inherits inh WHERE inh.inhrelid = a.attrelid AND i
643643
builder.column("columns.col_type_ids");
644644
builder.column("columns.col_type_name");
645645
builder.column("columns.col_notnull");
646-
builder.column("columns.col_statictics");
646+
builder.column("columns.col_statistics");
647647
builder.column("columns.col_local");
648648
builder.column("columns.col_acl");
649649
builder.column("columns.col_collation");

src/main/java/org/pgcodekeeper/core/parsers/antlr/pg/statement/CreateTable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ protected void fillColNotNull(AbstractTable table, Constraint_commonContext tblC
210210
AntlrTaskManager.submit(antlrTasks, () -> colNameCtx, colCtx -> {
211211
var col = (PgColumn) getSafe(AbstractTable::getColumn, table, colNameCtx);
212212
if (col != null) {
213-
fillColNotNull(col, tblConstrCtx);
213+
fillColNotNull(col, table.getName(), tblConstrCtx);
214214
}
215215
});
216216
}

src/main/java/org/pgcodekeeper/core/parsers/antlr/pg/statement/TableAbstract.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,19 @@ protected void fillColNotNull(AbstractTable table, Constraint_commonContext tblC
9595
Schema_qualified_nameContext colNameCtx) {
9696
var col = (PgColumn) getSafe(AbstractTable::getColumn, table, colNameCtx);
9797
if (col != null) {
98-
fillColNotNull(col, tblConstrCtx);
98+
fillColNotNull(col, table.getName(), tblConstrCtx);
9999
}
100100
}
101101

102-
protected static void fillColNotNull(PgColumn col, Constraint_commonContext constraint) {
102+
protected static void fillColNotNull(PgColumn col, String tableName, Constraint_commonContext constraint) {
103103
var body = constraint.constr_body();
104-
var constraintName = constraint.identifier();
105-
106104
col.setNotNull(body.NOT() != null);
107-
if (constraintName != null) {
108-
col.setNotNullConName(constraintName.getText());
109-
}
110105
col.setNotNullNoInherit(body.inherit_option() != null);
106+
107+
var constraintNameCtx = constraint.identifier();
108+
if (constraintNameCtx != null) {
109+
col.setNotNullConName(tableName, constraintNameCtx.getText());
110+
}
111111
}
112112

113113
private void addTableConstraint(Constraint_commonContext ctx, PgColumn col,
@@ -121,7 +121,7 @@ private void addTableConstraint(Constraint_commonContext ctx, PgColumn col,
121121
col.setDefaultValue(getExpressionText(def, stream));
122122
db.addAnalysisLauncher(new VexAnalysisLauncher(col, def, fileName));
123123
} else if (body.NULL() != null) {
124-
fillColNotNull(col, ctx);
124+
fillColNotNull(col, table.getName(), ctx);
125125
} else if (body.REFERENCES() != null) {
126126
IdentifierContext id = ctx.identifier();
127127
String constrName = id == null ? table.getName() + '_' + colName + "_fkey" : id.getText();

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ private void definitionDefaultNotNull(StringBuilder sbDefinition) {
113113

114114
if (notNull) {
115115
if (notNullConName != null) {
116-
sbDefinition.append(" CONSTRAINT ").append(getNotNullConName());
116+
sbDefinition.append(" CONSTRAINT ").append(notNullConName);
117117
}
118118
sbDefinition.append(NOT_NULL);
119119
if (isNotNullNoInherit) {
@@ -755,6 +755,12 @@ public void setNotNullNoInherit(boolean isNotNullNoInherit) {
755755
resetHash();
756756
}
757757

758+
public void setNotNullConName(String tableName, String conName) {
759+
if (!DEFAULT_NOT_NULL_CONSTRAINT_NAME.formatted(tableName, name).equals(conName)) {
760+
setNotNullConName(conName);
761+
}
762+
}
763+
758764
public void setNotNullConName(String notNullConName) {
759765
this.notNullConName = notNullConName;
760766
resetHash();

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ CREATE TABLE public.testtable (
88
field7 integer,
99
field8 integer,
1010
field9 integer NOT NULL,
11+
field10 integer constraint testtable_field10_not_null NOT NULL,
1112
CONSTRAINT field7_not_null_test NOT NULL field7 NO INHERIT,
1213
CONSTRAINT testtable_field8_not_null NOT NULL field8
1314
);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ CREATE TABLE public.testtable (
77
field6 integer NOT NULL,
88
field7 integer NOT NULL,
99
field8 integer NOT NULL,
10-
field9 integer NOT NULL
10+
field9 integer NOT NULL,
11+
field10 integer NOT NULL
1112
);

0 commit comments

Comments
 (0)