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,70 +1,61 @@
|
|||
using Insight.Database;
|
||||
using PlanTempus.Database.Core;
|
||||
using PlanTempus.Database.Core.ConnectionFactory;
|
||||
using System.Data;
|
||||
|
||||
namespace PlanTempus.Database.Core.DDL
|
||||
{
|
||||
/// <summary>
|
||||
/// This is by purpose not async await
|
||||
/// It is intended that this is created with the correct Application User, which is why the schema name is omitted.
|
||||
/// </summary>
|
||||
public class SetupIdentitySystem : IDbConfigure<SetupIdentitySystem.Command>
|
||||
{
|
||||
public class Command
|
||||
{
|
||||
public required string Schema { get; init; }
|
||||
}
|
||||
/// <summary>
|
||||
/// This is by purpose not async await
|
||||
/// It is intended that this is created with the correct Application User, which is why the schema name is omitted.
|
||||
/// </summary>
|
||||
public class SetupIdentitySystem : IDbConfigure<SetupIdentitySystem.Command>
|
||||
{
|
||||
public class Command
|
||||
{
|
||||
public required string Schema { get; init; }
|
||||
}
|
||||
|
||||
readonly IDbConnection _db;
|
||||
IDbTransaction _transaction = null;
|
||||
Command _command;
|
||||
Command _command;
|
||||
private readonly IDbConnectionFactory _connectionFactory;
|
||||
|
||||
public SetupIdentitySystem(IDbConnection db)
|
||||
{
|
||||
_db = db;
|
||||
}
|
||||
public SetupIdentitySystem(IDbConnectionFactory connectionFactory)
|
||||
{
|
||||
_connectionFactory = connectionFactory;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates the system tables in the specified schema within a transaction.
|
||||
/// </summary>
|
||||
/// <param name="schema">The schema name where the tables will be created.</param>
|
||||
public void With(Command command)
|
||||
{
|
||||
_command = command;
|
||||
/// <summary>
|
||||
/// Creates the system tables in the specified schema within a transaction.
|
||||
/// </summary>
|
||||
/// <param name="schema">The schema name where the tables will be created.</param>
|
||||
public void With(Command command)
|
||||
{
|
||||
_command = command;
|
||||
|
||||
using (_transaction = _db.OpenWithTransaction())
|
||||
{
|
||||
try
|
||||
{
|
||||
CreateUsersTable();
|
||||
CreateOrganizationsTable();
|
||||
CreateUserOrganizationsTable();
|
||||
SetupRLS();
|
||||
using var conn = _connectionFactory.Create();
|
||||
using var transaction = conn.OpenWithTransaction();
|
||||
try
|
||||
{
|
||||
CreateUsersTable(conn);
|
||||
CreateOrganizationsTable(conn);
|
||||
CreateUserOrganizationsTable(conn);
|
||||
SetupRLS(conn);
|
||||
|
||||
_transaction.Commit();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_transaction.Rollback();
|
||||
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));
|
||||
transaction.Commit();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw new InvalidOperationException("Failed to SetupIdentitySystem. Transaction is rolled back", ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
_db.ExecuteSql(sql);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Creates the users table
|
||||
/// </summary>
|
||||
void CreateUsersTable()
|
||||
{
|
||||
var sql = @$"
|
||||
/// <summary>
|
||||
/// Creates the users table
|
||||
/// </summary>
|
||||
void CreateUsersTable(IDbConnection db)
|
||||
{
|
||||
var sql = @$"
|
||||
CREATE TABLE IF NOT EXISTS {_command.Schema}.users (
|
||||
id SERIAL PRIMARY KEY,
|
||||
email VARCHAR(256) NOT NULL UNIQUE,
|
||||
|
|
@ -79,16 +70,16 @@ namespace PlanTempus.Database.Core.DDL
|
|||
last_login_at TIMESTAMPTZ NULL
|
||||
);";
|
||||
|
||||
ExecuteSql(sql);
|
||||
db.ExecuteSql(sql);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates the organizations table
|
||||
/// </summary>
|
||||
void CreateOrganizationsTable()
|
||||
{
|
||||
var sql = @$"
|
||||
/// <summary>
|
||||
/// Creates the organizations table
|
||||
/// </summary>
|
||||
void CreateOrganizationsTable(IDbConnection db)
|
||||
{
|
||||
var sql = @$"
|
||||
CREATE TABLE IF NOT EXISTS {_command.Schema}.organizations (
|
||||
id SERIAL PRIMARY KEY,
|
||||
connection_string VARCHAR(500) NOT NULL,
|
||||
|
|
@ -97,16 +88,16 @@ namespace PlanTempus.Database.Core.DDL
|
|||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);";
|
||||
|
||||
ExecuteSql(sql);
|
||||
db.ExecuteSql(sql);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates the user_organizations table
|
||||
/// </summary>
|
||||
void CreateUserOrganizationsTable()
|
||||
{
|
||||
var sql = @$"
|
||||
/// <summary>
|
||||
/// Creates the user_organizations table
|
||||
/// </summary>
|
||||
void CreateUserOrganizationsTable(IDbConnection db)
|
||||
{
|
||||
var sql = @$"
|
||||
CREATE TABLE IF NOT EXISTS {_command.Schema}.user_organizations (
|
||||
user_id INTEGER NOT NULL REFERENCES {_command.Schema}.users(id),
|
||||
organization_id INTEGER NOT NULL REFERENCES {_command.Schema}.organizations(id),
|
||||
|
|
@ -115,37 +106,37 @@ namespace PlanTempus.Database.Core.DDL
|
|||
PRIMARY KEY (user_id, organization_id)
|
||||
);";
|
||||
|
||||
ExecuteSql(sql);
|
||||
db.ExecuteSql(sql);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets up Row Level Security (RLS) for the organizations and user_organizations tables.
|
||||
/// </summary>
|
||||
void SetupRLS()
|
||||
{
|
||||
var sql = new[]
|
||||
{
|
||||
$"ALTER TABLE {_command.Schema}.organizations ENABLE ROW LEVEL SECURITY;",
|
||||
$"ALTER TABLE {_command.Schema}.user_organizations ENABLE ROW LEVEL SECURITY;",
|
||||
$"DROP POLICY IF EXISTS organization_access ON {_command.Schema}.organizations;",
|
||||
@$"CREATE POLICY organization_access ON {_command.Schema}.organizations
|
||||
/// <summary>
|
||||
/// Sets up Row Level Security (RLS) for the organizations and user_organizations tables.
|
||||
/// </summary>
|
||||
void SetupRLS(IDbConnection db)
|
||||
{
|
||||
var sql = new[]
|
||||
{
|
||||
$"ALTER TABLE {_command.Schema}.organizations ENABLE ROW LEVEL SECURITY;",
|
||||
$"ALTER TABLE {_command.Schema}.user_organizations ENABLE ROW LEVEL SECURITY;",
|
||||
$"DROP POLICY IF EXISTS organization_access ON {_command.Schema}.organizations;",
|
||||
@$"CREATE POLICY organization_access ON {_command.Schema}.organizations
|
||||
USING (id IN (
|
||||
SELECT organization_id
|
||||
FROM {_command.Schema}.user_organizations
|
||||
WHERE user_id = current_setting('app.user_id', TRUE)::INTEGER
|
||||
)) WITH CHECK (true);",
|
||||
$"DROP POLICY IF EXISTS user_organization_access ON {_command.Schema}.user_organizations;",
|
||||
@$"CREATE POLICY user_organization_access ON {_command.Schema}.user_organizations
|
||||
$"DROP POLICY IF EXISTS user_organization_access ON {_command.Schema}.user_organizations;",
|
||||
@$"CREATE POLICY user_organization_access ON {_command.Schema}.user_organizations
|
||||
USING (user_id = current_setting('app.user_id', TRUE)::INTEGER) WITH CHECK (true);"
|
||||
};
|
||||
};
|
||||
|
||||
foreach (var statement in sql)
|
||||
{
|
||||
ExecuteSql(statement);
|
||||
}
|
||||
}
|
||||
foreach (var statement in sql)
|
||||
{
|
||||
db.ExecuteSql(statement);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue