Cleaned up various fixtures. Skip publish-image for now.
This commit is contained in:
@@ -8,19 +8,18 @@ namespace JSMR.Tests.Fixtures;
|
||||
public static class MariaDbClone
|
||||
{
|
||||
public static async Task CreateTemplateAsync(
|
||||
string rootConn,
|
||||
string rootConnectionString,
|
||||
string templateDbName,
|
||||
Func<AppDbContext, Task>? seedAsync = null)
|
||||
{
|
||||
await using var root = new MySqlConnection(rootConn);
|
||||
await using MySqlConnection root = new(rootConnectionString);
|
||||
await root.OpenAsync();
|
||||
|
||||
await ExecAsync(root, $"DROP DATABASE IF EXISTS `{templateDbName}`;");
|
||||
await ExecAsync(root, $"CREATE DATABASE `{templateDbName}` CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;");
|
||||
|
||||
// Run EF once to build schema and seed
|
||||
var templateConn = new MySqlConnectionStringBuilder(rootConn) { Database = templateDbName }.ConnectionString;
|
||||
await using var ctx = AppDb(templateConn);
|
||||
var templateConn = new MySqlConnectionStringBuilder(rootConnectionString) { Database = templateDbName }.ConnectionString;
|
||||
await using var ctx = CreateAppDbContext(templateConn);
|
||||
await ctx.Database.EnsureCreatedAsync();
|
||||
|
||||
if (seedAsync != null)
|
||||
@@ -28,18 +27,18 @@ public static class MariaDbClone
|
||||
}
|
||||
|
||||
public static async Task<AppDbContext> CloneFromTemplateAsync(
|
||||
string rootConn,
|
||||
string rootConnectionString,
|
||||
string templateDbName,
|
||||
string newDbName,
|
||||
Func<AppDbContext, Task>? seed = null)
|
||||
{
|
||||
var newConnStr = new MySqlConnectionStringBuilder(rootConn) { Database = newDbName }.ConnectionString;
|
||||
var databaseName = $"t_{DateTime.UtcNow:yyyyMMddHHmmss}_{Guid.NewGuid():N}";
|
||||
var connectionString = new MySqlConnectionStringBuilder(rootConnectionString) { Database = databaseName }.ConnectionString;
|
||||
|
||||
await using var root = new MySqlConnection(rootConn);
|
||||
await using var root = new MySqlConnection(rootConnectionString);
|
||||
await root.OpenAsync();
|
||||
|
||||
// Create target DB
|
||||
await ExecAsync(root, $"CREATE DATABASE `{newDbName}` CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;");
|
||||
await ExecAsync(root, $"CREATE DATABASE `{databaseName}` CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;");
|
||||
|
||||
// Disable FK checks while recreating schema & loading data
|
||||
await ExecAsync(root, $"SET SESSION sql_log_bin = 0;"); // avoid binlog noise (optional)
|
||||
@@ -52,23 +51,20 @@ public static class MariaDbClone
|
||||
{
|
||||
var createTable = await ShowCreateTableAsync(root, templateDbName, table);
|
||||
// Run DDL in the new DB (the CREATE statement itself doesn't include db name)
|
||||
await ExecAsync(root, $"USE `{newDbName}`; {createTable};");
|
||||
await ExecAsync(root, $"USE `{databaseName}`; {createTable};");
|
||||
}
|
||||
|
||||
// 2) Copy data
|
||||
foreach (var table in tables)
|
||||
{
|
||||
var sql = $"INSERT INTO `{newDbName}`.`{table}` SELECT * FROM `{templateDbName}`.`{table}`;";
|
||||
var sql = $"INSERT INTO `{databaseName}`.`{table}` SELECT * FROM `{templateDbName}`.`{table}`;";
|
||||
await ExecAsync(root, sql);
|
||||
}
|
||||
|
||||
await ExecAsync(root, $"SET SESSION foreign_key_checks = 1;");
|
||||
await ExecAsync(root, $"SET SESSION sql_log_bin = 1;");
|
||||
|
||||
// Ready-to-use EF context for the cloned DB
|
||||
//return AppDb(newConnStr);
|
||||
|
||||
AppDbContext dbContext = AppDb(newConnStr);
|
||||
AppDbContext dbContext = CreateAppDbContext(connectionString);
|
||||
|
||||
if (seed != null)
|
||||
await seed(dbContext);
|
||||
@@ -76,14 +72,14 @@ public static class MariaDbClone
|
||||
return dbContext;
|
||||
}
|
||||
|
||||
private static AppDbContext AppDb(string connStr)
|
||||
private static AppDbContext CreateAppDbContext(string connectionString)
|
||||
{
|
||||
var opts = new DbContextOptionsBuilder<AppDbContext>()
|
||||
.UseMySql(connStr, ServerVersion.AutoDetect(connStr), o => o.EnableRetryOnFailure())
|
||||
var options = new DbContextOptionsBuilder<AppDbContext>()
|
||||
.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString), o => o.EnableRetryOnFailure())
|
||||
.EnableSensitiveDataLogging()
|
||||
.Options;
|
||||
|
||||
return new AppDbContext(opts);
|
||||
return new AppDbContext(options);
|
||||
}
|
||||
|
||||
private static async Task<List<string>> GetTableNamesAsync(MySqlConnection conn, string db)
|
||||
|
||||
Reference in New Issue
Block a user