import { MigrationInterface, QueryRunner } from 'typeorm';

export class ServerIcons1000000000009 implements MigrationInterface {
  name = 'ServerIcons1000000000009';

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`ALTER TABLE "servers" ADD COLUMN "icon" TEXT`);
    await queryRunner.query(`ALTER TABLE "servers" ADD COLUMN "iconUpdatedAt" INTEGER NOT NULL DEFAULT 0`);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`CREATE TABLE "servers_without_icons" (
      "id" TEXT PRIMARY KEY NOT NULL,
      "name" TEXT NOT NULL,
      "description" TEXT,
      "ownerId" TEXT NOT NULL,
      "ownerPublicKey" TEXT NOT NULL,
      "passwordHash" TEXT,
      "isPrivate" INTEGER NOT NULL DEFAULT 0,
      "maxUsers" INTEGER NOT NULL DEFAULT 0,
      "currentUsers" INTEGER NOT NULL DEFAULT 0,
      "slowModeInterval" INTEGER NOT NULL DEFAULT 0,
      "createdAt" INTEGER NOT NULL,
      "lastSeen" INTEGER NOT NULL
    )`);
    await queryRunner.query(`INSERT INTO "servers_without_icons" ("id", "name", "description", "ownerId", "ownerPublicKey", "passwordHash", "isPrivate", "maxUsers", "currentUsers", "slowModeInterval", "createdAt", "lastSeen")
      SELECT "id", "name", "description", "ownerId", "ownerPublicKey", "passwordHash", "isPrivate", "maxUsers", "currentUsers", "slowModeInterval", "createdAt", "lastSeen" FROM "servers"`);
    await queryRunner.query(`DROP TABLE "servers"`);
    await queryRunner.query(`ALTER TABLE "servers_without_icons" RENAME TO "servers"`);
  }
}
