using System;
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable
namespace PostFundManagement.Infrastructure.Database.Migrations
{
///
public partial class AddedTenant : Migration
{
///
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn(
name: "TenantId",
table: "Projects",
type: "bigint",
nullable: false,
defaultValue: 0L);
migrationBuilder.AddColumn(
name: "TenantId",
table: "Programmes",
type: "bigint",
nullable: false,
defaultValue: 0L);
migrationBuilder.AddColumn(
name: "TenantId",
table: "Portfolios",
type: "bigint",
nullable: false,
defaultValue: 0L);
migrationBuilder.AddColumn(
name: "TenantId",
table: "Notifications",
type: "bigint",
nullable: false,
defaultValue: 0L);
migrationBuilder.AddColumn(
name: "TenantId",
table: "Awards",
type: "bigint",
nullable: false,
defaultValue: 0L);
migrationBuilder.CreateTable(
name: "Tenants",
columns: table => new
{
Id = table.Column(type: "bigint", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
CreatedAt = table.Column(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
CreatedBy = table.Column(type: "uuid", nullable: false),
UpdatedAt = table.Column(type: "timestamp with time zone", nullable: true),
UpdatedBy = table.Column(type: "uuid", nullable: true),
Name = table.Column(type: "character varying(256)", maxLength: 256, nullable: false),
Status = table.Column(type: "integer", nullable: false, defaultValueSql: "1")
},
constraints: table =>
{
table.PrimaryKey("PK_Tenants", x => x.Id);
table.ForeignKey(
name: "FK_Tenants_Users_CreatedBy",
column: x => x.CreatedBy,
principalTable: "Users",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "FK_Tenants_Users_UpdatedBy",
column: x => x.UpdatedBy,
principalTable: "Users",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateTable(
name: "TenantOrganisations",
columns: table => new
{
Id = table.Column(type: "bigint", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
CreatedAt = table.Column(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
CreatedBy = table.Column(type: "uuid", nullable: false),
TenantId = table.Column(type: "bigint", nullable: false),
OrganisationId = table.Column(type: "bigint", nullable: false),
Status = table.Column(type: "integer", nullable: false, defaultValueSql: "1")
},
constraints: table =>
{
table.PrimaryKey("PK_TenantOrganisations", x => x.Id);
table.ForeignKey(
name: "FK_TenantOrganisations_Organisations_OrganisationId",
column: x => x.OrganisationId,
principalTable: "Organisations",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "FK_TenantOrganisations_Tenants_TenantId",
column: x => x.TenantId,
principalTable: "Tenants",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "FK_TenantOrganisations_Users_CreatedBy",
column: x => x.CreatedBy,
principalTable: "Users",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateTable(
name: "TenantUsers",
columns: table => new
{
Id = table.Column(type: "bigint", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
CreatedAt = table.Column(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
CreatedBy = table.Column(type: "uuid", nullable: false),
TenantId = table.Column(type: "bigint", nullable: false),
UserId = table.Column(type: "uuid", nullable: false),
Status = table.Column(type: "integer", nullable: false, defaultValueSql: "1")
},
constraints: table =>
{
table.PrimaryKey("PK_TenantUsers", x => x.Id);
table.ForeignKey(
name: "FK_TenantUsers_Tenants_TenantId",
column: x => x.TenantId,
principalTable: "Tenants",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "FK_TenantUsers_Users_CreatedBy",
column: x => x.CreatedBy,
principalTable: "Users",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "FK_TenantUsers_Users_UserId",
column: x => x.UserId,
principalTable: "Users",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateIndex(
name: "IX_Projects_TenantId",
table: "Projects",
column: "TenantId");
migrationBuilder.CreateIndex(
name: "IX_Programmes_TenantId",
table: "Programmes",
column: "TenantId");
migrationBuilder.CreateIndex(
name: "IX_Portfolios_TenantId",
table: "Portfolios",
column: "TenantId");
migrationBuilder.CreateIndex(
name: "IX_Notifications_TenantId",
table: "Notifications",
column: "TenantId");
migrationBuilder.CreateIndex(
name: "IX_Awards_TenantId",
table: "Awards",
column: "TenantId");
migrationBuilder.CreateIndex(
name: "IX_TenantOrganisations_CreatedBy",
table: "TenantOrganisations",
column: "CreatedBy");
migrationBuilder.CreateIndex(
name: "IX_TenantOrganisations_OrganisationId",
table: "TenantOrganisations",
column: "OrganisationId");
migrationBuilder.CreateIndex(
name: "IX_TenantOrganisations_TenantId",
table: "TenantOrganisations",
column: "TenantId");
migrationBuilder.CreateIndex(
name: "IX_Tenants_CreatedBy",
table: "Tenants",
column: "CreatedBy");
migrationBuilder.CreateIndex(
name: "IX_Tenants_UpdatedBy",
table: "Tenants",
column: "UpdatedBy");
migrationBuilder.CreateIndex(
name: "IX_TenantUsers_CreatedBy",
table: "TenantUsers",
column: "CreatedBy");
migrationBuilder.CreateIndex(
name: "IX_TenantUsers_TenantId",
table: "TenantUsers",
column: "TenantId");
migrationBuilder.CreateIndex(
name: "IX_TenantUsers_UserId",
table: "TenantUsers",
column: "UserId");
migrationBuilder.Sql(@"
DO $$
BEGIN
IF EXISTS (SELECT 1 FROM ""Users"" WHERE ""Id"" = '18caeb45-65f6-4103-9bda-ddf073a24de2'::uuid) THEN
INSERT INTO ""Tenants"" (""Id"", ""Name"", ""Status"", ""CreatedBy"", ""CreatedAt"")
OVERRIDING SYSTEM VALUE
VALUES (1, 'Default Tenant', 1, '18caeb45-65f6-4103-9bda-ddf073a24de2'::uuid, NOW())
ON CONFLICT (""Id"") DO NOTHING;
ELSE
RAISE EXCEPTION 'Target user 18caeb45-65f6-4103-9bda-ddf073a24de2 does not exist in Users table.';
END IF;
END $$;
");
migrationBuilder.Sql(@"SELECT setval(pg_get_serial_sequence('""Tenants""', 'Id'), GREATEST(coalesce(max(""Id""), 1), 1)) FROM ""Tenants"";");
migrationBuilder.Sql("UPDATE \"Notifications\" SET \"TenantId\" = 1 WHERE \"TenantId\" = 0;");
migrationBuilder.Sql("UPDATE \"Awards\" SET \"TenantId\" = 1 WHERE \"TenantId\" = 0;");
migrationBuilder.Sql("UPDATE \"Portfolios\" SET \"TenantId\" = 1 WHERE \"TenantId\" = 0;");
migrationBuilder.Sql("UPDATE \"Programmes\" SET \"TenantId\" = 1 WHERE \"TenantId\" = 0;");
migrationBuilder.Sql("UPDATE \"Projects\" SET \"TenantId\" = 1 WHERE \"TenantId\" = 0;");
migrationBuilder.CreateIndex(
name: "IX_TenantOrganisations_TenantId_OrganisationId",
table: "TenantOrganisations",
columns: ["TenantId", "OrganisationId"],
unique: true);
migrationBuilder.CreateIndex(
name: "IX_TenantUsers_TenantId_UserId",
table: "TenantUsers",
columns: ["TenantId", "UserId"],
unique: true);
migrationBuilder.AddForeignKey(
name: "FK_Awards_Tenants_TenantId",
table: "Awards",
column: "TenantId",
principalTable: "Tenants",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
migrationBuilder.AddForeignKey(
name: "FK_Notifications_Tenants_TenantId",
table: "Notifications",
column: "TenantId",
principalTable: "Tenants",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
migrationBuilder.AddForeignKey(
name: "FK_Portfolios_Tenants_TenantId",
table: "Portfolios",
column: "TenantId",
principalTable: "Tenants",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
migrationBuilder.AddForeignKey(
name: "FK_Programmes_Tenants_TenantId",
table: "Programmes",
column: "TenantId",
principalTable: "Tenants",
principalColumn: "Id");
migrationBuilder.AddForeignKey(
name: "FK_Projects_Tenants_TenantId",
table: "Projects",
column: "TenantId",
principalTable: "Tenants",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
}
///
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Awards_Tenants_TenantId",
table: "Awards");
migrationBuilder.DropForeignKey(
name: "FK_Notifications_Tenants_TenantId",
table: "Notifications");
migrationBuilder.DropForeignKey(
name: "FK_Portfolios_Tenants_TenantId",
table: "Portfolios");
migrationBuilder.DropForeignKey(
name: "FK_Programmes_Tenants_TenantId",
table: "Programmes");
migrationBuilder.DropForeignKey(
name: "FK_Projects_Tenants_TenantId",
table: "Projects");
migrationBuilder.DropTable(
name: "TenantOrganisations");
migrationBuilder.DropTable(
name: "TenantUsers");
migrationBuilder.DropTable(
name: "Tenants");
migrationBuilder.DropIndex(
name: "IX_TenantOrganisations_TenantId_OrganisationId",
table: "TenantOrganisations");
migrationBuilder.DropIndex(
name: "IX_TenantUsers_TenantId_UserId",
table: "TenantUsers");
migrationBuilder.DropIndex(
name: "IX_Projects_TenantId",
table: "Projects");
migrationBuilder.DropIndex(
name: "IX_Programmes_TenantId",
table: "Programmes");
migrationBuilder.DropIndex(
name: "IX_Portfolios_TenantId",
table: "Portfolios");
migrationBuilder.DropIndex(
name: "IX_Notifications_TenantId",
table: "Notifications");
migrationBuilder.DropIndex(
name: "IX_Awards_TenantId",
table: "Awards");
migrationBuilder.DropColumn(
name: "TenantId",
table: "Projects");
migrationBuilder.DropColumn(
name: "TenantId",
table: "Programmes");
migrationBuilder.DropColumn(
name: "TenantId",
table: "Portfolios");
migrationBuilder.DropColumn(
name: "TenantId",
table: "Notifications");
migrationBuilder.DropColumn(
name: "TenantId",
table: "Awards");
}
}
}