<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-billing-invoice-table" author="sasko.tanaskoski" dbms="mysql,mssql">
        <createTable tableName="acm_billing_invoice">
            <column name="cm_billing_invoice_id" type="${idType}">
                <constraints primaryKey="true" primaryKeyName="pk_billing_invoice"/>
            </column>

            <column name="cm_billing_invoice_number" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>

            <column name="cm_parent_object_id" type="${fkIdType}">
                <constraints nullable="false"/>
            </column>

            <column name="cm_parent_object_type" type="VARCHAR(128)">
                <constraints nullable="false"/>
            </column>
            
            <column name="cm_billing_invoice_creator" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>

            <column name="cm_billing_invoice_created" type="${timestampType}">
                <constraints nullable="false"/>
            </column>
            
            <column name="cm_class_name" type="VARCHAR(400)" defaultValue="com.armedia.acm.services.billing.model.BillingInvoice">
                <constraints nullable="false"/>
            </column>
        </createTable>
        <createIndex tableName="acm_billing_invoice" indexName="idx_parent_object_id">
            <column name="cm_parent_object_id"/>
        </createIndex>
    </changeSet>

    <changeSet id="01-create-billing-invoice-table" author="sasko.tanaskoski" dbms="oracle,postgres">
        <createTable tableName="acm_billing_invoice">
            <column name="cm_billing_invoice_id" type="${idType}">
                <constraints primaryKey="true" primaryKeyName="pk_billing_invoice"/>
            </column>

            <column name="cm_billing_invoice_number" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>

            <column name="cm_parent_object_id" type="${fkIdType}">
                <constraints nullable="false"/>
            </column>

            <column name="cm_parent_object_type" type="VARCHAR(128)">
                <constraints nullable="false"/>
            </column>

            <column name="cm_billing_invoice_creator" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>

            <column name="cm_billing_invoice_created" type="${timestampType}">
                <constraints nullable="false"/>
            </column>

            <column name="cm_class_name" type="VARCHAR(400)" defaultValue="com.armedia.acm.services.billing.model.BillingInvoice">
                <constraints nullable="false"/>
            </column>
        </createTable>
        <createIndex tableName="acm_billing_invoice" indexName="idx_billing_invoice_parent_object_id">
            <column name="cm_parent_object_id"/>
        </createIndex>
    </changeSet>

    <changeSet id="02-acm-billing-invoice-paid-flag" author="sasko.tanaskoski" dbms="oracle,mssql,postgres">
        <addColumn tableName="acm_billing_invoice">
            <column name="cm_billing_invoice_paid_flag" type="VARCHAR(10)" defaultValue="false"/>
        </addColumn>
    </changeSet>

    <changeSet id="02-acm-billing-invoice-paid-flag" author="sasko.tanaskoski" dbms="mysql">
        <addColumn tableName="acm_billing_invoice">
            <column name="cm_billing_invoice_paid_flag" type="TEXT"/>
        </addColumn>
    </changeSet>

    <changeSet id="03-create-billing-invoice-id-table" author="sasko.tanaskoski">
        <createTable tableName="acm_billing_invoice_id">
            <column name="cm_seq_num" type="${idType}"/>
            <column name="cm_seq_name" type="VARCHAR(255)"/>
        </createTable>
        <insert tableName="acm_billing_invoice_id">
            <column name="cm_seq_num" value="100"></column>
            <column name="cm_seq_name" value="acm_billing_invoice"></column>
        </insert>
    </changeSet>
    
    <changeSet id="04-create-billing-invoice-item-table" author="sasko.tanaskoski">
        <createTable tableName="acm_billing_invoice_item">
            <column name="cm_billing_invoice_id" type="${fkIdType}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_billing_item_id" type="${fkIdType}">
                <constraints nullable="false"/>
            </column>
        </createTable>
        <addUniqueConstraint tableName="acm_billing_invoice_item" columnNames="cm_billing_invoice_id, cm_billing_item_id"/>
    </changeSet>

    <changeSet id="05-add-billing-invoice-modified-and-modifier" author="sasko.tanaskoski">
        <addColumn tableName="acm_billing_invoice">
            <column name="cm_billing_invoice_modified" type="${timestampType}">
                <constraints nullable="false"/>
            </column>
            <column name="cm_billing_invoice_modifier" type="VARCHAR(1024)">
                <constraints nullable="false"/>
            </column>
        </addColumn>
    </changeSet>

    <changeSet id="06-add-billing-invoice-ecm-file" author="marko.stojanoski">
        <addColumn tableName="acm_billing_invoice">
            <column name="cm_billing_invoice_ecm_file_id" type="${idType}">
                <constraints nullable="true"/>
            </column>
        </addColumn>
    </changeSet>

    <changeSet id="07-change_cm_billing_invoice_datetime" author="mario.gjurcheski" dbms="mysql">
        <modifyDataType tableName="acm_billing_invoice" columnName="cm_billing_invoice_modified" newDataType="DATETIME"/>
        <modifyDataType tableName="acm_billing_invoice" columnName="cm_billing_invoice_created" newDataType="DATETIME"/>
    </changeSet>

</databaseChangeLog>
