From f3896ce790eb02dfc41a4e7c653071471b65ceca Mon Sep 17 00:00:00 2001 From: reidst Date: Sun, 10 Dec 2023 23:12:50 -0600 Subject: [PATCH] DbInitializer now overwrites old plant data --- Data/DbInitializer.cs | 171 ++++++++++++++++++++++++------------------ 1 file changed, 99 insertions(+), 72 deletions(-) diff --git a/Data/DbInitializer.cs b/Data/DbInitializer.cs index 9a646ef..0143785 100644 --- a/Data/DbInitializer.cs +++ b/Data/DbInitializer.cs @@ -26,6 +26,8 @@ public static async Task InitializeAsync(WebApplication app) ISeeGreenContext context = scope.ServiceProvider.GetRequiredService(); Console.WriteLine("[DebugLog][DbInitializer] Context retrieved."); + SqliteCommand command; + string seedDbPath = $"{Directory.GetCurrentDirectory()}/wwwroot/seed.db"; Console.WriteLine($"[DebugLog][DbInitializer] seedDbPath='{seedDbPath}'"); using SqliteConnection connection = new($"Data Source={seedDbPath}"); @@ -33,14 +35,18 @@ public static async Task InitializeAsync(WebApplication app) await connection.OpenAsync(); Console.WriteLine("[DebugLog][DbInitializer] Connection opened."); - if (!context.Categories.Any()) + if (context.Categories.Any()) + { + Console.WriteLine("[DebugLog][DbInitializer] Table 'Categories' is not empty; clearing..."); + context.Categories.RemoveRange(context.Categories.AsEnumerable()); + Console.WriteLine("[DebugLog][DbInitializer] Table 'Categories' has been emptied."); + } + command = connection.CreateCommand(); + command.CommandText = "SELECT * FROM Categories;"; + List categories = new(); + Console.WriteLine("[DebugLog][DbInitializer] Executing SELECT command..."); + using (SqliteDataReader reader = await command.ExecuteReaderAsync()) { - Console.WriteLine("[DebugLog][DbInitializer] Table 'Categories' is empty, adding seed data..."); - SqliteCommand command = connection.CreateCommand(); - command.CommandText = "SELECT * FROM Categories;"; - List categories = new(); - Console.WriteLine("[DebugLog][DbInitializer] Executing SELECT command..."); - using SqliteDataReader reader = await command.ExecuteReaderAsync(); Console.WriteLine("[DebugLog][DbInitializer] Reading data..."); while (await reader.ReadAsync()) { @@ -52,21 +58,25 @@ public static async Task InitializeAsync(WebApplication app) APG4sort = DbCast(reader["APG4sort"]), }); } - Console.WriteLine($"[DebugLog][DbInitializer] Adding {categories.Count} new Categories to the context..."); - await context.Categories.AddRangeAsync(categories.ToArray()); - Console.WriteLine("[DebugLog][DbInitializer] Saving changes..."); - await context.SaveChangesAsync(); - Console.WriteLine("[DebugLog][DbInitializer] Changes saved."); } + Console.WriteLine($"[DebugLog][DbInitializer] Adding {categories.Count} new Categories to the context..."); + await context.Categories.AddRangeAsync(categories); + Console.WriteLine("[DebugLog][DbInitializer] Saving changes..."); + await context.SaveChangesAsync(); + Console.WriteLine("[DebugLog][DbInitializer] Changes saved."); - if (!context.TaxonomicOrders.Any()) + if (context.TaxonomicOrders.Any()) + { + Console.WriteLine("[DebugLog][DbInitializer] Table 'TaxonomicOrders' is not yet empty; clearing..."); + context.TaxonomicOrders.RemoveRange(context.TaxonomicOrders.AsEnumerable()); + Console.WriteLine("[DebugLog][DbInitializer] Table 'TaxonomicOrders' has been emptied."); + } + command = connection.CreateCommand(); + command.CommandText = "SELECT * FROM TaxonomicOrders;"; + List taxonomicOrders = new(); + Console.WriteLine("[DebugLog][DbInitializer] Executing SELECT command..."); + using (SqliteDataReader reader = await command.ExecuteReaderAsync()) { - Console.WriteLine("[DebugLog][DbInitializer] Table 'TaxonomicOrders' is empty, adding seed data..."); - SqliteCommand command = connection.CreateCommand(); - command.CommandText = "SELECT * FROM TaxonomicOrders;"; - List taxonomicOrders = new(); - Console.WriteLine("[DebugLog][DbInitializer] Executing SELECT command..."); - using SqliteDataReader reader = await command.ExecuteReaderAsync(); Console.WriteLine("[DebugLog][DbInitializer] Reading data..."); while (await reader.ReadAsync()) { @@ -87,21 +97,25 @@ public static async Task InitializeAsync(WebApplication app) SortLevel6 = (int)DbCast(reader["SortLevel6"]), }); } - Console.WriteLine($"[DebugLog][DbInitializer] Adding {taxonomicOrders.Count} new TaxonomicOrders to the context..."); - await context.TaxonomicOrders.AddRangeAsync(taxonomicOrders.ToArray()); - Console.WriteLine("[DebugLog][DbInitializer] Saving changes..."); - await context.SaveChangesAsync(); - Console.WriteLine("[DebugLog][DbInitializer] Changes saved."); } + Console.WriteLine($"[DebugLog][DbInitializer] Adding {taxonomicOrders.Count} new TaxonomicOrders to the context..."); + await context.TaxonomicOrders.AddRangeAsync(taxonomicOrders); + Console.WriteLine("[DebugLog][DbInitializer] Saving changes..."); + await context.SaveChangesAsync(); + Console.WriteLine("[DebugLog][DbInitializer] Changes saved."); - if (!context.Families.Any()) + if (context.Families.Any()) + { + Console.WriteLine("[DebugLog][DbInitializer] Table 'Families' is not yet empty; clearing..."); + context.Families.RemoveRange(context.Families.AsEnumerable()); + Console.WriteLine("[DebugLog][DbInitializer] Table 'Families' has been emptied."); + } + command = connection.CreateCommand(); + command.CommandText = "SELECT * FROM Families;"; + List families = new(); + Console.WriteLine("[DebugLog][DbInitializer] Executing SELECT command..."); + using (SqliteDataReader reader = await command.ExecuteReaderAsync()) { - Console.WriteLine("[DebugLog][DbInitializer] Table 'Families' is empty, adding seed data..."); - SqliteCommand command = connection.CreateCommand(); - command.CommandText = "SELECT * FROM Families;"; - List families = new(); - Console.WriteLine("[DebugLog][DbInitializer] Executing SELECT command..."); - using SqliteDataReader reader = await command.ExecuteReaderAsync(); Console.WriteLine("[DebugLog][DbInitializer] Reading data..."); while (await reader.ReadAsync()) { @@ -113,21 +127,25 @@ public static async Task InitializeAsync(WebApplication app) TaxonomicOrderID = DbCast(reader["TaxonomicOrderID"]), }); } - Console.WriteLine($"[DebugLog][DbInitializer] Adding {families.Count} new Families to the context..."); - await context.Families.AddRangeAsync(families.ToArray()); - Console.WriteLine("[DebugLog][DbInitializer] Saving changes..."); - await context.SaveChangesAsync(); - Console.WriteLine("[DebugLog][DbInitializer] Changes saved."); } + Console.WriteLine($"[DebugLog][DbInitializer] Adding {families.Count} new Families to the context..."); + await context.Families.AddRangeAsync(families); + Console.WriteLine("[DebugLog][DbInitializer] Saving changes..."); + await context.SaveChangesAsync(); + Console.WriteLine("[DebugLog][DbInitializer] Changes saved."); - if (!context.Genera.Any()) + if (context.Genera.Any()) + { + Console.WriteLine("[DebugLog][DbInitializer] Table 'Genera' is not yet empty; clearing..."); + context.Genera.RemoveRange(context.Genera.AsEnumerable()); + Console.WriteLine("[DebugLog][DbInitializer] Table 'Genera' has been emptied."); + } + command = connection.CreateCommand(); + command.CommandText = "SELECT * FROM Genera;"; + List genera = new(); + Console.WriteLine("[DebugLog][DbInitializer] Executing SELECT command..."); + using (SqliteDataReader reader = await command.ExecuteReaderAsync()) { - Console.WriteLine("[DebugLog][DbInitializer] Table 'Genera' is empty, adding seed data..."); - SqliteCommand command = connection.CreateCommand(); - command.CommandText = "SELECT * FROM Genera;"; - List genera = new(); - Console.WriteLine("[DebugLog][DbInitializer] Executing SELECT command..."); - using SqliteDataReader reader = await command.ExecuteReaderAsync(); Console.WriteLine("[DebugLog][DbInitializer] Reading data..."); while (await reader.ReadAsync()) { @@ -138,21 +156,25 @@ public static async Task InitializeAsync(WebApplication app) FamilyID = DbCast(reader["FamilyID"]), }); } - Console.WriteLine($"[DebugLog][DbInitializer] Adding {genera.Count} new Genera to the context..."); - await context.Genera.AddRangeAsync(genera.ToArray()); - Console.WriteLine("[DebugLog][DbInitializer] Saving changes..."); - await context.SaveChangesAsync(); - Console.WriteLine("[DebugLog][DbInitializer] Changes saved."); } + Console.WriteLine($"[DebugLog][DbInitializer] Adding {genera.Count} new Genera to the context..."); + await context.Genera.AddRangeAsync(genera); + Console.WriteLine("[DebugLog][DbInitializer] Saving changes..."); + await context.SaveChangesAsync(); + Console.WriteLine("[DebugLog][DbInitializer] Changes saved."); - if (!context.Taxa.Any()) + if (context.Taxa.Any()) + { + Console.WriteLine("[DebugLog][DbInitializer] Table 'Taxa' is not yet empty; clearing..."); + context.Taxa.RemoveRange(context.Taxa.AsEnumerable()); + Console.WriteLine("[DebugLog][DbInitializer] Table 'Taxa' has been emptied."); + } + command = connection.CreateCommand(); + command.CommandText = "SELECT * FROM Taxa;"; + List taxa = new(); + Console.WriteLine("[DebugLog][DbInitializer] Executing SELECT command..."); + using (SqliteDataReader reader = await command.ExecuteReaderAsync()) { - Console.WriteLine("[DebugLog][DbInitializer] Table 'Taxa' is empty, adding seed data..."); - SqliteCommand command = connection.CreateCommand(); - command.CommandText = "SELECT * FROM Taxa;"; - List taxa = new(); - Console.WriteLine("[DebugLog][DbInitializer] Executing SELECT command..."); - using SqliteDataReader reader = await command.ExecuteReaderAsync(); Console.WriteLine("[DebugLog][DbInitializer] Reading data..."); while (await reader.ReadAsync()) { @@ -170,21 +192,25 @@ public static async Task InitializeAsync(WebApplication app) USDAsynonym = DbCast(reader["USDAsynonym"]), }); } - Console.WriteLine($"[DebugLog][DbInitializer] Adding {taxa.Count} new Taxa to the context..."); - await context.Taxa.AddRangeAsync(taxa.ToArray()); - Console.WriteLine("[DebugLog][DbInitializer] Saving changes..."); - await context.SaveChangesAsync(); - Console.WriteLine("[DebugLog][DbInitializer] Changes saved."); } + Console.WriteLine($"[DebugLog][DbInitializer] Adding {taxa.Count} new Taxa to the context..."); + await context.Taxa.AddRangeAsync(taxa); + Console.WriteLine("[DebugLog][DbInitializer] Saving changes..."); + await context.SaveChangesAsync(); + Console.WriteLine("[DebugLog][DbInitializer] Changes saved."); - if (!context.Synonyms.Any()) + if (context.Synonyms.Any()) + { + Console.WriteLine("[DebugLog][DbInitializer] Table 'Synonyms' is not yet empty; clearing..."); + context.Synonyms.RemoveRange(context.Synonyms.AsEnumerable()); + Console.WriteLine("[DebugLog][DbInitializer] Table 'Synonyms' has been emptied."); + } + command = connection.CreateCommand(); + command.CommandText = "SELECT * FROM Synonyms;"; + List synonyms = new(); + Console.WriteLine("[DebugLog][DbInitializer] Executing SELECT command..."); + using (SqliteDataReader reader = await command.ExecuteReaderAsync()) { - Console.WriteLine("[DebugLog][DbInitializer] Table 'Synonyms' is empty, adding seed data..."); - SqliteCommand command = connection.CreateCommand(); - command.CommandText = "SELECT * FROM Synonyms;"; - List synonyms = new(); - Console.WriteLine("[DebugLog][DbInitializer] Executing SELECT command..."); - using SqliteDataReader reader = await command.ExecuteReaderAsync(); Console.WriteLine("[DebugLog][DbInitializer] Reading data..."); while (await reader.ReadAsync()) { @@ -199,12 +225,13 @@ public static async Task InitializeAsync(WebApplication app) Authors = DbCast(reader["Authors"]), }); } - Console.WriteLine($"[DebugLog][DbInitializer] Adding {synonyms.Count} new Synonyms to the context..."); - await context.Synonyms.AddRangeAsync(synonyms.ToArray()); - Console.WriteLine("[DebugLog][DbInitializer] Saving changes..."); - await context.SaveChangesAsync(); - Console.WriteLine("[DebugLog][DbInitializer] Changes saved."); } + Console.WriteLine($"[DebugLog][DbInitializer] Adding {synonyms.Count} new Synonyms to the context..."); + await context.Synonyms.AddRangeAsync(synonyms); + Console.WriteLine("[DebugLog][DbInitializer] Saving changes..."); + await context.SaveChangesAsync(); + Console.WriteLine("[DebugLog][DbInitializer] Changes saved."); + Console.WriteLine("[DebugLog][DbInitializer] Successfully seeded database."); } }