import { MigrationInterface, QueryRunner, TableColumn } from "typeorm"

export class AddVideoColumnToEducationCourses1730720000000 implements MigrationInterface {

    public async up(queryRunner: QueryRunner): Promise<void> {
        // Check if the EducationCourses table exists
        const table = await queryRunner.getTable("EducationCourses");

        if (!table) {
           
            return;
        }

        // Check if video column already exists
        const videoColumn = table.findColumnByName("video");

        if (!videoColumn) {
            // Add new video column if it doesn't exist
            await queryRunner.addColumn("EducationCourses", new TableColumn({
                name: "video",
                type: "text",
                isNullable: true
            }));
            
        } else if (videoColumn.type !== "text") {
            // Modify existing column to TEXT type
            await queryRunner.changeColumn("EducationCourses", "video", new TableColumn({
                name: "video",
                type: "text",
                isNullable: true
            }));

        } 
    }

    public async down(queryRunner: QueryRunner): Promise<void> {
        // Remove video column from EducationCourses table
        const table = await queryRunner.getTable("EducationCourses");

        if (table) {
            const videoColumn = table.findColumnByName("video");
            if (videoColumn) {
                await queryRunner.dropColumn("EducationCourses", "video");
          
            }
        }
    }
}
