diff --git a/core/adodb/datadict/datadict-postgres.inc.php b/core/adodb/datadict/datadict-postgres.inc.php index 83fa455668..d7cf5a663a 100644 --- a/core/adodb/datadict/datadict-postgres.inc.php +++ b/core/adodb/datadict/datadict-postgres.inc.php @@ -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)) { @@ -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? @@ -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'; @@ -215,6 +224,7 @@ function AlterColumnSQL($tabname, $flds, $tableflds='',$tableoptions='') $sql[] = 'ALTER TABLE '.$tabname.' ALTER COLUMN '.$colname.' DROP NOT NULL'; } } + return $sql; }