Test and Run DbSetup, succes
This commit is contained in:
parent
db09261768
commit
2e0b20a53e
8 changed files with 102 additions and 76 deletions
|
|
@ -1,12 +1,9 @@
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace Core.Configurations.PostgresqlConfigurationBuilder
|
namespace Core.Configurations.PostgresqlConfigurationBuilder
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// LISTEN / NOTIFY in Postgresql
|
||||||
|
/// </summary>
|
||||||
public static class PostgresConfigurationExtensions
|
public static class PostgresConfigurationExtensions
|
||||||
{
|
{
|
||||||
public static IConfigurationBuilder AddPostgresConfiguration(
|
public static IConfigurationBuilder AddPostgresConfiguration(
|
||||||
|
|
|
||||||
|
|
@ -6,15 +6,16 @@ namespace Database.Identity
|
||||||
public class DbSetup
|
public class DbSetup
|
||||||
{
|
{
|
||||||
private readonly IDbConnection _db;
|
private readonly IDbConnection _db;
|
||||||
|
string _schema;
|
||||||
|
|
||||||
public DbSetup(IDbConnection db)
|
public DbSetup(IDbConnection db)
|
||||||
{
|
{
|
||||||
_db = db;
|
_db = db;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void CreateDatabase()
|
public async Task CreateDatabase(string schema)
|
||||||
{
|
{
|
||||||
var schema = "dev";
|
_schema = schema;
|
||||||
|
|
||||||
if (_db.State != ConnectionState.Open)
|
if (_db.State != ConnectionState.Open)
|
||||||
_db.Open();
|
_db.Open();
|
||||||
|
|
@ -22,50 +23,10 @@ namespace Database.Identity
|
||||||
using var transaction = _db.BeginTransaction();
|
using var transaction = _db.BeginTransaction();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Create tables
|
await CreateUserTable();
|
||||||
_db.Execute(@$"
|
await CreateTenantTable();
|
||||||
CREATE TABLE IF NOT EXISTS {schema}.users (
|
await CreateUserTenantTable();
|
||||||
id SERIAL PRIMARY KEY,
|
await SetupRLS();
|
||||||
email VARCHAR(256) NOT NULL UNIQUE,
|
|
||||||
password_hash VARCHAR(256) NOT NULL,
|
|
||||||
security_stamp VARCHAR(36) NOT NULL,
|
|
||||||
email_confirmed BOOLEAN NOT NULL DEFAULT FALSE,
|
|
||||||
created_date TIMESTAMP NOT NULL,
|
|
||||||
last_login_date TIMESTAMP NULL
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS {schema}.tenants (
|
|
||||||
id SERIAL PRIMARY KEY,
|
|
||||||
connection_string VARCHAR(500) NOT NULL,
|
|
||||||
created_date TIMESTAMP NOT NULL,
|
|
||||||
created_by INTEGER REFERENCES users(id),
|
|
||||||
is_active BOOLEAN DEFAULT true
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS {schema}.user_tenants (
|
|
||||||
user_id INTEGER REFERENCES users(id),
|
|
||||||
tenant_id INTEGER REFERENCES tenants(id),
|
|
||||||
created_date TIMESTAMP NOT NULL,
|
|
||||||
PRIMARY KEY (user_id, tenant_id)
|
|
||||||
);
|
|
||||||
|
|
||||||
-- Enable RLS på både tenants og user_tenants
|
|
||||||
ALTER TABLE {schema}.tenants ENABLE ROW LEVEL SECURITY;
|
|
||||||
ALTER TABLE {schema}.user_tenants ENABLE ROW LEVEL SECURITY;
|
|
||||||
|
|
||||||
-- RLS policy for tenants
|
|
||||||
DROP POLICY IF EXISTS tenant_access ON {schema}.tenants;
|
|
||||||
CREATE POLICY tenant_access ON {schema}.tenants
|
|
||||||
USING (id IN (
|
|
||||||
SELECT tenant_id
|
|
||||||
FROM {schema}.user_tenants
|
|
||||||
WHERE user_id = current_setting('app.user_id', TRUE)::INTEGER
|
|
||||||
));
|
|
||||||
|
|
||||||
-- RLS policy for user_tenants
|
|
||||||
DROP POLICY IF EXISTS user_tenant_access ON {schema}.user_tenants;
|
|
||||||
CREATE POLICY user_tenant_access ON {schema}.user_tenants
|
|
||||||
USING (user_id = current_setting('app.user_id', TRUE)::INTEGER);");
|
|
||||||
|
|
||||||
transaction.Commit();
|
transaction.Commit();
|
||||||
}
|
}
|
||||||
|
|
@ -75,5 +36,61 @@ namespace Database.Identity
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async Task CreateUserTable()
|
||||||
|
{
|
||||||
|
await _db.ExecuteSqlAsync(@$"
|
||||||
|
CREATE TABLE IF NOT EXISTS {_schema}.users (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
email VARCHAR(256) NOT NULL UNIQUE,
|
||||||
|
password_hash VARCHAR(256) NOT NULL,
|
||||||
|
security_stamp VARCHAR(36) NOT NULL,
|
||||||
|
email_confirmed BOOLEAN NOT NULL DEFAULT FALSE,
|
||||||
|
created_date TIMESTAMP NOT NULL,
|
||||||
|
last_login_date TIMESTAMP NULL
|
||||||
|
);");
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task CreateTenantTable()
|
||||||
|
{
|
||||||
|
await _db.ExecuteSqlAsync(@$"
|
||||||
|
CREATE TABLE IF NOT EXISTS {_schema}.tenants (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
connection_string VARCHAR(500) NOT NULL,
|
||||||
|
created_date TIMESTAMP NOT NULL,
|
||||||
|
created_by INTEGER REFERENCES {_schema}.users(id),
|
||||||
|
is_active BOOLEAN DEFAULT true
|
||||||
|
);");
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task CreateUserTenantTable()
|
||||||
|
{
|
||||||
|
await _db.ExecuteSqlAsync(@$"
|
||||||
|
CREATE TABLE IF NOT EXISTS {_schema}.user_tenants (
|
||||||
|
user_id INTEGER REFERENCES {_schema}.users(id),
|
||||||
|
tenant_id INTEGER REFERENCES {_schema}.tenants(id),
|
||||||
|
created_date TIMESTAMP NOT NULL,
|
||||||
|
PRIMARY KEY (user_id, tenant_id)
|
||||||
|
);");
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task SetupRLS()
|
||||||
|
{
|
||||||
|
await _db.ExecuteSqlAsync(@$"
|
||||||
|
ALTER TABLE {_schema}.tenants ENABLE ROW LEVEL SECURITY;
|
||||||
|
ALTER TABLE {_schema}.user_tenants ENABLE ROW LEVEL SECURITY;
|
||||||
|
|
||||||
|
DROP POLICY IF EXISTS tenant_access ON {_schema}.tenants;
|
||||||
|
CREATE POLICY tenant_access ON {_schema}.tenants
|
||||||
|
USING (id IN (
|
||||||
|
SELECT tenant_id
|
||||||
|
FROM {_schema}.user_tenants
|
||||||
|
WHERE user_id = current_setting('app.user_id', TRUE)::INTEGER
|
||||||
|
));
|
||||||
|
|
||||||
|
DROP POLICY IF EXISTS user_tenant_access ON {_schema}.user_tenants;
|
||||||
|
CREATE POLICY user_tenant_access ON {_schema}.user_tenants
|
||||||
|
USING (user_id = current_setting('app.user_id', TRUE)::INTEGER);");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,9 +17,9 @@ namespace Database.Tenants
|
||||||
public async Task CreateTenantInDatabase(string schema, string user, string password)
|
public async Task CreateTenantInDatabase(string schema, string user, string password)
|
||||||
{
|
{
|
||||||
if (!Regex.IsMatch(schema, "^[a-zA-Z0-9_]+$"))
|
if (!Regex.IsMatch(schema, "^[a-zA-Z0-9_]+$"))
|
||||||
{
|
|
||||||
throw new ArgumentException("Invalid schema name");
|
throw new ArgumentException("Invalid schema name");
|
||||||
}
|
|
||||||
|
|
||||||
await CreateUser(user, password);
|
await CreateUser(user, password);
|
||||||
await CreateSchema(schema);
|
await CreateSchema(schema);
|
||||||
await CreateRolesTable(schema);
|
await CreateRolesTable(schema);
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Application", "Application\
|
||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Database", "Database\Database.csproj", "{D5096A7F-E6D4-4C87-874E-2D9A6BEAD57F}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Database", "Database\Database.csproj", "{D5096A7F-E6D4-4C87-874E-2D9A6BEAD57F}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestPostgresLISTEN", "TestPostgresLISTEN\TestPostgresLISTEN.csproj", "{743EF625-6C74-419C-A492-AA069956F471}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestPostgresql", "TestPostgresLISTEN\TestPostgresql.csproj", "{743EF625-6C74-419C-A492-AA069956F471}"
|
||||||
EndProject
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
|
@ -46,3 +46,4 @@ Global
|
||||||
SolutionGuid = {AF20C396-63E0-48AE-A4EA-5D24A20C4845}
|
SolutionGuid = {AF20C396-63E0-48AE-A4EA-5D24A20C4845}
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
EndGlobal
|
EndGlobal
|
||||||
|
Global
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\Application\Application.csproj" />
|
<ProjectReference Include="..\Application\Application.csproj" />
|
||||||
|
<ProjectReference Include="..\Database\Database.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
|
||||||
|
|
@ -19,8 +19,8 @@ namespace Tests
|
||||||
|
|
||||||
conn.ExecuteSql("SELECT 1 as p");
|
conn.ExecuteSql("SELECT 1 as p");
|
||||||
|
|
||||||
var sql = "SELECT * FROM swp.foo";
|
//var sql = "SELECT * FROM swp.foo";
|
||||||
var customers = conn.Query(sql, commandType:CommandType.Text);
|
//var customers = conn.Query(sql, commandType:CommandType.Text);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -30,9 +30,19 @@ namespace Tests
|
||||||
var conn = Container.Resolve<IDbConnection>();
|
var conn = Container.Resolve<IDbConnection>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task TryDbSetup()
|
||||||
|
{
|
||||||
|
var conn = Container.Resolve<IDbConnection>();
|
||||||
|
|
||||||
|
var dbSetup = new Database.Identity.DbSetup(conn);
|
||||||
|
await dbSetup.CreateDatabase("swp");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void MyTestMethod()
|
public void SetupPostgresql_LISTEN()
|
||||||
{
|
{
|
||||||
var builder = new ConfigurationBuilder()
|
var builder = new ConfigurationBuilder()
|
||||||
.AddPostgresConfiguration(options =>
|
.AddPostgresConfiguration(options =>
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"AllowedHosts": "*",
|
"AllowedHosts": "*",
|
||||||
"ConnectionStrings": {
|
"ConnectionStrings": {
|
||||||
"ptdb": "Host=192.168.1.57;Port=5432;Database=ptdb01;User Id=sathumper;Password=3911;"
|
"ptdb": "Host=192.168.1.57;Port=5432;Database=ptdb01;User Id=postgres;Password=3911;"
|
||||||
},
|
},
|
||||||
"ApplicationInsights": {
|
"ApplicationInsights": {
|
||||||
"ConnectionString": "InstrumentationKey=6d2e76ee-5343-4691-a5e3-81add43cb584;IngestionEndpoint=https://northeurope-0.in.applicationinsights.azure.com/"
|
"ConnectionString": "InstrumentationKey=6d2e76ee-5343-4691-a5e3-81add43cb584;IngestionEndpoint=https://northeurope-0.in.applicationinsights.azure.com/"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue