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

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

        table.primaryKey('certificate_id')

        table.column('reltype')
            .type(FieldType.varchar)

        table.column('relid')
            .type(FieldType.bigint)

        table.column('pubkey')
            .type(FieldType.text)

        table.column('privkey')
            .type(FieldType.text)

        table.index('pub_cert_rel')
            .field('reltype')
            .field('relid')

        await schema.commit(table)
    }

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

        table.dropIfExists()

        await schema.commit(table)
    }
}
