<?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-3.1.xsd">
    <changeSet author="bburke@redhat.com" id="1.3.0.Beta1">
        <delete tableName="CLIENT_SESSION_ROLE"/>
        <delete tableName="CLIENT_SESSION_NOTE"/>
        <delete tableName="CLIENT_SESSION"/>
        <delete tableName="USER_SESSION_NOTE"/>
        <delete tableName="USER_SESSION"/>
        <createTable tableName="ADMIN_EVENT_ENTITY">
            <column name="ID" type="VARCHAR(36)">
                <constraints nullable="false"/>
            </column>
            <column name="ADMIN_EVENT_TIME" type="BIGINT"/>
            <column name="REALM_ID" type="VARCHAR(255)"/>
            <column name="OPERATION_TYPE" type="VARCHAR(255)"/>
            <column name="AUTH_REALM_ID" type="VARCHAR(255)"/>
            <column name="AUTH_CLIENT_ID" type="VARCHAR(255)"/>
            <column name="AUTH_USER_ID" type="VARCHAR(255)"/>
            <column name="IP_ADDRESS" type="VARCHAR(255)"/>
            <column name="RESOURCE_PATH" type="VARCHAR(2550)"/>
            <column name="REPRESENTATION" type="VARCHAR(25500)"/>
            <column name="ERROR" type="VARCHAR(255)"/>
        </createTable>
        <addColumn tableName="REALM">
            <column name="ADMIN_EVENTS_ENABLED" type="BOOLEAN" defaultValueBoolean="false">
                <constraints nullable="false"/>
            </column>
            <column name="ADMIN_EVENTS_DETAILS_ENABLED" type="BOOLEAN" defaultValueBoolean="false">
                <constraints nullable="false"/>
            </column>
        </addColumn>
        <createTable tableName="CLIENT_SESSION_AUTH_STATUS">
            <column name="AUTHENTICATOR" type="VARCHAR(32)">
                <constraints nullable="false"/>
            </column>
            <column name="STATUS" type="INT"/>
            <column name="CLIENT_SESSION" type="VARCHAR(36)">
                <constraints nullable="false"/>
            </column>
        </createTable>
        <addColumn tableName="CLIENT_SESSION">
            <column name="AUTH_USER_ID" type="VARCHAR(32)"/>
        </addColumn>
        <addColumn tableName="USER_REQUIRED_ACTION">
            <column name="REQUIRED_ACTION" type="VARCHAR(32)">
                <constraints nullable="false"/>
            </column>
        </addColumn>
        <!-- VERIFY_EMAIL, UPDATE_PROFILE, CONFIGURE_TOTP, UPDATE_PASSWORD -->
        <update tableName="USER_REQUIRED_ACTION">
            <column name="REQUIRED_ACTION" value="VERIFY_EMAIL"/>
            <where>ACTION = 0</where>
        </update>
        <update tableName="USER_REQUIRED_ACTION">
            <column name="REQUIRED_ACTION" value="UPDATE_PROFILE"/>
            <where>ACTION = 1</where>
        </update>
        <update tableName="USER_REQUIRED_ACTION">
            <column name="REQUIRED_ACTION" value="CONFIGURE_TOTP"/>
            <where>ACTION = 2</where>
        </update>
        <update tableName="USER_REQUIRED_ACTION">
            <column name="REQUIRED_ACTION" value="UPDATE_PASSWORD"/>
            <where>ACTION = 3</where>
        </update>
        <dropPrimaryKey constraintName="CONSTRAINT_2" tableName="USER_REQUIRED_ACTION"/>
        <dropColumn tableName="USER_REQUIRED_ACTION" columnName="ACTION"/>
        <addPrimaryKey columnNames="REQUIRED_ACTION, USER_ID" constraintName="CONSTRAINT_REQUIRED_ACTION" tableName="USER_REQUIRED_ACTION"/>
        <addPrimaryKey columnNames="CLIENT_SESSION, AUTHENTICATOR" constraintName="CONSTRAINT_AUTH_STATUS_PK" tableName="CLIENT_SESSION_AUTH_STATUS"/>
        <addForeignKeyConstraint baseColumnNames="CLIENT_SESSION" baseTableName="CLIENT_SESSION_AUTH_STATUS" constraintName="AUTH_STATUS_CONSTRAINT" referencedColumnNames="ID" referencedTableName="CLIENT_SESSION"/>
    </changeSet>
</databaseChangeLog>
