forked from GNUsocial/gnu-social
sorted enums and auto_increments on postgres.
Still needs inline indexes on table creation
This commit is contained in:
parent
065ecc5573
commit
599942f58a
@ -41,6 +41,7 @@ if (!defined('STATUSNET')) {
|
|||||||
* @category Database
|
* @category Database
|
||||||
* @package StatusNet
|
* @package StatusNet
|
||||||
* @author Evan Prodromou <evan@status.net>
|
* @author Evan Prodromou <evan@status.net>
|
||||||
|
* @author Brenda Wallace <shiny@cpan.org>
|
||||||
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
|
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
|
||||||
* @link http://status.net/
|
* @link http://status.net/
|
||||||
*/
|
*/
|
||||||
@ -79,7 +80,6 @@ class PgsqlSchema extends Schema
|
|||||||
$row = array();
|
$row = array();
|
||||||
|
|
||||||
while ($res->fetchInto($row, DB_FETCHMODE_ASSOC)) {
|
while ($res->fetchInto($row, DB_FETCHMODE_ASSOC)) {
|
||||||
// var_dump($row);
|
|
||||||
$cd = new ColumnDef();
|
$cd = new ColumnDef();
|
||||||
|
|
||||||
$cd->name = $row['field'];
|
$cd->name = $row['field'];
|
||||||
@ -155,7 +155,6 @@ class PgsqlSchema extends Schema
|
|||||||
}
|
}
|
||||||
|
|
||||||
$sql .= $this->_columnSql($cd);
|
$sql .= $this->_columnSql($cd);
|
||||||
|
|
||||||
switch ($cd->key) {
|
switch ($cd->key) {
|
||||||
case 'UNI':
|
case 'UNI':
|
||||||
$uniques[] = $cd->name;
|
$uniques[] = $cd->name;
|
||||||
@ -188,7 +187,7 @@ class PgsqlSchema extends Schema
|
|||||||
$res = $this->conn->query($sql);
|
$res = $this->conn->query($sql);
|
||||||
|
|
||||||
if (PEAR::isError($res)) {
|
if (PEAR::isError($res)) {
|
||||||
throw new Exception($res->getMessage());
|
throw new Exception($res->getMessage(). ' SQL was '. $sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@ -223,7 +222,7 @@ class PgsqlSchema extends Schema
|
|||||||
*/
|
*/
|
||||||
private function _columnTypeTranslation($type) {
|
private function _columnTypeTranslation($type) {
|
||||||
$map = array(
|
$map = array(
|
||||||
'datetime' => 'timestamp'
|
'datetime' => 'timestamp',
|
||||||
);
|
);
|
||||||
if(!empty($map[$type])) {
|
if(!empty($map[$type])) {
|
||||||
return $map[$type];
|
return $map[$type];
|
||||||
@ -397,16 +396,17 @@ class PgsqlSchema extends Schema
|
|||||||
$todrop = array_diff($cur, $new);
|
$todrop = array_diff($cur, $new);
|
||||||
$same = array_intersect($new, $cur);
|
$same = array_intersect($new, $cur);
|
||||||
$tomod = array();
|
$tomod = array();
|
||||||
|
|
||||||
foreach ($same as $m) {
|
foreach ($same as $m) {
|
||||||
$curCol = $this->_byName($td->columns, $m);
|
$curCol = $this->_byName($td->columns, $m);
|
||||||
$newCol = $this->_byName($columns, $m);
|
$newCol = $this->_byName($columns, $m);
|
||||||
|
|
||||||
|
|
||||||
if (!$newCol->equals($curCol)) {
|
if (!$newCol->equals($curCol)) {
|
||||||
$tomod[] = $newCol->name;
|
// BIG GIANT TODO!
|
||||||
|
// stop it detecting different types and trying to modify on every page request
|
||||||
|
// $tomod[] = $newCol->name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (count($toadd) + count($todrop) + count($tomod) == 0) {
|
if (count($toadd) + count($todrop) + count($tomod) == 0) {
|
||||||
// nothing to do
|
// nothing to do
|
||||||
return true;
|
return true;
|
||||||
@ -434,7 +434,7 @@ class PgsqlSchema extends Schema
|
|||||||
}
|
}
|
||||||
|
|
||||||
$sql = 'ALTER TABLE ' . $tableName . ' ' . implode(', ', $phrase);
|
$sql = 'ALTER TABLE ' . $tableName . ' ' . implode(', ', $phrase);
|
||||||
|
echo "<p>$sql</p>";
|
||||||
$res = $this->conn->query($sql);
|
$res = $this->conn->query($sql);
|
||||||
|
|
||||||
if (PEAR::isError($res)) {
|
if (PEAR::isError($res)) {
|
||||||
@ -496,12 +496,21 @@ class PgsqlSchema extends Schema
|
|||||||
*
|
*
|
||||||
* @return string correct SQL for that column
|
* @return string correct SQL for that column
|
||||||
*/
|
*/
|
||||||
|
|
||||||
private function _columnSql($cd)
|
private function _columnSql($cd)
|
||||||
{
|
{
|
||||||
$sql = "{$cd->name} ";
|
$sql = "{$cd->name} ";
|
||||||
$type = $this->_columnTypeTranslation($cd->type);
|
$type = $this->_columnTypeTranslation($cd->type);
|
||||||
|
|
||||||
|
//handle those mysql enum fields that postgres doesn't support
|
||||||
|
if (preg_match('!^enum!', $type)) {
|
||||||
|
$allowed_values = preg_replace('!^enum!', '', $type);
|
||||||
|
$sql .= " text check ({$cd->name} in $allowed_values)";
|
||||||
|
return $sql;
|
||||||
|
}
|
||||||
|
if (!empty($cd->auto_increment)) {
|
||||||
|
$type = 'serial';
|
||||||
|
}
|
||||||
|
|
||||||
if (!empty($cd->size)) {
|
if (!empty($cd->size)) {
|
||||||
$sql .= "{$type}({$cd->size}) ";
|
$sql .= "{$type}({$cd->size}) ";
|
||||||
} else {
|
} else {
|
||||||
@ -513,10 +522,6 @@ class PgsqlSchema extends Schema
|
|||||||
} else {
|
} else {
|
||||||
$sql .= ($cd->nullable) ? "null " : "not null ";
|
$sql .= ($cd->nullable) ? "null " : "not null ";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!empty($cd->auto_increment)) {
|
|
||||||
$sql .= " auto_increment ";
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!empty($cd->extra)) {
|
if (!empty($cd->extra)) {
|
||||||
$sql .= "{$cd->extra} ";
|
$sql .= "{$cd->extra} ";
|
||||||
|
Loading…
Reference in New Issue
Block a user