From dd40d59c8404d59a8aeb2ebaf49b3d2849913f5b Mon Sep 17 00:00:00 2001 From: Agniva De Sarker Date: Fri, 26 Jun 2020 21:14:37 +0530 Subject: [PATCH] MM-26514: Fix ALTER PRIMARY KEY migration for Postgres <9.3 (#14912) * MM-26514: Fix ALTER PRIMARY KEY migration for Postgres <9.3 We work around the lack of LATERAL keyword by making the query separately. * Complete full WHERE clause just to be sure --- store/sqlstore/supplier.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/store/sqlstore/supplier.go b/store/sqlstore/supplier.go index 04d353e675..3a2a0b0e84 100644 --- a/store/sqlstore/supplier.go +++ b/store/sqlstore/supplier.go @@ -786,7 +786,8 @@ func (ss *SqlSupplier) AlterPrimaryKey(tableName string, columnNames []string) b var currentPrimaryKey string var err error // get the current primary key as a comma separated list of columns - if ss.DriverName() == model.DATABASE_DRIVER_MYSQL { + switch ss.DriverName() { + case model.DATABASE_DRIVER_MYSQL: query := ` SELECT GROUP_CONCAT(column_name ORDER BY seq_in_index) AS PK FROM @@ -798,13 +799,13 @@ func (ss *SqlSupplier) AlterPrimaryKey(tableName string, columnNames []string) b GROUP BY index_name` currentPrimaryKey, err = ss.GetMaster().SelectStr(query, tableName) - } else if ss.DriverName() == model.DATABASE_DRIVER_POSTGRES { + case model.DATABASE_DRIVER_POSTGRES: query := ` SELECT string_agg(a.attname, ',') AS pk FROM pg_constraint AS c - CROSS JOIN LATERAL - UNNEST(c.conkey) AS cols(colnum) + CROSS JOIN + (SELECT unnest(conkey) FROM pg_constraint WHERE conrelid='` + strings.ToLower(tableName) + `'::REGCLASS AND contype='p') AS cols(colnum) INNER JOIN pg_attribute AS a ON a.attrelid = c.conrelid AND cols.colnum = a.attnum @@ -812,7 +813,7 @@ func (ss *SqlSupplier) AlterPrimaryKey(tableName string, columnNames []string) b c.contype = 'p' AND c.conrelid = '` + strings.ToLower(tableName) + `'::REGCLASS` currentPrimaryKey, err = ss.GetMaster().SelectStr(query) - } else if ss.DriverName() == model.DATABASE_DRIVER_SQLITE { + case model.DATABASE_DRIVER_SQLITE: // SQLite doesn't support altering primary key return true }