case 4396

This commit is contained in:
2023-01-24 21:51:09 +00:00
parent e0465fdd06
commit ca25d096ed
2 changed files with 277 additions and 272 deletions

4
.vscode/launch.json vendored
View File

@@ -118,8 +118,8 @@
"AYANOVA_DB_CONNECTION": "Server=localhost;Username=postgres;Password=raven;Database=AyaNova;CommandTimeout=300;", "AYANOVA_DB_CONNECTION": "Server=localhost;Username=postgres;Password=raven;Database=AyaNova;CommandTimeout=300;",
"AYANOVA_DATA_PATH": "c:\\temp\\ravendata", "AYANOVA_DATA_PATH": "c:\\temp\\ravendata",
"AYANOVA_USE_URLS": "http://*:7575;", "AYANOVA_USE_URLS": "http://*:7575;",
//"AYANOVA_PERMANENTLY_ERASE_DATABASE":"true", "AYANOVA_PERMANENTLY_ERASE_DATABASE":"true",
// "AYANOVA_REMOVE_LICENSE_FROM_DB":"true", "AYANOVA_REMOVE_LICENSE_FROM_DB":"true",
"AYANOVA_BACKUP_PG_DUMP_PATH": "C:\\data\\code\\postgres_14\\bin" "AYANOVA_BACKUP_PG_DUMP_PATH": "C:\\data\\code\\postgres_14\\bin"
}, },
"sourceFileMap": { "sourceFileMap": {

View File

@@ -405,298 +405,303 @@ namespace AyaNova.Util
//- select the superuser account back to the auser and useroptions //- select the superuser account back to the auser and useroptions
//- discover where else this needs to be done //- discover where else this needs to be done
//- $profit //- $profit
_log.LogInformation("Erasing Database \"{0}\"", _dbName); _log.LogInformation("Erasing Database \"{0}\"", _dbName);
AyaNova.Api.ControllerHelpers.ApiServerState apiServerState = (AyaNova.Api.ControllerHelpers.ApiServerState)ServiceProviderProvider.Provider.GetService(typeof(AyaNova.Api.ControllerHelpers.ApiServerState)); AyaNova.Api.ControllerHelpers.ApiServerState apiServerState = (AyaNova.Api.ControllerHelpers.ApiServerState)ServiceProviderProvider.Provider.GetService(typeof(AyaNova.Api.ControllerHelpers.ApiServerState));
apiServerState.SetClosed("Erasing database"); apiServerState.SetClosed("Erasing database");
//clear all connections so that the database can be dropped try
Npgsql.NpgsqlConnection.ClearAllPools();
using (var conn = new Npgsql.NpgsqlConnection(_dbConnectionString))
{ {
await conn.OpenAsync();
//prepare to delete by removing foreign keys //clear all connections so that the database can be dropped
using (var cmd = new Npgsql.NpgsqlCommand()) Npgsql.NpgsqlConnection.ClearAllPools();
using (var conn = new Npgsql.NpgsqlConnection(_dbConnectionString))
{ {
cmd.Connection = conn; await conn.OpenAsync();
cmd.CommandText = "update auser set customerid=null;";
await cmd.ExecuteNonQueryAsync();
cmd.Connection = conn; //prepare to delete by removing foreign keys
cmd.CommandText = "update aunit set replacedbyunitid=null, parentunitid=null, purchasedfromvendorid=null, unitmodelid=null;"; using (var cmd = new Npgsql.NpgsqlCommand())
await cmd.ExecuteNonQueryAsync();
cmd.CommandText = "update auser set headofficeid=null;";
await cmd.ExecuteNonQueryAsync();
cmd.CommandText = "update auser set vendorid=null;";
await cmd.ExecuteNonQueryAsync();
cmd.CommandText = "update aloanunit set unitid=null, workorderitemloanid=null;";
await cmd.ExecuteNonQueryAsync();
cmd.CommandText = "update aworkorder set fromquoteid=null;";
await cmd.ExecuteNonQueryAsync();
cmd.CommandText = "update aworkorder set frompmid=null;";
await cmd.ExecuteNonQueryAsync();
cmd.CommandText = "update apurchaseorderitem set workorderitempartrequestid=null;";
await cmd.ExecuteNonQueryAsync();
cmd.CommandText = "update aworkorderitem set fromcsrid=null;";
await cmd.ExecuteNonQueryAsync();
// if (jobIdToKeep != Guid.Empty)
// {
// //delete from aopsjoblog where jobid <> '8acec231-aab4-4fdc-b01e-8908378a0e7f';
// //delete from aopsjob where gid <> '8acec231-aab4-4fdc-b01e-8908378a0e7f';
// cmd.CommandText = $"delete from aopsjoblog where jobid <> '{jobIdToKeep}'";
// await cmd.ExecuteNonQueryAsync();
// cmd.CommandText = $"delete from aopsjob where gid <> '{jobIdToKeep}'";
// await cmd.ExecuteNonQueryAsync();
// }
if (!keepTaxCodes)
{ {
cmd.CommandText = "update aglobalbizsettings set taxpartpurchaseid=null,taxpartsaleid=null,taxratesaleid=null;"; cmd.Connection = conn;
cmd.CommandText = "update auser set customerid=null;";
await cmd.ExecuteNonQueryAsync();
cmd.Connection = conn;
cmd.CommandText = "update aunit set replacedbyunitid=null, parentunitid=null, purchasedfromvendorid=null, unitmodelid=null;";
await cmd.ExecuteNonQueryAsync();
cmd.CommandText = "update auser set headofficeid=null;";
await cmd.ExecuteNonQueryAsync();
cmd.CommandText = "update auser set vendorid=null;";
await cmd.ExecuteNonQueryAsync();
cmd.CommandText = "update aloanunit set unitid=null, workorderitemloanid=null;";
await cmd.ExecuteNonQueryAsync();
cmd.CommandText = "update aworkorder set fromquoteid=null;";
await cmd.ExecuteNonQueryAsync();
cmd.CommandText = "update aworkorder set frompmid=null;";
await cmd.ExecuteNonQueryAsync();
cmd.CommandText = "update apurchaseorderitem set workorderitempartrequestid=null;";
await cmd.ExecuteNonQueryAsync();
cmd.CommandText = "update aworkorderitem set fromcsrid=null;";
await cmd.ExecuteNonQueryAsync();
// if (jobIdToKeep != Guid.Empty)
// {
// //delete from aopsjoblog where jobid <> '8acec231-aab4-4fdc-b01e-8908378a0e7f';
// //delete from aopsjob where gid <> '8acec231-aab4-4fdc-b01e-8908378a0e7f';
// cmd.CommandText = $"delete from aopsjoblog where jobid <> '{jobIdToKeep}'";
// await cmd.ExecuteNonQueryAsync();
// cmd.CommandText = $"delete from aopsjob where gid <> '{jobIdToKeep}'";
// await cmd.ExecuteNonQueryAsync();
// }
if (!keepTaxCodes)
{
cmd.CommandText = "update aglobalbizsettings set taxpartpurchaseid=null,taxpartsaleid=null,taxratesaleid=null;";
await cmd.ExecuteNonQueryAsync();
}
//DELIBERATELY NOT ERASED:
//NOTE: these tables commented below are deliberately not affected as it's assumed for a **COMPLETE** erase users will erase manually or use the config setting to completely drop the database
//For anything less than that it's assumed users are testing things out and want to keep these tables but erase the biz data
//aglobalopsbackupsettings
//aglobalopsnotificationsettings
//aglobalbizsettings
//alogo
//areport
//TAX CODES are an edge case where they are generated by the seeder potentially so need to be erased in that case or migrate but might be kept when manually erasing from UI.
//await EraseTableAsync("aopsjoblog", conn, true);
// await EraseTableAsync("aopsjob", conn, true);
}
//Delete non stock translations
using (var cmd = new Npgsql.NpgsqlCommand())
{
cmd.Connection = conn;
//set to default translation so can delete all non default ones
cmd.CommandText = "update auseroptions set translationid=1;";
await cmd.ExecuteNonQueryAsync();
//case 4396
cmd.CommandText = "update acustomernotifysubscription set translationid=1;";
await cmd.ExecuteNonQueryAsync();
cmd.CommandText = "delete from atranslationitem where translationid > 4;";
await cmd.ExecuteNonQueryAsync();
cmd.CommandText = "delete from atranslation where id > 4;";
await cmd.ExecuteNonQueryAsync(); await cmd.ExecuteNonQueryAsync();
} }
//DELIBERATELY NOT ERASED:
//NOTE: these tables commented below are deliberately not affected as it's assumed for a **COMPLETE** erase users will erase manually or use the config setting to completely drop the database
//For anything less than that it's assumed users are testing things out and want to keep these tables but erase the biz data
//aglobalopsbackupsettings
//aglobalopsnotificationsettings
//aglobalbizsettings
//alogo
//areport
//TAX CODES are an edge case where they are generated by the seeder potentially so need to be erased in that case or migrate but might be kept when manually erasing from UI. //case 4221 truncate support
//BACKUP USER AND DATA TO BE PRESERVED THAT TRUNCATE WILL CASCADE DELETE
using (var cmd = new Npgsql.NpgsqlCommand())
{
cmd.Connection = conn;
cmd.CommandText = "CREATE TABLE auser_backup AS TABLE auser;";
await cmd.ExecuteNonQueryAsync();
cmd.CommandText = "CREATE TABLE auseroptions_backup AS TABLE auseroptions;";
await cmd.ExecuteNonQueryAsync();
}
//REMOVE ALL REMAINING DATA
await TruncateTableAsync("aunitmeterreading", conn);
await TruncateTableAsync("acustomerservicerequest", conn);
//--- WorkOrder
await TruncateTableAsync("aworkorderitemexpense", conn);
await TruncateTableAsync("aworkorderitemlabor", conn);
await TruncateTableAsync("aworkorderitemloan", conn);
await TruncateTableAsync("aworkorderitempart", conn);
await TruncateTableAsync("aworkorderitempartrequest", conn);
await TruncateTableAsync("aworkorderitemscheduleduser", conn);
await TruncateTableAsync("aworkorderitemtask", conn);
await TruncateTableAsync("aworkorderitemtravel", conn);
await TruncateTableAsync("aworkorderitemunit", conn);
await TruncateTableAsync("aworkorderitemoutsideservice", conn);
await TruncateTableAsync("aworkorderitem", conn);
await TruncateTableAsync("aworkorderstate", conn);
await TruncateTableAsync("aworkorder", conn);
//---
//--- QUOTE
await TruncateTableAsync("aquoteitemexpense", conn);
await TruncateTableAsync("aquoteitemlabor", conn);
await TruncateTableAsync("aquoteitemloan", conn);
await TruncateTableAsync("aquoteitempart", conn);
await TruncateTableAsync("aquoteitemscheduleduser", conn);
await TruncateTableAsync("aquoteitemtask", conn);
await TruncateTableAsync("aquoteitemtravel", conn);
await TruncateTableAsync("aquoteitemunit", conn);
await TruncateTableAsync("aquoteitemoutsideservice", conn);
await TruncateTableAsync("aquoteitem", conn);
await TruncateTableAsync("aquotestate", conn);
await TruncateTableAsync("aquote", conn);
//---
//await EraseTableAsync("aopsjoblog", conn, true); //--- PM
// await EraseTableAsync("aopsjob", conn, true); await TruncateTableAsync("apmitemexpense", conn);
await TruncateTableAsync("apmitemlabor", conn);
await TruncateTableAsync("apmitemloan", conn);
await TruncateTableAsync("apmitempart", conn);
await TruncateTableAsync("apmitemscheduleduser", conn);
await TruncateTableAsync("apmitemtask", conn);
await TruncateTableAsync("apmitemtravel", conn);
await TruncateTableAsync("apmitemunit", conn);
await TruncateTableAsync("apmitemoutsideservice", conn);
await TruncateTableAsync("apmitem", conn);
await TruncateTableAsync("apm", conn);//bugbug
//---
await TruncateTableAsync("afileattachment", conn);
await TruncateTableAsync("aevent", conn);
await TruncateTableAsync("adatalistsavedfilter", conn);
await TruncateTableAsync("adatalistcolumnview", conn);
await TruncateTableAsync("apicklisttemplate", conn, true);
await TruncateTableAsync("aformcustom", conn);
await TruncateTableAsync("asearchkey", conn);
await TruncateTableAsync("asearchdictionary", conn);
await TruncateTableAsync("atag", conn);
await TruncateTableAsync("apurchaseorderitem", conn);
await TruncateTableAsync("apurchaseorder", conn);
await TruncateTableAsync("apartassemblyitem", conn);
await TruncateTableAsync("apartassembly", conn);
await TruncateTableAsync("apartinventory", conn);
await TruncateTableAsync("apart", conn);
await TruncateTableAsync("aloanunit", conn);
await TruncateTableAsync("aunitmodel", conn);
await TruncateTableAsync("avendor", conn);
await TruncateTableAsync("aunit", conn);
await TruncateTableAsync("aproject", conn);//depends on User, dependants are wo,quote,pm
await TruncateTableAsync("acustomernote", conn);
await TruncateTableAsync("acustomer", conn);
await TruncateTableAsync("aheadoffice", conn);
await TruncateTableAsync("acontract", conn);
//----- NOTIFICATION
await TruncateTableAsync("ainappnotification", conn);
await TruncateTableAsync("anotifyevent", conn);
await TruncateTableAsync("anotifydeliverylog", conn);
await TruncateTableAsync("anotifysubscription", conn);
await TruncateTableAsync("acustomernotifyevent", conn);
await TruncateTableAsync("acustomernotifydeliverylog", conn);
await TruncateTableAsync("acustomernotifysubscription", conn);
await TruncateTableAsync("amemo", conn);
await TruncateTableAsync("areminder", conn);//depends on User
await TruncateTableAsync("areview", conn);//depends on User
await TruncateTableAsync("aservicerate", conn);
await TruncateTableAsync("atravelrate", conn);
if (!keepTaxCodes)
await EraseTableAsync("ataxcode", conn);
await TruncateTableAsync("aquotestatus", conn);
await TruncateTableAsync("aworkorderstatus", conn);
await TruncateTableAsync("aworkorderitemstatus", conn);
await TruncateTableAsync("aworkorderitempriority", conn);
await TruncateTableAsync("ataskgroup", conn);//items cascade
await TruncateTableAsync("ametricmm", conn, true);
await TruncateTableAsync("ametricdd", conn, true);
await TruncateTableAsync("adashboardview", conn);
await TruncateTableAsync("aintegration", conn);
//############# WARNING: there can be unintended consequences easily if new tables or fields are added that REFERENCE other tables triggering a cascade delete unexpectedly
//be sure about that before making changes and test thoroughly anything that calls this method:
// the seeding and manual erase and v8-migrate code when making such changes
//case 4221 truncate support
//COPY BACK USER AND DATA TO BE PRESERVED THAT TRUNCATE WILL CASCADE DELETE
using (var cmd = new Npgsql.NpgsqlCommand())
{
//AT this point the truncate commands in erasetable above have caused all user and useroptions to be deleted
//so no need to clean out those tables, instead put our backup superuser back in again
cmd.Connection = conn;
cmd.CommandText = "INSERT INTO auser (active, name, lastlogin, login, password, salt , roles, currentauthtoken, "
+ "dlkey, dlkeyexpire, totpsecret, temptoken, twofactorenabled, passwordresetcode, passwordresetcodeexpire, usertype, "
+ "employeenumber, notes, wiki, customfields, tags, allowlogin) "
+ "SELECT active, name, lastlogin, login, password, salt , roles, currentauthtoken, "
+ "dlkey, dlkeyexpire, totpsecret, temptoken, twofactorenabled, passwordresetcode, passwordresetcodeexpire, usertype, "
+ "employeenumber, notes, wiki, customfields, tags, allowlogin "
+ "FROM auser_backup where id = 1;";
await cmd.ExecuteNonQueryAsync();
cmd.CommandText = "INSERT INTO auseroptions (userid, translationid, languageoverride, timezoneoverride, "
+ "currencyname, hour12, emailaddress, phone1, phone2, phone3, mapurltemplate, uicolor) "
+ "SELECT userid, translationid, languageoverride, timezoneoverride, "
+ "currencyname, hour12, emailaddress, phone1, phone2, phone3, mapurltemplate, uicolor "
+ "FROM auseroptions_backup where userid = 1;";
await cmd.ExecuteNonQueryAsync();
cmd.CommandText = "DROP TABLE IF EXISTS AUSEROPTIONS_BACKUP, AUSER_BACKUP;";
await cmd.ExecuteNonQueryAsync();
}
//final housekeeping
using (var cmd = new Npgsql.NpgsqlCommand())
{
cmd.Connection = conn;
cmd.CommandText = "delete from \"apartwarehouse\" where id <> 1;";
await cmd.ExecuteNonQueryAsync();
cmd.CommandText = $"ALTER SEQUENCE apartwarehouse_id_seq RESTART WITH 2;";
await cmd.ExecuteNonQueryAsync();
cmd.CommandText = "ALTER SEQUENCE apurchaseorder_serial_seq RESTART WITH 1;";
await cmd.ExecuteNonQueryAsync();
cmd.CommandText = "ALTER SEQUENCE aworkorder_serial_seq RESTART WITH 1;";
await cmd.ExecuteNonQueryAsync();
cmd.CommandText = "ALTER SEQUENCE aquote_serial_seq RESTART WITH 1;";
await cmd.ExecuteNonQueryAsync();
cmd.CommandText = "ALTER SEQUENCE apm_serial_seq RESTART WITH 1;";
await cmd.ExecuteNonQueryAsync();
}
await conn.CloseAsync();
} }
//If we got here then it's safe to erase the attachment files
FileUtil.EraseEntireContentsOfAttachmentFilesFolder();
//Delete non stock translations _log.LogInformation("Importing any missing stock Report templates");
using (var cmd = new Npgsql.NpgsqlCommand()) await AyaNova.Biz.PrimeData.PrimeReportTemplates();
{ apiServerState.ResumePriorState();
cmd.Connection = conn; _log.LogInformation("Database erase completed");
//set to default translation so can delete all non default ones
cmd.CommandText = "update auseroptions set translationid=1;";
await cmd.ExecuteNonQueryAsync();
//case 4396
cmd.CommandText = "update acustomernotifysubscription set translationid=1;";
await cmd.ExecuteNonQueryAsync();
cmd.CommandText = "delete from atranslationitem where translationid > 4;";
await cmd.ExecuteNonQueryAsync();
cmd.CommandText = "delete from atranslation where id > 4;";
await cmd.ExecuteNonQueryAsync();
}
//case 4221 truncate support
//BACKUP USER AND DATA TO BE PRESERVED THAT TRUNCATE WILL CASCADE DELETE
using (var cmd = new Npgsql.NpgsqlCommand())
{
cmd.Connection = conn;
cmd.CommandText = "CREATE TABLE auser_backup AS TABLE auser;";
await cmd.ExecuteNonQueryAsync();
cmd.CommandText = "CREATE TABLE auseroptions_backup AS TABLE auseroptions;";
await cmd.ExecuteNonQueryAsync();
}
//REMOVE ALL REMAINING DATA
await TruncateTableAsync("aunitmeterreading", conn);
await TruncateTableAsync("acustomerservicerequest", conn);
//--- WorkOrder
await TruncateTableAsync("aworkorderitemexpense", conn);
await TruncateTableAsync("aworkorderitemlabor", conn);
await TruncateTableAsync("aworkorderitemloan", conn);
await TruncateTableAsync("aworkorderitempart", conn);
await TruncateTableAsync("aworkorderitempartrequest", conn);
await TruncateTableAsync("aworkorderitemscheduleduser", conn);
await TruncateTableAsync("aworkorderitemtask", conn);
await TruncateTableAsync("aworkorderitemtravel", conn);
await TruncateTableAsync("aworkorderitemunit", conn);
await TruncateTableAsync("aworkorderitemoutsideservice", conn);
await TruncateTableAsync("aworkorderitem", conn);
await TruncateTableAsync("aworkorderstate", conn);
await TruncateTableAsync("aworkorder", conn);
//---
//--- QUOTE
await TruncateTableAsync("aquoteitemexpense", conn);
await TruncateTableAsync("aquoteitemlabor", conn);
await TruncateTableAsync("aquoteitemloan", conn);
await TruncateTableAsync("aquoteitempart", conn);
await TruncateTableAsync("aquoteitemscheduleduser", conn);
await TruncateTableAsync("aquoteitemtask", conn);
await TruncateTableAsync("aquoteitemtravel", conn);
await TruncateTableAsync("aquoteitemunit", conn);
await TruncateTableAsync("aquoteitemoutsideservice", conn);
await TruncateTableAsync("aquoteitem", conn);
await TruncateTableAsync("aquotestate", conn);
await TruncateTableAsync("aquote", conn);
//---
//--- PM
await TruncateTableAsync("apmitemexpense", conn);
await TruncateTableAsync("apmitemlabor", conn);
await TruncateTableAsync("apmitemloan", conn);
await TruncateTableAsync("apmitempart", conn);
await TruncateTableAsync("apmitemscheduleduser", conn);
await TruncateTableAsync("apmitemtask", conn);
await TruncateTableAsync("apmitemtravel", conn);
await TruncateTableAsync("apmitemunit", conn);
await TruncateTableAsync("apmitemoutsideservice", conn);
await TruncateTableAsync("apmitem", conn);
await TruncateTableAsync("apm", conn);//bugbug
//---
await TruncateTableAsync("afileattachment", conn);
await TruncateTableAsync("aevent", conn);
await TruncateTableAsync("adatalistsavedfilter", conn);
await TruncateTableAsync("adatalistcolumnview", conn);
await TruncateTableAsync("apicklisttemplate", conn, true);
await TruncateTableAsync("aformcustom", conn);
await TruncateTableAsync("asearchkey", conn);
await TruncateTableAsync("asearchdictionary", conn);
await TruncateTableAsync("atag", conn);
await TruncateTableAsync("apurchaseorderitem", conn);
await TruncateTableAsync("apurchaseorder", conn);
await TruncateTableAsync("apartassemblyitem", conn);
await TruncateTableAsync("apartassembly", conn);
await TruncateTableAsync("apartinventory", conn);
await TruncateTableAsync("apart", conn);
await TruncateTableAsync("aloanunit", conn);
await TruncateTableAsync("aunitmodel", conn);
await TruncateTableAsync("avendor", conn);
await TruncateTableAsync("aunit", conn);
await TruncateTableAsync("aproject", conn);//depends on User, dependants are wo,quote,pm
await TruncateTableAsync("acustomernote", conn);
await TruncateTableAsync("acustomer", conn);
await TruncateTableAsync("aheadoffice", conn);
await TruncateTableAsync("acontract", conn);
//----- NOTIFICATION
await TruncateTableAsync("ainappnotification", conn);
await TruncateTableAsync("anotifyevent", conn);
await TruncateTableAsync("anotifydeliverylog", conn);
await TruncateTableAsync("anotifysubscription", conn);
await TruncateTableAsync("acustomernotifyevent", conn);
await TruncateTableAsync("acustomernotifydeliverylog", conn);
await TruncateTableAsync("acustomernotifysubscription", conn);
await TruncateTableAsync("amemo", conn);
await TruncateTableAsync("areminder", conn);//depends on User
await TruncateTableAsync("areview", conn);//depends on User
await TruncateTableAsync("aservicerate", conn);
await TruncateTableAsync("atravelrate", conn);
if (!keepTaxCodes)
await EraseTableAsync("ataxcode", conn);
await TruncateTableAsync("aquotestatus", conn);
await TruncateTableAsync("aworkorderstatus", conn);
await TruncateTableAsync("aworkorderitemstatus", conn);
await TruncateTableAsync("aworkorderitempriority", conn);
await TruncateTableAsync("ataskgroup", conn);//items cascade
await TruncateTableAsync("ametricmm", conn, true);
await TruncateTableAsync("ametricdd", conn, true);
await TruncateTableAsync("adashboardview", conn);
await TruncateTableAsync("aintegration", conn);
//############# WARNING: there can be unintended consequences easily if new tables or fields are added that REFERENCE other tables triggering a cascade delete unexpectedly
//be sure about that before making changes and test thoroughly anything that calls this method:
// the seeding and manual erase and v8-migrate code when making such changes
//case 4221 truncate support
//COPY BACK USER AND DATA TO BE PRESERVED THAT TRUNCATE WILL CASCADE DELETE
using (var cmd = new Npgsql.NpgsqlCommand())
{
//AT this point the truncate commands in erasetable above have caused all user and useroptions to be deleted
//so no need to clean out those tables, instead put our backup superuser back in again
cmd.Connection = conn;
cmd.CommandText = "INSERT INTO auser (active, name, lastlogin, login, password, salt , roles, currentauthtoken, "
+ "dlkey, dlkeyexpire, totpsecret, temptoken, twofactorenabled, passwordresetcode, passwordresetcodeexpire, usertype, "
+ "employeenumber, notes, wiki, customfields, tags, allowlogin) "
+ "SELECT active, name, lastlogin, login, password, salt , roles, currentauthtoken, "
+ "dlkey, dlkeyexpire, totpsecret, temptoken, twofactorenabled, passwordresetcode, passwordresetcodeexpire, usertype, "
+ "employeenumber, notes, wiki, customfields, tags, allowlogin "
+ "FROM auser_backup where id = 1;";
await cmd.ExecuteNonQueryAsync();
cmd.CommandText = "INSERT INTO auseroptions (userid, translationid, languageoverride, timezoneoverride, "
+ "currencyname, hour12, emailaddress, phone1, phone2, phone3, mapurltemplate, uicolor) "
+ "SELECT userid, translationid, languageoverride, timezoneoverride, "
+ "currencyname, hour12, emailaddress, phone1, phone2, phone3, mapurltemplate, uicolor "
+ "FROM auseroptions_backup where userid = 1;";
await cmd.ExecuteNonQueryAsync();
cmd.CommandText = "DROP TABLE IF EXISTS AUSEROPTIONS_BACKUP, AUSER_BACKUP;";
await cmd.ExecuteNonQueryAsync();
}
//final housekeeping
using (var cmd = new Npgsql.NpgsqlCommand())
{
cmd.Connection = conn;
cmd.CommandText = "delete from \"apartwarehouse\" where id <> 1;";
await cmd.ExecuteNonQueryAsync();
cmd.CommandText = $"ALTER SEQUENCE apartwarehouse_id_seq RESTART WITH 2;";
await cmd.ExecuteNonQueryAsync();
cmd.CommandText = "ALTER SEQUENCE apurchaseorder_serial_seq RESTART WITH 1;";
await cmd.ExecuteNonQueryAsync();
cmd.CommandText = "ALTER SEQUENCE aworkorder_serial_seq RESTART WITH 1;";
await cmd.ExecuteNonQueryAsync();
cmd.CommandText = "ALTER SEQUENCE aquote_serial_seq RESTART WITH 1;";
await cmd.ExecuteNonQueryAsync();
cmd.CommandText = "ALTER SEQUENCE apm_serial_seq RESTART WITH 1;";
await cmd.ExecuteNonQueryAsync();
}
await conn.CloseAsync();
} }
catch (Exception ex)
//If we got here then it's safe to erase the attachment files {
FileUtil.EraseEntireContentsOfAttachmentFilesFolder(); //NOTE: deliberately leaving server in CLOSED state so users see that there is a problem
//and investigate
_log.LogInformation("Importing any missing stock Report templates"); _log.LogCritical(ex, "Unable to erase database due to unexpected condition. Please contact technical support to resolve.");
await AyaNova.Biz.PrimeData.PrimeReportTemplates(); }
apiServerState.ResumePriorState();
_log.LogInformation("Database erase completed");
} }