[interchange] Fix error creating indexes in Postgres when QUOTE_IDENTIFIERS is set

Jon Jensen interchange-cvs at icdevgroup.org
Tue Mar 8 05:00:08 UTC 2016


commit 86fbcb14c402efda89b2329b73270ae513715ddf
Author: Jon Jensen <jon at endpoint.com>
Date:   Mon Mar 7 21:18:36 2016 -0700

    Fix error creating indexes in Postgres when QUOTE_IDENTIFIERS is set
    
    The problem was that Vend::Table::DBI was assembling an index name
    based on the quoted table name plus '_' plus quoted column name,
    resulting in a syntactically invalid name like "variants"_"sku",
    leading to errors like this:
    
    DBI: Post creation query 'CREATE  INDEX "variants"_"sku" ON "variants" ("sku")' failed: ERROR:  syntax error at or near "_"
    LINE 1: CREATE  INDEX "variants"_"sku" ON "variants" ("sku")
                                    ^
    Instead assemble the name unquoted, and quote the final result.

 lib/Vend/Table/DBI.pm |   22 ++++++++++++++--------
 1 files changed, 14 insertions(+), 8 deletions(-)
---
diff --git a/lib/Vend/Table/DBI.pm b/lib/Vend/Table/DBI.pm
index 68f94f6..06b4791 100644
--- a/lib/Vend/Table/DBI.pm
+++ b/lib/Vend/Table/DBI.pm
@@ -1,8 +1,6 @@
 # Vend::Table::DBI - Access a table stored in an DBI/DBD database
 #
-# $Id: DBI.pm,v 2.88 2008-06-30 23:09:53 jon Exp $
-#
-# Copyright (C) 2002-2008 Interchange Development Group
+# Copyright (C) 2002-2016 Interchange Development Group
 # Copyright (C) 1996-2002 Red Hat, Inc.
 #
 # This program is free software; you can redistribute it and/or modify
@@ -179,7 +177,7 @@ my %known_capability = (
 	},
 	ALTER_INDEX	 => { 
 		mysql => 'CREATE _UNIQUE_ INDEX $TABLE$_$COLUMN$ ON _TABLE_ (_COLUMN_)',
-		Pg => 'CREATE _UNIQUE_ INDEX $TABLE$_$COLUMN$ ON _TABLE_ (_COLUMN_)',
+		Pg => 'CREATE _UNIQUE_ INDEX $TABLE_COLUMN$ ON _TABLE_ (_COLUMN_)',
 		default => 'CREATE _UNIQUE_ INDEX $TABLE$_$COLUMN$ ON _TABLE_ (_COLUMN_)',
 	},
 	LIST_FIELDS_QUERY => { 
@@ -521,17 +519,25 @@ sub create {
 			my $col = $def;
 			$col =~ s/\W.*//s;
 			$key_index_found = 1 if lc($col) eq lc($key);
-			my $qcol = $config->{QUOTE_IDENTIFIERS} ? $db->quote_identifier($col) : $col;
+
+			my $qcol = $col;
 			my $qdef = $def;
-			$qdef =~ s/^\Q$col\E/$qcol/i if $config->{QUOTE_IDENTIFIERS};
+			my $qtable_col = "${tablename}_$col";
+			if ($config->{QUOTE_IDENTIFIERS}) {
+				$qcol = $db->quote_identifier($qcol);
+				$qdef =~ s/^\Q$col\E/$qcol/i;
+				$qtable_col = $db->quote_identifier($qtable_col);
+			}
+
 			my $template = $config->{ALTER_INDEX}
 						|| $known_capability{ALTER_INDEX}{default};
 			$template =~ s/\b_TABLE_\b/$qtable/g;
 			$template =~ s/\b_COLUMN_\b/$qcol/g;
 			$template =~ s/\b_DEF_\b/$qdef/g;
+			$template =~ s/\$TABLE_COLUMN\$/$qtable_col/g;
 			$template =~ s/\$TABLE\$/$qtable/g;
-			$template =~ s/\$DEF\$/$qdef/g;
 			$template =~ s/\$COLUMN\$/$qcol/g;
+			$template =~ s/\$DEF\$/$qdef/g;
 			$template =~ s/\b_UNIQUE_(\w+_)?/$uniq ? ($1 || $uniq) : ''/eg;
 			push @index, $template;
 		}
@@ -564,7 +570,7 @@ sub create {
 #::logDebug("Running: $_");
 		$db->do($_) 
 			or ::logError(
-							"DBI: Post creation query '%s' failed: %s" ,
+							"DBI: Index creation query '%s' failed: %s" ,
 							$_,
 							$DBI::errstr,
 				);



More information about the interchange-cvs mailing list