From 39c7cc14da2ee8ab5be7770c56ea7c20fe0eb163 Mon Sep 17 00:00:00 2001 From: Ralf Becker Date: Fri, 2 Jul 2004 22:12:10 +0000 Subject: [PATCH] fixed (hopefully last) postgres problem with the indices: sequences have not been set, after the data was inserted into the newly created table --- phpgwapi/inc/class.schema_proc.inc.php | 19 +++++++++++++++++-- phpgwapi/inc/class.schema_proc_pgsql.inc.php | 14 ++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/phpgwapi/inc/class.schema_proc.inc.php b/phpgwapi/inc/class.schema_proc.inc.php index 210c274248..120b74beaf 100644 --- a/phpgwapi/inc/class.schema_proc.inc.php +++ b/phpgwapi/inc/class.schema_proc.inc.php @@ -1,4 +1,4 @@ -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 // It is meant to use if the primary key, existing indices or column-order changes or @@ -269,7 +279,7 @@ $select[] = $value; } $select = implode(',',$select); - + $Ok = $this->RenameTable($sTableName,$tmp_name) && $this->CreateTable($sTableName,$aTableDef) && $this->m_odb->query("INSERT INTO $sTableName SELECT DISTINCT $select FROM $tmp_name",__LINE__,__FILE__); @@ -279,6 +289,11 @@ $this->m_odb->transaction_abort(); 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->m_odb->transaction_commit(); diff --git a/phpgwapi/inc/class.schema_proc_pgsql.inc.php b/phpgwapi/inc/class.schema_proc_pgsql.inc.php index f6e8a0c331..28cbedfac0 100644 --- a/phpgwapi/inc/class.schema_proc_pgsql.inc.php +++ b/phpgwapi/inc/class.schema_proc_pgsql.inc.php @@ -699,6 +699,20 @@ 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 "
Updating sequence 'seq_$sTableName' using: $sql"; } + return $oDb->query($sql,__LINE__,__FILE__); + } + return True; + } + function GetSequenceSQL($sTableName, &$sSequenceSQL) { $sSequenceSQL = sprintf("CREATE SEQUENCE seq_%s", $sTableName);