diff --git a/phpgwapi/inc/class.schema_proc.inc.php b/phpgwapi/inc/class.schema_proc.inc.php index c3f4b5d3a8..173de0410e 100644 --- a/phpgwapi/inc/class.schema_proc.inc.php +++ b/phpgwapi/inc/class.schema_proc.inc.php @@ -220,6 +220,58 @@ return $retVal && $this->m_oTranslator->CreateTable($this, $this->m_aTables, $sTableName, $aTableDef); } + // This function manually re-created the table incl. primary key and all other indices + // It is meant to use if the primary key, existing indices or column-order changes or + // columns are not longer used or new columns need to be created (with there default value or NULL) + function RefreshTable($sTableName, $aTableDef) + { + if($GLOBALS['DEBUG']) { echo "
schema_proc::RefreshTable('$sTableName',
".print_r($aTableDef,True).")\n\nm_aTables[$sTableName]=".print_r($this->m_aTables[$sTableName],True)."\n"; } + $old_fd = $this->m_aTables[$sTableName]['fd']; + + $Ok = $this->m_oDeltaProc->RefreshTable($this, $this->m_aTables, $sTableName, $aTableDef); + if(!$Ok || $this->m_bDeltaOnly) + { + return $Ok; // nothing else to do + } + $tmp_name = 'tmp_'.$sTableName; + $this->m_odb->transaction_begin(); + + $select = array(); + foreach($aTableDef['fd'] as $name => $data) + { + if (isset($old_fd[$name])) // existing column, use its value => column-name in query + { + $select[] = $name; + } + else // new column => use default value or NULL + { + if (!isset($data['default']) && (!isset($data['nullable']) || $data['nullable'])) + { + $select[] = 'NULL'; + } + else + { + $select[] = $this->m_odb->quote(isset($data['default']) ? $data['default'] : '',$data['type']); + } + } + } + $select = implode(',',$select); + + $Ok = $this->RenameTable($sTableName,$tmp_name) && + $this->CreateTable($sTableName,$aTableDef) && + $this->m_odb->query("INSERT INTO $sTableName SELECT $select FROM $tmp_name",__LINE__,__FILE__); + + if (!$Ok) + { + $this->m_odb->transaction_fail(); + return False; + } + $this->DropTable($tmp_name); + $this->m_odb->transaction_commit(); + + return True; + } + function f($value) { if($this->m_bDeltaOnly) diff --git a/phpgwapi/inc/class.schema_proc_array.inc.php b/phpgwapi/inc/class.schema_proc_array.inc.php index 02349c8483..a165807a22 100644 --- a/phpgwapi/inc/class.schema_proc_array.inc.php +++ b/phpgwapi/inc/class.schema_proc_array.inc.php @@ -155,7 +155,7 @@ } } - return Rrue; + return True; } function AddColumn($oProc, &$aTables, $sTableName, $sColumnName, &$aColumnDef) @@ -179,6 +179,13 @@ } return True; - } + } + + function RefreshTable($oProc, &$aTables, $sTableName, $aTableDef) + { + $aTables[$sTableName] = $aTableDef; + + return True; + } } ?>