More work on this Console Host

This commit is contained in:
Janus Knudsen 2025-02-21 17:03:49 +01:00
parent 1501ff442a
commit 8dd01d291d
11 changed files with 336 additions and 344 deletions

View file

@ -1,54 +1,46 @@
using Insight.Database;
using PlanTempus.Database.Core;
using PlanTempus.Database.Core.ConnectionFactory;
using System.Data;
namespace PlanTempus.Database.ConfigurationManagementSystem;
public class SetupConfiguration : IDbConfigure<SetupConfiguration.Command>
{
public class Command
{
public class Command { }
}
private readonly IDbConnectionFactory _connectionFactory;
private readonly IDbConnection _db;
public SetupConfiguration(IDbConnectionFactory connectionFactory)
{
_connectionFactory = connectionFactory;
}
public void With(Command notInUse)
{
using var conn = _connectionFactory.Create();
using var transaction = conn.OpenWithTransaction();
try
{
CreateConfigurationTable(conn);
CreateHistoryTable(conn);
CreateConfigurationIndexes(conn);
CreateModifiedAtTrigger(conn);
CreateNotifyTrigger(conn);
CreateHistoryTrigger(conn);
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);
}
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 = @"
void CreateConfigurationTable(IDbConnection db)
{
const string sql = @"
CREATE TABLE IF NOT EXISTS app_configuration (
id bigserial NOT NULL,
""key"" varchar(255) NOT NULL,
@ -62,12 +54,12 @@ public class SetupConfiguration : IDbConfigure<SetupConfiguration.Command>
etag uuid DEFAULT gen_random_uuid() NULL,
CONSTRAINT app_configuration_pkey PRIMARY KEY (id)
);";
ExecuteSql(sql);
}
db.ExecuteSql(sql);
}
void CreateHistoryTable()
{
const string sql = @"
void CreateHistoryTable(IDbConnection db)
{
const string sql = @"
CREATE TABLE IF NOT EXISTS app_configuration_history (
history_id bigserial NOT NULL,
action_type char(1) NOT NULL,
@ -85,20 +77,20 @@ public class SetupConfiguration : IDbConfigure<SetupConfiguration.Command>
etag uuid NULL,
CONSTRAINT app_configuration_history_pkey PRIMARY KEY (history_id)
);";
ExecuteSql(sql);
}
db.ExecuteSql(sql);
}
void CreateConfigurationIndexes()
{
const string sql = @"
void CreateConfigurationIndexes(IDbConnection db)
{
const string sql = @"
CREATE INDEX IF NOT EXISTS idx_app_configuration_key ON app_configuration(""key"");
CREATE INDEX IF NOT EXISTS idx_app_configuration_validity ON app_configuration(valid_from, expires_at);";
ExecuteSql(sql);
}
db.ExecuteSql(sql);
}
void CreateModifiedAtTrigger()
{
const string sql = @"
void CreateModifiedAtTrigger(IDbConnection db)
{
const string sql = @"
CREATE OR REPLACE FUNCTION update_app_configuration_modified_at()
RETURNS TRIGGER AS $$
BEGIN
@ -111,12 +103,12 @@ public class SetupConfiguration : IDbConfigure<SetupConfiguration.Command>
BEFORE UPDATE ON app_configuration
FOR EACH ROW
EXECUTE FUNCTION update_app_configuration_modified_at();";
ExecuteSql(sql);
}
db.ExecuteSql(sql);
}
void CreateNotifyTrigger()
{
const string sql = @"
void CreateNotifyTrigger(IDbConnection db)
{
const string sql = @"
CREATE OR REPLACE FUNCTION notify_app_configuration_change()
RETURNS TRIGGER AS $$
BEGIN
@ -129,12 +121,12 @@ public class SetupConfiguration : IDbConfigure<SetupConfiguration.Command>
AFTER INSERT OR UPDATE ON app_configuration
FOR EACH ROW
EXECUTE FUNCTION notify_app_configuration_change();";
ExecuteSql(sql);
}
db.ExecuteSql(sql);
}
void CreateHistoryTrigger()
{
const string sql = @"
void CreateHistoryTrigger(IDbConnection db)
{
const string sql = @"
CREATE OR REPLACE FUNCTION log_app_configuration_changes()
RETURNS TRIGGER AS $$
BEGIN
@ -173,8 +165,8 @@ public class SetupConfiguration : IDbConfigure<SetupConfiguration.Command>
CREATE OR REPLACE TRIGGER trg_app_configuration_history
AFTER INSERT OR UPDATE OR DELETE ON app_configuration
FOR EACH ROW EXECUTE FUNCTION log_app_configuration_changes();";
ExecuteSql(sql);
}
db.ExecuteSql(sql);
}
}