From e777135d6287a0d5c84d8a9c5aa0df63a4190dda Mon Sep 17 00:00:00 2001 From: Janus Knudsen Date: Thu, 13 Feb 2025 17:06:22 +0100 Subject: [PATCH] wip --- .../SetupConfiguration.cs | 118 +++++++++++------- SetupInfrastructure/Program.cs | 24 +++- 2 files changed, 92 insertions(+), 50 deletions(-) diff --git a/Database/ConfigurationManagementSystem/SetupConfiguration.cs b/Database/ConfigurationManagementSystem/SetupConfiguration.cs index 3ed6676..d307b13 100644 --- a/Database/ConfigurationManagementSystem/SetupConfiguration.cs +++ b/Database/ConfigurationManagementSystem/SetupConfiguration.cs @@ -3,28 +3,52 @@ using System.Data; namespace Database.ConfigurationManagementSystem; -public class SetupConfiguration +public class SetupConfiguration : Core.IDbConfigure { - 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); + } } diff --git a/SetupInfrastructure/Program.cs b/SetupInfrastructure/Program.cs index 7c384b8..3032f3c 100644 --- a/SetupInfrastructure/Program.cs +++ b/SetupInfrastructure/Program.cs @@ -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(); 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(); 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(); + setupConfigurationSystem.With(new Database.ConfigurationManagementSystem.SetupConfiguration.Command()); + Console.Write($"DONE, took: {sw.ElapsedMilliseconds} ms"); + // SetupConfiguration