134 lines
5.5 KiB
C#
134 lines
5.5 KiB
C#
using System;
|
|
using Microsoft.EntityFrameworkCore.Migrations;
|
|
|
|
#nullable disable
|
|
|
|
namespace Socialize.Api.Migrations
|
|
{
|
|
/// <inheritdoc />
|
|
public partial class AddOrganizations : Migration
|
|
{
|
|
/// <inheritdoc />
|
|
protected override void Up(MigrationBuilder migrationBuilder)
|
|
{
|
|
migrationBuilder.AddColumn<Guid>(
|
|
name: "OrganizationId",
|
|
table: "Workspaces",
|
|
type: "uuid",
|
|
nullable: false,
|
|
defaultValue: new Guid("00000000-0000-0000-0000-000000000000"));
|
|
|
|
migrationBuilder.CreateTable(
|
|
name: "Organizations",
|
|
columns: table => new
|
|
{
|
|
Id = table.Column<Guid>(type: "uuid", nullable: false),
|
|
Name = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: false),
|
|
Slug = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: false),
|
|
OwnerUserId = table.Column<Guid>(type: "uuid", nullable: false),
|
|
CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP")
|
|
},
|
|
constraints: table =>
|
|
{
|
|
table.PrimaryKey("PK_Organizations", x => x.Id);
|
|
});
|
|
|
|
migrationBuilder.CreateTable(
|
|
name: "OrganizationMemberships",
|
|
columns: table => new
|
|
{
|
|
Id = table.Column<Guid>(type: "uuid", nullable: false),
|
|
OrganizationId = table.Column<Guid>(type: "uuid", nullable: false),
|
|
UserId = table.Column<Guid>(type: "uuid", nullable: false),
|
|
Role = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
|
|
CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP")
|
|
},
|
|
constraints: table =>
|
|
{
|
|
table.PrimaryKey("PK_OrganizationMemberships", x => x.Id);
|
|
table.ForeignKey(
|
|
name: "FK_OrganizationMemberships_Organizations_OrganizationId",
|
|
column: x => x.OrganizationId,
|
|
principalTable: "Organizations",
|
|
principalColumn: "Id",
|
|
onDelete: ReferentialAction.Cascade);
|
|
});
|
|
|
|
migrationBuilder.Sql(
|
|
"""
|
|
INSERT INTO "Organizations" ("Id", "Name", "Slug", "OwnerUserId", "CreatedAt")
|
|
VALUES ('99999999-9999-9999-9999-999999999999', 'Northstar Collective', 'northstar-collective', 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', CURRENT_TIMESTAMP);
|
|
|
|
UPDATE "Workspaces"
|
|
SET "OrganizationId" = '99999999-9999-9999-9999-999999999999'
|
|
WHERE "OrganizationId" = '00000000-0000-0000-0000-000000000000';
|
|
|
|
INSERT INTO "OrganizationMemberships" ("Id", "OrganizationId", "UserId", "Role", "CreatedAt")
|
|
VALUES ('99999999-9999-9999-9999-000000000001', '99999999-9999-9999-9999-999999999999', 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'Owner', CURRENT_TIMESTAMP);
|
|
""");
|
|
|
|
migrationBuilder.CreateIndex(
|
|
name: "IX_Workspaces_OrganizationId",
|
|
table: "Workspaces",
|
|
column: "OrganizationId");
|
|
|
|
migrationBuilder.CreateIndex(
|
|
name: "IX_OrganizationMemberships_OrganizationId",
|
|
table: "OrganizationMemberships",
|
|
column: "OrganizationId");
|
|
|
|
migrationBuilder.CreateIndex(
|
|
name: "IX_OrganizationMemberships_OrganizationId_UserId",
|
|
table: "OrganizationMemberships",
|
|
columns: new[] { "OrganizationId", "UserId" },
|
|
unique: true);
|
|
|
|
migrationBuilder.CreateIndex(
|
|
name: "IX_OrganizationMemberships_UserId",
|
|
table: "OrganizationMemberships",
|
|
column: "UserId");
|
|
|
|
migrationBuilder.CreateIndex(
|
|
name: "IX_Organizations_OwnerUserId",
|
|
table: "Organizations",
|
|
column: "OwnerUserId");
|
|
|
|
migrationBuilder.CreateIndex(
|
|
name: "IX_Organizations_Slug",
|
|
table: "Organizations",
|
|
column: "Slug",
|
|
unique: true);
|
|
|
|
migrationBuilder.AddForeignKey(
|
|
name: "FK_Workspaces_Organizations_OrganizationId",
|
|
table: "Workspaces",
|
|
column: "OrganizationId",
|
|
principalTable: "Organizations",
|
|
principalColumn: "Id",
|
|
onDelete: ReferentialAction.Restrict);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
protected override void Down(MigrationBuilder migrationBuilder)
|
|
{
|
|
migrationBuilder.DropForeignKey(
|
|
name: "FK_Workspaces_Organizations_OrganizationId",
|
|
table: "Workspaces");
|
|
|
|
migrationBuilder.DropTable(
|
|
name: "OrganizationMemberships");
|
|
|
|
migrationBuilder.DropTable(
|
|
name: "Organizations");
|
|
|
|
migrationBuilder.DropIndex(
|
|
name: "IX_Workspaces_OrganizationId",
|
|
table: "Workspaces");
|
|
|
|
migrationBuilder.DropColumn(
|
|
name: "OrganizationId",
|
|
table: "Workspaces");
|
|
}
|
|
}
|
|
}
|