<?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.2.xsd"
                   logicalFilePath="upgrade_9.1.00.xml">
    <property name="false" value="false" dbms="mysql"/>
    <property name="false" value="0" dbms="derby"/>
    <property name="true" value="true" dbms="mysql"/>
    <property name="true" value="1" dbms="derby"/>
    <changeSet author="gateway" id="update_ssg_version">
        <update tableName="ssg_version">
            <column name="current_version" value="9.1.00"/>
        </update>
        <rollback>
            <update tableName="ssg_version">
                <column name="current_version" value="9.0.00"/>
            </update>
        </rollback>
    </changeSet>
    <changeSet author="gateway" id="solution_kit_meta_rename_read_only_to_readable_and_set_default_value">
        <renameColumn tableName="solution_kit_meta" oldColumnName="read_only" newColumnName="readable" columnDataType="${tinyint.type}" />
        <dropDefaultValue tableName="solution_kit_meta" columnName="readable" />
        <!--
         cannot use addNotNullConstraint with defaultNullValue as in derby defaultNullValue is always interpreted as string (see ticket https://liquibase.jira.com/browse/CORE-1064).
         Works on mysql though as columnDataType is supported.
         <addNotNullConstraint tableName="solution_kit_meta" columnName="readable" defaultNullValue="1" columnDataType="${tinyint.type}"/>
        -->
        <update tableName="solution_kit_meta">
            <column name="readable" valueComputed="${true}" />
            <where>readable IS NULL</where>
        </update>
        <addNotNullConstraint tableName="solution_kit_meta" columnName="readable" columnDataType="${tinyint.type}" />
        <addDefaultValue tableName="solution_kit_meta" columnName="readable" defaultValueComputed="${true}" />
        <rollback>
            <dropDefaultValue tableName="solution_kit_meta" columnName="readable" />
            <dropNotNullConstraint tableName="solution_kit_meta" columnName="readable" columnDataType="${tinyint.type}" />
            <renameColumn tableName="solution_kit_meta" oldColumnName="readable" newColumnName="read_only" columnDataType="${tinyint.type}" />
        </rollback>
    </changeSet>
    <changeSet author="gateway" id="solution_kit_meta_rename_readable_to_read_only">
        <!-- discarding existing read-only flags -->
        <dropDefaultValue tableName="solution_kit_meta" columnName="readable" />
        <dropColumn tableName="solution_kit_meta" columnName="readable"/>
        <!--
        forcing all entities to not read-only (i.e. user editable) so that upgrade from Longbow could work properly
        Unfortunately readonly-ness will be lost for skars installed after "solution_kit_meta_rename_read_only_to_readable_and_set_default_value" change-set.
        -->
        <addColumn tableName="solution_kit_meta">
            <column name="read_only" type="${tinyint.type}" defaultValueComputed="${false}">
                <constraints nullable="false"/>
            </column>
        </addColumn>
        <rollback>
            <dropDefaultValue tableName="solution_kit_meta" columnName="read_only" />
            <dropColumn tableName="solution_kit_meta" columnName="read_only"/>
            <!-- as readonly-ness was already lost there is no way to properly rollback the previous values -->
            <addColumn tableName="solution_kit_meta">
                <column name="readable" type="${tinyint.type}" defaultValueComputed="${true}">
                    <constraints nullable="false"/>
                </column>
            </addColumn>
        </rollback>
    </changeSet>
    <changeSet author="gateway" id="solution_kit_meta_add_version_stamp_column">
        <comment>
            Fixing SSG-12307.
            version_stamp is used to detect which readonly flag has priority in case when multiple skars own the same entity.
            Entity Protection Tracker should use the entity which was updated or created the last.
            Though timestamp is a logical choice in this case, unfortunately in a cluster environment there is no guarantee that
            node clocks will be in sync.
        </comment>
        <addColumn tableName="solution_kit_meta">
            <!--
                Though better option would be to use defaultValueNumeric="9223372036854775807", however derby failed to create the column:
                Error executing SQL ALTER TABLE APP.solution_kit_meta ADD version_stamp BIGINT DEFAULT 9.223372036854776E18 NOT NULL:
                    DEFAULT value or IDENTITY attribute value is not valid for column 'VERSION_STAMP'.
             -->
            <column name="version_stamp" type="BIGINT(19)" defaultValueComputed="9223372036854775807">
                <constraints nullable="false"/>
            </column>
        </addColumn>
        <rollback>
            <dropDefaultValue tableName="solution_kit_meta" columnName="version_stamp" />
            <dropColumn tableName="solution_kit_meta" columnName="version_stamp" />
        </rollback>
    </changeSet>
</databaseChangeLog>
