Skip to content

Commit

Permalink
Additional commit for CO-2211
Browse files Browse the repository at this point in the history
  • Loading branch information
Benn Oshrin committed Oct 12, 2021
1 parent 9e39142 commit 3c41125
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions app/src/Lib/Match/MatchgridBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ protected function configToSchema($dbc, \Cake\Datasource\EntityInterface $Matchg

// Track which attributes need case insensitive indexes
$ciAttrs = [];

if($indexes) {
// Since index names need to be unique across the schema, we'll use the
// matchgrid ID to make the names unique.
Expand Down Expand Up @@ -165,15 +166,19 @@ protected function configToSchema($dbc, \Cake\Datasource\EntityInterface $Matchg
// currently the least painful way to do it
$sql = rtrim($sql, ")") . " NULLS FIRST)";
} elseif(preg_match("/^CREATE INDEX (\w+) ON .*/", $sql, $matches)) {
$attr = $ciAttrs[ $matches[1] ];
// Not ideal to hardcode SQL, but this should be pretty portable.
// Note the validation rule for table_name and attr is pretty restrictive,
// so we don't need to SQL escape them. Similarly, we create the index
// name above.
// We use IF NOT EXISTS to avoid errors on rebuilding the matchgrid,
// however a better long term solution would be to add native support to
// DBAL for expression in index creation (CO-2217).
$sql = "CREATE INDEX IF NOT EXISTS " . $matches[1] . " ON " . $Matchgrid->prefixed_table_name . " (LOWER(" . $attr . "))";
// If we're creating an index on a case insensitive attribute, swap out
// the SQL for a lower cased index
if(!empty($ciAttrs[ $matches[1] ])) {
$attr = $ciAttrs[ $matches[1] ];
// Not ideal to hardcode SQL, but this should be pretty portable.
// Note the validation rule for table_name and attr is pretty restrictive,
// so we don't need to SQL escape them. Similarly, we create the index
// name above.
// We use IF NOT EXISTS to avoid errors on rebuilding the matchgrid,
// however a better long term solution would be to add native support to
// DBAL for expression in index creation (CO-2217).
$sql = "CREATE INDEX IF NOT EXISTS " . $matches[1] . " ON " . $Matchgrid->prefixed_table_name . " (LOWER(" . $attr . "))";
}
}

$stmt = $dbc->query($sql);
Expand Down

0 comments on commit 3c41125

Please sign in to comment.