Format ./web/backend/Database/Database.Seed

This commit is contained in:
Aaron Po
2026-06-20 15:13:32 -04:00
parent bfda60d831
commit 58833f330c
5 changed files with 54 additions and 92 deletions

View File

@@ -17,20 +17,24 @@ using Microsoft.Data.SqlClient;
/// </exception>
string BuildConnectionString()
{
string server = Environment.GetEnvironmentVariable("DB_SERVER")
?? throw new InvalidOperationException("DB_SERVER environment variable is not set");
string server =
Environment.GetEnvironmentVariable("DB_SERVER")
?? throw new InvalidOperationException("DB_SERVER environment variable is not set");
string dbName = Environment.GetEnvironmentVariable("DB_NAME")
?? throw new InvalidOperationException("DB_NAME environment variable is not set");
string dbName =
Environment.GetEnvironmentVariable("DB_NAME")
?? throw new InvalidOperationException("DB_NAME environment variable is not set");
string user = Environment.GetEnvironmentVariable("DB_USER")
?? throw new InvalidOperationException("DB_USER environment variable is not set");
string user =
Environment.GetEnvironmentVariable("DB_USER")
?? throw new InvalidOperationException("DB_USER environment variable is not set");
string password = Environment.GetEnvironmentVariable("DB_PASSWORD")
?? throw new InvalidOperationException("DB_PASSWORD environment variable is not set");
string password =
Environment.GetEnvironmentVariable("DB_PASSWORD")
?? throw new InvalidOperationException("DB_PASSWORD environment variable is not set");
string trustServerCertificate = Environment.GetEnvironmentVariable("DB_TRUST_SERVER_CERTIFICATE")
?? "True";
string trustServerCertificate =
Environment.GetEnvironmentVariable("DB_TRUST_SERVER_CERTIFICATE") ?? "True";
SqlConnectionStringBuilder builder = new()
{
@@ -39,13 +43,12 @@ string BuildConnectionString()
UserID = user,
Password = password,
TrustServerCertificate = bool.Parse(trustServerCertificate),
Encrypt = true
Encrypt = true,
};
return builder.ConnectionString;
}
try
{
string connectionString = BuildConnectionString();
@@ -74,17 +77,14 @@ try
connection = null;
}
if (connection == null) throw new Exception($"Failed to connect to database after {maxRetries} attempts.");
if (connection == null)
throw new Exception($"Failed to connect to database after {maxRetries} attempts.");
Console.WriteLine("Starting seeding...");
using (connection)
{
ISeeder[] seeders =
[
new LocationSeeder(),
new UserSeeder()
];
ISeeder[] seeders = [new LocationSeeder(), new UserSeeder()];
foreach (ISeeder seeder in seeders)
{
@@ -103,4 +103,4 @@ catch (Exception ex)
Console.Error.WriteLine("Seed failed:");
Console.Error.WriteLine(ex);
return 1;
}
}