import { MigrationInterface, QueryRunner } from 'typeorm';

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

  public async up(queryRunner: QueryRunner): Promise<any> {
    await queryRunner.query(
      `CREATE TABLE "temporary_book" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "nameCanonical" varchar NOT NULL, "rating" integer, CONSTRAINT "CHK_bbef8dbd3571ec559f7a980fc5" CHECK ((LENGTH(nameCanonical) > 0)))`,
      undefined
    );
    await queryRunner.query(
      `INSERT INTO "temporary_book"("id", "nameCanonical", "rating") SELECT "id", "nameCanonical", "rating" FROM "book"`,
      undefined
    );
    await queryRunner.query(`DROP TABLE "book"`, undefined);
    await queryRunner.query(`ALTER TABLE "temporary_book" RENAME TO "book"`, undefined);
    await queryRunner.query(
      `CREATE TABLE "temporary_book" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "nameCanonical" varchar NOT NULL, CONSTRAINT "CHK_bbef8dbd3571ec559f7a980fc5" CHECK ((LENGTH(nameCanonical) > 0)))`,
      undefined
    );
    await queryRunner.query(
      `INSERT INTO "temporary_book"("id", "nameCanonical") SELECT "id", "nameCanonical" FROM "book"`,
      undefined
    );
    await queryRunner.query(`DROP TABLE "book"`, undefined);
    await queryRunner.query(`ALTER TABLE "temporary_book" RENAME TO "book"`, undefined);
  }

  public async down(queryRunner: QueryRunner): Promise<any> {
    await queryRunner.query(`ALTER TABLE "book" RENAME TO "temporary_book"`, undefined);
    await queryRunner.query(
      `CREATE TABLE "book" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "nameCanonical" varchar NOT NULL, "rating" integer, CONSTRAINT "CHK_bbef8dbd3571ec559f7a980fc5" CHECK ((LENGTH(nameCanonical) > 0)))`,
      undefined
    );
    await queryRunner.query(
      `INSERT INTO "book"("id", "nameCanonical") SELECT "id", "nameCanonical" FROM "temporary_book"`,
      undefined
    );
    await queryRunner.query(`DROP TABLE "temporary_book"`, undefined);
    await queryRunner.query(`ALTER TABLE "book" RENAME TO "temporary_book"`, undefined);
    await queryRunner.query(
      `CREATE TABLE "book" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "nameCanonical" varchar NOT NULL, "rating" integer, CONSTRAINT "CHK_bbef8dbd3571ec559f7a980fc5" CHECK ((LENGTH(nameCanonical) > 0)), CONSTRAINT "CHK_62ebe0be1f2d31406118960aa5" CHECK ((rating >= 0 AND rating <= 9007199254740991)))`,
      undefined
    );
    await queryRunner.query(
      `INSERT INTO "book"("id", "nameCanonical", "rating") SELECT "id", "nameCanonical", "rating" FROM "temporary_book"`,
      undefined
    );
    await queryRunner.query(`DROP TABLE "temporary_book"`, undefined);
  }
}
