<?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.4.xsd"
  logicalFilePath="changelog-6.11.0">

  <changeSet author="author" id="changelog-6.11.0-timescale" runInTransaction="false">

    <preConditions onFail="MARK_RAN">
      <dbms type="postgresql" />
      <sqlCheck expectedResult="1">
        SELECT COUNT(*) FROM pg_available_extensions WHERE name = 'timescaledb';
      </sqlCheck>
    </preConditions>

    <sql splitStatements="true" endDelimiter=";">
      CREATE EXTENSION IF NOT EXISTS timescaledb;

      ALTER TABLE tc_actions DROP CONSTRAINT IF EXISTS tc_actions_pkey;

      SELECT create_hypertable(
        'tc_actions',
        'actiontime',
        migrate_data => TRUE
      );

      CREATE INDEX IF NOT EXISTS tc_actions_id_idx ON tc_actions(id);
    </sql>

  </changeSet>

  <changeSet author="author" id="changelog-6.11.0-other">

    <preConditions onFail="MARK_RAN">
      <not>
        <changeSetExecuted changeLogFile="changelog-6.11.0" id="changelog-6.11.0-timescale" author="author" />
      </not>
    </preConditions>

    <createIndex indexName="tc_actions_actiontime_idx" tableName="tc_actions">
      <column name="actiontime" />
    </createIndex>

  </changeSet>

  <changeSet author="author" id="changelog-6.11.0">

    <createTable tableName="tc_revoked_tokens">
      <column name="id" type="BIGINT">
        <constraints primaryKey="true" nullable="false" />
      </column>
    </createTable>

  </changeSet>

</databaseChangeLog>
