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

/**
 * CreateP5xIpRangesTableMigration
 * ----------------------------------
 * Put some description here.
 */
@Injectable()
export default class CreateP5xIpRangesTableMigration 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('p5x_ip_ranges')

        table.primaryKey('id')

        table.column('name')
            .type(FieldType.varchar)
            .required()

        table.column('start_ip')
            .type(FieldType.varchar)
            .required()

        table.column('end_ip')
            .type(FieldType.varchar)
            .required()

        table.column('subnet')
            .type(FieldType.int)
            .required()

        table.column('gateway_ip')
            .type(FieldType.varchar)
            .required()

        await schema.commit(table)
    }

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

        table.dropIfExists()

        await schema.commit(table)
    }
}
