<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
	xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.0.xsd
        http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">

	<changeSet id="1" author="flowable">
		<preConditions onFail="MARK_RAN">
			<not>
				<tableExists tableName="ACT_DE_MODEL"/>
				<tableExists tableName="ACT_DE_MODEL_HISTORY"/>
				<tableExists tableName="ACT_DE_MODEL_RELATION"/>
<!--                <tableExists tableName="ACT_RE_MODEL"/>-->
<!--                <tableExists tableName="ACT_RE_DEPLOYMENT"/>-->
<!--                <tableExists tableName="ACT_RU_EXECUTION"/>-->
<!--                <tableExists tableName="ACT_RE_PROCDEF"/>-->
<!--                <tableExists tableName="ACT_EVT_LOG"/>-->
<!--                <tableExists tableName="ACT_PROCDEF_INFO"/>-->
<!--                <tableExists tableName="ACT_RU_ACTINST"/>-->
			</not>
        </preConditions>
        <!-- model -->
		<createTable tableName="ACT_DE_MODEL">
			<column name="id" type="varchar(255)">
				<constraints primaryKey="true" nullable="false" />
			</column>
			<column name="name" type="varchar(400)">
				<constraints nullable="false" />
			</column>
			<column name="model_key" type="varchar(400)">
				<constraints nullable="false" />
			</column>
			<column name="description" type="varchar(4000)">
				<constraints nullable="true" />
			</column>
			<column name="model_comment" type="varchar(4000)">
				<constraints nullable="true" />
			</column>
			<column name="created" type="datetime(6)">
				<constraints nullable="true" />
			</column>
			<column name="created_by" type="varchar(255)">
				<constraints nullable="true" />
			</column>
			<column name="last_updated" type="datetime(6)">
				<constraints nullable="true" />
			</column>
			<column name="last_updated_by" type="varchar(255)">
				<constraints nullable="true" />
			</column>
			<column name="version" type="int">
				<constraints nullable="true" />
			</column>
			<column name="model_editor_json" type="longtext">
				<constraints nullable="true" />
			</column>
			<column name="thumbnail" type="longblob">
				<constraints nullable="true" />
			</column>
			<column name="model_type" type="int">
                <constraints nullable="true" />
            </column>
		</createTable>
        
        <createIndex indexName="idx_proc_mod_created"
            tableName="ACT_DE_MODEL" unique="false">
            <column name="created_by" />
        </createIndex>
        
        <!-- Model history -->
        <createTable tableName="ACT_DE_MODEL_HISTORY">
            <column name="id" type="varchar(255)">
                <constraints primaryKey="true" nullable="false" />
            </column>
            <column name="name" type="varchar(400)">
                <constraints nullable="false" />
            </column>
            <column name="model_key" type="varchar(400)">
				<constraints nullable="false" />
			</column>
            <column name="description" type="varchar(4000)">
                <constraints nullable="true" />
            </column>
            <column name="model_comment" type="varchar(4000)">
                <constraints nullable="true" />
            </column>
            <column name="created" type="datetime(6)">
                <constraints nullable="true" />
            </column>
            <column name="created_by" type="varchar(255)">
                <constraints nullable="true" />
            </column>
            <column name="last_updated" type="datetime(6)">
                <constraints nullable="true" />
            </column>
            <column name="last_updated_by" type="varchar(255)">
                <constraints nullable="true" />
            </column>
            <column name="removal_date" type="datetime(6)">
                <constraints nullable="true" />
            </column>
            <column name="version" type="int">
                <constraints nullable="true" />
            </column>
            <column name="model_editor_json" type="longtext">
                <constraints nullable="true" />
            </column>
            <column name="model_id" type="varchar(255)">
                <constraints nullable="false" />
            </column>
            <column name="model_type" type="int">
                <constraints nullable="true" />
            </column>
        </createTable>
        
        <createIndex indexName="idx_proc_mod_history_proc"
            tableName="ACT_DE_MODEL_HISTORY" unique="false">
            <column name="model_id" />
        </createIndex>
	
		<!-- MODEL RELATION -->
		
		 <createTable tableName="ACT_DE_MODEL_RELATION">
		 	<column name="id" type="varchar(255)">
                <constraints primaryKey="true" nullable="false" />
            </column>
            <column name="parent_model_id" type="varchar(255)" />
            <column name="model_id" type="varchar(255)" />
            <column name="relation_type" type="varchar(255)" />
        </createTable>
        
        <addForeignKeyConstraint baseColumnNames="parent_model_id"
			baseTableName="ACT_DE_MODEL_RELATION" constraintName="fk_relation_parent"
			referencedColumnNames="id" referencedTableName="ACT_DE_MODEL" />
			
		<addForeignKeyConstraint baseColumnNames="model_id"
			baseTableName="ACT_DE_MODEL_RELATION" constraintName="fk_relation_child"
			referencedColumnNames="id" referencedTableName="ACT_DE_MODEL" />
			
	</changeSet>
    
    <!-- 
        Bugfix: saving a model multiple times can sometimes give a conflict exception on sql server 
        due to the precision storage of the datetime data type. The datetime2 type has a better precision. 
    -->
    <changeSet id="2" author="flowable" dbms="mssql">
    
        <modifyDataType tableName="ACT_DE_MODEL" columnName="created" newDataType="datetime2" />
        <modifyDataType tableName="ACT_DE_MODEL" columnName="last_updated" newDataType="datetime2" />
        
        <modifyDataType tableName="ACT_DE_MODEL_HISTORY" columnName="created" newDataType="datetime2" />
        <modifyDataType tableName="ACT_DE_MODEL_HISTORY" columnName="last_updated" newDataType="datetime2" />
        <modifyDataType tableName="ACT_DE_MODEL_HISTORY" columnName="removal_date" newDataType="datetime2" />
        
    </changeSet>

    <changeSet id="3" author="flowable">
		<preConditions onFail="MARK_RAN">
			<not>
				<columnExists tableName="ACT_DE_MODEL" columnName="tenant_id"/>
				<columnExists tableName="ACT_DE_MODEL_HISTORY" columnName="tenant_id"/>
			</not>
        </preConditions>
        <addColumn tableName="ACT_DE_MODEL">
            <column  name="tenant_id" type="varchar(255)" />
        </addColumn>
        <addColumn tableName="ACT_DE_MODEL_HISTORY">
            <column  name="tenant_id" type="varchar(255)" />
        </addColumn>
    </changeSet>

</databaseChangeLog>