Add database Initializer
This commit is contained in:
parent
177942f7fe
commit
3425896c0b
5 changed files with 98 additions and 11 deletions
43
Geekbot.net/Database/DatabaseInitializer.cs
Normal file
43
Geekbot.net/Database/DatabaseInitializer.cs
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
using System;
|
||||||
|
using Geekbot.net.Lib;
|
||||||
|
using Geekbot.net.Lib.Logger;
|
||||||
|
|
||||||
|
namespace Geekbot.net.Database
|
||||||
|
{
|
||||||
|
public class DatabaseInitializer
|
||||||
|
{
|
||||||
|
private readonly RunParameters _runParameters;
|
||||||
|
private readonly GeekbotLogger _logger;
|
||||||
|
|
||||||
|
public DatabaseInitializer(RunParameters runParameters, GeekbotLogger logger)
|
||||||
|
{
|
||||||
|
_runParameters = runParameters;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DatabaseContext Initzialize()
|
||||||
|
{
|
||||||
|
DatabaseContext database = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (_runParameters.InMemory)
|
||||||
|
{
|
||||||
|
database = new InMemoryDatabase("geekbot");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
database = new SqlDatabase(new SqlConnectionString());
|
||||||
|
}
|
||||||
|
|
||||||
|
database.Database.EnsureCreated();
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
_logger.Error(LogSource.Geekbot, "Could not Connect to datbase", e);
|
||||||
|
Environment.Exit(GeekbotExitCode.DatabaseConnectionFailed.GetHashCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
return database;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
19
Geekbot.net/Database/InMemoryDatabase.cs
Normal file
19
Geekbot.net/Database/InMemoryDatabase.cs
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace Geekbot.net.Database
|
||||||
|
{
|
||||||
|
public class InMemoryDatabase : DatabaseContext
|
||||||
|
{
|
||||||
|
private readonly string _name;
|
||||||
|
|
||||||
|
public InMemoryDatabase(string name)
|
||||||
|
{
|
||||||
|
_name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||||
|
=> optionsBuilder.UseLoggerFactory(new LoggerFactory().AddConsole())
|
||||||
|
.UseInMemoryDatabase(databaseName: _name);
|
||||||
|
}
|
||||||
|
}
|
16
Geekbot.net/Database/SqlConnectionString.cs
Normal file
16
Geekbot.net/Database/SqlConnectionString.cs
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
namespace Geekbot.net.Database
|
||||||
|
{
|
||||||
|
public class SqlConnectionString
|
||||||
|
{
|
||||||
|
public string Server { get; set; } = "localhost";
|
||||||
|
public string Port { get; set; } = "3306";
|
||||||
|
public string Database { get; set; } = "geekbot";
|
||||||
|
public string Username { get; set; } = "geekbot";
|
||||||
|
public string Password { get; set; } = "";
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return $"Server={Server};Port={Port};Database={Database};Uid={Username};Pwd={Password};";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
19
Geekbot.net/Database/SqlDatabase.cs
Normal file
19
Geekbot.net/Database/SqlDatabase.cs
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace Geekbot.net.Database
|
||||||
|
{
|
||||||
|
public class SqlDatabase : DatabaseContext
|
||||||
|
{
|
||||||
|
private readonly SqlConnectionString _connectionString;
|
||||||
|
|
||||||
|
public SqlDatabase(SqlConnectionString connectionString)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||||
|
=> optionsBuilder.UseLoggerFactory(new LoggerFactory().AddConsole())
|
||||||
|
.UseMySql(_connectionString.ToString());
|
||||||
|
}
|
||||||
|
}
|
|
@ -107,17 +107,7 @@ namespace Geekbot.net
|
||||||
_firstStart = true;
|
_firstStart = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
DatabaseContext database = null;
|
var database = new DatabaseInitializer(runParameters, logger).Initzialize();
|
||||||
try
|
|
||||||
{
|
|
||||||
database = new DatabaseContext();
|
|
||||||
database.Database.EnsureCreated();
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
logger.Error(LogSource.Geekbot, "Could not Connect to datbase", e);
|
|
||||||
Environment.Exit(GeekbotExitCode.DatabaseConnectionFailed.GetHashCode());
|
|
||||||
}
|
|
||||||
|
|
||||||
_services = new ServiceCollection();
|
_services = new ServiceCollection();
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue