<?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="migrate_pk_hash_to_ext_id">
        <!-- drop existing "ID" column -->
        <dropColumn columnName="id"
                    tableName="pk_hash_to_ext_id_map"/>

        <dropIndex tableName="pk_hash_to_ext_id_map" indexName="pk_hash_to_xid_idx"/>

        <!-- create new primary key constraint on key hash -->
        <addPrimaryKey columnNames="public_key_hash" constraintName="pubkey_hash_to_external_id_pk" tableName="pk_hash_to_ext_id_map"/>

        <createIndex indexName="ext_id_idx" tableName="pk_hash_to_ext_id_map">
            <column name="external_id"/>
        </createIndex>

    </changeSet>

    <changeSet author="R3.Corda" id="modify identity_value column type" dbms="postgresql">
        <addColumn tableName="node_identities">
            <column name="temp_identity_value" type="varbinary(64000)">
                <constraints nullable="true"/>
            </column>
        </addColumn>
        <sql>UPDATE node_identities SET temp_identity_value = lo_get(identity_value)</sql>
        <addNotNullConstraint tableName="node_identities" columnName="temp_identity_value"/>
        <dropColumn tableName="node_identities" columnName="identity_value"/>
        <renameColumn tableName="node_identities" oldColumnName="temp_identity_value" newColumnName="identity_value"/>
    </changeSet>

    <changeSet author="R3.Corda" id="migrate_identity_service_to_use_publicKey.toShortString()">
        <customChange class="net.corda.node.migration.PersistentIdentityMigration">
        </customChange>
    </changeSet>

</databaseChangeLog>