defmodule AshBackpex.TestRepo.Migrations.CreateTestTables do
  @moduledoc """
  Updates resources based on their most recent snapshots.

  This file was autogenerated with `mix ash_sqlite.generate_migrations`
  """

  use Ecto.Migration

  def up do
    create table(:users, primary_key: false) do
      add :id, :text, null: false, primary_key: true
      add :name, :text, null: false
      add :email, :text, null: false
      add :active, :boolean, null: false, default: true
      add :inserted_at, :utc_datetime_usec, null: false, default: fragment("CURRENT_TIMESTAMP")
      add :updated_at, :utc_datetime_usec, null: false, default: fragment("CURRENT_TIMESTAMP")
    end

    create table(:posts, primary_key: false) do
      add :id, :text, null: false, primary_key: true
      add :title, :text, null: false
      add :content, :text
      add :published, :boolean, null: false, default: false
      add :published_at, :utc_datetime_usec
      add :view_count, :integer, null: false, default: 0
      add :rating, :real
      add :tags, :text
      add :metadata, :text
      add :status, :text
      add :author_id, references(:users, column: :id, type: :text)
      add :inserted_at, :utc_datetime_usec, null: false, default: fragment("CURRENT_TIMESTAMP")
      add :updated_at, :utc_datetime_usec, null: false, default: fragment("CURRENT_TIMESTAMP")
    end

    create table(:comments, primary_key: false) do
      add :id, :text, null: false, primary_key: true
      add :body, :text, null: false
      add :post_id, references(:posts, column: :id, type: :text)
      add :author_id, references(:users, column: :id, type: :text)
      add :inserted_at, :utc_datetime_usec, null: false, default: fragment("CURRENT_TIMESTAMP")
      add :updated_at, :utc_datetime_usec, null: false, default: fragment("CURRENT_TIMESTAMP")
    end

    create index(:posts, [:author_id])
    create index(:comments, [:post_id])
    create index(:comments, [:author_id])
  end

  def down do
    drop table(:comments)
    drop table(:posts)
    drop table(:users)
  end
end
