Started migrating to postgresql
This commit is contained in:
@@ -1,49 +0,0 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Moonlight.ApiServer.Configuration;
|
||||
using Pomelo.EntityFrameworkCore.MySql.Infrastructure;
|
||||
|
||||
namespace Moonlight.ApiServer.Helpers;
|
||||
|
||||
public abstract class DatabaseContext : DbContext
|
||||
{
|
||||
public abstract string Prefix { get; }
|
||||
|
||||
private readonly AppConfiguration Configuration;
|
||||
|
||||
public DatabaseContext(AppConfiguration configuration)
|
||||
{
|
||||
Configuration = configuration;
|
||||
}
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
if (optionsBuilder.IsConfigured)
|
||||
return;
|
||||
|
||||
var config = Configuration.Database;
|
||||
|
||||
var connectionString = $"host={config.Host};" +
|
||||
$"port={config.Port};" +
|
||||
$"database={config.Database};" +
|
||||
$"uid={config.Username};" +
|
||||
$"pwd={config.Password}";
|
||||
|
||||
optionsBuilder.UseMySql(
|
||||
connectionString,
|
||||
ServerVersion.AutoDetect(connectionString),
|
||||
builder =>
|
||||
{
|
||||
builder.EnableRetryOnFailure(5);
|
||||
builder.SchemaBehavior(MySqlSchemaBehavior.Translate, (name, objectName) => $"{name}_{objectName}");
|
||||
builder.MigrationsHistoryTable($"{Prefix}_MigrationHistory");
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.Model.SetDefaultSchema(Prefix);
|
||||
|
||||
base.OnModelCreating(modelBuilder);
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
using System.Collections;
|
||||
|
||||
namespace Moonlight.ApiServer.Helpers;
|
||||
|
||||
public class DatabaseContextCollection : IEnumerable<Type>
|
||||
{
|
||||
private readonly List<Type> Types = new();
|
||||
|
||||
public IEnumerator<Type> GetEnumerator() => Types.GetEnumerator();
|
||||
IEnumerator IEnumerable.GetEnumerator() => Types.GetEnumerator();
|
||||
|
||||
public void Add<T>() where T : DatabaseContext
|
||||
=> Types.Add(typeof(T));
|
||||
|
||||
public void Remove(Type type) => Types.Remove(type);
|
||||
public void Remove<T>() where T : DatabaseContext => Remove(typeof(T));
|
||||
}
|
||||
Reference in New Issue
Block a user