mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-04-05 18:09:04 +00:00
Initiate db drop/recreation in seed application, update broken procs
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user