From 04133a88539b42e98c010aed2d507e220b25819e Mon Sep 17 00:00:00 2001 From: Denis Flaven Date: Wed, 22 Jun 2016 12:03:36 +0000 Subject: [PATCH] #1167 Error while upgrading db model from v 2.1 to 2.2 with orphan attachments. SVN:trunk[4237] --- .../itop-attachments/module.attachments.php | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/datamodels/1.x/itop-attachments/module.attachments.php b/datamodels/1.x/itop-attachments/module.attachments.php index a3482585e..1e7269278 100644 --- a/datamodels/1.x/itop-attachments/module.attachments.php +++ b/datamodels/1.x/itop-attachments/module.attachments.php @@ -88,7 +88,25 @@ if (!class_exists('AttachmentInstaller')) */ public static function BeforeDatabaseCreation(Config $oConfiguration, $sPreviousVersion, $sCurrentVersion) { - // If you want to migrate data from one format to another, do it here + if ($sPreviousVersion != '') + { + // Migrating from a previous version + // Check for records where item_id = '', since they are not attached to any object and cannot be migrated to the objkey schema + $sTableName = MetaModel::DBGetTable('Attachment'); + $sCountQuery = "SELECT COUNT(*) FROM `$sTableName` WHERE (`item_id`='' OR `item_id` IS NULL)"; + $iCount = CMDBSource::QueryToScalar($sCountQuery); + if ($iCount > 0) + { + SetupPage::log_info("Cleanup of orphan attachments that cannot be migrated to the new ObjKey model: $iCount record(s) must be deleted."); + $sRepairQuery = "DELETE FROM `$sTableName` WHERE (`item_id`='' OR `item_id` IS NULL)"; + $iRet = CMDBSource::Query($sRepairQuery); // Throws an exception in case of error + SetupPage::log_info("Cleanup of orphan attachments successfully completed."); + } + else + { + SetupPage::log_info("No orphan attachment found."); + } + } } /**