class CreateEventOutboxEvents < ActiveRecord::Migration
  def change
    return if table_exists?(:event_outbox_events)

    create_table :event_outbox_events do |t|
      t.string :event_type, :null => false
      t.string :source_type, :null => false
      t.integer :source_id, :null => false
      t.integer :project_id
      t.integer :issue_id
      t.integer :journal_id
      t.integer :user_id
      t.datetime :occurred_at, :null => false
      t.text :payload, :null => false
      t.datetime :processed_at
      t.integer :attempts, :null => false, :default => 0
      t.text :last_error
      t.datetime :locked_at
      t.string :locked_by
      t.timestamps
    end

    add_index :event_outbox_events, [:processed_at, :id]
    add_index :event_outbox_events, :event_type
    add_index :event_outbox_events, :issue_id
    add_index :event_outbox_events, :project_id
    add_index :event_outbox_events, :occurred_at
  end
end
