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

/**
 * CreateSettingsTableMigration
 * ----------------------------------
 * Create a table to store random one-off settings.
 */
@Injectable()
export default class CreateSettingsTableMigration 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_settings')

        table.primaryKey('id')

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

        table.column('setting_json')
            .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('p5x_settings')

        table.dropIfExists()

        await schema.commit(table)
    }
}
