import {MigrationInterface, QueryRunner, Table, TableUnique} from "typeorm";

/**
 * Creates the `tag` table.
 */
export class CreateTagTable1771658108802 implements MigrationInterface {

    public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.createTable(
            new Table({
                name: "tag",
                columns: [
                    {
                        name: "id",
                        type: "uuid",
                        isPrimary: true,
                        generationStrategy: "uuid",
                        isGenerated: true,
                        default: "uuid_generate_v4()",
                    },
                    {
                        name: "description",
                        type: "varchar",
                        length: "100",
                        isUnique: true,
                        isNullable: false,
                    },
                    {
                        name: "create_date",
                        type: "timestamp",
                        default: "now()",
                    },
                    {
                        name: "update_date",
                        type: "timestamp",
                        default: "now()",
                    },
                ],
            }),
            true // ifNotExists
        );

        // Add a unique constraint on desciption
        await queryRunner.createUniqueConstraint(
            "tag",
            new TableUnique({
                columnNames: ["description"],
                name: "UQ_tag_description",
            })
        );
    }

    public async down(queryRunner: QueryRunner): Promise<void> {
        // Drop the unique constraint first
        await queryRunner.dropUniqueConstraint("tag", "UQ_tag_description");
        await queryRunner.dropTable("tag", true);
    }
}
