62 lines
2.3 KiB
C#
62 lines
2.3 KiB
C#
using ErsatzTV.Infrastructure;
|
|
using ErsatzTV.Infrastructure.Data;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Design;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
|
|
namespace ErsatzTV;
|
|
|
|
public class TvContextDesignTimeFactory : IDesignTimeDbContextFactory<TvContext>
|
|
{
|
|
public TvContext CreateDbContext(string[] args)
|
|
{
|
|
string provider = GetProvider(args);
|
|
|
|
var optionsBuilder = new DbContextOptionsBuilder<TvContext>();
|
|
if (provider.Equals("MySql", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
TvContext.IsSqlite = false;
|
|
TvContext.LastInsertedRowId = "last_insert_id()";
|
|
TvContext.CaseInsensitiveCollation = "utf8mb4_general_ci";
|
|
string connectionString =
|
|
Environment.GetEnvironmentVariable("MySql__ConnectionString") ??
|
|
"Server=localhost;Database=ersatztv_design_time;User=root;Password=ersatztv;";
|
|
optionsBuilder.UseMySql(
|
|
connectionString,
|
|
new MySqlServerVersion(new Version(8, 0, 36)),
|
|
builder => builder.MigrationsAssembly("ErsatzTV.Infrastructure.MySql"));
|
|
}
|
|
else
|
|
{
|
|
TvContext.IsSqlite = true;
|
|
TvContext.LastInsertedRowId = "last_insert_rowid()";
|
|
TvContext.CaseInsensitiveCollation = "NOCASE";
|
|
string configFolder = Environment.GetEnvironmentVariable("ETV_CONFIG_FOLDER");
|
|
string databasePath = string.IsNullOrWhiteSpace(configFolder)
|
|
? Path.Combine(Path.GetTempPath(), "ersatztv-design-time.sqlite3")
|
|
: Path.Combine(configFolder, "ersatztv.sqlite3");
|
|
optionsBuilder.UseSqlite(
|
|
$"Data Source={databasePath}",
|
|
builder => builder.MigrationsAssembly("ErsatzTV.Infrastructure.Sqlite"));
|
|
}
|
|
|
|
return new TvContext(
|
|
optionsBuilder.Options,
|
|
NullLoggerFactory.Instance,
|
|
new SlowQueryInterceptor(NullLogger<SlowQueryInterceptor>.Instance));
|
|
}
|
|
|
|
private static string GetProvider(string[] args)
|
|
{
|
|
for (int index = 0; index < args.Length - 1; index++)
|
|
{
|
|
if (args[index].Equals("--provider", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
return args[index + 1];
|
|
}
|
|
}
|
|
|
|
return "Sqlite";
|
|
}
|
|
}
|