From b4b939e1a9b742e30293decc891d6c110fe9663b Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Tue, 6 Jul 2021 11:54:32 +0200 Subject: [PATCH] Fix add migration of export/import profile when field name changes --- htdocs/install/default.css | 4 ++ htdocs/install/upgrade2.php | 81 +++++++++++++++++++++++++++++++++ htdocs/langs/en_US/install.lang | 1 + 3 files changed, 86 insertions(+) diff --git a/htdocs/install/default.css b/htdocs/install/default.css index fe85649844d..731ac4131b4 100644 --- a/htdocs/install/default.css +++ b/htdocs/install/default.css @@ -80,6 +80,10 @@ div.soustitre { text-align: right; } +.wordbreak { + word-break: break-word; +} + .minwidth100 { min-width: 100px; } .minwidth200 { min-width: 200px; } .minwidth300 { min-width: 300px; } diff --git a/htdocs/install/upgrade2.php b/htdocs/install/upgrade2.php index d0d1ae256b4..d69baad5a76 100644 --- a/htdocs/install/upgrade2.php +++ b/htdocs/install/upgrade2.php @@ -448,8 +448,17 @@ if (!GETPOST('action', 'aZ09') || preg_match('/upgrade/i', GETPOST('action', 'aZ migrate_contacts_socialnetworks(); migrate_thirdparties_socialnetworks(); } + + // Scripts for 14.0 + $afterversionarray = explode('.', '13.0.9'); + $beforeversionarray = explode('.', '14.0.9'); + if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) { + migrate_export_import_profiles('export'); + migrate_export_import_profiles('import'); + } } + // Code executed only if migration is LAST ONE. Must always be done. if (versioncompare($versiontoarray, $versionranarray) >= 0 || versioncompare($versiontoarray, $versionranarray) <= -3) { // Reload modules (this must be always done and only into last targeted version, because code to reload module may need table structure of last version) @@ -4904,3 +4913,75 @@ function migrate_thirdparties_socialnetworks() print ''.$langs->trans('MigrationFieldsSocialNetworks', 'Thirdparties')."
\n"; print ''; } + + +/** + * Migrate export and import profiles to fix field name that was renamed + * + * @param string $mode 'export' or 'import' + * @return void + */ +function migrate_export_import_profiles($mode = 'export') +{ + global $db, $langs; + + $error = 0; + $resultstring = ''; + + $db->begin(); + + print ''; + $sql = 'SELECT rowid, field'; + if ($mode == 'export') { + $sql .= ', filter'; + } + $sql .= ' FROM '.MAIN_DB_PREFIX.$mode.'_model WHERE'; + $sql .= " type LIKE 'propale_%' OR type LIKE 'commande_%' OR type LIKE 'facture_%'"; + //print $sql; + $resql = $db->query($sql); + if ($resql) { + while ($obj = $db->fetch_object($resql)) { + $oldfield = $obj->field; + $newfield = str_replace(array('f.facnumber,', 'f.total,', 'f.tva,'), array('f.ref,', 'f.total_ht,', 'f.total_tva,'), $oldfield); + + if ($mode == 'export') { + $oldfilter = $obj->filter; + $newfilter = str_replace(array('f.facnumber,', 'f.total,', 'f.tva,'), array('f.ref,', 'f.total_ht,', 'f.total_tva,'), $oldfilter); + } else { + $oldfilter = ''; + $newfilter = ''; + } + + if ($oldfield != $newfield || $oldfilter != $newfilter) { + $sqlupd = 'UPDATE '.MAIN_DB_PREFIX.$mode."_model SET field = '".$db->escape($newfield)."'"; + if ($mode == 'export') { + $sql .= ", filter = '".$db->escape($newfilter)."'"; + } + $sqlupd .= ' WHERE rowid='.$obj->rowid; + $resultstring .= ''.$sqlupd."\n"; + $resqlupd = $db->query($sqlupd); + if (!$resqlupd) { + dol_print_error($db); + $error++; + } + } + } + } else { + $error++; + } + if (!$error) { + $db->commit(); + } else { + dol_print_error($db); + $db->rollback(); + } + print ''.$langs->trans('MigrationImportOrExportProfiles', $mode)."
\n"; + print ''; + + if ($resultstring) { + print $resultstring; + } else { + print ''.$langs->trans("NothingToDo")."\n"; + } +} + diff --git a/htdocs/langs/en_US/install.lang b/htdocs/langs/en_US/install.lang index 63947dad154..c1ffee936d7 100644 --- a/htdocs/langs/en_US/install.lang +++ b/htdocs/langs/en_US/install.lang @@ -206,6 +206,7 @@ MigrationUserPhotoPath=Migration of photo paths for users MigrationFieldsSocialNetworks=Migration of users fields social networks (%s) MigrationReloadModule=Reload module %s MigrationResetBlockedLog=Reset module BlockedLog for v7 algorithm +MigrationImportOrExportProfiles=Migration of import or export profiles (%s) ShowNotAvailableOptions=Show unavailable options HideNotAvailableOptions=Hide unavailable options ErrorFoundDuringMigration=Error(s) were reported during the migration process so next step is not available. To ignore errors, you can click here, but the application or some features may not work correctly until the errors are resolved.