Skip to content

Commit

Permalink
ADODB PGSQL DATADICT FIX:
Browse files Browse the repository at this point in the history
a) AlterColumnSQL appears to pass the type instead of column name to the NULL/NOT NULL query
b) If you are converting from a BOOL->INT in sql, whilst select (1::INT4)::BOOL is valid, you can not cast in the ALTER TABLE syntax without a USING clause. In addition, it appears one needs to drop then readd any default value.

git-svn-id: http://mantisbt.svn.sourceforge.net/svnroot/mantisbt/trunk@4715 f5dc347c-c33d-0410-90a0-b07cc1902cb9
  • Loading branch information
mantis committed Nov 3, 2007
1 parent ed95d06 commit 10871e5
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions core/adodb/datadict/datadict-postgres.inc.php
Expand Up @@ -180,6 +180,7 @@ function AlterColumnSQL($tabname, $flds, $tableflds='',$tableoptions='')
$tabname = $this->TableName($tabname);
$sql = array();
list($lines,$pkey) = $this->_GenFields($flds);
$set_null = false;
$alter = 'ALTER TABLE ' . $tabname . $this->alterCol . ' ';
foreach($lines as $v) {
if ($not_null = preg_match('/NOT NULL/i',$v)) {
Expand All @@ -193,10 +194,18 @@ function AlterColumnSQL($tabname, $flds, $tableflds='',$tableoptions='')
}

if (preg_match('/^([^ ]+) .*DEFAULT ([^ ]+)/',$v,$matches)) {
$existing = $this->MetaColumns($tabname);
list(,$colname,$default) = $matches;
$v = preg_replace('/^' . preg_quote($colname) . '\s/', '', $v);
$sql[] = $alter . $colname . ' TYPE ' . str_replace('DEFAULT '.$default,'',$v);
$sql[] = 'ALTER TABLE '.$tabname.' ALTER COLUMN '.$colname.' SET DEFAULT ' . $default;
$t = trim(str_replace('DEFAULT '.$default,'',$v));
if ( $existing[strtoupper($colname)]->type == 'bool' && $t == 'INTEGER') {
$sql[] = 'ALTER TABLE '.$tabname.' ALTER COLUMN '.$colname.' DROP DEFAULT';
$sql[] = $alter . $colname . ' TYPE ' . $t . ' USING (redirect_delay::BOOL)::INT ';
$sql[] = 'ALTER TABLE '.$tabname.' ALTER COLUMN '.$colname.' SET DEFAULT ' . $default;
} else {
$sql[] = $alter . $colname . ' TYPE ' . $t;
$sql[] = 'ALTER TABLE '.$tabname.' ALTER COLUMN '.$colname.' SET DEFAULT ' . $default;
}
}
else {
// drop default?
Expand All @@ -205,7 +214,7 @@ function AlterColumnSQL($tabname, $flds, $tableflds='',$tableoptions='')
$sql[] = $alter . $colname . ' TYPE ' . $rest;
}

list($colname) = explode(' ',$v);
//list($colname) = explode(' ',$v);
if ($not_null) {
// this does not error out if the column is already not null
$sql[] = 'ALTER TABLE '.$tabname.' ALTER COLUMN '.$colname.' SET NOT NULL';
Expand All @@ -215,6 +224,7 @@ function AlterColumnSQL($tabname, $flds, $tableflds='',$tableoptions='')
$sql[] = 'ALTER TABLE '.$tabname.' ALTER COLUMN '.$colname.' DROP NOT NULL';
}
}

return $sql;
}

Expand Down

0 comments on commit 10871e5

Please sign in to comment.