<?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"
        xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.0.xsd
        http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
    <changeSet id="1" author="borisuvarov">
        <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
            USE label_copy_export;;

            CREATE FUNCTION
                stripSpecialChars(dirty_string VARCHAR(2048)) RETURNS VARCHAR(2048) CHARSET utf8

            BEGIN
            /**
             * MySQL function to remove Special characters: \n, \t, \r and replace blank when null
             * @param VARCHAR dirty_string : dirty string as input
             * @return VARCHAR clean_string : clean string as output
             *
             * Usage Syntax: stripSpeciaChars(string);
             * Usage SQL> SELECT stripSpeciaChars("sdfa7987\t\n\ru");
             * result : 'sdfa7987u'
             */
              RETURN IFNULL(TRIM(REPLACE(REPLACE(REPLACE(dirty_string,'\t',''),'\n',''),'\r','')),'');
            END;;

        </sql>
        <rollback>
            <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
                USE label_copy_export;;

                DROP FUNCTION IF EXISTS stripSpecialChars;;
            </sql>
        </rollback>
    </changeSet>
</databaseChangeLog>
