More work on this Console Host
This commit is contained in:
parent
1501ff442a
commit
8dd01d291d
11 changed files with 336 additions and 344 deletions
|
|
@ -1,34 +1,32 @@
|
||||||
using Insight.Database;
|
using Insight.Database;
|
||||||
using PlanTempus.Database.Core;
|
using PlanTempus.Database.Core;
|
||||||
|
using PlanTempus.Database.Core.ConnectionFactory;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
|
|
||||||
namespace PlanTempus.Database.ConfigurationManagementSystem;
|
namespace PlanTempus.Database.ConfigurationManagementSystem;
|
||||||
|
|
||||||
public class SetupConfiguration : IDbConfigure<SetupConfiguration.Command>
|
public class SetupConfiguration : IDbConfigure<SetupConfiguration.Command>
|
||||||
{
|
{
|
||||||
public class Command
|
public class Command { }
|
||||||
|
|
||||||
|
private readonly IDbConnectionFactory _connectionFactory;
|
||||||
|
|
||||||
|
public SetupConfiguration(IDbConnectionFactory connectionFactory)
|
||||||
{
|
{
|
||||||
|
_connectionFactory = connectionFactory;
|
||||||
}
|
|
||||||
|
|
||||||
private readonly IDbConnection _db;
|
|
||||||
|
|
||||||
public SetupConfiguration(IDbConnection connection)
|
|
||||||
{
|
|
||||||
_db = connection;
|
|
||||||
}
|
}
|
||||||
public void With(Command notInUse)
|
public void With(Command notInUse)
|
||||||
{
|
{
|
||||||
using (var transaction = _db.OpenWithTransaction())
|
using var conn = _connectionFactory.Create();
|
||||||
{
|
using var transaction = conn.OpenWithTransaction();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
CreateConfigurationTable();
|
CreateConfigurationTable(conn);
|
||||||
CreateHistoryTable();
|
CreateHistoryTable(conn);
|
||||||
CreateConfigurationIndexes();
|
CreateConfigurationIndexes(conn);
|
||||||
CreateModifiedAtTrigger();
|
CreateModifiedAtTrigger(conn);
|
||||||
CreateNotifyTrigger();
|
CreateNotifyTrigger(conn);
|
||||||
CreateHistoryTrigger();
|
CreateHistoryTrigger(conn);
|
||||||
|
|
||||||
transaction.Commit();
|
transaction.Commit();
|
||||||
}
|
}
|
||||||
|
|
@ -37,16 +35,10 @@ public class SetupConfiguration : IDbConfigure<SetupConfiguration.Command>
|
||||||
transaction.Rollback();
|
transaction.Rollback();
|
||||||
throw new InvalidOperationException("Failed to SetupConfiguration in Database", ex);
|
throw new InvalidOperationException("Failed to SetupConfiguration in Database", ex);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ExecuteSql(string sql)
|
void CreateConfigurationTable(IDbConnection db)
|
||||||
{
|
|
||||||
_db.ExecuteSql(sql);
|
|
||||||
}
|
|
||||||
|
|
||||||
void CreateConfigurationTable()
|
|
||||||
{
|
{
|
||||||
const string sql = @"
|
const string sql = @"
|
||||||
CREATE TABLE IF NOT EXISTS app_configuration (
|
CREATE TABLE IF NOT EXISTS app_configuration (
|
||||||
|
|
@ -62,10 +54,10 @@ public class SetupConfiguration : IDbConfigure<SetupConfiguration.Command>
|
||||||
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)
|
||||||
);";
|
);";
|
||||||
ExecuteSql(sql);
|
db.ExecuteSql(sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CreateHistoryTable()
|
void CreateHistoryTable(IDbConnection db)
|
||||||
{
|
{
|
||||||
const string sql = @"
|
const string sql = @"
|
||||||
CREATE TABLE IF NOT EXISTS app_configuration_history (
|
CREATE TABLE IF NOT EXISTS app_configuration_history (
|
||||||
|
|
@ -85,18 +77,18 @@ public class SetupConfiguration : IDbConfigure<SetupConfiguration.Command>
|
||||||
etag uuid NULL,
|
etag uuid NULL,
|
||||||
CONSTRAINT app_configuration_history_pkey PRIMARY KEY (history_id)
|
CONSTRAINT app_configuration_history_pkey PRIMARY KEY (history_id)
|
||||||
);";
|
);";
|
||||||
ExecuteSql(sql);
|
db.ExecuteSql(sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CreateConfigurationIndexes()
|
void CreateConfigurationIndexes(IDbConnection db)
|
||||||
{
|
{
|
||||||
const string sql = @"
|
const string sql = @"
|
||||||
CREATE INDEX IF NOT EXISTS idx_app_configuration_key ON app_configuration(""key"");
|
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);";
|
CREATE INDEX IF NOT EXISTS idx_app_configuration_validity ON app_configuration(valid_from, expires_at);";
|
||||||
ExecuteSql(sql);
|
db.ExecuteSql(sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CreateModifiedAtTrigger()
|
void CreateModifiedAtTrigger(IDbConnection db)
|
||||||
{
|
{
|
||||||
const string sql = @"
|
const string sql = @"
|
||||||
CREATE OR REPLACE FUNCTION update_app_configuration_modified_at()
|
CREATE OR REPLACE FUNCTION update_app_configuration_modified_at()
|
||||||
|
|
@ -111,10 +103,10 @@ public class SetupConfiguration : IDbConfigure<SetupConfiguration.Command>
|
||||||
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();";
|
||||||
ExecuteSql(sql);
|
db.ExecuteSql(sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CreateNotifyTrigger()
|
void CreateNotifyTrigger(IDbConnection db)
|
||||||
{
|
{
|
||||||
const string sql = @"
|
const string sql = @"
|
||||||
CREATE OR REPLACE FUNCTION notify_app_configuration_change()
|
CREATE OR REPLACE FUNCTION notify_app_configuration_change()
|
||||||
|
|
@ -129,10 +121,10 @@ public class SetupConfiguration : IDbConfigure<SetupConfiguration.Command>
|
||||||
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();";
|
||||||
ExecuteSql(sql);
|
db.ExecuteSql(sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CreateHistoryTrigger()
|
void CreateHistoryTrigger(IDbConnection db)
|
||||||
{
|
{
|
||||||
const string sql = @"
|
const string sql = @"
|
||||||
CREATE OR REPLACE FUNCTION log_app_configuration_changes()
|
CREATE OR REPLACE FUNCTION log_app_configuration_changes()
|
||||||
|
|
@ -173,7 +165,7 @@ public class SetupConfiguration : IDbConfigure<SetupConfiguration.Command>
|
||||||
CREATE OR REPLACE TRIGGER trg_app_configuration_history
|
CREATE OR REPLACE 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();";
|
||||||
ExecuteSql(sql);
|
db.ExecuteSql(sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
8
Database/Core/ConnectionFactory/IDbConnectionFactory.cs
Normal file
8
Database/Core/ConnectionFactory/IDbConnectionFactory.cs
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
namespace PlanTempus.Database.Core.ConnectionFactory
|
||||||
|
{
|
||||||
|
public interface IDbConnectionFactory
|
||||||
|
{
|
||||||
|
System.Data.IDbConnection Create();
|
||||||
|
System.Data.IDbConnection Create(string username, string password);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,14 +1,9 @@
|
||||||
using Npgsql;
|
using Npgsql;
|
||||||
using PlanTempus.Database.ModuleRegistry;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Data;
|
using System.Data;
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace PlanTempus.Database.Core.ConnectionFactory
|
namespace PlanTempus.Database.Core.ConnectionFactory
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
public class PostgresConnectionFactory : IDbConnectionFactory, IAsyncDisposable
|
public class PostgresConnectionFactory : IDbConnectionFactory, IAsyncDisposable
|
||||||
{
|
{
|
||||||
private readonly NpgsqlDataSource _baseDataSource;
|
private readonly NpgsqlDataSource _baseDataSource;
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
using System.Data;
|
using System.Data;
|
||||||
using Insight.Database;
|
using Insight.Database;
|
||||||
using PlanTempus.Database.Common;
|
using PlanTempus.Database.Common;
|
||||||
using PlanTempus.Database.Core;
|
using PlanTempus.Database.Core.ConnectionFactory;
|
||||||
|
|
||||||
namespace PlanTempus.Database.Core.DCL
|
namespace PlanTempus.Database.Core.DCL
|
||||||
{
|
{
|
||||||
|
|
@ -19,12 +19,12 @@ namespace PlanTempus.Database.Core.DCL
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
IDbConnection _db;
|
|
||||||
Command _command;
|
Command _command;
|
||||||
|
private readonly IDbConnectionFactory _connectionFactory;
|
||||||
|
|
||||||
public SetupApplicationUser(IDbConnection db)
|
public SetupApplicationUser(IDbConnectionFactory connectionFactory)
|
||||||
{
|
{
|
||||||
_db = db;
|
_connectionFactory = connectionFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void With(Command command)
|
public void With(Command command)
|
||||||
|
|
@ -34,13 +34,13 @@ namespace PlanTempus.Database.Core.DCL
|
||||||
if (!Validations.IsValidSchemaName(_command.Schema))
|
if (!Validations.IsValidSchemaName(_command.Schema))
|
||||||
throw new ArgumentException("Invalid schema name", _command.Schema);
|
throw new ArgumentException("Invalid schema name", _command.Schema);
|
||||||
|
|
||||||
using (var transaction = _db.OpenWithTransaction())
|
using var conn = _connectionFactory.Create();
|
||||||
{
|
using var transaction = conn.OpenWithTransaction();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
CreateSchema();
|
CreateSchema(conn);
|
||||||
CreateRole();
|
CreateRole(conn);
|
||||||
GrantSchemaRights();
|
GrantSchemaRights(conn);
|
||||||
|
|
||||||
transaction.Commit();
|
transaction.Commit();
|
||||||
}
|
}
|
||||||
|
|
@ -49,21 +49,16 @@ namespace PlanTempus.Database.Core.DCL
|
||||||
transaction.Rollback();
|
transaction.Rollback();
|
||||||
throw new InvalidOperationException("Failed to SetupApplicationUser in Database", ex);
|
throw new InvalidOperationException("Failed to SetupApplicationUser in Database", ex);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
private void ExecuteSql(string sql)
|
|
||||||
{
|
|
||||||
_db.ExecuteSql(sql);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void CreateSchema()
|
private void CreateSchema(IDbConnection db)
|
||||||
{
|
{
|
||||||
var sql = $"CREATE SCHEMA IF NOT EXISTS {_command.Schema}";
|
var sql = $"CREATE SCHEMA IF NOT EXISTS {_command.Schema}";
|
||||||
ExecuteSql(sql);
|
db.ExecuteSql(sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void CreateRole()
|
private void CreateRole(IDbConnection db)
|
||||||
{
|
{
|
||||||
var sql = $@"
|
var sql = $@"
|
||||||
DO $$
|
DO $$
|
||||||
|
|
@ -72,42 +67,42 @@ namespace PlanTempus.Database.Core.DCL
|
||||||
CREATE ROLE {_command.User} WITH CREATEDB CREATEROLE LOGIN PASSWORD '{_command.Password}';
|
CREATE ROLE {_command.User} WITH CREATEDB CREATEROLE LOGIN PASSWORD '{_command.Password}';
|
||||||
END IF;
|
END IF;
|
||||||
END $$;";
|
END $$;";
|
||||||
ExecuteSql(sql);
|
db.ExecuteSql(sql);
|
||||||
|
|
||||||
var sql1 = $"ALTER ROLE {_command.User} SET search_path='{_command.Schema}';";
|
var sql1 = $"ALTER ROLE {_command.User} SET search_path='{_command.Schema}';";
|
||||||
ExecuteSql(sql1);
|
db.ExecuteSql(sql1);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GrantSchemaRights()
|
private void GrantSchemaRights(IDbConnection db)
|
||||||
{
|
{
|
||||||
// Grant USAGE og alle CREATE rettigheder på schema niveau
|
// Grant USAGE og alle CREATE rettigheder på schema niveau
|
||||||
var sql = $@"
|
var sql = $@"
|
||||||
GRANT USAGE ON SCHEMA {_command.Schema} TO {_command.User};
|
GRANT USAGE ON SCHEMA {_command.Schema} TO {_command.User};
|
||||||
GRANT ALL ON SCHEMA {_command.Schema} TO {_command.User};";
|
GRANT ALL ON SCHEMA {_command.Schema} TO {_command.User};";
|
||||||
ExecuteSql(sql);
|
db.ExecuteSql(sql);
|
||||||
|
|
||||||
// Grant rettigheder på eksisterende og fremtidige tabeller
|
// Grant rettigheder på eksisterende og fremtidige tabeller
|
||||||
var sql1 = $"GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA {_command.Schema} TO {_command.User};";
|
var sql1 = $"GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA {_command.Schema} TO {_command.User};";
|
||||||
ExecuteSql(sql1);
|
db.ExecuteSql(sql1);
|
||||||
|
|
||||||
var sql2 = $"ALTER DEFAULT PRIVILEGES IN SCHEMA {_command.Schema} GRANT ALL PRIVILEGES ON TABLES TO {_command.User};";
|
var sql2 = $"ALTER DEFAULT PRIVILEGES IN SCHEMA {_command.Schema} GRANT ALL PRIVILEGES ON TABLES TO {_command.User};";
|
||||||
ExecuteSql(sql2);
|
db.ExecuteSql(sql2);
|
||||||
|
|
||||||
// Grant sequence rettigheder
|
// Grant sequence rettigheder
|
||||||
var sql3 = $"GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA {_command.Schema} TO {_command.User};";
|
var sql3 = $"GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA {_command.Schema} TO {_command.User};";
|
||||||
ExecuteSql(sql3);
|
db.ExecuteSql(sql3);
|
||||||
|
|
||||||
// Grant execute på functions
|
// Grant execute på functions
|
||||||
var sql4 = $"GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA {_command.Schema} TO {_command.User};";
|
var sql4 = $"GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA {_command.Schema} TO {_command.User};";
|
||||||
ExecuteSql(sql4);
|
db.ExecuteSql(sql4);
|
||||||
|
|
||||||
// Grant for fremtidige functions
|
// Grant for fremtidige functions
|
||||||
var sql5 = $"ALTER DEFAULT PRIVILEGES IN SCHEMA {_command.Schema} GRANT EXECUTE ON FUNCTIONS TO {_command.User};";
|
var sql5 = $"ALTER DEFAULT PRIVILEGES IN SCHEMA {_command.Schema} GRANT EXECUTE ON FUNCTIONS TO {_command.User};";
|
||||||
ExecuteSql(sql5);
|
db.ExecuteSql(sql5);
|
||||||
|
|
||||||
// Grant for fremtidige sequences
|
// Grant for fremtidige sequences
|
||||||
var sql6 = $"ALTER DEFAULT PRIVILEGES IN SCHEMA {_command.Schema} GRANT USAGE ON SEQUENCES TO {_command.User};";
|
var sql6 = $"ALTER DEFAULT PRIVILEGES IN SCHEMA {_command.Schema} GRANT USAGE ON SEQUENCES TO {_command.User};";
|
||||||
ExecuteSql(sql6);
|
db.ExecuteSql(sql6);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
using Insight.Database;
|
using Insight.Database;
|
||||||
using PlanTempus.Database.Common;
|
using PlanTempus.Database.Common;
|
||||||
using PlanTempus.Database.Core;
|
using PlanTempus.Database.Core;
|
||||||
|
using PlanTempus.Database.Core.ConnectionFactory;
|
||||||
|
|
||||||
namespace PlanTempus.Database.Core.DCL
|
namespace PlanTempus.Database.Core.DCL
|
||||||
{
|
{
|
||||||
|
|
@ -19,12 +20,12 @@ namespace PlanTempus.Database.Core.DCL
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
IDbConnection _db;
|
|
||||||
Command _command;
|
Command _command;
|
||||||
|
private readonly IDbConnectionFactory _connectionFactory;
|
||||||
|
|
||||||
public SetupDbAdmin(IDbConnection db)
|
public SetupDbAdmin(IDbConnectionFactory connectionFactory)
|
||||||
{
|
{
|
||||||
_db = db;
|
_connectionFactory = connectionFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -35,13 +36,13 @@ namespace PlanTempus.Database.Core.DCL
|
||||||
if (!Validations.IsValidSchemaName(_command.Schema))
|
if (!Validations.IsValidSchemaName(_command.Schema))
|
||||||
throw new ArgumentException("Invalid schema name", _command.Schema);
|
throw new ArgumentException("Invalid schema name", _command.Schema);
|
||||||
|
|
||||||
using (var transaction = _db.OpenWithTransaction())
|
using var conn = _connectionFactory.Create();
|
||||||
{
|
using var transaction = conn.OpenWithTransaction();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
CreateSchema();
|
CreateSchema(conn);
|
||||||
CreateRole();
|
CreateRole(conn);
|
||||||
GrantSchemaRights();
|
GrantSchemaRights(conn);
|
||||||
|
|
||||||
transaction.Commit();
|
transaction.Commit();
|
||||||
}
|
}
|
||||||
|
|
@ -50,21 +51,16 @@ namespace PlanTempus.Database.Core.DCL
|
||||||
transaction.Rollback();
|
transaction.Rollback();
|
||||||
throw new InvalidOperationException("Failed to SetupApplicationUser in Database", ex);
|
throw new InvalidOperationException("Failed to SetupApplicationUser in Database", ex);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
private void ExecuteSql(string sql)
|
|
||||||
{
|
|
||||||
_db.ExecuteSql(sql);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void CreateSchema()
|
private void CreateSchema(IDbConnection db)
|
||||||
{
|
{
|
||||||
var sql = $"CREATE SCHEMA IF NOT EXISTS {_command.Schema}";
|
var sql = $"CREATE SCHEMA IF NOT EXISTS {_command.Schema}";
|
||||||
ExecuteSql(sql);
|
db.ExecuteSql(sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void CreateRole()
|
private void CreateRole(IDbConnection db)
|
||||||
{
|
{
|
||||||
var sql = $@"
|
var sql = $@"
|
||||||
DO $$
|
DO $$
|
||||||
|
|
@ -73,24 +69,24 @@ namespace PlanTempus.Database.Core.DCL
|
||||||
CREATE ROLE {_command.User} WITH CREATEDB CREATEROLE LOGIN PASSWORD '{_command.Password}';
|
CREATE ROLE {_command.User} WITH CREATEDB CREATEROLE LOGIN PASSWORD '{_command.Password}';
|
||||||
END IF;
|
END IF;
|
||||||
END $$;";
|
END $$;";
|
||||||
ExecuteSql(sql);
|
db.ExecuteSql(sql);
|
||||||
|
|
||||||
var sql1 = $"ALTER ROLE {_command.User} SET search_path='{_command.Schema}';";
|
var sql1 = $"ALTER ROLE {_command.User} SET search_path='{_command.Schema}';";
|
||||||
ExecuteSql(sql1);
|
db.ExecuteSql(sql1);
|
||||||
|
|
||||||
var sql2 = $"ALTER SCHEMA {_command.Schema} OWNER TO {_command.User};";
|
var sql2 = $"ALTER SCHEMA {_command.Schema} OWNER TO {_command.User};";
|
||||||
ExecuteSql(sql2);
|
db.ExecuteSql(sql2);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GrantSchemaRights()
|
private void GrantSchemaRights(IDbConnection db)
|
||||||
{
|
{
|
||||||
// Grant USAGE og alle CREATE rettigheder på schema niveau
|
// Grant USAGE og alle CREATE rettigheder på schema niveau
|
||||||
//GRANT USAGE ON SCHEMA {_command.Schema} TO {_command.User};
|
//GRANT USAGE ON SCHEMA {_command.Schema} TO {_command.User};
|
||||||
var sql = $@"
|
var sql = $@"
|
||||||
GRANT CREATE ON SCHEMA {_command.Schema} TO {_command.User};";
|
GRANT CREATE ON SCHEMA {_command.Schema} TO {_command.User};";
|
||||||
|
|
||||||
ExecuteSql(sql);
|
db.ExecuteSql(sql);
|
||||||
|
|
||||||
// Grant rettigheder på eksisterende og fremtidige tabeller
|
// Grant rettigheder på eksisterende og fremtidige tabeller
|
||||||
//var sql1 = $"GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA {_command.Schema} TO {_command.User};";
|
//var sql1 = $"GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA {_command.Schema} TO {_command.User};";
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
using Insight.Database;
|
using Insight.Database;
|
||||||
using PlanTempus.Database.Core;
|
using PlanTempus.Database.Core.ConnectionFactory;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
|
|
||||||
namespace PlanTempus.Database.Core.DDL
|
namespace PlanTempus.Database.Core.DDL
|
||||||
|
|
@ -15,13 +15,12 @@ namespace PlanTempus.Database.Core.DDL
|
||||||
public required string Schema { get; init; }
|
public required string Schema { get; init; }
|
||||||
}
|
}
|
||||||
|
|
||||||
readonly IDbConnection _db;
|
|
||||||
IDbTransaction _transaction = null;
|
|
||||||
Command _command;
|
Command _command;
|
||||||
|
private readonly IDbConnectionFactory _connectionFactory;
|
||||||
|
|
||||||
public SetupIdentitySystem(IDbConnection db)
|
public SetupIdentitySystem(IDbConnectionFactory connectionFactory)
|
||||||
{
|
{
|
||||||
_db = db;
|
_connectionFactory = connectionFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -32,37 +31,29 @@ namespace PlanTempus.Database.Core.DDL
|
||||||
{
|
{
|
||||||
_command = command;
|
_command = command;
|
||||||
|
|
||||||
using (_transaction = _db.OpenWithTransaction())
|
using var conn = _connectionFactory.Create();
|
||||||
{
|
using var transaction = conn.OpenWithTransaction();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
CreateUsersTable();
|
CreateUsersTable(conn);
|
||||||
CreateOrganizationsTable();
|
CreateOrganizationsTable(conn);
|
||||||
CreateUserOrganizationsTable();
|
CreateUserOrganizationsTable(conn);
|
||||||
SetupRLS();
|
SetupRLS(conn);
|
||||||
|
|
||||||
_transaction.Commit();
|
transaction.Commit();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_transaction.Rollback();
|
transaction.Rollback();
|
||||||
throw new InvalidOperationException("Failed to SetupIdentitySystem. Transaction is rolled back", ex);
|
throw new InvalidOperationException("Failed to SetupIdentitySystem. Transaction is rolled back", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
private void ExecuteSql(string sql)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrEmpty(sql))
|
|
||||||
throw new ArgumentNullException(nameof(sql));
|
|
||||||
|
|
||||||
_db.ExecuteSql(sql);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates the users table
|
/// Creates the users table
|
||||||
/// </summary>
|
/// </summary>
|
||||||
void CreateUsersTable()
|
void CreateUsersTable(IDbConnection db)
|
||||||
{
|
{
|
||||||
var sql = @$"
|
var sql = @$"
|
||||||
CREATE TABLE IF NOT EXISTS {_command.Schema}.users (
|
CREATE TABLE IF NOT EXISTS {_command.Schema}.users (
|
||||||
|
|
@ -79,14 +70,14 @@ namespace PlanTempus.Database.Core.DDL
|
||||||
last_login_at TIMESTAMPTZ NULL
|
last_login_at TIMESTAMPTZ NULL
|
||||||
);";
|
);";
|
||||||
|
|
||||||
ExecuteSql(sql);
|
db.ExecuteSql(sql);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates the organizations table
|
/// Creates the organizations table
|
||||||
/// </summary>
|
/// </summary>
|
||||||
void CreateOrganizationsTable()
|
void CreateOrganizationsTable(IDbConnection db)
|
||||||
{
|
{
|
||||||
var sql = @$"
|
var sql = @$"
|
||||||
CREATE TABLE IF NOT EXISTS {_command.Schema}.organizations (
|
CREATE TABLE IF NOT EXISTS {_command.Schema}.organizations (
|
||||||
|
|
@ -97,14 +88,14 @@ namespace PlanTempus.Database.Core.DDL
|
||||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
|
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||||
);";
|
);";
|
||||||
|
|
||||||
ExecuteSql(sql);
|
db.ExecuteSql(sql);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates the user_organizations table
|
/// Creates the user_organizations table
|
||||||
/// </summary>
|
/// </summary>
|
||||||
void CreateUserOrganizationsTable()
|
void CreateUserOrganizationsTable(IDbConnection db)
|
||||||
{
|
{
|
||||||
var sql = @$"
|
var sql = @$"
|
||||||
CREATE TABLE IF NOT EXISTS {_command.Schema}.user_organizations (
|
CREATE TABLE IF NOT EXISTS {_command.Schema}.user_organizations (
|
||||||
|
|
@ -115,14 +106,14 @@ namespace PlanTempus.Database.Core.DDL
|
||||||
PRIMARY KEY (user_id, organization_id)
|
PRIMARY KEY (user_id, organization_id)
|
||||||
);";
|
);";
|
||||||
|
|
||||||
ExecuteSql(sql);
|
db.ExecuteSql(sql);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sets up Row Level Security (RLS) for the organizations and user_organizations tables.
|
/// Sets up Row Level Security (RLS) for the organizations and user_organizations tables.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
void SetupRLS()
|
void SetupRLS(IDbConnection db)
|
||||||
{
|
{
|
||||||
var sql = new[]
|
var sql = new[]
|
||||||
{
|
{
|
||||||
|
|
@ -142,7 +133,7 @@ namespace PlanTempus.Database.Core.DDL
|
||||||
|
|
||||||
foreach (var statement in sql)
|
foreach (var statement in sql)
|
||||||
{
|
{
|
||||||
ExecuteSql(statement);
|
db.ExecuteSql(statement);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,8 @@ using System.Data;
|
||||||
|
|
||||||
namespace PlanTempus.Database.Core.Sql
|
namespace PlanTempus.Database.Core.Sql
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
public class DatabaseScope : IDisposable
|
public class DatabaseScope : IDisposable
|
||||||
{
|
{
|
||||||
private readonly IDbConnection _connection;
|
private readonly IDbConnection _connection;
|
||||||
|
|
@ -46,10 +48,10 @@ namespace PlanTempus.Database.Core.Sql
|
||||||
|
|
||||||
public class SqlOperations : IDatabaseOperations
|
public class SqlOperations : IDatabaseOperations
|
||||||
{
|
{
|
||||||
private readonly IDbConnectionFactory _connectionFactory;
|
private readonly ConnectionFactory.IDbConnectionFactory _connectionFactory;
|
||||||
private readonly TelemetryClient _telemetryClient;
|
private readonly TelemetryClient _telemetryClient;
|
||||||
|
|
||||||
public SqlOperations(IDbConnectionFactory connectionFactory, TelemetryClient telemetryClient)
|
public SqlOperations(ConnectionFactory.IDbConnectionFactory connectionFactory, TelemetryClient telemetryClient)
|
||||||
{
|
{
|
||||||
_connectionFactory = connectionFactory;
|
_connectionFactory = connectionFactory;
|
||||||
_telemetryClient = telemetryClient;
|
_telemetryClient = telemetryClient;
|
||||||
|
|
|
||||||
|
|
@ -3,11 +3,7 @@ using Npgsql;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
namespace PlanTempus.Database.ModuleRegistry
|
namespace PlanTempus.Database.ModuleRegistry
|
||||||
{
|
{
|
||||||
public interface IDbConnectionFactory
|
|
||||||
{
|
|
||||||
IDbConnection Create();
|
|
||||||
IDbConnection Create(string username, string password);
|
|
||||||
}
|
|
||||||
public class DbPostgreSqlModule : Module
|
public class DbPostgreSqlModule : Module
|
||||||
{
|
{
|
||||||
public required string ConnectionString { get; set; }
|
public required string ConnectionString { get; set; }
|
||||||
|
|
@ -16,7 +12,7 @@ namespace PlanTempus.Database.ModuleRegistry
|
||||||
{
|
{
|
||||||
Insight.Database.Providers.PostgreSQL.PostgreSQLInsightDbProvider.RegisterProvider();
|
Insight.Database.Providers.PostgreSQL.PostgreSQLInsightDbProvider.RegisterProvider();
|
||||||
|
|
||||||
builder.Register<IDbConnectionFactory>(c =>
|
builder.Register<Core.ConnectionFactory.IDbConnectionFactory>(c =>
|
||||||
new Core.ConnectionFactory.PostgresConnectionFactory(ConnectionString))
|
new Core.ConnectionFactory.PostgresConnectionFactory(ConnectionString))
|
||||||
.SingleInstance();
|
.SingleInstance();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
using Autofac;
|
using Autofac;
|
||||||
using Insight.Database;
|
using Insight.Database;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using Npgsql;
|
||||||
using PlanTempus.Database.ConfigurationManagementSystem;
|
using PlanTempus.Database.ConfigurationManagementSystem;
|
||||||
using PlanTempus.Database.Core.DCL;
|
using PlanTempus.Database.Core.DCL;
|
||||||
using PlanTempus.Database.Core.DDL;
|
using PlanTempus.Database.Core.DDL;
|
||||||
|
|
@ -23,28 +25,28 @@ namespace PlanTempus.SetupInfrastructure
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal class Program
|
internal class Program
|
||||||
{
|
{
|
||||||
static IContainer _container;
|
|
||||||
static ConsoleColor _backgroundColor = Console.BackgroundColor;
|
|
||||||
static ConsoleColor _foregroundColor = Console.ForegroundColor;
|
|
||||||
|
|
||||||
static async Task Main(string[] args)
|
static async Task Main(string[] args)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var host = Host.CreateDefaultBuilder(args)
|
var host = Host.CreateDefaultBuilder(args)
|
||||||
.UseEnvironment("Development")
|
.UseEnvironment("Dev")
|
||||||
.UseServiceProviderFactory(new Autofac.Extensions.DependencyInjection.AutofacServiceProviderFactory())
|
.UseServiceProviderFactory(new Autofac.Extensions.DependencyInjection.AutofacServiceProviderFactory())
|
||||||
.ConfigureContainer<ContainerBuilder>((hostContext, builder) =>
|
.ConfigureContainer<ContainerBuilder>((hostContext, builder) =>
|
||||||
{
|
{
|
||||||
var startup = new Startup();
|
var startup = new Startup();
|
||||||
startup.ConfigureContainer();
|
startup.ConfigureContainer(builder);
|
||||||
})
|
})
|
||||||
|
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
await host.StartAsync();
|
await host.StartAsync();
|
||||||
Console.WriteLine("Host has started.");
|
Console.WriteLine("Host has started.");
|
||||||
Run();
|
|
||||||
|
|
||||||
|
var console = host.Services.GetRequiredService<ConsoleService>();
|
||||||
|
console.Run();
|
||||||
|
|
||||||
await host.WaitForShutdownAsync();
|
await host.WaitForShutdownAsync();
|
||||||
}
|
}
|
||||||
|
|
@ -52,10 +54,26 @@ namespace PlanTempus.SetupInfrastructure
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Host failed to start: {ex}");
|
Console.WriteLine($"Host failed to start: {ex}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
public class ConsoleService
|
||||||
|
{
|
||||||
|
|
||||||
|
static ConsoleColor _backgroundColor = Console.BackgroundColor;
|
||||||
|
static ConsoleColor _foregroundColor = Console.ForegroundColor;
|
||||||
|
|
||||||
|
private readonly SetupDbAdmin _setupDbAdmin;
|
||||||
|
private readonly SetupIdentitySystem _setupIdentitySystem;
|
||||||
|
private readonly SetupConfiguration _setupConfiguration;
|
||||||
|
private readonly SetupApplicationUser _setupApplicationUser;
|
||||||
|
|
||||||
|
public ConsoleService(SetupDbAdmin setupDbAdmin, SetupIdentitySystem setupIdentitySystem, SetupConfiguration setupConfiguration, SetupApplicationUser setupApplicationUser)
|
||||||
|
{
|
||||||
|
_setupDbAdmin = setupDbAdmin;
|
||||||
|
_setupIdentitySystem = setupIdentitySystem;
|
||||||
|
_setupConfiguration = setupConfiguration;
|
||||||
|
_setupApplicationUser = setupApplicationUser;
|
||||||
|
}
|
||||||
static bool IsSuperAdmin()
|
static bool IsSuperAdmin()
|
||||||
{
|
{
|
||||||
//test db access
|
//test db access
|
||||||
|
|
@ -63,7 +81,8 @@ namespace PlanTempus.SetupInfrastructure
|
||||||
|
|
||||||
string query = @"SELECT usename, usesuper FROM pg_user WHERE usename = CURRENT_USER;";
|
string query = @"SELECT usename, usesuper FROM pg_user WHERE usename = CURRENT_USER;";
|
||||||
|
|
||||||
var conn = _container.Resolve<IDbConnection>();
|
var conn = new NpgsqlConnection();
|
||||||
|
|
||||||
var result = (dynamic)conn.QuerySql(query).Single();
|
var result = (dynamic)conn.QuerySql(query).Single();
|
||||||
|
|
||||||
string username = result.usename;
|
string username = result.usename;
|
||||||
|
|
@ -135,10 +154,8 @@ namespace PlanTempus.SetupInfrastructure
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void Run()
|
public void Run()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
Welcome();
|
Welcome();
|
||||||
string userPass;
|
string userPass;
|
||||||
|
|
||||||
|
|
@ -165,8 +182,8 @@ namespace PlanTempus.SetupInfrastructure
|
||||||
|
|
||||||
Console.Write("Database.Core.DCL.SetupDbAdmin...");
|
Console.Write("Database.Core.DCL.SetupDbAdmin...");
|
||||||
sw.Start();
|
sw.Start();
|
||||||
var setupDbAdmin = _container.Resolve<SetupDbAdmin>();
|
|
||||||
setupDbAdmin.With(new SetupDbAdmin.Command { Password = "3911", Schema = "system", User = "heimdall" });
|
_setupDbAdmin.With(new SetupDbAdmin.Command { Password = "3911", Schema = "system", User = "heimdall" });
|
||||||
Console.WriteLine($"DONE, took: {sw.ElapsedMilliseconds} ms");
|
Console.WriteLine($"DONE, took: {sw.ElapsedMilliseconds} ms");
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -178,16 +195,14 @@ namespace PlanTempus.SetupInfrastructure
|
||||||
//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("heimdall", "3911"));
|
//_container = new Startup().ConfigureContainer(new Startup.ConnectionStringTemplateParameters("heimdall", "3911"));
|
||||||
|
|
||||||
var setupIdentitySystem = _container.Resolve<SetupIdentitySystem>();
|
_setupIdentitySystem.With(new SetupIdentitySystem.Command { Schema = "system" });
|
||||||
setupIdentitySystem.With(new SetupIdentitySystem.Command { Schema = "system" });
|
|
||||||
Console.WriteLine($"DONE, took: {sw.ElapsedMilliseconds} ms");
|
Console.WriteLine($"DONE, took: {sw.ElapsedMilliseconds} ms");
|
||||||
|
|
||||||
Console.WriteLine("::");
|
Console.WriteLine("::");
|
||||||
Console.WriteLine("::");
|
Console.WriteLine("::");
|
||||||
Console.Write("Database.ConfigurationManagementSystem.SetupConfiguration...");
|
Console.Write("Database.ConfigurationManagementSystem.SetupConfiguration...");
|
||||||
sw.Restart();
|
sw.Restart();
|
||||||
var setupConfigurationSystem = _container.Resolve<SetupConfiguration>();
|
_setupConfiguration.With(new SetupConfiguration.Command());
|
||||||
setupConfigurationSystem.With(new SetupConfiguration.Command());
|
|
||||||
Console.Write($"DONE, took: {sw.ElapsedMilliseconds} ms");
|
Console.Write($"DONE, took: {sw.ElapsedMilliseconds} ms");
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -195,8 +210,8 @@ namespace PlanTempus.SetupInfrastructure
|
||||||
Console.WriteLine("::");
|
Console.WriteLine("::");
|
||||||
Console.Write("Database.Core.DCL.SetupApplicationUser...");
|
Console.Write("Database.Core.DCL.SetupApplicationUser...");
|
||||||
sw.Start();
|
sw.Start();
|
||||||
var setupApplicationUser = _container.Resolve<SetupApplicationUser>();
|
|
||||||
setupApplicationUser.With(new SetupApplicationUser.Command { Password = "3911", Schema = "system", User = "sathumper" });
|
_setupApplicationUser.With(new SetupApplicationUser.Command { Password = "3911", Schema = "system", User = "sathumper" });
|
||||||
Console.WriteLine($"DONE, took: {sw.ElapsedMilliseconds} ms");
|
Console.WriteLine($"DONE, took: {sw.ElapsedMilliseconds} ms");
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -207,7 +222,6 @@ namespace PlanTempus.SetupInfrastructure
|
||||||
Console.ForegroundColor = _foregroundColor;
|
Console.ForegroundColor = _foregroundColor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
|
|
|
||||||
|
|
@ -18,10 +18,10 @@ namespace PlanTempus.SetupInfrastructure
|
||||||
return configuration;
|
return configuration;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IContainer ConfigureContainer()
|
public void ConfigureContainer(ContainerBuilder builder)
|
||||||
{
|
{
|
||||||
|
|
||||||
var builder = new ContainerBuilder();
|
//var builder = new ContainerBuilder();
|
||||||
var configuration = Configuration();
|
var configuration = Configuration();
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -45,8 +45,10 @@ namespace PlanTempus.SetupInfrastructure
|
||||||
.AsClosedTypesOf(typeof(IDbConfigure<>))
|
.AsClosedTypesOf(typeof(IDbConfigure<>))
|
||||||
.AsSelf();
|
.AsSelf();
|
||||||
|
|
||||||
|
builder.RegisterType<ConsoleService>();
|
||||||
|
|
||||||
return builder.Build();
|
|
||||||
|
//return builder.Build();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,19 +3,20 @@ using System.Data;
|
||||||
using Insight.Database;
|
using Insight.Database;
|
||||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
using PlanTempus.Database.Core.Sql;
|
using PlanTempus.Database.Core.Sql;
|
||||||
|
using PlanTempus.Database.Core.ConnectionFactory;
|
||||||
|
|
||||||
namespace PlanTempus.Tests
|
namespace PlanTempus.Tests
|
||||||
{
|
{
|
||||||
[TestClass]
|
[TestClass]
|
||||||
public class PostgresTests : TestFixture
|
public class PostgresTests : TestFixture
|
||||||
{
|
{
|
||||||
Database.ModuleRegistry.IDbConnectionFactory _connFactory;
|
IDbConnectionFactory _connFactory;
|
||||||
IDatabaseOperations _databaseOperations;
|
IDatabaseOperations _databaseOperations;
|
||||||
|
|
||||||
[TestInitialize]
|
[TestInitialize]
|
||||||
public void MyTestMethod()
|
public void MyTestMethod()
|
||||||
{
|
{
|
||||||
_connFactory = Container.Resolve<Database.ModuleRegistry.IDbConnectionFactory>();
|
_connFactory = Container.Resolve<IDbConnectionFactory>();
|
||||||
_databaseOperations = Container.Resolve<IDatabaseOperations>();
|
_databaseOperations = Container.Resolve<IDatabaseOperations>();
|
||||||
}
|
}
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue