# typed: false
class CreateHttpLogEntries < ActiveRecord::Migration[7.0]
  def change
    enable_extension "pg_stat_statements"
    enable_extension "pg_trgm"

    create_table :http_log_entries do |t|
      t.string :uri_scheme, null: false
      t.string :uri_host, null: false
      t.string :uri_path, null: false
      t.string :uri_query
      t.string :uri_hash

      t.integer :verb, null: false

      # request/response headers
      t.references :request_headers, null: false
      t.references :response_headers, null: false

      t.integer :status_code, null: false
      t.integer :response_time_ms, null: false
      t.string :content_type, null: false
      t.binary :response_sha256, null: false

      t.datetime :requested_at, null: false
      t.timestamps null: false

      t.index %w[uri_host uri_path uri_query],
              name: "index_http_log_entries_on_uri_host_path_query"
    end

    create_table :http_log_entry_headers do |t|
      t.binary :sha256, null: false
      t.jsonb :headers, null: false
      t.index :sha256, unique: true
      t.timestamps null: false
    end

    create_table :blob_entries, id: false, primary_key: :sha256 do |t|
      t.binary :sha256, null: false
      t.binary :base_sha256

      t.string :content_type, null: false
      t.integer :size, null: false
      t.binary :contents, null: false
      t.timestamps null: false

      t.index :sha256, unique: true
    end
  end
end
