using System; using Microsoft.EntityFrameworkCore.Migrations; #nullable disable namespace Socialize.Api.Migrations { /// public partial class AddOrganizations : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.AddColumn( 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(type: "uuid", nullable: false), Name = table.Column(type: "character varying(256)", maxLength: 256, nullable: false), Slug = table.Column(type: "character varying(128)", maxLength: 128, nullable: false), OwnerUserId = table.Column(type: "uuid", nullable: false), CreatedAt = table.Column(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(type: "uuid", nullable: false), OrganizationId = table.Column(type: "uuid", nullable: false), UserId = table.Column(type: "uuid", nullable: false), Role = table.Column(type: "character varying(64)", maxLength: 64, nullable: false), CreatedAt = table.Column(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); } /// 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"); } } }