Initiate db drop/recreation in seed application, update broken procs

This commit is contained in:
Aaron Po
2026-01-29 23:05:08 -05:00
parent ca49d19bf7
commit 0053d84de8
5 changed files with 102 additions and 75 deletions

View File

@@ -1,5 +1,7 @@
using DBSeed;
using Microsoft.Data.SqlClient;
using DbUp;
using System.Reflection;
try
{
@@ -14,8 +16,47 @@ try
await using var connection = new SqlConnection(connectionString);
await connection.OpenAsync();
// drop and recreate the database
var useMaster = connection.CreateCommand();
useMaster.CommandText = "USE master;";
await useMaster.ExecuteNonQueryAsync();
var dbName = "Biergarten";
var dropDb = connection.CreateCommand();
dropDb.CommandText = $@"
IF DB_ID(N'{dbName}') IS NOT NULL
BEGIN
ALTER DATABASE [{dbName}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
DROP DATABASE [{dbName}];
END";
await dropDb.ExecuteNonQueryAsync();
var createDb = connection.CreateCommand();
createDb.CommandText = $@"CREATE DATABASE [{dbName}];";
await createDb.ExecuteNonQueryAsync();
await connection.CloseAsync();
await connection.OpenAsync();
Console.WriteLine("Connected to database.");
Console.WriteLine("Starting migrations...");
// Run Database.Core migrations (embedded resources) via DbUp
var migrationAssembly = Assembly.Load("Database.Core");
var upgrader = DeployChanges
.To.SqlDatabase(connectionString)
.WithScriptsEmbeddedInAssembly(migrationAssembly)
.LogToConsole()
.Build();
var upgradeResult = upgrader.PerformUpgrade();
if (!upgradeResult.Successful)
throw upgradeResult.Error;
Console.WriteLine("Migrations completed.");
ISeeder[] seeders =
[
new LocationSeeder(),
@@ -30,6 +71,7 @@ try
}
Console.WriteLine("Seed completed successfully.");
await connection.CloseAsync();
return 0;
}
catch (Exception ex)