From 8888b82852deedc788e4a338984a6e4445bfb97f Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Tue, 19 Sep 2023 12:34:30 +0300 Subject: [PATCH] CheckSchema: unique vs unique index improvement --- .github/workflows/ci.yml | 2 +- db_information.go | 1 + db_information_test.go | 2 +- desc/constraint.go | 14 +++++++------- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e6bd96e..a917db1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,7 +21,7 @@ jobs: services: postgres: - image: postgres:15-alpine + image: postgres:16-alpine env: POSTGRES_USER: postgres POSTGRES_PASSWORD: admin!123 diff --git a/db_information.go b/db_information.go index bb27fce..4f22a09 100644 --- a/db_information.go +++ b/db_information.go @@ -469,6 +469,7 @@ func (db *DB) ListColumns(ctx context.Context, tableNames ...string) ([]*desc.Co if uniqueIndex.TableName == column.TableName { for _, columnName := range uniqueIndex.Columns { if columnName == column.Name { + column.Unique = false column.UniqueIndex = uniqueIndex.IndexName break uniqueIndexLoop } diff --git a/db_information_test.go b/db_information_test.go index 2ec1fc5..d746d7d 100644 --- a/db_information_test.go +++ b/db_information_test.go @@ -6,7 +6,7 @@ import ( ) // This should match the CI's postgres version. -const expectedDBVersion = "15" +const expectedDBVersion = "16.0" func TestInformation_GetVersion(t *testing.T) { db, err := openEmptyTestConnection() diff --git a/desc/constraint.go b/desc/constraint.go index 0523e12..6cf7a51 100644 --- a/desc/constraint.go +++ b/desc/constraint.go @@ -125,13 +125,13 @@ func (c *Constraint) BuildColumn(column *Column) error { case PrimaryKeyConstraintType: column.PrimaryKey = true case UniqueConstraintType: - // if len(c.Unique.Columns) == 0 { - // // simple unique to itself. - // column.Unique = true - // } else { - // column.UniqueIndex = c.ConstraintName - // } - column.Unique = true + if len(c.Unique.Columns) == 0 || (len(c.Unique.Columns) == 1 && c.Unique.Columns[0] == c.ColumnName) { + // simple unique to itself. + column.Unique = true + } else { + column.UniqueIndex = c.ConstraintName + } + // column.Unique = true case CheckConstraintType: column.CheckConstraint = c.Check.Expression case ForeignKeyConstraintType: