<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-timesheet-table" author="riste.tutureski" dbms="oracle,postgresql,mssql">
        <createTable tableName="acm_timesheet">
            <column name="cm_timesheet_id" type="${idType}">
                <constraints primaryKey="true" primaryKeyName="pk_cm_timesheet_id"/>
            </column>
            <column name="cm_timesheet_user_id" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_timesheet_start_date" type="${timestampType}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_timesheet_end_date" type="${timestampType}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_timesheet_status" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_timesheet_created" type="${timestampType}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_timesheet_creator" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_timesheet_modified" type="${timestampType}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_timesheet_modifier" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
        </createTable>
    </changeSet>

    <changeSet id="01-create-timesheet-table" author="riste.tutureski" dbms="mysql">
        <createTable tableName="acm_timesheet">
            <column name="cm_timesheet_id" type="${idType}">
                <constraints primaryKey="true" primaryKeyName="pk_cm_timesheet_id"/>
            </column>
            <column name="cm_timesheet_user_id" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_timesheet_start_date" type="${timestampType}" defaultValueComputed="${timestampFunction}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_timesheet_end_date" type="${timestampType}" defaultValueComputed="${timestampFunction}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_timesheet_status" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_timesheet_created" type="${timestampType}" defaultValueComputed="${timestampFunction}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_timesheet_creator" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_timesheet_modified" type="${timestampType}" defaultValueComputed="${timestampFunction}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_timesheet_modifier" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
        </createTable>
    </changeSet>

    <changeSet id="02-create-time-table" author="riste.tutureski" dbms="oracle,postgresql,mssql">
        <createTable tableName="acm_time">
            <column name="cm_time_id" type="${idType}">
                <constraints primaryKey="true" primaryKeyName="pk_cm_time_id"/>
            </column>
            <column name="cm_time_timesheet_id" type="${fkIdType}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_time_code" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_time_type" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_time_date" type="${timestampType}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_time_value" type="NUMBER(32,0)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_time_created" type="${timestampType}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_time_creator" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_time_modified" type="${timestampType}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_time_modifier" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
        </createTable>
        <addForeignKeyConstraint baseTableName="acm_time" baseColumnNames="cm_time_timesheet_id" constraintName="fk_time_timesheet_id"
            referencedTableName="acm_timesheet" referencedColumnNames="cm_timesheet_id" deferrable="${deferrable}"
            initiallyDeferred="${initially.deferred}"/>
    </changeSet>

    <changeSet id="02-create-time-table" author="riste.tutureski" dbms="mysql">
        <createTable tableName="acm_time">
            <column name="cm_time_id" type="${idType}">
                <constraints primaryKey="true" primaryKeyName="pk_cm_time_id"/>
            </column>
            <column name="cm_time_timesheet_id" type="${fkIdType}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_time_code" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_time_type" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_time_date" type="${timestampType}" defaultValueComputed="${timestampFunction}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_time_value" type="NUMBER(32,0)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_time_created" type="${timestampType}" defaultValueComputed="${timestampFunction}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_time_creator" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
            <column name="cm_time_modified" type="${timestampType}" defaultValueComputed="${timestampFunction}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_time_modifier" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
        </createTable>
        <addForeignKeyConstraint baseTableName="acm_time" baseColumnNames="cm_time_timesheet_id" constraintName="fk_time_timesheet_id"
            referencedTableName="acm_timesheet" referencedColumnNames="cm_timesheet_id" deferrable="${deferrable}"
            initiallyDeferred="${initially.deferred}"/>
    </changeSet>

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

    <changeSet id="04-add-details-column-in-acm-timesheet-table" author="riste.tutureski">
        <addColumn tableName="acm_timesheet">
            <column name="cm_timesheet_details" type="CLOB">
                <constraints nullable="true"/>
            </column>
        </addColumn>
    </changeSet>

    <changeSet id="05-add-object_id-column-in-acm-time-table" author="riste.tutureski">
        <addColumn tableName="acm_time">
            <column name="cm_time_object_id" type="${fkIdType}">
                <!-- Set nullable to true because other that Complaint and Case, we have Other types
                which don't have any ID in database -->
                <constraints nullable="true"/>
            </column>
        </addColumn>
    </changeSet>

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

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

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

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

    <changeSet id="10-add-title-in-acm-timesheet-table" author="nadica.cuculova">
        <addColumn tableName="acm_timesheet">
            <column name="cm_timesheet_title" type="VARCHAR(1024)"/>
        </addColumn>
    </changeSet>

    <changeSet id="11-acm-timesheet-add-discriminator-column-extensibility" author="marko.stojanoski">
        <addColumn tableName="acm_timesheet">
            <column name="cm_class_name" type="VARCHAR(400)"/>
        </addColumn>
    </changeSet>

    <changeSet id="12-acm-time-add-discriminator-column-extensibility" author="marko.stojanoski">
        <addColumn tableName="acm_time">
            <column name="cm_class_name" type="VARCHAR(400)"/>
        </addColumn>
    </changeSet>

    <changeSet id="13-acm-time-add-charge-code-column" author="marko.stojanoski">
        <addColumn tableName="acm_time">
            <column name="cm_time_charge_code" type="VARCHAR(400)"/>
        </addColumn>
    </changeSet>

    <changeSet id="14-acm-time-add-total-cost-column" author="marko.stojanoski">
        <addColumn tableName="acm_time">
            <column name="cm_time_total_cost" type="NUMBER(32,2)"/>
        </addColumn>
    </changeSet>

    <changeSet id="15-acm-time-total-cost-column-default-value" author="marko.stojanoski">
        <addDefaultValue tableName="acm_time" columnName="cm_time_total_cost" columnDataType="NUMBER(32,2)" defaultValueNumeric="0"/>
    </changeSet>

    <changeSet id="16-acm-time-update-total-cost-column" author="marko.stojanoski">
        <update tableName="acm_time">
            <column name="cm_time_total_cost" valueNumeric="0"/>
            <where>cm_time_total_cost IS NULL</where>
        </update>
    </changeSet>

    <!-- good old mysql doesn't allow functions in default value specs so we have to create it as nullable, then set the values,
         then make it not null -->
    <changeSet id="17-add-number-in-acm-timesheet-table" author="marjan.trifunov">
        <addColumn tableName="acm_timesheet">
            <column name="cm_timesheet_number" type="VARCHAR(1024)">
                <constraints unique="true" uniqueConstraintName="uk_timesheet_number" nullable="true"/>
            </column>
        </addColumn>
    </changeSet>

    <changeSet id="18-set-timesheet-number-for-existing-timesheets" author="marjan.trifunov">
        <update tableName="acm_timesheet">
            <column name="cm_timesheet_number" valueComputed="${strToIntLeader}cm_timesheet_id${strToIntTrailer}"/>
            <where>cm_timesheet_number IS NULL</where>
        </update>
    </changeSet>

    <changeSet id="19-make-timesheet-number-required" author="dmiller">
        <addNotNullConstraint tableName="acm_timesheet" columnName="cm_timesheet_number" columnDataType="VARCHAR(1024)"/>
    </changeSet>

    <changeSet id="01-change_cm_timesheet_datetime" author="mario.gjurcheski" dbms="mysql">
        <modifyDataType tableName="acm_timesheet" columnName="cm_timesheet_start_date" newDataType="DATETIME"/>
        <modifyDataType tableName="acm_timesheet" columnName="cm_timesheet_end_date" newDataType="DATETIME"/>
        <modifyDataType tableName="acm_timesheet" columnName="cm_timesheet_created" newDataType="DATETIME"/>
        <modifyDataType tableName="acm_timesheet" columnName="cm_timesheet_modified" newDataType="DATETIME"/>

    </changeSet>

    <changeSet id="01-change_cm_time_datetime" author="mario.gjurcheski" dbms="mysql">
        <modifyDataType tableName="acm_time" columnName="cm_time_date" newDataType="DATETIME"/>
        <modifyDataType tableName="acm_time" columnName="cm_time_created" newDataType="DATETIME"/>
        <modifyDataType tableName="acm_time" columnName="cm_time_modified" newDataType="DATETIME"/>
    </changeSet>
</databaseChangeLog>