From a4321774056fe1044e057e9d9f1d1ed0aedaafb7 Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Sat, 2 May 2020 20:10:25 +0000 Subject: [PATCH] Properly erase db without fucking things up --- server/AyaNova/util/DbUtil.cs | 43 +++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/server/AyaNova/util/DbUtil.cs b/server/AyaNova/util/DbUtil.cs index 3059f66e..86443aa8 100644 --- a/server/AyaNova/util/DbUtil.cs +++ b/server/AyaNova/util/DbUtil.cs @@ -289,28 +289,33 @@ namespace AyaNova.Util await cmd.ExecuteNonQueryAsync(); } + //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(); - //REMOVE ALL DATA with few exceptions of manager user, license, schema tables - //and job logs because this is called by job code - - await EraseTableAsync("atranslationitem", conn); - await EraseTableAsync("atranslation", conn); - //Load the default TRANSLATIONS - await AyaNova.Biz.PrimeData.PrimeTranslations(); + cmd.CommandText = "delete from atranslationitem where translationid > 4;"; + await cmd.ExecuteNonQueryAsync(); + cmd.CommandText = "delete from atranslation where id > 4;"; + await cmd.ExecuteNonQueryAsync(); + } + + //REMOVE ALL REMAINING DATA await EraseTableAsync("afileattachment", conn); await EraseTableAsync("awidget", conn); await EraseTableAsync("aevent", conn); await EraseTableAsync("adatalistview", conn); - await EraseTableAsync("apicklisttemplate", conn); + await EraseTableAsync("apicklisttemplate", conn, true); await EraseTableAsync("aformcustom", conn); await EraseTableAsync("asearchkey", conn); await EraseTableAsync("asearchdictionary", conn); - await EraseTableAsync("atag", conn); - //CUSTOMER + await EraseTableAsync("atag", conn); await EraseTableAsync("acustomer", conn); - await EraseTableAsync("acontract", conn); await EraseTableAsync("aheadoffice", conn); await EraseTableAsync("aloanunit", conn); @@ -332,8 +337,6 @@ namespace AyaNova.Util await EraseTableAsync("aworkorderitem", conn); await EraseTableAsync("aworkordertemplate", conn); await EraseTableAsync("aworkordertemplateitem", conn); - - await conn.CloseAsync(); } @@ -348,13 +351,23 @@ namespace AyaNova.Util /////////////////////////////////////////// // Erase all data from the table specified // - private static async Task EraseTableAsync(string sTable, Npgsql.NpgsqlConnection conn) + private static async Task EraseTableAsync(string sTable, Npgsql.NpgsqlConnection conn, bool tableHasNoSequence = false) { using (var cmd = new Npgsql.NpgsqlCommand()) { cmd.Connection = conn; - cmd.CommandText = "TRUNCATE \"" + sTable + "\" RESTART IDENTITY;"; + //Boo! Can't do this becuase it will fail if there is a foreign key which nearly all tables have unless cascade option is used + //but then cascade causes things to delete in any referenced table + // cmd.CommandText = "TRUNCATE \"" + sTable + "\" RESTART IDENTITY;"; + + cmd.CommandText = $"delete from {sTable};"; await cmd.ExecuteNonQueryAsync(); + if (!tableHasNoSequence) + { + cmd.CommandText = $"ALTER SEQUENCE {sTable}_id_seq RESTART WITH 1;"; + await cmd.ExecuteNonQueryAsync(); + + } } }