import { BaseSchema } from '@adonisjs/lucid/schema'

export default class extends BaseSchema {
    protected tableName = 'role_permission'

    async up() {
        this.schema.createTable(this.tableName, (table) => {
            table.increments('id').notNullable()
            table.integer('role_id').unsigned().notNullable().references('id').inTable('roles').onDelete('CASCADE')
            table.integer('permission_id').unsigned().notNullable().references('id').inTable('permissions').onDelete('CASCADE')

            table.timestamp('created_at').notNullable()
            table.timestamp('updated_at').nullable()

            table.unique(['role_id', 'permission_id'])
        })
    }

    async down() {
        this.schema.dropTable(this.tableName)
    }
}
