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

  <!-- Managing both DB that use sequences and db that use auto increment -->
  <property name="autoIncrement" value="true" dbms="mysql,mssql,h2,sybase,db2,hsqldb"/>
  <property name="autoIncrement" value="false" dbms="oracle,postgresql"/>

  <!-- Managing auto generation of timestamp by Database -->
  <property name="now" value="now()" dbms="mysql,hsqldb,postgresql,h2"/>
  <property name="now" value="sysdate" dbms="oracle"/>
  <property name="now" value="CURRENT_TIMESTAMP" dbms="mssql"/>


  <changeSet author="wiki" id="1.0.0-1">
    <createTable tableName="WIKI_WIKIS">
      <column name="WIKI_ID" type="BIGINT" autoIncrement="${autoIncrement}" startWith="1">
        <constraints nullable="false" primaryKey="true" primaryKeyName="PK_WIKI_WIKIS_ID"/>
      </column>
      <column name="NAME" type="NVARCHAR(550)"/>
      <column name="OWNER" type="NVARCHAR(200)">
        <constraints nullable="false"/>
      </column>
      <column name="TYPE" type="NVARCHAR(50)">
        <constraints nullable="false"/>
      </column>
      <column name="WIKI_HOME" type="BIGINT"/>
      <column name="SYNTAX" type="NVARCHAR(30)"/>
      <column name="ALLOW_MULTI_SYNTAX" type="BOOLEAN"/>
    </createTable>
    <modifySql dbms="mysql">
      <append value=" ENGINE=INNODB CHARSET=UTF8 COLLATE utf8_general_ci"/>
    </modifySql>
  </changeSet>

  <changeSet author="wiki" id="1.0.0-2">
    <createTable tableName="WIKI_PAGES">
      <column name="PAGE_ID" type="BIGINT" autoIncrement="${autoIncrement}" startWith="1">
        <constraints nullable="false" primaryKey="true" primaryKeyName="PK_WIKI_PAGES_ID"/>
      </column>
      <column name="WIKI_ID" type="BIGINT">
        <constraints nullable="false"/>
      </column>
      <column name="PARENT_PAGE_ID" type="BIGINT"/>
      <column name="AUTHOR" type="NVARCHAR(200)"/>
      <column name="NAME" type="NVARCHAR(550)">
        <constraints nullable="false"/>
      </column>
      <column name="OWNER" type="NVARCHAR(200)"/>
      <column name="CREATED_DATE" type="TIMESTAMP">
        <constraints nullable="false"/>
      </column>
      <column name="UPDATED_DATE" type="TIMESTAMP">
        <constraints nullable="false"/>
      </column>
      <column name="CONTENT" type="CLOB"/>
      <column name="SYNTAX" type="NVARCHAR(30)"/>
      <column name="TITLE" type="NVARCHAR(550)"/>
      <column name="EDITION_COMMENT" type="NVARCHAR(1000)"/>
      <column name="URL" type="NVARCHAR(500)"/>
      <column name="MINOR_EDIT" type="BOOLEAN"/>
      <column name="ACTIVITY_ID" type="NVARCHAR(36)"/>
      <column name="DELETED" type="BOOLEAN" defaultValueBoolean="false"/>
    </createTable>
    <modifySql dbms="mysql">
      <append value=" ENGINE=INNODB CHARSET=UTF8 COLLATE utf8_general_ci"/>
    </modifySql>
  </changeSet>

  <changeSet author="wiki" id="1.0.0-3">
    <createTable tableName="WIKI_PAGE_ATTACHMENTS">
      <column name="ATTACHMENT_ID" type="BIGINT" autoIncrement="${autoIncrement}" startWith="1">
        <constraints nullable="false" primaryKey="true" primaryKeyName="PK_WIKI_ATTACHEMENTS_ID"/>
      </column>
      <column name="WIKI_PAGE_ID" type="BIGINT">
        <constraints nullable="false" foreignKeyName="FK_WIKI_ATTACHMENTS_PAGES" references="WIKI_PAGES(PAGE_ID)" />
      </column>
      <column name="NAME" type="NVARCHAR(550)">
        <constraints nullable="false"/>
      </column>
      <column name="CREATOR" type="NVARCHAR(200)"/>
      <column name="CREATED_DATE" type="TIMESTAMP">
        <constraints nullable="false"/>
      </column>
      <column name="UPDATED_DATE" type="TIMESTAMP">
        <constraints nullable="false"/>
      </column>
      <column name="TITLE" type="NVARCHAR(550)"/>
      <column name="FULL_TITLE" type="NVARCHAR(550)"/>
      <column name="CONTENT" type="LONGBLOB"/>
      <column name="MIMETYPE" type="NVARCHAR(100)"/>
    </createTable>
    <modifySql dbms="mysql">
      <append value=" ENGINE=INNODB CHARSET=UTF8 COLLATE utf8_general_ci"/>
    </modifySql>
  </changeSet>

  <changeSet author="wiki" id="1.0.0-5">
    <createTable tableName="WIKI_PAGE_VERSIONS">
      <column name="PAGE_VERSION_ID" type="BIGINT" autoIncrement="${autoIncrement}" startWith="1">
        <constraints nullable="false" primaryKey="true" primaryKeyName="PK_WIKI_PAGE_VERSIONS_ID"/>
      </column>
      <column name="VERSION_NUMBER" type="BIGINT">
        <constraints nullable="false"/>
      </column>
      <column name="AUTHOR" type="NVARCHAR(200)"/>
      <column name="NAME" type="NVARCHAR(550)">
        <constraints nullable="false"/>
      </column>
      <column name="TITLE" type="NVARCHAR(550)"/>
      <column name="CREATED_DATE" type="TIMESTAMP">
        <constraints nullable="false"/>
      </column>
      <column name="UPDATED_DATE" type="TIMESTAMP">
        <constraints nullable="false"/>
      </column>
      <column name="CONTENT" type="CLOB"/>
      <column name="SYNTAX" type="NVARCHAR(30)"/>
      <column name="EDITION_COMMENT" type="NVARCHAR(1000)"/>
      <column name="MINOR_EDIT" type="BOOLEAN"/>
      <column name="PAGE_ID" type="BIGINT">
        <constraints nullable="false"/>
      </column>
    </createTable>
    <modifySql dbms="mysql">
      <append value=" ENGINE=INNODB CHARSET=UTF8 COLLATE utf8_general_ci"/>
    </modifySql>
  </changeSet>

  <changeSet author="wiki" id="1.0.0-6">
    <createTable tableName="WIKI_PAGE_MOVES">
      <column name="PAGE_MOVE_ID" type="BIGINT" autoIncrement="${autoIncrement}" startWith="1">
        <constraints nullable="false" primaryKey="true" primaryKeyName="PK_WIKI_PAGE_MOVES_ID"/>
      </column>
      <column name="WIKI_TYPE" type="NVARCHAR(50)">
        <constraints nullable="false"/>
      </column>
      <column name="WIKI_OWNER" type="NVARCHAR(100)">
        <constraints nullable="false"/>
      </column>
      <column name="PAGE_NAME" type="NVARCHAR(550)">
        <constraints nullable="false"/>
      </column>
      <column name="CREATED_DATE" type="TIMESTAMP">
        <constraints nullable="false"/>
      </column>
      <column name="PAGE_ID" type="BIGINT">
        <constraints nullable="false"/>
      </column>
    </createTable>
    <modifySql dbms="mysql">
      <append value=" ENGINE=INNODB CHARSET=UTF8 COLLATE utf8_general_ci"/>
    </modifySql>
  </changeSet>

  <changeSet author="wiki" id="1.0.0-7">
    <createTable tableName="WIKI_WATCHERS">
      <column name="USERNAME" type="NVARCHAR(200)">
        <constraints nullable="false"/>
      </column>
      <column name="PAGE_ID" type="BIGINT">
        <constraints nullable="false"/>
      </column>
    </createTable>
    <modifySql dbms="mysql">
      <append value=" ENGINE=INNODB CHARSET=UTF8 COLLATE utf8_general_ci"/>
    </modifySql>
  </changeSet>

  <changeSet author="wiki" id="1.0.0-8">
    <createTable tableName="WIKI_EMOTION_ICONS">
      <column name="EMOTION_ICON_ID" type="BIGINT" autoIncrement="${autoIncrement}" startWith="1">
        <constraints nullable="false" primaryKey="true" primaryKeyName="PK_WIKI_EMOTION_ICONS_ID"/>
      </column>
      <column name="NAME" type="NVARCHAR(550)">
        <constraints nullable="false"/>
      </column>
      <column name="IMAGE" type="LONGBLOB"/>
    </createTable>
    <modifySql dbms="mysql">
      <append value=" ENGINE=INNODB CHARSET=UTF8 COLLATE utf8_general_ci"/>
    </modifySql>
  </changeSet>

  <changeSet author="wiki" id="1.0.0-9">
    <createTable tableName="WIKI_TEMPLATES">
      <column name="TEMPLATE_ID" type="BIGINT" autoIncrement="${autoIncrement}" startWith="1">
        <constraints nullable="false" primaryKey="true" primaryKeyName="PK_WIKI_TEMPLATES_ID"/>
      </column>
      <column name="WIKI_ID" type="BIGINT">
        <constraints nullable="true"/>
      </column>
      <column name="AUTHOR" type="NVARCHAR(200)"/>
      <column name="NAME" type="NVARCHAR(550)">
        <constraints nullable="false"/>
      </column>
      <column name="DESCRIPTION" type="NVARCHAR(1000)"/>
      <column name="CONTENT" type="CLOB"/>
      <column name="SYNTAX" type="NVARCHAR(30)"/>
      <column name="TITLE" type="NVARCHAR(550)"/>
      <column name="CREATED_DATE" type="TIMESTAMP">
        <constraints nullable="false"/>
      </column>
      <column name="UPDATED_DATE" type="TIMESTAMP">
        <constraints nullable="false"/>
      </column>
    </createTable>
    <modifySql dbms="mysql">
      <append value=" ENGINE=INNODB CHARSET=UTF8 COLLATE utf8_general_ci"/>
    </modifySql>
  </changeSet>

  <changeSet author="wiki" id="1.0.0-10">
    <createTable tableName="WIKI_DRAFT_PAGES">
      <column name="DRAFT_PAGE_ID" type="BIGINT" autoIncrement="${autoIncrement}" startWith="1">
        <constraints nullable="false" primaryKey="true" primaryKeyName="PK_WIKI_DRAFT_PAGES_ID"/>
      </column>
      <column name="TARGET_PAGE_ID" type="BIGINT"/>
      <column name="TARGET_PAGE_REVISION" type="NVARCHAR(50)"/>
      <column name="NEW_PAGE" type="BOOLEAN"/>
      <column name="AUTHOR" type="NVARCHAR(200)"/>
      <column name="NAME" type="NVARCHAR(550)">
        <constraints nullable="false"/>
      </column>
      <column name="TITLE" type="NVARCHAR(550)"/>
      <column name="CONTENT" type="CLOB"/>
      <column name="SYNTAX" type="NVARCHAR(30)"/>
      <column name="CREATED_DATE" type="TIMESTAMP">
        <constraints nullable="false"/>
      </column>
      <column name="UPDATED_DATE" type="TIMESTAMP">
        <constraints nullable="false"/>
      </column>
    </createTable>
    <modifySql dbms="mysql">
      <append value=" ENGINE=INNODB CHARSET=UTF8 COLLATE utf8_general_ci"/>
    </modifySql>
  </changeSet>

  <changeSet author="wiki" id="1.0.0-11">
    <createTable tableName="WIKI_DRAFT_ATTACHMENTS">
      <column name="ATTACHMENT_ID" type="BIGINT" autoIncrement="${autoIncrement}" startWith="1">
        <constraints nullable="false" primaryKey="true" primaryKeyName="PK_WIKI_DRAFT_ATTACHMENTS_ID"/>
      </column>
      <column name="DRAFT_PAGE_ID" type="BIGINT">
        <constraints nullable="false" foreignKeyName="FK_WIKI_DRAFTATTACHMENTS_PAGES" references="WIKI_DRAFT_PAGES(DRAFT_PAGE_ID)" />
      </column>
      <column name="NAME" type="NVARCHAR(550)"/>
      <column name="CREATOR" type="NVARCHAR(200)"/>
      <column name="CREATED_DATE" type="TIMESTAMP">
        <constraints nullable="false"/>
      </column>
      <column name="UPDATED_DATE" type="TIMESTAMP">
        <constraints nullable="false"/>
      </column>
      <column name="TITLE" type="NVARCHAR(550)"/>
      <column name="FULL_TITLE" type="NVARCHAR(550)"/>
      <column name="CONTENT" type="LONGBLOB"/>
      <column name="MIMETYPE" type="NVARCHAR(100)"/>
    </createTable>
    <modifySql dbms="mysql">
      <append value=" ENGINE=INNODB CHARSET=UTF8 COLLATE utf8_general_ci"/>
    </modifySql>
  </changeSet>

  <changeSet author="wiki" id="1.0.0-13">
    <createTable tableName="WIKI_WIKI_PERMISSIONS">
      <column name="WIKI_ID" type="BIGINT">
        <constraints nullable="false"/>
      </column>
      <column name="IDENTITY" type="NVARCHAR(200)">
        <constraints nullable="false"/>
      </column>
      <column name="IDENTITY_TYPE" type="NVARCHAR(50)">
        <constraints nullable="false"/>
      </column>
      <column name="PERMISSION" 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="wiki" id="1.0.0-14">
    <createTable tableName="WIKI_PAGE_PERMISSIONS">
      <column name="PAGE_ID" type="BIGINT">
        <constraints nullable="false"/>
      </column>
      <column name="IDENTITY" type="NVARCHAR(200)">
        <constraints nullable="false"/>
      </column>
      <column name="IDENTITY_TYPE" type="NVARCHAR(50)">
        <constraints nullable="false"/>
      </column>
      <column name="PERMISSION" 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="wiki" id="1.0.0-15">
    <createTable tableName="WIKI_PAGES_RELATED_PAGES">
      <column name="PAGE_ID" type="BIGINT">
        <constraints nullable="false"/>
      </column>
      <column name="RELATED_PAGE_ID" type="BIGINT">
        <constraints nullable="false"/>
      </column>
    </createTable>
    <modifySql dbms="mysql">
      <append value=" ENGINE=INNODB CHARSET=UTF8 COLLATE utf8_general_ci"/>
    </modifySql>
  </changeSet>

  <changeSet author="wiki" id="1.0.0-16">
    <addForeignKeyConstraint baseColumnNames="WIKI_HOME" baseTableName="WIKI_WIKIS"
                             constraintName="FK_WIKI_WIKIS_PAGES_01" deferrable="false" initiallyDeferred="false"
                             onDelete="SET NULL" onUpdate="NO ACTION"
                             referencedColumnNames="PAGE_ID" referencedTableName="WIKI_PAGES"/>
  </changeSet>

  <changeSet author="wiki" id="1.0.0-17">
    <addForeignKeyConstraint baseColumnNames="WIKI_ID" baseTableName="WIKI_PAGES"
                             constraintName="FK_WIKI_PAGES_WIKIS_01" deferrable="false" initiallyDeferred="false"
                             onDelete="NO ACTION" onUpdate="NO ACTION"
                             referencedColumnNames="WIKI_ID" referencedTableName="WIKI_WIKIS"/>
  </changeSet>

  <changeSet author="wiki" id="1.0.0-18">
    <addForeignKeyConstraint baseColumnNames="PARENT_PAGE_ID" baseTableName="WIKI_PAGES"
                             constraintName="FK_WIKI_PAGES_PAGES_01" deferrable="false" initiallyDeferred="false"
                             onDelete="NO ACTION" onUpdate="NO ACTION"
                             referencedColumnNames="PAGE_ID" referencedTableName="WIKI_PAGES"/>
  </changeSet>

  <changeSet author="wiki" id="1.0.0-19">
    <addForeignKeyConstraint baseColumnNames="PAGE_ID" baseTableName="WIKI_PAGE_MOVES"
                             constraintName="FK_WIKI_PAGE_MOVES_PAGES_01" deferrable="false"
                             initiallyDeferred="false"
                             onDelete="NO ACTION" onUpdate="NO ACTION"
                             referencedColumnNames="PAGE_ID" referencedTableName="WIKI_PAGES"/>
  </changeSet>

  <changeSet author="wiki" id="1.0.0-20">
    <addForeignKeyConstraint baseColumnNames="PAGE_ID" baseTableName="WIKI_WATCHERS"
                             constraintName="FK_WIKI_WATCHERS_PAGES_01" deferrable="false"
                             initiallyDeferred="false"
                             onDelete="NO ACTION" onUpdate="NO ACTION"
                             referencedColumnNames="PAGE_ID" referencedTableName="WIKI_PAGES"/>
  </changeSet>

  <changeSet author="wiki" id="1.0.0-21">
    <addForeignKeyConstraint baseColumnNames="WIKI_ID" baseTableName="WIKI_TEMPLATES"
                             constraintName="FK_WIKI_TEMPLATES_WIKIS_01" deferrable="false"
                             initiallyDeferred="false"
                             onDelete="NO ACTION" onUpdate="NO ACTION"
                             referencedColumnNames="WIKI_ID" referencedTableName="WIKI_WIKIS"/>
  </changeSet>

  <changeSet author="wiki" id="1.0.0-22">
    <addForeignKeyConstraint baseColumnNames="TARGET_PAGE_ID" baseTableName="WIKI_DRAFT_PAGES"
                             constraintName="FK_WIKI_DRAFT_PAGES_PAGES_01" deferrable="false"
                             initiallyDeferred="false"
                             onDelete="CASCADE" onUpdate="NO ACTION"
                             referencedColumnNames="PAGE_ID" referencedTableName="WIKI_PAGES"/>
  </changeSet>

  <changeSet author="wiki" id="1.0.0-23">
    <addForeignKeyConstraint baseColumnNames="PAGE_ID" baseTableName="WIKI_PAGE_VERSIONS"
                             constraintName="FK_WIKI_PAGE_VERSIONS_PAGES_01" deferrable="false"
                             initiallyDeferred="false"
                             onDelete="NO ACTION" onUpdate="NO ACTION"
                             referencedColumnNames="PAGE_ID" referencedTableName="WIKI_PAGES"/>
  </changeSet>

  <changeSet author="wiki" id="1.0.0-25">
    <addForeignKeyConstraint baseColumnNames="WIKI_ID" baseTableName="WIKI_WIKI_PERMISSIONS"
                             constraintName="FK_WIKI_WIKIS_PERM_WIKIS_01" deferrable="false"
                             initiallyDeferred="false"
                             onDelete="NO ACTION" onUpdate="NO ACTION"
                             referencedColumnNames="WIKI_ID" referencedTableName="WIKI_WIKIS"/>
  </changeSet>

  <changeSet author="wiki" id="1.0.0-26">
    <addForeignKeyConstraint baseColumnNames="PAGE_ID" baseTableName="WIKI_PAGE_PERMISSIONS"
                             constraintName="FK_WIKI_PAGES_PERM_PAGES_01" deferrable="false"
                             initiallyDeferred="false"
                             onDelete="NO ACTION" onUpdate="NO ACTION"
                             referencedColumnNames="PAGE_ID" referencedTableName="WIKI_PAGES"/>
  </changeSet>

  <changeSet author="wiki" id="1.0.0-27">
    <addForeignKeyConstraint baseColumnNames="RELATED_PAGE_ID" baseTableName="WIKI_PAGES_RELATED_PAGES"
                             constraintName="FK_WIKI_RELATED_PAGES_PAGES_01" deferrable="false"
                             initiallyDeferred="false"
                             onDelete="NO ACTION" onUpdate="NO ACTION"
                             referencedColumnNames="PAGE_ID" referencedTableName="WIKI_PAGES"/>
  </changeSet>

  <changeSet author="wiki" id="1.0.0-28">
    <addForeignKeyConstraint baseColumnNames="PAGE_ID" baseTableName="WIKI_PAGES_RELATED_PAGES"
                             constraintName="FK_WIKI_RELATED_PAGES_PAGES_02" deferrable="false"
                             initiallyDeferred="false"
                             onDelete="NO ACTION" onUpdate="NO ACTION"
                             referencedColumnNames="PAGE_ID" referencedTableName="WIKI_PAGES"/>
  </changeSet>

  <changeSet author="wiki" id="1.0.0-29">
    <addUniqueConstraint columnNames="USERNAME, PAGE_ID" tableName="WIKI_WATCHERS"
                         constraintName="UK_WIKI_WATCHERS_01"/>
  </changeSet>

  <changeSet author="wiki" id="1.0.0-30">
    <addPrimaryKey columnNames="PAGE_ID, RELATED_PAGE_ID" tableName="WIKI_PAGES_RELATED_PAGES"
                   constraintName="PK_WIKI_PAGES_RELATED_PAGES"/>
  </changeSet>


  <!-- Sequences -->

  <changeSet author="wiki" id="1.0.0-31" dbms="oracle,postgresql">
    <createSequence sequenceName="SEQ_WIKI_WIKIS_WIKI_ID" startValue="1"/>
  </changeSet>

  <changeSet author="wiki" id="1.0.0-32" dbms="oracle,postgresql">
    <createSequence sequenceName="SEQ_WIKI_PAGES_PAGE_ID" startValue="1"/>
  </changeSet>

  <changeSet author="wiki" id="1.0.0-33" dbms="oracle,postgresql">
    <createSequence sequenceName="SEQ_WIKI_PAGE_ATTACH_ATTACH_ID" startValue="1"/>
  </changeSet>

  <changeSet author="wiki" id="1.0.0-34" dbms="oracle,postgresql">
    <createSequence sequenceName="SEQ_WIKI_PAGE_VERSIONS_VERS_ID" startValue="1"/>
  </changeSet>

  <changeSet author="wiki" id="1.0.0-35" dbms="oracle,postgresql">
    <createSequence sequenceName="SEQ_WIKI_PAGE_MOVES_MOVE_ID" startValue="1"/>
  </changeSet>

  <changeSet author="wiki" id="1.0.0-36" dbms="oracle,postgresql">
    <createSequence sequenceName="SEQ_WIKI_EMOTION_ICONS_ICON_ID" startValue="1"/>
  </changeSet>

  <changeSet author="wiki" id="1.0.0-37" dbms="oracle,postgresql">
    <createSequence sequenceName="SEQ_WIKI_TEMPLATES_TEMPLATE_ID" startValue="1"/>
  </changeSet>

  <changeSet author="wiki" id="1.0.0-38" dbms="oracle,postgresql">
    <createSequence sequenceName="SEQ_WIKI_DRAFT_PAGES_DRAFT_ID" startValue="1"/>
  </changeSet>

</databaseChangeLog>