<?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-000036.xml">

  <changeSet author="appian" id="tag-000035">
    <tagDatabase tag="000035"/>
  </changeSet>

  <changeSet author="appian" id="000036.1.0">
    <preConditions onFail="MARK_RAN"><not><dbms type="oracle"/></not></preConditions>
    <comment>Populate the usr table for existing Tempo news entry authors.</comment>
    <sql>
      insert into usr (username) (select distinct fe.author from tp_feed_entry fe
        where not exists (select u.username from usr u where u.username = fe.author))
    </sql>
  </changeSet>
  <changeSet author="appian" id="000036.1.1">
    <preConditions onFail="MARK_RAN"><dbms type="oracle"/></preConditions>
    <comment>Populate the usr table for existing Tempo news entry authors.</comment>
    <sql>
      insert into usr (id, username) select usr_sq.nextval, tmp.author from (
        select distinct fe.author from tp_feed_entry fe
        where not exists (select u.username from usr u where u.username = fe.author)) tmp
    </sql>
  </changeSet>

  <changeSet author="appian" id="000036.1.2">
    <comment>Create a new column author_id in tp_feed_entry</comment>
    <addColumn tableName="tp_feed_entry">
      <column name="author_id" type="${longType}"/>
    </addColumn>
  </changeSet>
  
  <changeSet author="appian" id="000036.1.3">
    <comment>Migrate data from author to author_id</comment>
    <sql>
      update tp_feed_entry
      set author_id = (
        select usr.id
        from usr
        where usr.username = tp_feed_entry.author
      );
    </sql>
  </changeSet>

  <changeSet author="appian" id="000036.1.4">
    <comment>Set author_id column to be non-nullable</comment>
    <addNotNullConstraint tableName="tp_feed_entry" columnName="author_id" columnDataType="${longType}"/>
  </changeSet>

  <changeSet author="appian" id="000036.1.5">
    <addForeignKeyConstraint baseColumnNames="author_id" baseTableName="tp_feed_entry"
      constraintName="tp_feed_entry_auth_fk" deferrable="false" initiallyDeferred="false"
      referencedColumnNames="id" referencedTableName="usr" referencesUniqueColumn="false"/>
  </changeSet>
  
  <!-- MySQL automatically creates indexes for FK columns, but other DBs don't, so we have to do it ourselves. -->
  <changeSet author="appian" id="000036.1.6">
    <validCheckSum></validCheckSum>
    <preConditions onFail="MARK_RAN"><not><or><dbms type="mysql"/><dbms type="mariadb"/></or></not></preConditions>
    <createIndex indexName="tp_feed_entry_auth_fk" tableName="tp_feed_entry" unique="false">
      <column name="author_id"/>
    </createIndex>
  </changeSet>
  
  <changeSet author="appian" id="000036.1.7">
    <comment>Drop the old author column</comment>
    <dropColumn tableName="tp_feed_entry" columnName="author"/>
  </changeSet>
</databaseChangeLog>
