<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
  xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
                      http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.27.xsd">

  <changeSet id="0006-1-idempotency-keys" author="ordinis">
    <comment>Idempotency keys для exactly-once семантики write API. Retention 30 дней через cleanup job.</comment>
    <createTable tableName="idempotency_keys">
      <column name="key" type="VARCHAR(128)">
        <constraints primaryKey="true" nullable="false"/>
      </column>
      <column name="request_hash" type="VARCHAR(64)">
        <constraints nullable="false"/>
      </column>
      <column name="response_status" type="INTEGER"/>
      <column name="response_body" type="JSONB"/>
      <column name="created_at" type="TIMESTAMPTZ" defaultValueComputed="NOW()">
        <constraints nullable="false"/>
      </column>
      <column name="expires_at" type="TIMESTAMPTZ">
        <constraints nullable="false"/>
      </column>
    </createTable>

    <createIndex indexName="idx_idempotency_expires" tableName="idempotency_keys">
      <column name="expires_at"/>
    </createIndex>
  </changeSet>

</databaseChangeLog>
