This commit is contained in:
Janus Knudsen 2025-02-13 17:06:22 +01:00
parent 7bcb7b0e66
commit e777135d62
2 changed files with 92 additions and 50 deletions

View file

@ -3,28 +3,52 @@ using System.Data;
namespace Database.ConfigurationManagementSystem;
public class SetupConfiguration
public class SetupConfiguration : Core.IDbConfigure<SetupConfiguration.Command>
{
private readonly IDbConnection _connection;
public class Command
{
}
public SetupConfiguration(IDbConnection connection)
{
_connection = connection;
}
public async Task CreateDatabaseStructure(IDbConnection connection)
{
await CreateConfigurationTable();
await CreateHistoryTable();
await CreateConfigurationIndexes();
await CreateModifiedAtTrigger();
await CreateNotifyTrigger();
await CreateHistoryTrigger();
}
private readonly IDbConnection _db;
public async Task CreateConfigurationTable()
{
const string sql = @"
CREATE TABLE app_configuration (
public SetupConfiguration(IDbConnection connection)
{
_db = connection;
}
public void With(Command notInUse)
{
using (var transaction = _db.OpenWithTransaction())
{
try
{
CreateConfigurationTable();
CreateHistoryTable();
CreateConfigurationIndexes();
CreateModifiedAtTrigger();
CreateNotifyTrigger();
CreateHistoryTrigger();
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
throw new InvalidOperationException("Failed to SetupConfiguration in Database", ex);
}
}
}
private void ExecuteSql(string sql)
{
_db.ExecuteSql(sql);
}
void CreateConfigurationTable()
{
const string sql = @"
CREATE TABLE IF NOT EXISTS app_configuration (
id bigserial NOT NULL,
""key"" varchar(255) NOT NULL,
value text NULL,
@ -37,13 +61,13 @@ public class SetupConfiguration
etag uuid DEFAULT gen_random_uuid() NULL,
CONSTRAINT app_configuration_pkey PRIMARY KEY (id)
);";
await _connection.ExecuteAsync(sql);
}
ExecuteSql(sql);
}
public async Task CreateHistoryTable()
{
const string sql = @"
CREATE TABLE app_configuration_history (
void CreateHistoryTable()
{
const string sql = @"
CREATE TABLE IF NOT EXISTS app_configuration_history (
history_id bigserial NOT NULL,
action_type char(1) NOT NULL,
action_timestamp timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP,
@ -60,20 +84,20 @@ public class SetupConfiguration
etag uuid NULL,
CONSTRAINT app_configuration_history_pkey PRIMARY KEY (history_id)
);";
await _connection.ExecuteAsync(sql);
}
ExecuteSql(sql);
}
public async Task CreateConfigurationIndexes()
{
const string sql = @"
void CreateConfigurationIndexes()
{
const string sql = @"
CREATE INDEX idx_app_configuration_key ON app_configuration(""key"");
CREATE INDEX idx_app_configuration_validity ON app_configuration(valid_from, expires_at);";
await _connection.ExecuteAsync(sql);
}
ExecuteSql(sql);
}
public async Task CreateModifiedAtTrigger()
{
const string sql = @"
void CreateModifiedAtTrigger()
{
const string sql = @"
CREATE OR REPLACE FUNCTION update_app_configuration_modified_at()
RETURNS TRIGGER AS $$
BEGIN
@ -86,12 +110,12 @@ public class SetupConfiguration
BEFORE UPDATE ON app_configuration
FOR EACH ROW
EXECUTE FUNCTION update_app_configuration_modified_at();";
await _connection.ExecuteAsync(sql);
}
ExecuteSql(sql);
}
public async Task CreateNotifyTrigger()
{
const string sql = @"
void CreateNotifyTrigger()
{
const string sql = @"
CREATE OR REPLACE FUNCTION notify_app_configuration_change()
RETURNS TRIGGER AS $$
BEGIN
@ -104,12 +128,12 @@ public class SetupConfiguration
AFTER INSERT OR UPDATE ON app_configuration
FOR EACH ROW
EXECUTE FUNCTION notify_app_configuration_change();";
await _connection.ExecuteAsync(sql);
}
ExecuteSql(sql);
}
public async Task CreateHistoryTrigger()
{
const string sql = @"
void CreateHistoryTrigger()
{
const string sql = @"
CREATE OR REPLACE FUNCTION log_app_configuration_changes()
RETURNS TRIGGER AS $$
BEGIN
@ -148,8 +172,8 @@ public class SetupConfiguration
CREATE TRIGGER trg_app_configuration_history
AFTER INSERT OR UPDATE OR DELETE ON app_configuration
FOR EACH ROW EXECUTE FUNCTION log_app_configuration_changes();";
await _connection.ExecuteAsync(sql);
}
ExecuteSql(sql);
}
}

View file

@ -1,6 +1,7 @@
using Autofac;
using Insight.Database;
using System.Data;
using System.Diagnostics;
namespace SetupInfrastructure
{
@ -46,17 +47,34 @@ namespace SetupInfrastructure
if (IsSuperAdmin())
{
Console.ForegroundColor = ConsoleColor.Green;
var sw = new Stopwatch();
Console.Write("Database.Core.DCL.SetupApplicationUser...");
sw.Start();
var setupApplicationUser = _container.Resolve<Database.Core.DCL.SetupApplicationUser>();
setupApplicationUser.With(new Database.Core.DCL.SetupApplicationUser.Command { Password = "3911", Schema = "system", User = "sathumper" });
Console.WriteLine("Database.Core.DCL.SetupApplicationUser done!");
Console.WriteLine();
Console.Write($"DONE, took: {sw.ElapsedMilliseconds} ms");
Console.WriteLine("::");
Console.WriteLine("::");
Console.Write("Database.Core.DDL.SetupIdentitySystem...");
sw.Restart();
//create new container with application user, we use that role from now.
_container = new Startup().ConfigureContainer(new Startup.ConnectionStringTemplateParameters("sathumper", "3911"));
var setupIdentitySystem = _container.Resolve<Database.Core.DDL.SetupIdentitySystem>();
setupIdentitySystem.With(new Database.Core.DDL.SetupIdentitySystem.Command());
// SetupIdentitySystem
Console.Write($"DONE, took: {sw.ElapsedMilliseconds} ms");
Console.WriteLine("::");
Console.WriteLine("::");
Console.Write("Database.Core.DDL.SetupIdentitySystem...");
sw.Restart();
var setupConfigurationSystem = _container.Resolve<Database.ConfigurationManagementSystem.SetupConfiguration>();
setupConfigurationSystem.With(new Database.ConfigurationManagementSystem.SetupConfiguration.Command());
Console.Write($"DONE, took: {sw.ElapsedMilliseconds} ms");
// SetupConfiguration