mirror of
https://github.com/silverstripe/silverstripe-postgresql
synced 2024-10-22 15:05:45 +00:00
BUGFIX: resolves ticket 6078 - enums now removed. Also, indexes now default to btree.
This commit is contained in:
parent
f125bc22b1
commit
a59e02efee
@ -502,18 +502,20 @@ class PostgreSQLDatabase extends SS_Database {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SET check constraint (The constraint HAS to be dropped)
|
// SET check constraint (The constraint HAS to be dropped)
|
||||||
if(!empty($matches[4])) {
|
|
||||||
$existing_constraint=$this->query("SELECT conname FROM pg_constraint WHERE conname='{$tableName}_{$colName}_check';")->value();
|
$existing_constraint=$this->query("SELECT conname FROM pg_constraint WHERE conname='{$tableName}_{$colName}_check';")->value();
|
||||||
//If you run into constraint row violation conflicts, here's how to reset it:
|
//If you run into constraint row violation conflicts, here's how to reset it:
|
||||||
//alter table "SiteTree" drop constraint "SiteTree_ClassName_check";
|
//alter table "SiteTree" drop constraint "SiteTree_ClassName_check";
|
||||||
//update "SiteTree" set "ClassName"='NewValue' WHERE "ClassName"='OldValue';
|
//update "SiteTree" set "ClassName"='NewValue' WHERE "ClassName"='OldValue';
|
||||||
//Repeat this for _Live and for _versions
|
//Repeat this for _Live and for _versions
|
||||||
if($existing_constraint){
|
|
||||||
|
//First, delete any existing constraint on this column, even if it's no longer an enum
|
||||||
|
if($existing_constraint)
|
||||||
$alterCol .= ",\nDROP CONSTRAINT \"{$tableName}_{$colName}_check\"";
|
$alterCol .= ",\nDROP CONSTRAINT \"{$tableName}_{$colName}_check\"";
|
||||||
}
|
|
||||||
|
//Now create the constraint (if we've asked for one)
|
||||||
|
if(!empty($matches[4]))
|
||||||
$alterCol .= ",\nADD CONSTRAINT \"{$tableName}_{$colName}_check\" $matches[4]";
|
$alterCol .= ",\nADD CONSTRAINT \"{$tableName}_{$colName}_check\" $matches[4]";
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return isset($alterCol) ? $alterCol : '';
|
return isset($alterCol) ? $alterCol : '';
|
||||||
}
|
}
|
||||||
@ -681,12 +683,14 @@ class PostgreSQLDatabase extends SS_Database {
|
|||||||
case 'unique':
|
case 'unique':
|
||||||
$indexSpec='unique (' . $indexSpec['value'] . ')';
|
$indexSpec='unique (' . $indexSpec['value'] . ')';
|
||||||
break;
|
break;
|
||||||
case 'btree':
|
|
||||||
$indexSpec='using btree (' . $indexSpec['value'] . ')';
|
|
||||||
break;
|
|
||||||
case 'hash':
|
case 'hash':
|
||||||
$indexSpec='using hash (' . $indexSpec['value'] . ')';
|
$indexSpec='using hash (' . $indexSpec['value'] . ')';
|
||||||
break;
|
break;
|
||||||
|
case 'index':
|
||||||
|
//The default index is 'btree', which we'll use by default (below):
|
||||||
|
default:
|
||||||
|
$indexSpec='using btree (' . $indexSpec['value'] . ')';
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -734,10 +738,17 @@ class PostgreSQLDatabase extends SS_Database {
|
|||||||
if(isset($indexSpec['where']))
|
if(isset($indexSpec['where']))
|
||||||
$where='WHERE ' . $indexSpec['where'];
|
$where='WHERE ' . $indexSpec['where'];
|
||||||
|
|
||||||
//One last check:
|
//Fix up the value entry to be quoted:
|
||||||
$existing=DB::query("SELECT tablename FROM pg_indexes WHERE indexname='$tableCol';");
|
$value_bits=explode(',', $indexSpec['value']);
|
||||||
|
$new_values=Array();
|
||||||
|
foreach($value_bits as $value){
|
||||||
|
$new_values[]="\"" . trim($value, ' "') . "\"";
|
||||||
|
}
|
||||||
|
$indexSpec['value']=implode(',', $new_values);
|
||||||
|
|
||||||
if(!$existing){
|
//One last check:
|
||||||
|
$existing=DB::query("SELECT tablename FROM pg_indexes WHERE indexname='" . strtolower($tableCol) . "';");
|
||||||
|
if(!$existing->first()){
|
||||||
//create a type-specific index
|
//create a type-specific index
|
||||||
//NOTE: hash should be removed. This is only here to demonstrate how other indexes can be made
|
//NOTE: hash should be removed. This is only here to demonstrate how other indexes can be made
|
||||||
switch($indexSpec['type']){
|
switch($indexSpec['type']){
|
||||||
@ -746,28 +757,30 @@ class PostgreSQLDatabase extends SS_Database {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case 'unique':
|
case 'unique':
|
||||||
$spec="create unique index $tableCol ON \"" . $tableName . "\" (\"" . $indexSpec['value'] . "\") $fillfactor $where";
|
$spec="create unique index $tableCol ON \"" . $tableName . "\" (" . $indexSpec['value'] . ") $fillfactor $where";
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'btree':
|
case 'btree':
|
||||||
$spec="create index $tableCol ON \"" . $tableName . "\" USING btree (\"" . $indexSpec['value'] . "\") $fillfactor $where";
|
$spec="create index $tableCol ON \"" . $tableName . "\" USING btree (" . $indexSpec['value'] . ") $fillfactor $where";
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'hash':
|
case 'hash':
|
||||||
//NOTE: this is not a recommended index type
|
//NOTE: this is not a recommended index type
|
||||||
$spec="create index $tableCol ON \"" . $tableName . "\" USING hash (\"" . $indexSpec['value'] . "\") $fillfactor $where";
|
$spec="create index $tableCol ON \"" . $tableName . "\" USING hash (" . $indexSpec['value'] . ") $fillfactor $where";
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'index':
|
case 'index':
|
||||||
//'index' is the same as default, just a normal index with the default type decided by the database.
|
//'index' is the same as default, just a normal index with the default type decided by the database.
|
||||||
default:
|
default:
|
||||||
$spec="create index $tableCol ON \"" . $tableName . "\" (\"" . $indexSpec['value'] . "\") $fillfactor $where";
|
$spec="create index $tableCol ON \"" . $tableName . "\" (" . $indexSpec['value'] . ") $fillfactor $where";
|
||||||
}
|
}
|
||||||
|
|
||||||
return trim($spec) . ';';
|
return trim($spec) . ';';
|
||||||
} else
|
} else {
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
$indexName=trim($indexName, '()');
|
$indexName=trim($indexName, '()');
|
||||||
return $indexName;
|
return $indexName;
|
||||||
@ -1368,7 +1381,7 @@ class PostgreSQLDatabase extends SS_Database {
|
|||||||
$keywords=str_replace('"', "'", $keywords);
|
$keywords=str_replace('"', "'", $keywords);
|
||||||
|
|
||||||
$keywords = Convert::raw2sql(trim($keywords));
|
$keywords = Convert::raw2sql(trim($keywords));
|
||||||
$htmlEntityKeywords = htmlentities($keywords);
|
$htmlEntityKeywords = htmlentities($keywords, ENT_NOQUOTES);
|
||||||
|
|
||||||
//We can get a list of all the tsvector columns though this query:
|
//We can get a list of all the tsvector columns though this query:
|
||||||
//We know what tables to search in based on the $classesToSearch variable:
|
//We know what tables to search in based on the $classesToSearch variable:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user