Setup db automation
This commit is contained in:
parent
1540f9f655
commit
72544d62e2
12 changed files with 473 additions and 337 deletions
49
Database/RolesPermissionSystem/NavigationSystem.cs
Normal file
49
Database/RolesPermissionSystem/NavigationSystem.cs
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
using Insight.Database;
|
||||
using System.Data;
|
||||
|
||||
namespace Database.RolesPermissionSystem
|
||||
{
|
||||
internal class NavigationSystem
|
||||
{
|
||||
|
||||
private readonly IDbConnection _db;
|
||||
|
||||
public NavigationSystem(IDbConnection db)
|
||||
{
|
||||
_db = db;
|
||||
}
|
||||
|
||||
|
||||
private async Task CreateNavigationLinkTemplatesTable(string schema)
|
||||
{
|
||||
var sql = $@"
|
||||
CREATE TABLE IF NOT EXISTS {schema}.navigation_link_templates (
|
||||
id SERIAL PRIMARY KEY,
|
||||
parent_id INTEGER NULL,
|
||||
url VARCHAR(500) NOT NULL,
|
||||
permission_id INTEGER NULL,
|
||||
icon VARCHAR(100) NULL,
|
||||
default_order INTEGER NOT NULL,
|
||||
FOREIGN KEY (permission_id) REFERENCES {schema}.permissions(id),
|
||||
FOREIGN KEY (parent_id) REFERENCES {schema}.navigation_link_templates(id)
|
||||
)";
|
||||
await _db.ExecuteAsync(sql);
|
||||
}
|
||||
|
||||
private async Task CreateNavigationLinkTemplateTranslationsTable(string schema)
|
||||
{
|
||||
var sql = $@"
|
||||
CREATE TABLE IF NOT EXISTS {schema}.navigation_link_template_translations (
|
||||
id SERIAL PRIMARY KEY,
|
||||
template_id INTEGER NOT NULL,
|
||||
language VARCHAR(10) NOT NULL,
|
||||
display_name VARCHAR(100) NOT NULL,
|
||||
FOREIGN KEY (template_id) REFERENCES {schema}.navigation_link_templates(id)
|
||||
)";
|
||||
await _db.ExecuteAsync(sql);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -5,87 +5,87 @@ using Insight.Database;
|
|||
|
||||
namespace Database.RolesPermissionSystem
|
||||
{
|
||||
public class Setup
|
||||
{
|
||||
private readonly IDbConnection _db;
|
||||
public class Setup
|
||||
{
|
||||
private readonly IDbConnection _db;
|
||||
|
||||
public Setup(IDbConnection db)
|
||||
{
|
||||
_db = db ?? throw new ArgumentNullException(nameof(db));
|
||||
}
|
||||
public Setup(IDbConnection db)
|
||||
{
|
||||
_db = db ?? throw new ArgumentNullException(nameof(db));
|
||||
}
|
||||
|
||||
/// <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 async Task CreateSystem(string schema)
|
||||
{
|
||||
if (!Validations.IsValidSchemaName(schema))
|
||||
throw new ArgumentException("Invalid schema name", nameof(schema));
|
||||
/// <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 CreateSystem(string schema)
|
||||
{
|
||||
if (!Validations.IsValidSchemaName(schema))
|
||||
throw new ArgumentException("Invalid schema name", nameof(schema));
|
||||
|
||||
using (var transaction = _db.BeginTransaction())
|
||||
{
|
||||
try
|
||||
{
|
||||
await CreateRolesTable(schema, transaction).ConfigureAwait(false);
|
||||
await CreatePermissionsTable(schema, transaction).ConfigureAwait(false);
|
||||
await CreatePermissionTypesTable(schema, transaction).ConfigureAwait(false);
|
||||
await CreateRolePermissionsTable(schema, transaction).ConfigureAwait(false);
|
||||
using (var transaction = _db.BeginTransaction())
|
||||
{
|
||||
try
|
||||
{
|
||||
CreateRolesTable(schema, transaction);
|
||||
CreatePermissionsTable(schema, transaction);
|
||||
CreatePermissionTypesTable(schema, transaction);
|
||||
CreateRolePermissionsTable(schema, transaction);
|
||||
|
||||
transaction.Commit();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw new InvalidOperationException("Failed to create system tables.", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
transaction.Commit();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw new InvalidOperationException("Failed to create system tables.", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private async Task ExecuteSqlAsync(string sql, IDbTransaction transaction)
|
||||
{
|
||||
if (string.IsNullOrEmpty(sql))
|
||||
throw new ArgumentNullException(nameof(sql));
|
||||
private void ExecuteSql(string sql, IDbTransaction transaction)
|
||||
{
|
||||
if (string.IsNullOrEmpty(sql))
|
||||
throw new ArgumentNullException(nameof(sql));
|
||||
|
||||
await _db.ExecuteAsync(sql, transaction: transaction).ConfigureAwait(false);
|
||||
}
|
||||
_db.Execute(sql, transaction: transaction);
|
||||
}
|
||||
|
||||
private async Task CreatePermissionTypesTable(string schema, IDbTransaction transaction)
|
||||
{
|
||||
var sql = $@"
|
||||
private void CreatePermissionTypesTable(string schema, IDbTransaction transaction)
|
||||
{
|
||||
var sql = $@"
|
||||
CREATE TABLE IF NOT EXISTS {schema}.permission_types (
|
||||
id SERIAL PRIMARY KEY,
|
||||
name VARCHAR(100) NOT NULL UNIQUE
|
||||
)";
|
||||
await ExecuteSqlAsync(sql, transaction).ConfigureAwait(false);
|
||||
}
|
||||
ExecuteSql(sql, transaction);
|
||||
}
|
||||
|
||||
private async Task CreatePermissionsTable(string schema, IDbTransaction transaction)
|
||||
{
|
||||
var sql = $@"
|
||||
private void CreatePermissionsTable(string schema, IDbTransaction transaction)
|
||||
{
|
||||
var sql = $@"
|
||||
CREATE TABLE IF NOT EXISTS {schema}.permissions (
|
||||
id SERIAL PRIMARY KEY,
|
||||
name VARCHAR(100) NOT NULL UNIQUE,
|
||||
type_id INTEGER NOT NULL,
|
||||
FOREIGN KEY (type_id) REFERENCES {schema}.permission_types(id)
|
||||
)";
|
||||
await ExecuteSqlAsync(sql, transaction).ConfigureAwait(false);
|
||||
}
|
||||
ExecuteSql(sql, transaction);
|
||||
}
|
||||
|
||||
private async Task CreateRolesTable(string schema, IDbTransaction transaction)
|
||||
{
|
||||
var sql = $@"
|
||||
private void CreateRolesTable(string schema, IDbTransaction transaction)
|
||||
{
|
||||
var sql = $@"
|
||||
CREATE TABLE IF NOT EXISTS {schema}.roles (
|
||||
id SERIAL PRIMARY KEY,
|
||||
name VARCHAR(100) NOT NULL UNIQUE
|
||||
)";
|
||||
await ExecuteSqlAsync(sql, transaction).ConfigureAwait(false);
|
||||
}
|
||||
ExecuteSql(sql, transaction);
|
||||
}
|
||||
|
||||
private async Task CreateRolePermissionsTable(string schema, IDbTransaction transaction)
|
||||
{
|
||||
var sql = $@"
|
||||
private void CreateRolePermissionsTable(string schema, IDbTransaction transaction)
|
||||
{
|
||||
var sql = $@"
|
||||
CREATE TABLE IF NOT EXISTS {schema}.role_permissions (
|
||||
role_id INTEGER NOT NULL,
|
||||
permission_id INTEGER NOT NULL,
|
||||
|
|
@ -93,7 +93,7 @@ namespace Database.RolesPermissionSystem
|
|||
FOREIGN KEY (role_id) REFERENCES {schema}.roles(id),
|
||||
FOREIGN KEY (permission_id) REFERENCES {schema}.permissions(id)
|
||||
)";
|
||||
await ExecuteSqlAsync(sql, transaction).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
ExecuteSql(sql, transaction);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue