fixed (hopefully last) postgres problem with the indices: sequences have not been set, after the data was inserted into the newly created table

This commit is contained in:
Ralf Becker 2004-07-02 22:12:10 +00:00
parent 4ae1356b53
commit 39c7cc14da
2 changed files with 31 additions and 2 deletions

View File

@ -1,4 +1,4 @@
<?php re<?php
/**************************************************************************\ /**************************************************************************\
* eGroupWare - Setup * * eGroupWare - Setup *
* http://www.egroupware.org * * http://www.egroupware.org *
@ -219,6 +219,16 @@
return $retVal && $this->m_oTranslator->CreateTable($this, $this->m_aTables, $sTableName, $aTableDef); return $retVal && $this->m_oTranslator->CreateTable($this, $this->m_aTables, $sTableName, $aTableDef);
} }
function UpdateSequence($sTableName,$sColumnName)
{
if (method_exists($this->m_oTranslator,'UpdateSequence'))
{
return $this->m_oTranslator->UpdateSequence($this->m_odb,$sTableName,$sColumnName);
}
return True;
}
// This function manually re-created the table incl. primary key and all other indices // 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 // It is meant to use if the primary key, existing indices or column-order changes or
@ -269,7 +279,7 @@
$select[] = $value; $select[] = $value;
} }
$select = implode(',',$select); $select = implode(',',$select);
$Ok = $this->RenameTable($sTableName,$tmp_name) && $Ok = $this->RenameTable($sTableName,$tmp_name) &&
$this->CreateTable($sTableName,$aTableDef) && $this->CreateTable($sTableName,$aTableDef) &&
$this->m_odb->query("INSERT INTO $sTableName SELECT DISTINCT $select FROM $tmp_name",__LINE__,__FILE__); $this->m_odb->query("INSERT INTO $sTableName SELECT DISTINCT $select FROM $tmp_name",__LINE__,__FILE__);
@ -279,6 +289,11 @@
$this->m_odb->transaction_abort(); $this->m_odb->transaction_abort();
return False; return False;
} }
// do we need to update the new sequences value ?
if (count($aTableDef['pk']) == 1 && $aTableDef['fd'][$aTableDef['pk'][0]]['type'] == 'auto')
{
$this->UpdateSequence($sTableName,$aTableDef['pk'][0]);
}
$this->DropTable($tmp_name); $this->DropTable($tmp_name);
$this->m_odb->transaction_commit(); $this->m_odb->transaction_commit();

View File

@ -699,6 +699,20 @@
return $Ok; return $Ok;
} }
function UpdateSequence($oDb,$sTableName,$sColName)
{
$sql = "SELECT MAX($sColName) FROM $sTableName";
$oDb->query($sql,__LINE__,__FILE__);
if ($oDb->next_record() && $oDb->f(0))
{
$sql = "SELECT setval('seq_$sTableName',".(1 + $oDb->f(0)).")";
if($GLOBALS['DEBUG']) { echo "<br>Updating sequence 'seq_$sTableName' using: $sql"; }
return $oDb->query($sql,__LINE__,__FILE__);
}
return True;
}
function GetSequenceSQL($sTableName, &$sSequenceSQL) function GetSequenceSQL($sTableName, &$sSequenceSQL)
{ {
$sSequenceSQL = sprintf("CREATE SEQUENCE seq_%s", $sTableName); $sSequenceSQL = sprintf("CREATE SEQUENCE seq_%s", $sTableName);