<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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-2.0.xsd"
  logicalFilePath="db-changelog-000085-multipage-sites.xml">
  
  <changeSet author="appian" id="tag-000084">
    <tagDatabase tag="000084"/>
  </changeSet>
  
  <changeSet author="appian" id="000085.1.0">
    <comment>Create new column icon_id</comment>
    <addColumn tableName="site_page">
      <column name="icon_id" type="${shortStringType}"/>
    </addColumn> 
  </changeSet>

  <changeSet author="appian" id="000085.1.1">
    <comment>Set icon_id based on page type</comment>
    <update tableName="site_page">
      <column name="icon_id" value="f0e7"/>
      <where>appian_object_type = '{http://www.appian.com/ae/types/2009}ProcessModel'</where>
    </update>
  </changeSet>
  
  <changeSet author="appian" id="000085.1.2">
    <comment>Set icon_id based on page type</comment>
    <update tableName="site_page">
      <column name="icon_id" value="f046"/>
      <where>appian_object_type = '{http://www.appian.com/ae/types/2009}TaskReport'</where>
    </update> 
  </changeSet>
  
  <changeSet author="appian" id="000085.2.0">
    <comment>Create new column visibility</comment>
    <addColumn tableName="site_page">
      <column name="visibility" type="${expressionStringType}" value="fn!true()"/>
    </addColumn>
  </changeSet>

  <changeSet author="appian" id="000085.3.0">
    <comment>Populate site_page.name with the appropriate site name</comment>
    <customChange class="com.appiancorp.tempo.rdbms.PopulateSitePageName"/>
  </changeSet>

  <changeSet author="appian" id="000085.4.0">
    <validCheckSum></validCheckSum>
    <preConditions onFail="MARK_RAN">
      <and>
        <not>
          <dbms type="db2"/>
        </not>
        <not>
          <or><dbms type="mysql"/><dbms type="mariadb"/></or>
        </not>
      </and>
    </preConditions>
    <dropIndex indexName="site_page_sid_id_fk_idx" tableName="site_page" />
  </changeSet>

  <changeSet author="appian" id="000085.4.1">
    <comment>Drop bad foreign key</comment>
    <dropForeignKeyConstraint baseTableName="site_page" constraintName="site_page_siteid_fk"/>
  </changeSet>

  <changeSet author="appian" id="000085.4.2">
    <comment>FK from site_page.site_id to site.id</comment>
    <addForeignKeyConstraint constraintName="site_page_siteid_fk" baseTableName="site_page"
      baseColumnNames="site_id" referencedTableName="site" referencedColumnNames="id"
      onDelete="CASCADE" />
  </changeSet>

  <changeSet author="appian" id="000085.4.3">
    <validCheckSum></validCheckSum>
    <preConditions onFail="MARK_RAN">
      <not>
        <or><dbms type="mysql"/><dbms type="mariadb"/></or>
      </not>
    </preConditions>
    <createIndex indexName="site_page_sid_id_fk_idx" tableName="site_page" unique="false">
      <column name="site_id"/>
    </createIndex>
  </changeSet>

</databaseChangeLog>
