<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<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-3.5.xsd"
                   logicalFilePath="migration/node-services.changelog-init.xml">

    <changeSet author="R3.Corda" id="remove_flow_metadata_columns">
        <dropColumn tableName="node_transactions" columnName="initiator"/>
        <dropColumn tableName="node_transactions" columnName="participants"/>
        <dropColumn tableName="node_transactions" columnName="states_to_record"/>
    </changeSet>

    <changeSet author="R3.Corda" id="create_sender_distr_recs_table">
        <createTable tableName="node_sender_distr_recs">
            <column name="timestamp" type="TIMESTAMP">
                <constraints nullable="false"/>
            </column>
            <column name="timestamp_discriminator" type="INT">
                <constraints nullable="false"/>
            </column>
            <column name="transaction_id" type="NVARCHAR(144)">
                <constraints nullable="false"/>
            </column>
            <column name="peer_party_id" type="NVARCHAR(144)">
                <constraints nullable="false"/>
            </column>
            <column name="sender_states_to_record" type="INT">
                <constraints nullable="false"/>
            </column>
            <column name="receiver_states_to_record" type="INT">
                <constraints nullable="false"/>
            </column>
        </createTable>
    </changeSet>

    <changeSet author="R3.Corda" id="node_sender_distr_recs_pkey">
        <addPrimaryKey columnNames="peer_party_id, timestamp, timestamp_discriminator, transaction_id" constraintName="node_sender_distr_recs_pkey"
                       tableName="node_sender_distr_recs"/>
    </changeSet>

    <changeSet author="R3.Corda" id="node_sender_distr_recs_add_indexes">
        <createIndex indexName="node_sender_distr_recs_idx1" tableName="node_sender_distr_recs">
            <column name="transaction_id"/>
            <column name="timestamp"/>
            <column name="timestamp_discriminator"/>
        </createIndex>
    </changeSet>

    <changeSet author="R3.Corda" id="create_receiver_distr_recs_table">
        <createTable tableName="node_receiver_distr_recs">
            <column name="timestamp" type="TIMESTAMP">
                <constraints nullable="false"/>
            </column>
            <column name="timestamp_discriminator" type="INT">
                <constraints nullable="false"/>
            </column>
            <column name="transaction_id" type="NVARCHAR(144)">
                <constraints nullable="false"/>
            </column>
            <column name="peer_party_id" type="NVARCHAR(144)">
                <constraints nullable="false"/>
            </column>
            <column name="distribution_list" type="BLOB">
                <constraints nullable="false"/>
            </column>
            <column name="receiver_states_to_record" type="INT">
                <constraints nullable="false"/>
            </column>
        </createTable>
    </changeSet>

    <changeSet author="R3.Corda" id="node_receiver_distr_recs_pkey">
        <addPrimaryKey columnNames="peer_party_id, timestamp, timestamp_discriminator, transaction_id" constraintName="node_receiver_distr_recs_pkey"
                       tableName="node_receiver_distr_recs"/>
    </changeSet>

    <changeSet author="R3.Corda" id="node_receiver_distr_recs_add_indexes">
        <createIndex indexName="node_receiver_distr_recs_idx1" tableName="node_receiver_distr_recs">
            <column name="transaction_id"/>
            <column name="timestamp"/>
            <column name="timestamp_discriminator"/>
        </createIndex>
    </changeSet>

    <changeSet author="R3.Corda" id="create_recovery_party_info_table">
        <createTable tableName="node_recovery_party_info">
            <column name="party_id" type="NVARCHAR(144)">
                <constraints nullable="false"/>
            </column>
            <column name="party_name" type="NVARCHAR(255)">
                <constraints nullable="false"/>
            </column>
        </createTable>
    </changeSet>

    <changeSet author="R3.Corda" id="node_recovery_party_info_pkey">
        <addPrimaryKey columnNames="party_id" constraintName="node_recovery_party_info_pkey" tableName="node_recovery_party_info"/>
    </changeSet>

    <changeSet author="R3.Corda" id="FK__sender_distr_recs__peer_party_id">
        <addForeignKeyConstraint baseColumnNames="peer_party_id" baseTableName="node_sender_distr_recs"
                                 constraintName="FK__send_distr__peer_party_id"
                                 referencedColumnNames="party_id" referencedTableName="node_recovery_party_info"/>
    </changeSet>

    <changeSet author="R3.Corda" id="FK__receiver_distr_recs__peer_party_id">
        <addForeignKeyConstraint baseColumnNames="peer_party_id" baseTableName="node_receiver_distr_recs"
                                 constraintName="FK__recv_distr__peer_party_id"
                                 referencedColumnNames="party_id" referencedTableName="node_recovery_party_info"/>
    </changeSet>

</databaseChangeLog>