feat(backend): add PostgreSQL schema, RLS policies, and multi-tenant middleware
- Add EF Core migrations for initial schema (clubs, members, work_items, shifts, shift_signups) - Implement RLS policies with SET LOCAL for tenant isolation - Add Finbuckle multi-tenant middleware with ClaimStrategy + HeaderStrategy fallback - Create TenantValidationMiddleware to enforce JWT claims match X-Tenant-Id header - Add tenant-aware DB interceptors (SaveChangesTenantInterceptor, TenantDbConnectionInterceptor) - Configure AppDbContext with tenant scoping and RLS support - Add test infrastructure: CustomWebApplicationFactory, TestAuthHandler, DatabaseFixture - Write TDD integration tests for multi-tenant isolation and RLS enforcement - Add health check null safety for connection string Tasks: 7 (PostgreSQL schema + migrations + RLS), 8 (Finbuckle multi-tenancy + validation), 12 (test infrastructure)
This commit is contained in:
@@ -0,0 +1,201 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace WorkClub.Infrastructure.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class InitialCreate : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "clubs",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
TenantId = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
|
||||
Name = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
|
||||
SportType = table.Column<int>(type: "integer", nullable: false),
|
||||
Description = table.Column<string>(type: "character varying(2000)", maxLength: 2000, nullable: true),
|
||||
CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
|
||||
UpdatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_clubs", x => x.Id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "members",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
TenantId = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
|
||||
ExternalUserId = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
|
||||
DisplayName = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
|
||||
Email = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
|
||||
Role = table.Column<int>(type: "integer", nullable: false),
|
||||
ClubId = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
|
||||
UpdatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_members", x => x.Id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "shift_signups",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
TenantId = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
|
||||
ShiftId = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
MemberId = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
SignedUpAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_shift_signups", x => x.Id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "shifts",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
TenantId = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
|
||||
Title = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
|
||||
Description = table.Column<string>(type: "character varying(2000)", maxLength: 2000, nullable: true),
|
||||
Location = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: true),
|
||||
StartTime = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
|
||||
EndTime = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
|
||||
Capacity = table.Column<int>(type: "integer", nullable: false, defaultValue: 1),
|
||||
ClubId = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
CreatedById = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
|
||||
UpdatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
|
||||
xmin = table.Column<uint>(type: "xid", rowVersion: true, nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_shifts", x => x.Id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "work_items",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
TenantId = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
|
||||
Title = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
|
||||
Description = table.Column<string>(type: "character varying(2000)", maxLength: 2000, nullable: true),
|
||||
Status = table.Column<int>(type: "integer", nullable: false),
|
||||
AssigneeId = table.Column<Guid>(type: "uuid", nullable: true),
|
||||
CreatedById = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
ClubId = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
DueDate = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
|
||||
CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
|
||||
UpdatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
|
||||
xmin = table.Column<uint>(type: "xid", rowVersion: true, nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_work_items", x => x.Id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_clubs_tenant_id",
|
||||
table: "clubs",
|
||||
column: "TenantId");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_members_club_id",
|
||||
table: "members",
|
||||
column: "ClubId");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_members_tenant_external_user",
|
||||
table: "members",
|
||||
columns: new[] { "TenantId", "ExternalUserId" });
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_members_tenant_id",
|
||||
table: "members",
|
||||
column: "TenantId");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_shift_signups_shift_id",
|
||||
table: "shift_signups",
|
||||
column: "ShiftId");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_shift_signups_shift_member",
|
||||
table: "shift_signups",
|
||||
columns: new[] { "ShiftId", "MemberId" },
|
||||
unique: true);
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_shift_signups_tenant_id",
|
||||
table: "shift_signups",
|
||||
column: "TenantId");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_shifts_club_id",
|
||||
table: "shifts",
|
||||
column: "ClubId");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_shifts_start_time",
|
||||
table: "shifts",
|
||||
column: "StartTime");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_shifts_tenant_id",
|
||||
table: "shifts",
|
||||
column: "TenantId");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_work_items_assignee_id",
|
||||
table: "work_items",
|
||||
column: "AssigneeId");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_work_items_club_id",
|
||||
table: "work_items",
|
||||
column: "ClubId");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_work_items_status",
|
||||
table: "work_items",
|
||||
column: "Status");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_work_items_tenant_id",
|
||||
table: "work_items",
|
||||
column: "TenantId");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "clubs");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "members");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "shift_signups");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "shifts");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "work_items");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user