Fix add migration of export/import profile when field name changes

This commit is contained in:
Laurent Destailleur 2021-07-06 11:54:32 +02:00
parent 7dab4ef571
commit b4b939e1a9
3 changed files with 86 additions and 0 deletions

View File

@ -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; }

View File

@ -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 '<b>'.$langs->trans('MigrationFieldsSocialNetworks', 'Thirdparties')."</b><br>\n";
print '</td></tr>';
}
/**
* 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 '<tr class="trforrunsql"><td colspan="4">';
$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 .= '<tr class="trforrunsql" style=""><td class="wordbreak" colspan="4">'.$sqlupd."</td></tr>\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 '<b>'.$langs->trans('MigrationImportOrExportProfiles', $mode)."</b><br>\n";
print '</td></tr>';
if ($resultstring) {
print $resultstring;
} else {
print '<tr class="trforrunsql" style=""><td class="wordbreak" colspan="4">'.$langs->trans("NothingToDo")."</td></tr>\n";
}
}

View File

@ -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 <a href="%s">click here</a>, but the application or some features may not work correctly until the errors are resolved.