From 535488ca2826e03e1ef96019ed2f6ac72964db7c Mon Sep 17 00:00:00 2001 From: Ralf Becker Date: Wed, 5 Aug 2015 08:33:13 +0000 Subject: [PATCH] PostgreSQL: automatic shorten all content requiring it, before schema update, to not stall update --- phpgwapi/inc/class.schema_proc.inc.php | 22 ++++++++++++++++++++++ phpgwapi/setup/tables_update.inc.php | 12 ------------ 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/phpgwapi/inc/class.schema_proc.inc.php b/phpgwapi/inc/class.schema_proc.inc.php index 726a94ec6a..154776b721 100644 --- a/phpgwapi/inc/class.schema_proc.inc.php +++ b/phpgwapi/inc/class.schema_proc.inc.php @@ -441,6 +441,21 @@ class schema_proc function AlterColumn($sTableName, $sColumnName, $aColumnDef, $bCopyData=True) { $table_def = $this->GetTableDefinition($sTableName); + + // PostgreSQL: varchar or ascii column shortened, use substring to avoid error if current content is to long + if($this->sType == 'pgsql' && in_array($table_def['fd'][$sColumnName]['type'], array('varchar', 'ascii')) && + in_array($aColumnDef['type'], array('varchar', 'ascii')) && + $table_def['fd'][$sColumnName]['precision'] > $aColumnDef['precision']) + { + $this->m_odb->update($sTableName, array( + "$sColumnName=SUBSTRING($sColumnName FROM 1 FOR ".(int)$aColumnDef['precision'].')', + ), "LENGTH($sColumnName) > ".(int)$aColumnDef['precision'], __LINE__, __FILE__); + + if (($shortend = $this->m_odb->affected_rows())) + { + error_log(__METHOD__."('$sTableName', '$sColumnName', ".array2string($aColumnDef).") $shortend values shortened"); + } + } $table_def['fd'][$sColumnName] = $aColumnDef; $aSql = $this->dict->AlterColumnSQL($sTableName,$ado_col = $this->_egw2adodb_columndef(array( @@ -667,6 +682,13 @@ class schema_proc { $value = "ENCODE($value,'escape')"; } + // varchar or ascii column shortened, use substring to avoid error if current content is to long + elseif(in_array($old_table_def['fd'][$name]['type'], array('varchar', 'ascii')) && + in_array($data['type'], array('varchar', 'ascii')) && + $old_table_def['fd'][$name]['precision'] > $data['precision']) + { + $value = "SUBSTRING($value FROM 1 FOR ".(int)$data['size'].')'; + } // cast everything which is a different type elseif($old_table_def['fd'][$name]['type'] != $data['type'] && ($type_translated = $this->TranslateType($data['type']))) { diff --git a/phpgwapi/setup/tables_update.inc.php b/phpgwapi/setup/tables_update.inc.php index 5f3a5a78f2..fbe9a87372 100644 --- a/phpgwapi/setup/tables_update.inc.php +++ b/phpgwapi/setup/tables_update.inc.php @@ -163,12 +163,6 @@ function phpgwapi_upgrade14_2_004() 'nullable' => False ));*/ - // shorten all acl_location entries to 16 chars, to not stall update for PostgreSQL - $GLOBALS['egw_setup']->db->update('egw_acl', array( - 'acl_location=SUBSTRING(acl_location FROM 1 FOR 16)', - 'acl_appname=SUBSTRING(acl_appname FROM 1 FOR 16)', - ), 'LENGTH(acl_location) > 16 OR LENGTH(acl_appname) > 16', __LINE__, __FILE__); - $GLOBALS['egw_setup']->oProc->RefreshTable('egw_acl',array( 'fd' => array( 'acl_appname' => array('type' => 'ascii','precision' => '16','nullable' => False), @@ -376,12 +370,6 @@ function phpgwapi_upgrade14_2_012() function phpgwapi_upgrade14_2_013() { - // shorten all history_appname/history_status entries to 16/32 chars, to not stall update for PostgreSQL - $GLOBALS['egw_setup']->db->update('egw_history_log', array( - 'history_appname=SUBSTRING(history_appname FROM 1 FOR 16)', - 'history_status=SUBSTRING(history_status FROM 1 FOR 32)', - ), 'LENGTH(history_appname) > 16 OR LENGTH(history_status) > 32', __LINE__, __FILE__); - $GLOBALS['egw_setup']->oProc->AlterColumn('egw_history_log','history_appname',array( 'type' => 'ascii', 'precision' => '16',