<?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"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
                      http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.3.xsd">

  <property name="autoIncrement" value="true" dbms="mysql,mssql,h2,hsqldb"/>
  <property name="autoIncrement" value="false" dbms="oracle,postgresql"/>

  <!-- JCR Index Queue -->
  <changeSet author="jcr-index" id="1.0.0-1" dbms="oracle,postgresql">
    <createSequence sequenceName="SEQ_JCR_INDEXING_QUEUE" startValue="1"/>
  </changeSet>
  <changeSet author="jcr-index" id="1.0.0-2">
    <createTable tableName="JCR_INDEXING_QUEUE">
      <column name="INDEXING_QUEUE_ID" type="BIGINT" autoIncrement="${autoIncrement}" startWith="1">
        <constraints nullable="false" primaryKey="true" primaryKeyName="PK_JCR_INDEXING_QUEUE_ID"/>
      </column>
      <column name="JCR_UUID" type="NVARCHAR(70)">
        <constraints nullable="false"/>
      </column>
      <column name="WORKSPACE" type="NVARCHAR(30)">
        <constraints nullable="false"/>
      </column>
      <column name="OPERATION_TYPE" type="BIGINT">
        <constraints nullable="false"/>
      </column>
      <column name="IS_PARENT_CHANGE" type="BOOLEAN">
        <constraints nullable="false"/>
      </column>
      <column name="MODIFICATION_DATE" type="TIMESTAMP(3)" >
        <constraints nullable="false"/>
      </column>
    </createTable>
    <modifySql dbms="mysql">
      <append value=" ENGINE=INNODB CHARSET=UTF8 COLLATE utf8_general_ci"/>
    </modifySql>
  </changeSet>
  <changeSet author="jcr-index" id="1.0.0-3">
    <createTable tableName="JCR_INDEXING_QUEUE_NODES">
      <column name="JCR_INDEXING_QUEUE_NODE_ID" type="BIGINT" autoIncrement="${autoIncrement}" startWith="1">
        <constraints nullable="false" primaryKey="true" primaryKeyName="PK_JCR_INDEXING_QUEUE_NODE_ID"/>
      </column>
      <column name="INDEXING_QUEUE_ID" type="BIGINT">
        <constraints foreignKeyName="FK_JCR_INDEXING_QUEUE" references="JCR_INDEXING_QUEUE(INDEXING_QUEUE_ID)" nullable="false" deleteCascade="true" />
      </column>
      <column name="NODE_NAME" type="NVARCHAR(50)">
        <constraints nullable="false" />
      </column>
    </createTable>
    <modifySql dbms="mysql">
      <append value=" ENGINE=INNODB CHARSET=UTF8 COLLATE utf8_general_ci"/>
    </modifySql>
  </changeSet>
  <changeSet author="jcr-index" id="1.0.0-4" dbms="oracle,postgresql">
    <dropPrimaryKey tableName="JCR_INDEXING_QUEUE_NODES" constraintName="PK_JCR_INDEXING_QUEUE_NODE_ID" />
    <dropColumn tableName="JCR_INDEXING_QUEUE_NODES" columnName="JCR_INDEXING_QUEUE_NODE_ID" />
    <addPrimaryKey tableName="JCR_INDEXING_QUEUE_NODES" columnNames="INDEXING_QUEUE_ID, NODE_NAME" constraintName="PK_JCR_INDEXING_QUEUE_NODE_COMPOSITE_ID" />
  </changeSet>

  <changeSet author="jcr-index" id="1.0.0-5" dbms="hsqldb">
    <createSequence sequenceName="SEQ_JCR_INDEXING_QUEUE" startValue="1"/>
  </changeSet>

</databaseChangeLog>