# frozen_string_literal: true

class CreateUxToolkitRepositoryRefreshStates < ActiveRecord::Migration[6.1]
  def change
    create_table :ux_toolkit_repository_refresh_states do |t|
      t.integer :repository_id, null: false
      t.integer :project_id
      t.boolean :enabled, null: false, default: false
      t.integer :refresh_interval_minutes, null: false, default: 60
      t.datetime :last_run_at
      t.datetime :next_run_at
      t.string :last_status
      t.text :last_message
      t.integer :consecutive_failure_count, null: false, default: 0
      t.datetime :locked_until
      t.timestamps
    end

    add_index :ux_toolkit_repository_refresh_states, :repository_id, unique: true, name: 'idx_ux_repo_refresh_states_repo'
    add_index :ux_toolkit_repository_refresh_states, :project_id, name: 'idx_ux_repo_refresh_states_project'
    add_index :ux_toolkit_repository_refresh_states, [:enabled, :next_run_at], name: 'idx_ux_repo_refresh_states_due'
    add_index :ux_toolkit_repository_refresh_states, :locked_until, name: 'idx_ux_repo_refresh_states_lock'
  end
end
