From b4b346f694f1978f6bdeec8be2deb34a156f5180 Mon Sep 17 00:00:00 2001 From: Kevin Locke Date: Wed, 3 Jun 2015 01:17:48 -0600 Subject: [PATCH] Fix row order in SHOW INDEX query The order of rows returned from SHOW INDEX is significant, and it is used to determine the column order in multi-column indexes. Update the query to return rows in the expected order to avoid errors during upgrade (e.g. in wp_term_taxonomy_term_id_taxonomy where the column order is not alphabetical). Signed-off-by: Kevin Locke --- pg4wp/driver_pgsql_install.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/pg4wp/driver_pgsql_install.php b/pg4wp/driver_pgsql_install.php index 7c0e437..5b147bf 100644 --- a/pg4wp/driver_pgsql_install.php +++ b/pg4wp/driver_pgsql_install.php @@ -79,6 +79,7 @@ WHERE '.$where : '').';'; $pattern = '/SHOW INDEX FROM\s+(\w+)/'; preg_match( $pattern, $sql, $matches); $table = $matches[1]; + // Note: Row order must be in column index position order $sql = 'SELECT bc.relname AS "Table", CASE WHEN i.indisunique THEN \'0\' ELSE \'1\' END AS "Non_unique", CASE WHEN i.indisprimary THEN \'PRIMARY\' WHEN bc.relname LIKE \'%usermeta\' AND ic.relname = \'umeta_key\' @@ -91,7 +92,16 @@ WHERE bc.oid = i.indrelid AND (i.indkey[0] = a.attnum OR i.indkey[1] = a.attnum OR i.indkey[2] = a.attnum OR i.indkey[3] = a.attnum OR i.indkey[4] = a.attnum OR i.indkey[5] = a.attnum OR i.indkey[6] = a.attnum OR i.indkey[7] = a.attnum) AND a.attrelid = bc.oid AND bc.relname = \''.$table.'\' - ORDER BY a.attname;'; + ORDER BY "Key_name", CASE a.attnum + WHEN i.indkey[0] THEN 0 + WHEN i.indkey[1] THEN 1 + WHEN i.indkey[2] THEN 2 + WHEN i.indkey[3] THEN 3 + WHEN i.indkey[4] THEN 4 + WHEN i.indkey[5] THEN 5 + WHEN i.indkey[6] THEN 6 + WHEN i.indkey[7] THEN 7 + END;'; } // SHOW TABLES emulation elseif( preg_match('/SHOW\s+(FULL\s+)?TABLES\s+(?:LIKE\s+(.+)|WHERE\s+(.+))?/i', $sql, $matches))