defmodule Towerops.Repo.Migrations.CreateConfigChangeEvents do
  use Ecto.Migration

  def change do
    create table(:config_change_events, primary_key: false) do
      add :id, :binary_id, primary_key: true
      add :device_id, references(:devices, type: :binary_id, on_delete: :delete_all), null: false
      add :organization_id, references(:organizations, type: :binary_id, on_delete: :delete_all), null: false
      add :backup_before_id, references(:device_mikrotik_backups, type: :binary_id, on_delete: :nilify_all)
      add :backup_after_id, references(:device_mikrotik_backups, type: :binary_id, on_delete: :nilify_all)
      add :changed_at, :utc_datetime, null: false
      add :diff_summary, :text
      add :sections_changed, {:array, :string}, default: []
      add :change_size, :integer, default: 0

      timestamps(type: :utc_datetime, updated_at: false)
    end

    create index(:config_change_events, [:device_id, :changed_at])
    create index(:config_change_events, [:organization_id, :changed_at])
    create index(:config_change_events, [:changed_at])
  end
end
