case 4212

This commit is contained in:
2022-10-26 19:28:36 +00:00
parent 50c969b4ce
commit 12aa3b16fb
3 changed files with 54 additions and 17 deletions

View File

@@ -500,6 +500,17 @@ namespace AyaNova.Util
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 EraseTableAsync("aunitmeterreading", conn);
@@ -633,25 +644,48 @@ namespace AyaNova.Util
// await EraseTableAsync("XXXXX", conn);
//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();
}
//after cleanup
//final housekeeping
using (var cmd = new Npgsql.NpgsqlCommand())
{
cmd.Connection = conn;
cmd.CommandText = "delete from \"auseroptions\" where UserId <> 1;";
await cmd.ExecuteNonQueryAsync();
cmd.CommandText = "ALTER SEQUENCE auseroptions_id_seq RESTART WITH 2;";
await cmd.ExecuteNonQueryAsync();
cmd.CommandText = "delete from \"auser\" where id <> 1;";
await cmd.ExecuteNonQueryAsync();
cmd.CommandText = "ALTER SEQUENCE auser_id_seq RESTART WITH 2;";
await cmd.ExecuteNonQueryAsync();
//already deleted above no need for this...I think, not sure why it was here
// cmd.CommandText = "delete from \"adashboardview\" where userid <> 1;";
// await cmd.ExecuteNonQueryAsync();
// cmd.CommandText = $"ALTER SEQUENCE adashboardview_id_seq RESTART WITH 2;";
// await cmd.ExecuteNonQueryAsync();
//Removed for case 4221 handled above
// // cmd.CommandText = "delete from \"auseroptions\" where UserId <> 1;";
// // await cmd.ExecuteNonQueryAsync();
// // cmd.CommandText = "ALTER SEQUENCE auseroptions_id_seq RESTART WITH 2;";
// // await cmd.ExecuteNonQueryAsync();
// // cmd.CommandText = "delete from \"auser\" where id <> 1;";
// // await cmd.ExecuteNonQueryAsync();
// // cmd.CommandText = "ALTER SEQUENCE auser_id_seq RESTART WITH 2;";
// // await cmd.ExecuteNonQueryAsync();
cmd.CommandText = "delete from \"apartwarehouse\" where id <> 1;";
await cmd.ExecuteNonQueryAsync();
cmd.CommandText = $"ALTER SEQUENCE apartwarehouse_id_seq RESTART WITH 2;";
@@ -695,7 +729,7 @@ namespace AyaNova.Util
//but then cascade causes things to delete in any referenced table
cmd.CommandText = "TRUNCATE \"" + sTable + "\" RESTART IDENTITY CASCADE;";
//// cmd.CommandText = $"delete from {sTable};";
//// cmd.CommandText = $"delete from {sTable};";
await cmd.ExecuteNonQueryAsync();
// // if (!tableHasNoSequence)
// // {