/** @param {import('knex').Knex} knex */
export async function up(knex) {
  // Fix rmt_hosts.credential_id FK (original pointed to non-existent 'credentials.id')
  try {
    await knex.schema.alterTable('rmt_hosts', (table) => {
      table.dropForeign('credential_id')
    })
  } catch {
    // FK may not exist (SQLite doesn't enforce FKs)
  }
  await knex.schema.alterTable('rmt_hosts', (table) => {
    table.foreign('credential_id').references('rmt_credentials.id').onDelete('SET NULL')
  })

  // Fix rmt_executions.host_id FK (original pointed to non-existent 'hosts.id')
  try {
    await knex.schema.alterTable('rmt_executions', (table) => {
      table.dropForeign('host_id')
    })
  } catch {
    // FK may not exist (SQLite doesn't enforce FKs)
  }
  await knex.schema.alterTable('rmt_executions', (table) => {
    table.foreign('host_id').references('rmt_hosts.id').onDelete('SET NULL')
  })
}

/** @param {import('knex').Knex} knex */
export async function down(knex) {
  // No-op: we don't want to restore broken FKs
  void knex
}
