using System; using Microsoft.EntityFrameworkCore.Migrations; #nullable disable namespace ClassNotify.Server.Data.Migrations { /// public partial class InitialPostgreSql : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.CreateTable( name: "DeviceCredentials", columns: table => new { DeviceId = table.Column(type: "text", nullable: false), InstallationId = table.Column(type: "character varying(100)", maxLength: 100, nullable: false), DeviceTokenHash = table.Column(type: "text", nullable: false), EncryptedRecoverableToken = table.Column(type: "text", nullable: false), CreatedAt = table.Column(type: "timestamp with time zone", nullable: false), RotatedAt = table.Column(type: "timestamp with time zone", nullable: true) }, constraints: table => { table.PrimaryKey("PK_DeviceCredentials", x => x.DeviceId); }); migrationBuilder.CreateTable( name: "MigrationMarkers", columns: table => new { Id = table.Column(type: "character varying(100)", maxLength: 100, nullable: false), MigratedAt = table.Column(type: "timestamp with time zone", nullable: false), Details = table.Column(type: "text", nullable: true) }, constraints: table => { table.PrimaryKey("PK_MigrationMarkers", x => x.Id); }); migrationBuilder.CreateTable( name: "Tenants", columns: table => new { Id = table.Column(type: "text", nullable: false), Name = table.Column(type: "character varying(200)", maxLength: 200, nullable: false), IsActive = table.Column(type: "boolean", nullable: false), CreatedAt = table.Column(type: "timestamp with time zone", nullable: false) }, constraints: table => { table.PrimaryKey("PK_Tenants", x => x.Id); }); migrationBuilder.CreateTable( name: "ActivationCodes", columns: table => new { Code = table.Column(type: "character varying(50)", maxLength: 50, nullable: false), TenantId = table.Column(type: "text", nullable: false), ClassroomId = table.Column(type: "character varying(50)", maxLength: 50, nullable: false), ExpiresAt = table.Column(type: "timestamp with time zone", nullable: false), IsUsed = table.Column(type: "boolean", nullable: false), UsedAt = table.Column(type: "timestamp with time zone", nullable: true), UsedByDeviceId = table.Column(type: "text", nullable: true), InstallationId = table.Column(type: "character varying(100)", maxLength: 100, nullable: true) }, constraints: table => { table.PrimaryKey("PK_ActivationCodes", x => x.Code); table.ForeignKey( name: "FK_ActivationCodes_Tenants_TenantId", column: x => x.TenantId, principalTable: "Tenants", principalColumn: "Id", onDelete: ReferentialAction.Restrict); }); migrationBuilder.CreateTable( name: "Classrooms", columns: table => new { Id = table.Column(type: "text", nullable: false), TenantId = table.Column(type: "text", nullable: false), DisplayName = table.Column(type: "character varying(100)", maxLength: 100, nullable: false), Grade = table.Column(type: "character varying(50)", maxLength: 50, nullable: true), RoomNumber = table.Column(type: "character varying(50)", maxLength: 50, nullable: true), CreatedAt = table.Column(type: "timestamp with time zone", nullable: false) }, constraints: table => { table.PrimaryKey("PK_Classrooms", x => new { x.TenantId, x.Id }); table.ForeignKey( name: "FK_Classrooms_Tenants_TenantId", column: x => x.TenantId, principalTable: "Tenants", principalColumn: "Id", onDelete: ReferentialAction.Cascade); }); migrationBuilder.CreateTable( name: "Notifications", columns: table => new { Id = table.Column(type: "character varying(100)", maxLength: 100, nullable: false), TenantId = table.Column(type: "text", nullable: false), ClassroomId = table.Column(type: "character varying(50)", maxLength: 50, nullable: false), SenderUserId = table.Column(type: "text", nullable: true), SenderName = table.Column(type: "character varying(100)", maxLength: 100, nullable: false), Message = table.Column(type: "text", nullable: false), Title = table.Column(type: "character varying(200)", maxLength: 200, nullable: false), Priority = table.Column(type: "character varying(50)", maxLength: 50, nullable: false), CreatedAt = table.Column(type: "timestamp with time zone", nullable: false) }, constraints: table => { table.PrimaryKey("PK_Notifications", x => x.Id); table.ForeignKey( name: "FK_Notifications_Tenants_TenantId", column: x => x.TenantId, principalTable: "Tenants", principalColumn: "Id", onDelete: ReferentialAction.Cascade); }); migrationBuilder.CreateTable( name: "Users", columns: table => new { Id = table.Column(type: "text", nullable: false), TenantId = table.Column(type: "text", nullable: false), Username = table.Column(type: "character varying(100)", maxLength: 100, nullable: false), PasswordHash = table.Column(type: "text", nullable: false), FullName = table.Column(type: "character varying(100)", maxLength: 100, nullable: false), Role = table.Column(type: "character varying(50)", maxLength: 50, nullable: false), CreatedAt = table.Column(type: "timestamp with time zone", nullable: false) }, constraints: table => { table.PrimaryKey("PK_Users", x => x.Id); table.ForeignKey( name: "FK_Users_Tenants_TenantId", column: x => x.TenantId, principalTable: "Tenants", principalColumn: "Id", onDelete: ReferentialAction.Restrict); }); migrationBuilder.CreateTable( name: "Devices", columns: table => new { Id = table.Column(type: "text", nullable: false), TenantId = table.Column(type: "text", nullable: false), ClassroomId = table.Column(type: "text", nullable: false), DeviceTokenHash = table.Column(type: "text", nullable: false), RawDeviceToken = table.Column(type: "text", nullable: true), InstallationId = table.Column(type: "character varying(100)", maxLength: 100, nullable: true), Hostname = table.Column(type: "character varying(200)", maxLength: 200, nullable: true), IsActive = table.Column(type: "boolean", nullable: false), CreatedAt = table.Column(type: "timestamp with time zone", nullable: false), LastHeartbeatAt = table.Column(type: "timestamp with time zone", nullable: true) }, constraints: table => { table.PrimaryKey("PK_Devices", x => x.Id); table.ForeignKey( name: "FK_Devices_Classrooms_TenantId_ClassroomId", columns: x => new { x.TenantId, x.ClassroomId }, principalTable: "Classrooms", principalColumns: new[] { "TenantId", "Id" }, onDelete: ReferentialAction.Restrict); table.ForeignKey( name: "FK_Devices_Tenants_TenantId", column: x => x.TenantId, principalTable: "Tenants", principalColumn: "Id", onDelete: ReferentialAction.Restrict); }); migrationBuilder.CreateTable( name: "NotificationReceipts", columns: table => new { Id = table.Column(type: "character varying(100)", maxLength: 100, nullable: false), NotificationId = table.Column(type: "character varying(100)", maxLength: 100, nullable: false), DeviceId = table.Column(type: "character varying(100)", maxLength: 100, nullable: false), Status = table.Column(type: "character varying(50)", maxLength: 50, nullable: false), DeviceReceivedAt = table.Column(type: "timestamp with time zone", nullable: true), DisplayedAt = table.Column(type: "timestamp with time zone", nullable: true), AcknowledgedAt = table.Column(type: "timestamp with time zone", nullable: true), FailedAt = table.Column(type: "timestamp with time zone", nullable: true), FailureReason = table.Column(type: "character varying(500)", maxLength: 500, nullable: true), UpdatedAt = table.Column(type: "timestamp with time zone", nullable: false) }, constraints: table => { table.PrimaryKey("PK_NotificationReceipts", x => x.Id); table.ForeignKey( name: "FK_NotificationReceipts_Devices_DeviceId", column: x => x.DeviceId, principalTable: "Devices", principalColumn: "Id", onDelete: ReferentialAction.Cascade); table.ForeignKey( name: "FK_NotificationReceipts_Notifications_NotificationId", column: x => x.NotificationId, principalTable: "Notifications", principalColumn: "Id", onDelete: ReferentialAction.Cascade); }); migrationBuilder.CreateIndex( name: "IX_ActivationCodes_ClassroomId", table: "ActivationCodes", column: "ClassroomId"); migrationBuilder.CreateIndex( name: "IX_ActivationCodes_Code", table: "ActivationCodes", column: "Code", unique: true); migrationBuilder.CreateIndex( name: "IX_ActivationCodes_InstallationId", table: "ActivationCodes", column: "InstallationId"); migrationBuilder.CreateIndex( name: "IX_ActivationCodes_TenantId", table: "ActivationCodes", column: "TenantId"); migrationBuilder.CreateIndex( name: "IX_Classrooms_TenantId_Id", table: "Classrooms", columns: new[] { "TenantId", "Id" }); migrationBuilder.CreateIndex( name: "IX_DeviceCredentials_DeviceId", table: "DeviceCredentials", column: "DeviceId", unique: true); migrationBuilder.CreateIndex( name: "IX_DeviceCredentials_InstallationId", table: "DeviceCredentials", column: "InstallationId", unique: true); migrationBuilder.CreateIndex( name: "IX_Devices_Id", table: "Devices", column: "Id", unique: true); migrationBuilder.CreateIndex( name: "IX_Devices_InstallationId", table: "Devices", column: "InstallationId", unique: true, filter: "\"InstallationId\" IS NOT NULL"); migrationBuilder.CreateIndex( name: "IX_Devices_TenantId", table: "Devices", column: "TenantId"); migrationBuilder.CreateIndex( name: "IX_Devices_TenantId_ClassroomId", table: "Devices", columns: new[] { "TenantId", "ClassroomId" }); migrationBuilder.CreateIndex( name: "IX_NotificationReceipts_DeviceId_UpdatedAt", table: "NotificationReceipts", columns: new[] { "DeviceId", "UpdatedAt" }, descending: new[] { false, true }); migrationBuilder.CreateIndex( name: "IX_NotificationReceipts_NotificationId_DeviceId", table: "NotificationReceipts", columns: new[] { "NotificationId", "DeviceId" }, unique: true); migrationBuilder.CreateIndex( name: "IX_Notifications_ClassroomId_CreatedAt", table: "Notifications", columns: new[] { "ClassroomId", "CreatedAt" }, descending: new[] { false, true }); migrationBuilder.CreateIndex( name: "IX_Notifications_TenantId_CreatedAt", table: "Notifications", columns: new[] { "TenantId", "CreatedAt" }, descending: new[] { false, true }); migrationBuilder.CreateIndex( name: "IX_Users_TenantId", table: "Users", column: "TenantId"); migrationBuilder.CreateIndex( name: "IX_Users_Username", table: "Users", column: "Username", unique: true); } /// protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.DropTable( name: "ActivationCodes"); migrationBuilder.DropTable( name: "DeviceCredentials"); migrationBuilder.DropTable( name: "MigrationMarkers"); migrationBuilder.DropTable( name: "NotificationReceipts"); migrationBuilder.DropTable( name: "Users"); migrationBuilder.DropTable( name: "Devices"); migrationBuilder.DropTable( name: "Notifications"); migrationBuilder.DropTable( name: "Classrooms"); migrationBuilder.DropTable( name: "Tenants"); } } }