<?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.0.00.xml">
    <changeSet author="gateway" id="update_ssg_version">
        <update tableName="ssg_version">
            <column name="current_version" value="9.0.00"/>
        </update>
        <rollback>
            <update tableName="ssg_version">
                <column name="current_version" value="8.4.00"/>
            </update>
        </rollback>
    </changeSet>
    <changeSet author="gateway" id="create_published_service_properties">
        <validCheckSum>8:5f21406af83614d196d7a16d7ad68c3d</validCheckSum>
        <createTable tableName="published_service_property">
            <column name="published_service_goid" type="${goid.type}">
                <constraints nullable="false"/>
            </column>
            <column name="name" type="VARCHAR(128)">
                <constraints nullable="false"/>
            </column>
            <column name="${value.column.name}" type="${mediumtext.type}">
                <constraints nullable="false"/>
            </column>
        </createTable>
        <modifySql dbms="mysql">
            <append value="ENGINE=INNODB CHARSET=utf8mb3 COLLATE utf8mb3_general_ci"/>
        </modifySql>
    </changeSet>
    <changeSet author="gateway" id="add_version_column_to_sample_messages_table">
        <addColumn tableName="sample_messages">
            <column name="version" type="INT(10)" defaultValueNumeric="0">
                <constraints nullable="false"/>
            </column>
        </addColumn>
        <dropDefaultValue tableName="sample_messages" columnName="version" />
    </changeSet>
    <changeSet id="add_work_queue_table" author="gateway">
        <validCheckSum>8:b87e86ef3f5bf1d400752c0bbf04b657</validCheckSum>
        <createTable tableName="work_queue">
            <column name="goid" type="${goid.type}">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="version" type="INT(10)">
                <constraints nullable="false"/>
            </column>
            <column name="name" type="VARCHAR(128)">
                <constraints nullable="false"/>
            </column>
            <column name="max_queue_size" type="INT">
                <constraints nullable="false"/>
            </column>
            <column name="thread_pool_max" type="INT">
                <constraints nullable="false"/>
            </column>
            <column name="reject_policy" type="VARCHAR(32)">
                <constraints nullable="false"/>
            </column>
            <column name="security_zone_goid" type="${goid.type}"/>
        </createTable>
        <modifySql dbms="mysql">
            <append value="ENGINE=INNODB CHARSET=utf8mb3 COLLATE utf8mb3_general_ci"/>
        </modifySql>
    </changeSet>
    <changeSet id="create-index-work-queue-name" author="gateway">
        <createIndex indexName="index_work_queue_name" tableName="work_queue" unique="true">
            <column name="name"/>
        </createIndex>
    </changeSet>
    <changeSet id="add-fk-work-queue-security-zone" author="gateway">
        <addForeignKeyConstraint baseTableName="work_queue" baseColumnNames="security_zone_goid"
                                 constraintName="work_queue_security_zone" referencedTableName="security_zone"
                                 referencedColumnNames="goid" onDelete="SET NULL" onUpdate="NO ACTION"/>
    </changeSet>
    <changeSet id="create-manage-work-queue-role" author="gateway">
        <comment>Manage Work Queue</comment>
        <insert tableName="rbac_role">
            <column name="goid" valueComputed="toGoid(0, -1700)"/>
            <column name="version" valueNumeric="0"/>
            <column name="name" value="Manage Work Queue"/>
            <column name="tag"/>
            <column name="entity_type" value="WORK_QUEUE"/>
            <column name="entity_goid"/>
            <column name="description" value="Users assigned to the {0} role have the ability to read, create, update and delete work queues."/>
            <column name="user_created" valueComputed="${false}"/>
        </insert>
        <insert tableName="rbac_permission">
            <column name="goid" valueComputed="toGoid(0, -1701)"/>
            <column name="version" valueNumeric="0"/>
            <column name="role_goid" valueComputed="toGoid(0, -1700)"/>
            <column name="operation_type" value="READ"/>
            <column name="other_operation"/>
            <column name="entity_type" value="WORK_QUEUE"/>
        </insert>
        <insert tableName="rbac_permission">
            <column name="goid" valueComputed="toGoid(0, -1702)"/>
            <column name="version" valueNumeric="0"/>
            <column name="role_goid" valueComputed="toGoid(0, -1700)"/>
            <column name="operation_type" value="CREATE"/>
            <column name="other_operation"/>
            <column name="entity_type" value="WORK_QUEUE"/>
        </insert>
        <insert tableName="rbac_permission">
            <column name="goid" valueComputed="toGoid(0, -1703)"/>
            <column name="version" valueNumeric="0"/>
            <column name="role_goid" valueComputed="toGoid(0, -1700)"/>
            <column name="operation_type" value="UPDATE"/>
            <column name="other_operation"/>
            <column name="entity_type" value="WORK_QUEUE"/>
        </insert>
        <insert tableName="rbac_permission">
            <column name="goid" valueComputed="toGoid(0, -1704)"/>
            <column name="version" valueNumeric="0"/>
            <column name="role_goid" valueComputed="toGoid(0, -1700)"/>
            <column name="operation_type" value="DELETE"/>
            <column name="other_operation"/>
            <column name="entity_type" value="WORK_QUEUE"/>
        </insert>
    </changeSet>
    <changeSet id="add_scheduled_task_table" author="gateway">
        <validCheckSum>8:ddc1c4780d35f0377a6a7401b762c323</validCheckSum>
        <createTable tableName="scheduled_task">
            <column name="goid" type="${goid.type}">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="version" type="INT(10)">
                <constraints nullable="false"/>
            </column>
            <column name="name" type="VARCHAR(128)">
                <constraints nullable="false"/>
            </column>
            <column name="policy_goid" type="${goid.type}">
                <constraints nullable="false"/>
            </column>
            <column name="use_one_node" type="${tinyint.type}" defaultValueNumeric="0">
                <constraints nullable="false"/>
            </column>
            <column name="job_type" type="VARCHAR(128)">
                <constraints nullable="false"/>
            </column>
            <column name="job_status" type="VARCHAR(128)"/>
            <column name="execution_date" type="BIGINT(19)"/>
            <column name="executed_date" type="BIGINT(19)"/>
            <column name="cron_expression" type="VARCHAR(255)"/>
            <column name="${properties.column.name}" type="${mediumtext.type}"/>
            <column name="security_zone_goid" type="${goid.type}"/>
        </createTable>
        <modifySql dbms="mysql">
            <append value="ENGINE=INNODB CHARSET=utf8mb3 COLLATE utf8mb3_general_ci"/>
        </modifySql>
    </changeSet>
    <changeSet id="create-index-scheduled_task-name" author="gateway">
        <createIndex indexName="index_scheduled_task_name" tableName="scheduled_task" unique="true">
            <column name="name"/>
        </createIndex>
    </changeSet>
    <changeSet id="add-fk-scheduled_task-security-zone" author="gateway">
        <addForeignKeyConstraint baseTableName="scheduled_task" baseColumnNames="security_zone_goid"
                                 constraintName="scheduled_task_security_zone" referencedTableName="security_zone"
                                 referencedColumnNames="goid" onDelete="SET NULL" onUpdate="NO ACTION"/>
    </changeSet>
    <changeSet id="add-fk-scheduled_task-policy" author="gateway">
        <addForeignKeyConstraint baseTableName="scheduled_task" baseColumnNames="policy_goid"
                                 constraintName="scheduled_task_policy" referencedTableName="policy"
                                 referencedColumnNames="goid" onDelete="NO ACTION" onUpdate="NO ACTION"/>
    </changeSet>
    <changeSet id="create-scheduled_task-role" author="gateway">
        <comment>Manage Scheduled Task</comment>
        <insert tableName="rbac_role">
            <column name="goid" valueComputed="toGoid(0, -1750)"/>
            <column name="version" valueNumeric="0"/>
            <column name="name" value="Manage Scheduled Task"/>
            <column name="tag"/>
            <column name="entity_type" value="SCHEDULED_TASK"/>
            <column name="entity_goid"/>
            <column name="description" value="Users assigned to the {0} role have the ability to read, create, update and delete Scheduled Task."/>
            <column name="user_created" valueComputed="${false}"/>
        </insert>
        <insert tableName="rbac_permission">
            <column name="goid" valueComputed="toGoid(0, -1751)"/>
            <column name="version" valueNumeric="0"/>
            <column name="role_goid" valueComputed="toGoid(0, -1750)"/>
            <column name="operation_type" value="READ"/>
            <column name="other_operation"/>
            <column name="entity_type" value="SCHEDULED_TASK"/>
        </insert>
        <insert tableName="rbac_permission">
            <column name="goid" valueComputed="toGoid(0, -1752)"/>
            <column name="version" valueNumeric="0"/>
            <column name="role_goid" valueComputed="toGoid(0, -1750)"/>
            <column name="operation_type" value="CREATE"/>
            <column name="other_operation"/>
            <column name="entity_type" value="SCHEDULED_TASK"/>
        </insert>
        <insert tableName="rbac_permission">
            <column name="goid" valueComputed="toGoid(0, -1753)"/>
            <column name="version" valueNumeric="0"/>
            <column name="role_goid" valueComputed="toGoid(0, -1750)"/>
            <column name="operation_type" value="UPDATE"/>
            <column name="other_operation"/>
            <column name="entity_type" value="SCHEDULED_TASK"/>
        </insert>
        <insert tableName="rbac_permission">
            <column name="goid" valueComputed="toGoid(0, -1754)"/>
            <column name="version" valueNumeric="0"/>
            <column name="role_goid" valueComputed="toGoid(0, -1750)"/>
            <column name="operation_type" value="DELETE"/>
            <column name="other_operation"/>
            <column name="entity_type" value="SCHEDULED_TASK"/>
        </insert>
        <insert tableName="rbac_permission">
            <column name="goid" valueComputed="toGoid(0, -1756)"/>
            <column name="version" valueNumeric="0"/>
            <column name="role_goid" valueComputed="toGoid(0, -1750)"/>
            <column name="operation_type" value="READ"/>
            <column name="other_operation"/>
            <column name="entity_type" value="POLICY"/>
        </insert>
        <insert tableName="rbac_predicate">
            <column name="goid" valueComputed="toGoid(0, -1757)"/>
            <column name="version" valueNumeric="0"/>
            <column name="permission_goid" valueComputed="toGoid(0, -1756)"/>
        </insert>
        <insert tableName="rbac_predicate_attribute">
            <column name="goid" valueComputed="toGoid(0, -1757)"/>
            <column name="attribute" value="type"/>
            <column name="${value.column.name}" value="Policy-Backed Service Operation Policy Fragment"/>
            <column name="mode" value="eq"/>
        </insert>
    </changeSet>
    <changeSet author="gateway" id="server_module_files_phase_two_db_changes">
        <comment>Update Server Module File tables for second phase</comment>
        <addColumn tableName="server_module_file_data">
            <column name="signature_properties" type="${mediumtext.type}"/>
        </addColumn>
        <update tableName="server_module_file_state">
            <column name="${state.column.name}" value="REJECTED"/>
        </update>
    </changeSet>
    <changeSet id="create_solution_kit_tables" author="gateway">
        <validCheckSum>8:bbced6f353aa15dc295f835777b421ce</validCheckSum>
        <validCheckSum>8:fc68067381052307bbdb4f7c01fbbf76</validCheckSum>
        <createTable tableName="solution_kit">
            <column name="goid" type="${goid.type}">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="version" type="INT(10)">
                <constraints nullable="false"/>
            </column>
            <column name="name" type="VARCHAR(255)">
                <constraints nullable="false"/>
            </column>
            <column name="sk_guid" type="VARCHAR(255)">
                <constraints nullable="false"/>
            </column>
            <column name="sk_version" type="VARCHAR(16)">
                <constraints nullable="false"/>
            </column>
            <column name="properties" type="${mediumtext.type}"/>
            <column name="install_properties" type="${mediumtext.type}"/>
            <column name="mappings" type="${mediumtext.type}">
                <constraints nullable="false"/>
            </column>
            <column name="uninstall_bundle" type="${mediumtext.type}"/>
            <column name="last_update_time" type="BIGINT(19)" defaultValueNumeric="0">
                <constraints nullable="false"/>
            </column>
        </createTable>
        <rollback>
            <delete tableName="solution_kit"/>
            <dropTable tableName="solution_kit"/>
        </rollback>
        <modifySql dbms="mysql">
            <append value="ENGINE=INNODB CHARSET=utf8mb3 COLLATE utf8mb3_general_ci"/>
        </modifySql>
    </changeSet>
    <changeSet id="add_solution_kit_index" author="gateway" failOnError="false">
        <preConditions onFail="MARK_RAN" onSqlOutput="TEST">
            <not>
                <indexExists indexName="solution_kit_index" tableName="solution_kit" />
            </not>
        </preConditions>
        <createIndex indexName="solution_kit_index" tableName="solution_kit" unique="false">
            <column name="sk_guid"/>
        </createIndex>
        <rollback>
            <dropIndex tableName="solution_kit" indexName="solution_kit_index"/>
        </rollback>
    </changeSet>
    <changeSet id="add_solution_kit_meta" author="gateway" failOnError="false">
        <preConditions onFail="MARK_RAN" onSqlOutput="TEST">
            <not>
                <tableExists tableName="solution_kit_meta" />
            </not>
        </preConditions>
        <createTable tableName="solution_kit_meta">
            <column name="goid" type="${goid.type}">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="entity_type" type="VARCHAR(50)"> <!-- other sizes seen in ssg-base.xmlVARCHAR(64) and VARCHAR(255) -->
                <constraints nullable="false"/>
            </column>
            <column name="entity_goid" type="${goid.type}">
                <constraints nullable="false"/>
            </column>
            <column name="solution_kit_goid" type="${goid.type}">
                <constraints nullable="false" references="solution_kit(goid)" foreignKeyName="fk_solution_kit_goid" deleteCascade="true"/>
            </column>
            <column name="user_created" type="${tinyint.type}" defaultValueNumeric="0"/>
            <column name="user_editable" type="${tinyint.type}" defaultValueNumeric="1"/>
        </createTable>
        <rollback>
            <delete tableName="solution_kit_meta"/>
            <dropTable tableName="solution_kit_meta"/>
        </rollback>
        <modifySql dbms="mysql">
            <append value="ENGINE=INNODB CHARSET=utf8mb3 COLLATE utf8mb3_general_ci"/>
        </modifySql>
    </changeSet>
    <changeSet author="gateway" id="rename_solution_kit_properties_field_by_using_dynamic_value">
        <renameColumn tableName="solution_kit" oldColumnName="properties" newColumnName="${properties.column.name}" columnDataType="${mediumtext.type}" />
    </changeSet>
    <changeSet author="gateway" id="add_new_field_parent_goid_into_solution_kit">
        <addColumn tableName="solution_kit">
            <column name="parent_goid" type="${goid.type}"/>
        </addColumn>
    </changeSet>
    <changeSet author="gateway" id="scheduled_task-role-with-identity">
        <comment></comment>
        <insert tableName="rbac_permission">
            <column name="goid" valueComputed="toGoid(0, -1758)"/>
            <column name="version" valueNumeric="0"/>
            <column name="role_goid" valueComputed="toGoid(0, -1750)"/>
            <column name="operation_type" value="READ"/>
            <column name="other_operation"/>
            <column name="entity_type" value="ID_PROVIDER_CONFIG"/>
        </insert>
        <insert tableName="rbac_permission">
            <column name="goid" valueComputed="toGoid(0, -1759)"/>
            <column name="version" valueNumeric="0"/>
            <column name="role_goid" valueComputed="toGoid(0, -1750)"/>
            <column name="operation_type" value="READ"/>
            <column name="other_operation"/>
            <column name="entity_type" value="USER"/>
        </insert>
    </changeSet>
    <changeSet author="gateway" id="refactor_solution_kit_meta_columns">
        <dropColumn tableName="solution_kit_meta" columnName="user_created" />
        <renameColumn tableName="solution_kit_meta" oldColumnName="user_editable" newColumnName="read_only" columnDataType="${tinyint.type}" />
    </changeSet>
    <changeSet author="gateway" id="solution_kit_meta_remove_entity_goid_add_entity_id_instead">
        <addColumn tableName="solution_kit_meta">
            <column name="entity_id" type="VARCHAR(255)" defaultValue="">
                <constraints nullable="false"/>
            </column>
        </addColumn>
        <sql>
            update solution_kit_meta set entity_id = goidToString(entity_goid)
        </sql>
        <dropDefaultValue tableName="solution_kit_meta" columnName="entity_id" />
        <dropColumn tableName="solution_kit_meta" columnName="entity_goid"/>
        <rollback>
            <addColumn tableName="solution_kit_meta">
                <column name="entity_goid" type="${goid.type}" defaultValue="toGoid(0, -1)"> <!-- rollback to default goid don't know how to convert from string to goid -->
                    <constraints nullable="false"/>
                </column>
            </addColumn>
            <dropDefaultValue tableName="solution_kit_meta" columnName="entity_goid" />
            <dropColumn tableName="solution_kit_meta" columnName="entity_id"/>
        </rollback>
    </changeSet>
    <changeSet author="gateway" id="add_execute_immediately_column_to_scheduled_task">
        <addColumn tableName="scheduled_task">
            <column name="execute_immediately" type="${tinyint.type}" defaultValueNumeric="0">
                <constraints nullable="false"/>
            </column>
        </addColumn>
    </changeSet>
    <changeSet author="gateway" id="rename_execute_immediately_column_to_execute_on_create">
        <renameColumn tableName="scheduled_task" oldColumnName="execute_immediately" newColumnName="execute_on_create" columnDataType="${tinyint.type}"/>
    </changeSet>
</databaseChangeLog>
