Skip to content

Commit

Permalink
DbInitializer now overwrites old plant data
Browse files Browse the repository at this point in the history
  • Loading branch information
reidst committed Dec 11, 2023
1 parent ef437c8 commit f3896ce
Showing 1 changed file with 99 additions and 72 deletions.
171 changes: 99 additions & 72 deletions Data/DbInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,27 @@ public static async Task InitializeAsync(WebApplication app)
ISeeGreenContext context = scope.ServiceProvider.GetRequiredService<ISeeGreenContext>();
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}");
Console.WriteLine("[DebugLog][DbInitializer] connecting to SQLite seed database...");
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> 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> 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())
{
Expand All @@ -52,21 +58,25 @@ public static async Task InitializeAsync(WebApplication app)
APG4sort = DbCast<long>(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> 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> 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())
{
Expand All @@ -87,21 +97,25 @@ public static async Task InitializeAsync(WebApplication app)
SortLevel6 = (int)DbCast<long>(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> 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> 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())
{
Expand All @@ -113,21 +127,25 @@ public static async Task InitializeAsync(WebApplication app)
TaxonomicOrderID = DbCast<string?>(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> 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> 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())
{
Expand All @@ -138,21 +156,25 @@ public static async Task InitializeAsync(WebApplication app)
FamilyID = DbCast<string?>(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> 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> 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())
{
Expand All @@ -170,21 +192,25 @@ public static async Task InitializeAsync(WebApplication app)
USDAsynonym = DbCast<string?>(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> 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> 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())
{
Expand All @@ -199,12 +225,13 @@ public static async Task InitializeAsync(WebApplication app)
Authors = DbCast<string?>(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.");
}
}
Expand Down

0 comments on commit f3896ce

Please sign in to comment.