import {DatabaseService, FieldType, Inject, Injectable, Migration} from '@extollo/lib'

/**
 * CreateGolinksTableMigration
 * ----------------------------------
 * Create Table to store URL redirections
 */
@Injectable()
export default class CreateGolinksTableMigration extends Migration {
    @Inject()
    protected readonly db!: DatabaseService

    /**
     * Apply the migration.
     */
    async up(): Promise<void> {
        const schema = this.db.get().schema()
        const table = await schema.table('go_links')

        table.primaryKey('go_link_id').required()

        table.column('active')
            .type(FieldType.bool)
            .default(true)

        table.column('short')
            .type(FieldType.varchar)
            .required()
            .unique()

        table.column('url')
            .type(FieldType.text)
            .required()

        await schema.commit(table)
    }

    /**
     * Undo the migration.
     */
    async down(): Promise<void> {
        const schema = this.db.get().schema()
        const table = await schema.table('go_links')

        table.dropIfExists()

        await schema.commit(table)
    }
}
