<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.1.xsd">

    <property name="cmObjectTypeLength" value="1024" dbms="postgresql,oracle"/>
    <property name="cmObjectTypeLength" value="200" dbms="mysql"/>

    <property name="cmUserIdLength" value="1024" dbms="postgresql,oracle"/>
    <property name="cmUserIdLength" value="255" dbms="mysql,mssql"/>

    <changeSet id="01-subscription-tables" author="marst" dbms="oracle,postgresql,mssql">
        <createTable tableName="acm_subscription">
            <column name="cm_subscription_id" type="${idType}">
                <constraints primaryKey="true" primaryKeyName="pk_cm_subscription_id"/>
            </column>
            <column name="cm_user_id" type="VARCHAR(${cmUserIdLength})">
                <constraints nullable="false"/>
            </column>
            <column name="cm_object_id" type="${fkIdType}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_object_type" type="VARCHAR(${cmObjectTypeLength})">
                <constraints nullable="false"/>
            </column>
            <column name="cm_object_name" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_object_number" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_subscription_created" type="${timestampType}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_subscription_modified" type="${timestampType}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_subscription_creator" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_subscription_modifier" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
        </createTable>

        <createTable tableName="acm_subscription_event">
            <column name="cm_event_id" type="${idType}">
                <constraints primaryKey="true" primaryKeyName="pk_cm_subscription_event"/>
            </column>
            <column name="cm_event_object_type" type="VARCHAR(${cmObjectTypeLength})">
                <constraints nullable="false"/>
            </column>
            <column name="cm_event_object_id" type="${fkIdType}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_event_user" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_event_date" type="${timestampType}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_event_subscription_owner" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_event_type" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_event_object_name" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_event_object_number" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_event_created" type="${timestampType}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_event_modified" type="${timestampType}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_event_creator" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_event_modifier" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
        </createTable>

        <createIndex tableName="acm_subscription" unique="true" indexName="idx_user_object_type">
            <column name="cm_user_id" type="VARCHAR(${cmUserIdLength})"/>
            <column name="cm_object_id" type="${fkIdType}"/>
            <column name="cm_object_type" type="VARCHAR(${cmObjectTypeLength})"/>
        </createIndex>

    </changeSet>

    <changeSet id="01-subscription-tables" author="marst" dbms="mysql">
        <createTable tableName="acm_subscription">
            <column name="cm_subscription_id" type="${idType}">
                <constraints primaryKey="true" primaryKeyName="pk_cm_subscription_id"/>
            </column>
            <column name="cm_user_id" type="VARCHAR(${cmUserIdLength})">
                <constraints nullable="false"/>
            </column>
            <column name="cm_object_id" type="${fkIdType}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_object_type" type="VARCHAR(${cmObjectTypeLength})">
                <constraints nullable="false"/>
            </column>
            <column name="cm_object_name" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_object_number" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_subscription_created" type="${timestampType}" defaultValueComputed="${timestampFunction}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_subscription_modified" type="${timestampType}" defaultValueComputed="${timestampFunction}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_subscription_creator" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_subscription_modifier" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
        </createTable>

        <createTable tableName="acm_subscription_event">
            <column name="cm_event_id" type="${idType}">
                <constraints primaryKey="true" primaryKeyName="pk_cm_subscription_event"/>
            </column>
            <column name="cm_event_object_type" type="VARCHAR(${cmObjectTypeLength})">
                <constraints nullable="false"/>
            </column>
            <column name="cm_event_object_id" type="${fkIdType}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_event_user" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_event_date" type="${timestampType}" defaultValueComputed="${timestampFunction}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_event_subscription_owner" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_event_type" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_event_object_name" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_event_object_number" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_event_created" type="${timestampType}" defaultValueComputed="${timestampFunction}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_event_modified" type="${timestampType}" defaultValueComputed="${timestampFunction}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_event_creator" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_event_modifier" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
        </createTable>

        <createIndex tableName="acm_subscription" unique="true" indexName="idx_user_object_type">
            <column name="cm_user_id" type="VARCHAR(${cmUserIdLength})"/>
            <column name="cm_object_id" type="${fkIdType}"/>
            <column name="cm_object_type" type="VARCHAR(${cmObjectTypeLength})"/>
        </createIndex>

    </changeSet>

    <changeSet id="02-add-drop-columns-subscription-table-and-index-to-subscription-event-table" author="marst">

        <dropColumn tableName="acm_subscription" columnName="cm_object_number"/>

        <addColumn tableName="acm_subscription">
            <column name="cm_object_title" type="VARCHAR(2048)"></column>
        </addColumn>

        <createIndex tableName="acm_subscription_event" indexName="idx_event_modified">
            <column name="cm_event_modified" type="${timestampType}"/>
        </createIndex>
    </changeSet>

    <changeSet id="17-acm_subscription-id" author="nebojsha.davidovikj">
        <createTable tableName="acm_subscription_id">
            <column name="cm_seq_num" type="${idType}"/>
            <column name="cm_seq_name" type="VARCHAR(255)"/>
        </createTable>
        <insert tableName="acm_subscription_id">
            <column name="cm_seq_num" value="100"></column>
            <column name="cm_seq_name" value="acm_subscription"></column>
        </insert>
    </changeSet>

    <changeSet id="18-acm_subscription_event-id" author="nebojsha.davidovikj">
        <createTable tableName="acm_subscription_event_id">
            <column name="cm_seq_num" type="${idType}"/>
            <column name="cm_seq_name" type="VARCHAR(255)"/>
        </createTable>
        <insert tableName="acm_subscription_event_id">
            <column name="cm_seq_num" value="100"></column>
            <column name="cm_seq_name" value="acm_subscription_event"></column>
        </insert>
    </changeSet>

    <changeSet id="19-add_related_subscription_id" author="nadica.cuculova">
        <addColumn tableName="acm_subscription_event">
            <column name="cm_subscription_id" type="VARCHAR(255)"/>
        </addColumn>
    </changeSet>

    <changeSet id="01-change_cm_subscription_event_datetime" author="mario.gjurcheski" dbms="mysql">
        <modifyDataType tableName="acm_subscription_event" columnName="cm_event_date" newDataType="DATETIME"/>
        <modifyDataType tableName="acm_subscription_event" columnName="cm_event_created" newDataType="DATETIME"/>
        <modifyDataType tableName="acm_subscription_event" columnName="cm_event_modified" newDataType="DATETIME"/>
    </changeSet>

    <changeSet id="01-change_cm_subscription_datetime" author="mario.gjurcheski" dbms="mysql">
        <modifyDataType tableName="acm_subscription" columnName="cm_subscription_created" newDataType="DATETIME"/>
        <modifyDataType tableName="acm_subscription" columnName="cm_subscription_modified" newDataType="DATETIME"/>
    </changeSet>
</databaseChangeLog>
