using System; using Microsoft.EntityFrameworkCore.Migrations; using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; #nullable disable namespace MicCheck.Api.Migrations { /// public partial class InitialCreate : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.CreateTable( name: "AuditLogs", columns: table => new { Id = table.Column(type: "integer", nullable: false) .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), ResourceType = table.Column(type: "character varying(100)", maxLength: 100, nullable: false), ResourceId = table.Column(type: "character varying(100)", maxLength: 100, nullable: false), Action = table.Column(type: "character varying(50)", maxLength: 50, nullable: false), Changes = table.Column(type: "text", nullable: true), OrganizationId = table.Column(type: "integer", nullable: false), ProjectId = table.Column(type: "integer", nullable: true), EnvironmentId = table.Column(type: "integer", nullable: true), ActorUserId = table.Column(type: "integer", nullable: true), CreatedAt = table.Column(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(type: "integer", nullable: false) .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), FeatureId = table.Column(type: "integer", nullable: false), SegmentId = table.Column(type: "integer", nullable: false), EnvironmentId = table.Column(type: "integer", nullable: false), Priority = table.Column(type: "integer", nullable: false) }, constraints: table => { table.PrimaryKey("PK_FeatureSegments", x => x.Id); }); migrationBuilder.CreateTable( name: "Identities", columns: table => new { Id = table.Column(type: "integer", nullable: false) .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), Identifier = table.Column(type: "character varying(2000)", maxLength: 2000, nullable: false), EnvironmentId = table.Column(type: "integer", nullable: false), CreatedAt = table.Column(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(type: "integer", nullable: false) .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), Name = table.Column(type: "character varying(200)", maxLength: 200, nullable: false), CreatedAt = table.Column(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(type: "integer", nullable: false), ProjectId = table.Column(type: "integer", nullable: false), Permissions = table.Column(type: "text", nullable: false), IsAdmin = table.Column(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(type: "integer", nullable: false) .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), Email = table.Column(type: "character varying(500)", maxLength: 500, nullable: false), PasswordHash = table.Column(type: "text", nullable: false), FirstName = table.Column(type: "character varying(200)", maxLength: 200, nullable: false), LastName = table.Column(type: "character varying(200)", maxLength: 200, nullable: false), IsActive = table.Column(type: "boolean", nullable: false), IsTwoFactorEnabled = table.Column(type: "boolean", nullable: false), CreatedAt = table.Column(type: "timestamp with time zone", nullable: false), LastLoginAt = table.Column(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(type: "integer", nullable: false) .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), WebhookId = table.Column(type: "integer", nullable: false), EventType = table.Column(type: "character varying(100)", maxLength: 100, nullable: false), PayloadJson = table.Column(type: "text", nullable: false), ResponseStatusCode = table.Column(type: "integer", nullable: true), ResponseBody = table.Column(type: "text", nullable: true), Success = table.Column(type: "boolean", nullable: false), ErrorMessage = table.Column(type: "text", nullable: true), AttemptedAt = table.Column(type: "timestamp with time zone", nullable: false), Duration = table.Column(type: "interval", nullable: false) }, constraints: table => { table.PrimaryKey("PK_WebhookDeliveryLogs", x => x.Id); }); migrationBuilder.CreateTable( name: "Webhooks", columns: table => new { Id = table.Column(type: "integer", nullable: false) .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), Url = table.Column(type: "character varying(500)", maxLength: 500, nullable: false), Secret = table.Column(type: "character varying(200)", maxLength: 200, nullable: true), Scope = table.Column(type: "integer", nullable: false), EnvironmentId = table.Column(type: "integer", nullable: true), OrganizationId = table.Column(type: "integer", nullable: true), Enabled = table.Column(type: "boolean", nullable: false), CreatedAt = table.Column(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(type: "integer", nullable: false) .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), IdentityId = table.Column(type: "integer", nullable: false), Key = table.Column(type: "character varying(200)", maxLength: 200, nullable: false), Value = table.Column(type: "character varying(2000)", maxLength: 2000, nullable: false), ValueType = table.Column(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(type: "integer", nullable: false) .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), Key = table.Column(type: "character varying(512)", maxLength: 512, nullable: false), Prefix = table.Column(type: "character varying(8)", maxLength: 8, nullable: false), Name = table.Column(type: "character varying(200)", maxLength: 200, nullable: false), OrganizationId = table.Column(type: "integer", nullable: false), IsActive = table.Column(type: "boolean", nullable: false), ExpiresAt = table.Column(type: "timestamp with time zone", nullable: true), CreatedAt = table.Column(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(type: "integer", nullable: false) .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), Name = table.Column(type: "character varying(200)", maxLength: 200, nullable: false), OrganizationId = table.Column(type: "integer", nullable: false), CreatedAt = table.Column(type: "timestamp with time zone", nullable: false), HideDisabledFlags = table.Column(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(type: "integer", nullable: false), UserId = table.Column(type: "integer", nullable: false), Role = table.Column(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(type: "integer", nullable: false) .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), Token = table.Column(type: "character varying(512)", maxLength: 512, nullable: false), UserId = table.Column(type: "integer", nullable: false), ExpiresAt = table.Column(type: "timestamp with time zone", nullable: false), IsRevoked = table.Column(type: "boolean", nullable: false), CreatedAt = table.Column(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(type: "integer", nullable: false) .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), Name = table.Column(type: "character varying(200)", maxLength: 200, nullable: false), ApiKey = table.Column(type: "character varying(100)", maxLength: 100, nullable: false), ProjectId = table.Column(type: "integer", nullable: false), CreatedAt = table.Column(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(type: "integer", nullable: false) .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), Name = table.Column(type: "character varying(150)", maxLength: 150, nullable: false), Type = table.Column(type: "integer", nullable: false), InitialValue = table.Column(type: "character varying(20000)", maxLength: 20000, nullable: true), Description = table.Column(type: "character varying(2000)", maxLength: 2000, nullable: true), DefaultEnabled = table.Column(type: "boolean", nullable: false), ProjectId = table.Column(type: "integer", nullable: false), CreatedAt = table.Column(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(type: "integer", nullable: false) .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), Name = table.Column(type: "character varying(200)", maxLength: 200, nullable: false), ProjectId = table.Column(type: "integer", nullable: false), CreatedAt = table.Column(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(type: "integer", nullable: false) .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), FeatureId = table.Column(type: "integer", nullable: false), EnvironmentId = table.Column(type: "integer", nullable: false), IdentityId = table.Column(type: "integer", nullable: true), Enabled = table.Column(type: "boolean", nullable: false), Value = table.Column(type: "character varying(20000)", maxLength: 20000, nullable: true), FeatureSegmentId = table.Column(type: "integer", nullable: true), CreatedAt = table.Column(type: "timestamp with time zone", nullable: false), UpdatedAt = table.Column(type: "timestamp with time zone", nullable: false), Version = table.Column(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(type: "integer", nullable: false) .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), Label = table.Column(type: "character varying(200)", maxLength: 200, nullable: false), Color = table.Column(type: "character varying(20)", maxLength: 20, nullable: false), ProjectId = table.Column(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(type: "integer", nullable: false) .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), SegmentId = table.Column(type: "integer", nullable: false), ParentRuleId = table.Column(type: "integer", nullable: true), Type = table.Column(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(type: "integer", nullable: false) .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), RuleId = table.Column(type: "integer", nullable: false), Property = table.Column(type: "character varying(200)", maxLength: 200, nullable: false), Operator = table.Column(type: "integer", nullable: false), Value = table.Column(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"); } /// 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"); } } }