<?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-latest.xsd">

    <changeSet id="019-create-batch-consumptions-table" author="effigenix">
        <createTable tableName="batch_consumptions">
            <column name="id" type="varchar(36)">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="batch_id" type="varchar(36)">
                <constraints nullable="false"
                             foreignKeyName="fk_consumption_batch"
                             references="batches(id)"/>
            </column>
            <column name="input_batch_id" type="varchar(36)">
                <constraints nullable="false"/>
            </column>
            <column name="article_id" type="varchar(36)">
                <constraints nullable="false"/>
            </column>
            <column name="quantity_used_amount" type="decimal(19,6)">
                <constraints nullable="false"/>
            </column>
            <column name="quantity_used_unit" type="varchar(10)">
                <constraints nullable="false"/>
            </column>
            <column name="consumed_at" type="timestamptz">
                <constraints nullable="false"/>
            </column>
        </createTable>

        <addUniqueConstraint tableName="batch_consumptions"
                             columnNames="batch_id, input_batch_id"
                             constraintName="uq_consumption_batch_input"/>

        <createIndex tableName="batch_consumptions" indexName="idx_consumption_batch_id">
            <column name="batch_id"/>
        </createIndex>

        <createIndex tableName="batch_consumptions" indexName="idx_consumption_input_batch_id">
            <column name="input_batch_id"/>
        </createIndex>
    </changeSet>

</databaseChangeLog>
