<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">

    <changeSet id="01-create-costsheet-table" author="riste.tutureski" dbms="oracle,postgresql,mssql">
        <createTable tableName="acm_costsheet">
            <column name="cm_costsheet_id" type="${idType}">
                <constraints primaryKey="true" primaryKeyName="pk_cm_costsheet_id"/>
            </column>
            <column name="cm_costsheet_user_id" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_costsheet_object_id" type="NUMBER(32,0)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_costsheet_object_type" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_costsheet_object_number" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_costsheet_status" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_costsheet_created" type="${timestampType}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_costsheet_creator" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_costsheet_modified" type="${timestampType}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_costsheet_modifier" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
        </createTable>
    </changeSet>

    <changeSet id="01-create-costsheet-table" author="riste.tutureski" dbms="mysql">
        <createTable tableName="acm_costsheet">
            <column name="cm_costsheet_id" type="${idType}">
                <constraints primaryKey="true" primaryKeyName="pk_cm_costsheet_id"/>
            </column>
            <column name="cm_costsheet_user_id" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_costsheet_object_id" type="NUMBER(32,0)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_costsheet_object_type" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_costsheet_object_number" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_costsheet_status" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_costsheet_created" type="${timestampType}" defaultValueComputed="${timestampFunction}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_costsheet_creator" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_costsheet_modified" type="${timestampType}" defaultValueComputed="${timestampFunction}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_costsheet_modifier" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
        </createTable>
    </changeSet>
    <changeSet id="02-create-cost-table" author="riste.tutureski" dbms="oracle,postgresql,mssql">
        <createTable tableName="acm_cost">
            <column name="cm_cost_id" type="${idType}">
                <constraints primaryKey="true" primaryKeyName="pk_cm_cost_id"/>
            </column>
            <column name="cm_cost_costsheet_id" type="${fkIdType}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_cost_date" type="${timestampType}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_cost_value" type="NUMBER(32,0)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_cost_title" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_cost_description" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_cost_created" type="${timestampType}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_cost_creator" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_cost_modified" type="${timestampType}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_cost_modifier" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
        </createTable>
        <addForeignKeyConstraint baseTableName="acm_cost" baseColumnNames="cm_cost_costsheet_id" constraintName="fk_cost_costsheet_id"
            referencedTableName="acm_costsheet" referencedColumnNames="cm_costsheet_id" deferrable="${deferrable}"
            initiallyDeferred="${initially.deferred}"/>
    </changeSet>

    <changeSet id="02-create-cost-table" author="riste.tutureski" dbms="mysql">
        <createTable tableName="acm_cost">
            <column name="cm_cost_id" type="${idType}">
                <constraints primaryKey="true" primaryKeyName="pk_cm_cost_id"/>
            </column>
            <column name="cm_cost_costsheet_id" type="${fkIdType}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_cost_date" type="${timestampType}" defaultValueComputed="${timestampFunction}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_cost_value" type="NUMBER(32,0)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_cost_title" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_cost_description" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_cost_created" type="${timestampType}" defaultValueComputed="${timestampFunction}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_cost_creator" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_cost_modified" type="${timestampType}" defaultValueComputed="${timestampFunction}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_cost_modifier" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
        </createTable>
        <addForeignKeyConstraint baseTableName="acm_cost" baseColumnNames="cm_cost_costsheet_id" constraintName="fk_cost_costsheet_id"
            referencedTableName="acm_costsheet" referencedColumnNames="cm_costsheet_id" deferrable="${deferrable}"
            initiallyDeferred="${initially.deferred}"/>
    </changeSet>

    <changeSet id="03-change-cost-value-type-precision" author="riste.tutureski">
        <dropColumn tableName="acm_cost" columnName="cm_cost_value"/>
        <addColumn tableName="acm_cost">
            <column name="cm_cost_value" type="NUMBER(32,2)">
                <constraints nullable="false"/>
            </column>
        </addColumn>
    </changeSet>

    <changeSet id="04-remove-nullable-constraint-for-description-in-cost-table" author="riste.tutureski">
        <dropNotNullConstraint tableName="acm_cost" columnName="cm_cost_description" columnDataType="VARCHAR(1024)"/>
    </changeSet>

    <changeSet id="05-add-details-column-in-acm-costsheet-table" author="riste.tutureski">
        <addColumn tableName="acm_costsheet">
            <column name="cm_costsheet_details" type="CLOB">
                <constraints nullable="true"/>
            </column>
        </addColumn>
    </changeSet>

    <changeSet id="06-add-container-id-in-acm-costsheet-table" author="riste.tutureski">
        <addColumn tableName="acm_costsheet">
            <column name="cm_container_id" type="${fkIdType}"/>
        </addColumn>
    </changeSet>

    <changeSet id="07-object-type-in-acm-costsheet-table" author="nebojsha.davidovikj">
        <addColumn tableName="acm_costsheet">
            <column name="cm_object_type" type="VARCHAR(100)" defaultValue="COSTSHEET">
                <constraints nullable="false"/>
            </column>
        </addColumn>
    </changeSet>

    <changeSet id="08-acm_costsheet-id" author="nebojsha.davidovikj">
        <createTable tableName="acm_costsheet_id">
            <column name="cm_seq_num" type="${idType}"/>
            <column name="cm_seq_name" type="VARCHAR(255)"/>
        </createTable>
        <insert tableName="acm_costsheet_id">
            <column name="cm_seq_num" value="100"></column>
            <column name="cm_seq_name" value="acm_costsheet"></column>
        </insert>
    </changeSet>
    <changeSet id="09-acm_cost-id" author="nebojsha.davidovikj">
        <createTable tableName="acm_cost_id">
            <column name="cm_seq_num" type="${idType}"/>
            <column name="cm_seq_name" type="VARCHAR(255)"/>
        </createTable>
        <insert tableName="acm_cost_id">
            <column name="cm_seq_num" value="100"></column>
            <column name="cm_seq_name" value="acm_cost"></column>
        </insert>
    </changeSet>

    <!-- adding discriminator column, needed for extensibility -->
    <changeSet id="10-acm_costsheet-add-discriminator-column-extensibility" author="riste.tutureski">
        <addColumn tableName="acm_costsheet">
            <column name="cm_class_name" type="VARCHAR(400)"/>
        </addColumn>
    </changeSet>

    <changeSet id="11-add-title-in-acm-costsheet-table" author="nadica.cuculova">
        <addColumn tableName="acm_costsheet">
            <column name="cm_costsheet_title" type="VARCHAR(1024)"/>
        </addColumn>
    </changeSet>

    <changeSet id="12-add-number-in-acm-costsheet-table-nullable" author="marjan.trifunov">
        <preConditions onError="MARK_RAN" onFail="MARK_RAN">
            <not>
                <columnExists tableName="acm_costsheet" columnName="cm_costsheet_number"/>
            </not>
        </preConditions>
        <addColumn tableName="acm_costsheet">
            <column name="cm_costsheet_number" type="VARCHAR(1024)">
                <constraints unique="true" uniqueConstraintName="uk_costsheet_number" nullable="true"/>
            </column>
        </addColumn>
    </changeSet>

    <changeSet id="13-populate-number-in-acm-costsheet-table" author="marjan.trifunov" dbms="mysql,mssql,oracle">
        <sql>
            <comment>Populate costsheet number column</comment>
            UPDATE acm_costsheet c
            SET c.cm_costsheet_number = c.cm_costsheet_id
            WHERE c.cm_costsheet_number IS NULL OR c.cm_costsheet_number = ''
        </sql>
    </changeSet>

    <changeSet id="13-populate-number-in-acm-costsheet-table" author="marjan.trifunov" dbms="postgresql">
        <sql>
            <comment>Populate costsheet number column</comment>
            UPDATE acm_costsheet
            SET cm_costsheet_number = cm_costsheet_id
            WHERE cm_costsheet_number IS NULL OR cm_costsheet_number = ''
        </sql>
    </changeSet>

    <changeSet id="14-require-costsheet-number" author="dmiller" failOnError="false">
        <addNotNullConstraint tableName="acm_costsheet" columnName="cm_costsheet_number" columnDataType="VARCHAR(1024)"/>
    </changeSet>

    <changeSet id="15-add-restricted-flag" author="ivana.shekerova">
        <addColumn tableName="acm_costsheet">
            <column name="cm_costsheet_restricted_flag" type="VARCHAR(32)" defaultValue="false">
                <constraints nullable="false"/>
            </column>
        </addColumn>
    </changeSet>

    <changeSet id="16-change_cm_cost_datetime" author="mario.gjurcheski" dbms="mysql">
        <modifyDataType tableName="acm_cost" columnName="cm_cost_date" newDataType="DATETIME"/>
        <modifyDataType tableName="acm_cost" columnName="cm_cost_created" newDataType="DATETIME"/>
        <modifyDataType tableName="acm_cost" columnName="cm_cost_modified" newDataType="DATETIME"/>
    </changeSet>

    <changeSet id="17-change_cm_costsheet_datetime" author="mario.gjurcheski" dbms="mysql">
        <modifyDataType tableName="acm_costsheet" columnName="cm_costsheet_created" newDataType="DATETIME"/>
        <modifyDataType tableName="acm_costsheet" columnName="cm_costsheet_modified" newDataType="DATETIME"/>
    </changeSet>
</databaseChangeLog>