Files
mic-check/src/api/MicCheck.Api/Migrations/20260408214714_InitialCreate.cs
James Wampler 6887d09f9c version-0.1 (#1)
Squash and merge of version-0.1

Co-authored-by: James Wampler <james@wamp.dev>
Co-committed-by: James Wampler <james@wamp.dev>
2026-07-01 13:30:07 -07:00

648 lines
31 KiB
C#
Executable File

using System;
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable
namespace MicCheck.Api.Migrations
{
/// <inheritdoc />
public partial class InitialCreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "AuditLogs",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
ResourceType = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
ResourceId = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
Action = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
Changes = table.Column<string>(type: "text", nullable: true),
OrganizationId = table.Column<int>(type: "integer", nullable: false),
ProjectId = table.Column<int>(type: "integer", nullable: true),
EnvironmentId = table.Column<int>(type: "integer", nullable: true),
ActorUserId = table.Column<int>(type: "integer", nullable: true),
CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_AuditLogs", x => x.Id);
});
migrationBuilder.CreateTable(
name: "FeatureSegments",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
FeatureId = table.Column<int>(type: "integer", nullable: false),
SegmentId = table.Column<int>(type: "integer", nullable: false),
EnvironmentId = table.Column<int>(type: "integer", nullable: false),
Priority = table.Column<int>(type: "integer", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_FeatureSegments", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Identities",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
Identifier = table.Column<string>(type: "character varying(2000)", maxLength: 2000, nullable: false),
EnvironmentId = table.Column<int>(type: "integer", nullable: false),
CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Identities", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Organizations",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
Name = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Organizations", x => x.Id);
});
migrationBuilder.CreateTable(
name: "UserProjectPermissions",
columns: table => new
{
UserId = table.Column<int>(type: "integer", nullable: false),
ProjectId = table.Column<int>(type: "integer", nullable: false),
Permissions = table.Column<string>(type: "text", nullable: false),
IsAdmin = table.Column<bool>(type: "boolean", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_UserProjectPermissions", x => new { x.UserId, x.ProjectId });
});
migrationBuilder.CreateTable(
name: "Users",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
Email = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: false),
PasswordHash = table.Column<string>(type: "text", nullable: false),
FirstName = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
LastName = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
IsActive = table.Column<bool>(type: "boolean", nullable: false),
IsTwoFactorEnabled = table.Column<bool>(type: "boolean", nullable: false),
CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
LastLoginAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Users", x => x.Id);
});
migrationBuilder.CreateTable(
name: "WebhookDeliveryLogs",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
WebhookId = table.Column<int>(type: "integer", nullable: false),
EventType = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
PayloadJson = table.Column<string>(type: "text", nullable: false),
ResponseStatusCode = table.Column<int>(type: "integer", nullable: true),
ResponseBody = table.Column<string>(type: "text", nullable: true),
Success = table.Column<bool>(type: "boolean", nullable: false),
ErrorMessage = table.Column<string>(type: "text", nullable: true),
AttemptedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
Duration = table.Column<TimeSpan>(type: "interval", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_WebhookDeliveryLogs", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Webhooks",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
Url = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: false),
Secret = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: true),
Scope = table.Column<int>(type: "integer", nullable: false),
EnvironmentId = table.Column<int>(type: "integer", nullable: true),
OrganizationId = table.Column<int>(type: "integer", nullable: true),
Enabled = table.Column<bool>(type: "boolean", nullable: false),
CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Webhooks", x => x.Id);
});
migrationBuilder.CreateTable(
name: "IdentityTraits",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
IdentityId = table.Column<int>(type: "integer", nullable: false),
Key = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
Value = table.Column<string>(type: "character varying(2000)", maxLength: 2000, nullable: false),
ValueType = table.Column<int>(type: "integer", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_IdentityTraits", x => x.Id);
table.ForeignKey(
name: "FK_IdentityTraits_Identities_IdentityId",
column: x => x.IdentityId,
principalTable: "Identities",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "ApiKeys",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
Key = table.Column<string>(type: "character varying(512)", maxLength: 512, nullable: false),
Prefix = table.Column<string>(type: "character varying(8)", maxLength: 8, nullable: false),
Name = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
OrganizationId = table.Column<int>(type: "integer", nullable: false),
IsActive = table.Column<bool>(type: "boolean", nullable: false),
ExpiresAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_ApiKeys", x => x.Id);
table.ForeignKey(
name: "FK_ApiKeys_Organizations_OrganizationId",
column: x => x.OrganizationId,
principalTable: "Organizations",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "Projects",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
Name = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
OrganizationId = table.Column<int>(type: "integer", nullable: false),
CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
HideDisabledFlags = table.Column<bool>(type: "boolean", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Projects", x => x.Id);
table.ForeignKey(
name: "FK_Projects_Organizations_OrganizationId",
column: x => x.OrganizationId,
principalTable: "Organizations",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "OrganizationUsers",
columns: table => new
{
OrganizationId = table.Column<int>(type: "integer", nullable: false),
UserId = table.Column<int>(type: "integer", nullable: false),
Role = table.Column<int>(type: "integer", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_OrganizationUsers", x => new { x.OrganizationId, x.UserId });
table.ForeignKey(
name: "FK_OrganizationUsers_Organizations_OrganizationId",
column: x => x.OrganizationId,
principalTable: "Organizations",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_OrganizationUsers_Users_UserId",
column: x => x.UserId,
principalTable: "Users",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "RefreshTokens",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
Token = table.Column<string>(type: "character varying(512)", maxLength: 512, nullable: false),
UserId = table.Column<int>(type: "integer", nullable: false),
ExpiresAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
IsRevoked = table.Column<bool>(type: "boolean", nullable: false),
CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_RefreshTokens", x => x.Id);
table.ForeignKey(
name: "FK_RefreshTokens_Users_UserId",
column: x => x.UserId,
principalTable: "Users",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "Environments",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
Name = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
ApiKey = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
ProjectId = table.Column<int>(type: "integer", nullable: false),
CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Environments", x => x.Id);
table.ForeignKey(
name: "FK_Environments_Projects_ProjectId",
column: x => x.ProjectId,
principalTable: "Projects",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "Features",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
Name = table.Column<string>(type: "character varying(150)", maxLength: 150, nullable: false),
Type = table.Column<int>(type: "integer", nullable: false),
InitialValue = table.Column<string>(type: "character varying(20000)", maxLength: 20000, nullable: true),
Description = table.Column<string>(type: "character varying(2000)", maxLength: 2000, nullable: true),
DefaultEnabled = table.Column<bool>(type: "boolean", nullable: false),
ProjectId = table.Column<int>(type: "integer", nullable: false),
CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Features", x => x.Id);
table.ForeignKey(
name: "FK_Features_Projects_ProjectId",
column: x => x.ProjectId,
principalTable: "Projects",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "Segments",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
Name = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
ProjectId = table.Column<int>(type: "integer", nullable: false),
CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Segments", x => x.Id);
table.ForeignKey(
name: "FK_Segments_Projects_ProjectId",
column: x => x.ProjectId,
principalTable: "Projects",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "FeatureStates",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
FeatureId = table.Column<int>(type: "integer", nullable: false),
EnvironmentId = table.Column<int>(type: "integer", nullable: false),
IdentityId = table.Column<int>(type: "integer", nullable: true),
Enabled = table.Column<bool>(type: "boolean", nullable: false),
Value = table.Column<string>(type: "character varying(20000)", maxLength: 20000, nullable: true),
FeatureSegmentId = table.Column<int>(type: "integer", nullable: true),
CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
UpdatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
Version = table.Column<int>(type: "integer", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_FeatureStates", x => x.Id);
table.ForeignKey(
name: "FK_FeatureStates_Environments_EnvironmentId",
column: x => x.EnvironmentId,
principalTable: "Environments",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_FeatureStates_FeatureSegments_FeatureSegmentId",
column: x => x.FeatureSegmentId,
principalTable: "FeatureSegments",
principalColumn: "Id",
onDelete: ReferentialAction.SetNull);
table.ForeignKey(
name: "FK_FeatureStates_Features_FeatureId",
column: x => x.FeatureId,
principalTable: "Features",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_FeatureStates_Identities_IdentityId",
column: x => x.IdentityId,
principalTable: "Identities",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "Tags",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
Label = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
Color = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false),
ProjectId = table.Column<int>(type: "integer", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Tags", x => x.Id);
table.ForeignKey(
name: "FK_Tags_Features_ProjectId",
column: x => x.ProjectId,
principalTable: "Features",
principalColumn: "Id");
});
migrationBuilder.CreateTable(
name: "SegmentRules",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
SegmentId = table.Column<int>(type: "integer", nullable: false),
ParentRuleId = table.Column<int>(type: "integer", nullable: true),
Type = table.Column<int>(type: "integer", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_SegmentRules", x => x.Id);
table.ForeignKey(
name: "FK_SegmentRules_SegmentRules_ParentRuleId",
column: x => x.ParentRuleId,
principalTable: "SegmentRules",
principalColumn: "Id");
table.ForeignKey(
name: "FK_SegmentRules_Segments_SegmentId",
column: x => x.SegmentId,
principalTable: "Segments",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "SegmentConditions",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
RuleId = table.Column<int>(type: "integer", nullable: false),
Property = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
Operator = table.Column<int>(type: "integer", nullable: false),
Value = table.Column<string>(type: "character varying(1000)", maxLength: 1000, nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_SegmentConditions", x => x.Id);
table.ForeignKey(
name: "FK_SegmentConditions_SegmentRules_RuleId",
column: x => x.RuleId,
principalTable: "SegmentRules",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_ApiKeys_Key",
table: "ApiKeys",
column: "Key",
unique: true);
migrationBuilder.CreateIndex(
name: "IX_ApiKeys_OrganizationId",
table: "ApiKeys",
column: "OrganizationId");
migrationBuilder.CreateIndex(
name: "IX_AuditLogs_OrganizationId_CreatedAt",
table: "AuditLogs",
columns: new[] { "OrganizationId", "CreatedAt" });
migrationBuilder.CreateIndex(
name: "IX_Environments_ApiKey",
table: "Environments",
column: "ApiKey",
unique: true);
migrationBuilder.CreateIndex(
name: "IX_Environments_ProjectId",
table: "Environments",
column: "ProjectId");
migrationBuilder.CreateIndex(
name: "IX_Features_ProjectId_Name",
table: "Features",
columns: new[] { "ProjectId", "Name" },
unique: true);
migrationBuilder.CreateIndex(
name: "IX_FeatureStates_EnvironmentId",
table: "FeatureStates",
column: "EnvironmentId");
migrationBuilder.CreateIndex(
name: "IX_FeatureStates_FeatureId_EnvironmentId_IdentityId",
table: "FeatureStates",
columns: new[] { "FeatureId", "EnvironmentId", "IdentityId" },
unique: true);
migrationBuilder.CreateIndex(
name: "IX_FeatureStates_FeatureSegmentId",
table: "FeatureStates",
column: "FeatureSegmentId",
unique: true);
migrationBuilder.CreateIndex(
name: "IX_FeatureStates_IdentityId",
table: "FeatureStates",
column: "IdentityId");
migrationBuilder.CreateIndex(
name: "IX_Identities_EnvironmentId_Identifier",
table: "Identities",
columns: new[] { "EnvironmentId", "Identifier" },
unique: true);
migrationBuilder.CreateIndex(
name: "IX_IdentityTraits_IdentityId_Key",
table: "IdentityTraits",
columns: new[] { "IdentityId", "Key" },
unique: true);
migrationBuilder.CreateIndex(
name: "IX_OrganizationUsers_UserId",
table: "OrganizationUsers",
column: "UserId");
migrationBuilder.CreateIndex(
name: "IX_Projects_OrganizationId",
table: "Projects",
column: "OrganizationId");
migrationBuilder.CreateIndex(
name: "IX_RefreshTokens_Token",
table: "RefreshTokens",
column: "Token",
unique: true);
migrationBuilder.CreateIndex(
name: "IX_RefreshTokens_UserId",
table: "RefreshTokens",
column: "UserId");
migrationBuilder.CreateIndex(
name: "IX_SegmentConditions_RuleId",
table: "SegmentConditions",
column: "RuleId");
migrationBuilder.CreateIndex(
name: "IX_SegmentRules_ParentRuleId",
table: "SegmentRules",
column: "ParentRuleId");
migrationBuilder.CreateIndex(
name: "IX_SegmentRules_SegmentId",
table: "SegmentRules",
column: "SegmentId");
migrationBuilder.CreateIndex(
name: "IX_Segments_ProjectId",
table: "Segments",
column: "ProjectId");
migrationBuilder.CreateIndex(
name: "IX_Tags_ProjectId",
table: "Tags",
column: "ProjectId");
migrationBuilder.CreateIndex(
name: "IX_Users_Email",
table: "Users",
column: "Email",
unique: true);
migrationBuilder.CreateIndex(
name: "IX_WebhookDeliveryLogs_WebhookId",
table: "WebhookDeliveryLogs",
column: "WebhookId");
migrationBuilder.CreateIndex(
name: "IX_Webhooks_EnvironmentId",
table: "Webhooks",
column: "EnvironmentId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "ApiKeys");
migrationBuilder.DropTable(
name: "AuditLogs");
migrationBuilder.DropTable(
name: "FeatureStates");
migrationBuilder.DropTable(
name: "IdentityTraits");
migrationBuilder.DropTable(
name: "OrganizationUsers");
migrationBuilder.DropTable(
name: "RefreshTokens");
migrationBuilder.DropTable(
name: "SegmentConditions");
migrationBuilder.DropTable(
name: "Tags");
migrationBuilder.DropTable(
name: "UserProjectPermissions");
migrationBuilder.DropTable(
name: "WebhookDeliveryLogs");
migrationBuilder.DropTable(
name: "Webhooks");
migrationBuilder.DropTable(
name: "Environments");
migrationBuilder.DropTable(
name: "FeatureSegments");
migrationBuilder.DropTable(
name: "Identities");
migrationBuilder.DropTable(
name: "Users");
migrationBuilder.DropTable(
name: "SegmentRules");
migrationBuilder.DropTable(
name: "Features");
migrationBuilder.DropTable(
name: "Segments");
migrationBuilder.DropTable(
name: "Projects");
migrationBuilder.DropTable(
name: "Organizations");
}
}
}