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

View file

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