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

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

        table.primaryKey('pending_transaction_id').required()

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

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

        table.column('amount')
            .type(FieldType.doublePrecision)
            .required()

        table.column('tx_date')
            .type(FieldType.date)
            .required()

        await schema.commit(table)
    }

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

        table.dropIfExists()

        await schema.commit(table)
    }
}
