USE `vn`;
CREATE 
     OR REPLACE ALGORITHM = UNDEFINED 
    DEFINER = `root`@`%` 
    SQL SECURITY DEFINER
VIEW `claim` AS
    SELECT 
        `c`.`id` AS `id`,
        `c`.`Fecha` AS `ticketCreated`,
        `c`.`cl_est_id` AS `claimStateFk`,
        `c`.`notas` AS `observation`,
        `c`.`Id_Cliente` AS `clientFk`,
        `c`.`Id_Trabajador` AS `workerFk`,
        `c`.`sensib` AS `responsibility`,
        `c`.`mana` AS `isChargedToMana`,
        `c`.`ticketFk` AS `ticketFk`,
        `c`.`odbc_date` AS `created`,
        `c`.`cl_dep_id` AS `claimDepartmentFk`
    FROM
        `vn2008`.`cl_main` `c`;


/*
    -- View switch
    Find: `([a-zA-Z0-9 _]+)` AS `([a-zA-Z0-9 _]+)`
    Repc: `$2` AS `$1`

    -- Trigger table
    Find: (BEFORE|AFTER) (INSERT|UPDATE|DELETE) ON `([a-zA-Z0-9 _]+)`
    Repc: $1 $2 ON `buy`

    -- Trigger
    Find: OLD.
    Find: NEW.
*/

RENAME TABLE `vn`.`claim` TO `vn`.`claim__`;


ALTER TABLE `vn2008`.`cl_main` RENAME `vn`.`claim`;

-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Rename columns

ALTER TABLE `vn`.`claim` 
CHANGE COLUMN `Fecha` `ticketCreated` DATE NOT NULL ,
CHANGE COLUMN `cl_dep_id` `claimDepartmentFk` TINYINT(3) UNSIGNED NULL DEFAULT NULL ,
CHANGE COLUMN `cl_est_id` `claimStateFk` INT(10) UNSIGNED NOT NULL DEFAULT '1' ,
CHANGE COLUMN `notas` `observation` TEXT NULL DEFAULT NULL ,
CHANGE COLUMN `Id_Cliente` `clientFk` INT(11) NOT NULL ,
CHANGE COLUMN `Id_Trabajador` `workerFk` INT(11) NOT NULL ,
CHANGE COLUMN `sensib` `responsibility` INT(1) UNSIGNED NOT NULL DEFAULT '3' ,
CHANGE COLUMN `mana` `isChargedToMana` TINYINT(1) NOT NULL DEFAULT '0' ,
CHANGE COLUMN `odbc_date` `created` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ;

-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Replacement view

CREATE 
    ALGORITHM = UNDEFINED 
    DEFINER = `root`@`%` 
    SQL SECURITY DEFINER
VIEW `vn2008`.`cl_main` AS
    SELECT 
        `c`.`id` AS `id`,
        `c`.`ticketCreated` AS `Fecha`,
        `c`.`claimStateFk` AS `cl_est_id`,
        `c`.`observation` AS `notas`,
        `c`.`clientFk` AS `Id_Cliente`,
        `c`.`workerFk` AS `Id_Trabajador`,
        `c`.`responsibility` AS `sensib`,
        `c`.`isChargedToMana` AS `mana`,
        `c`.`ticketFk` AS `ticketFk`,
        `c`.`created` AS `odbc_date`,
        `c`.`claimDepartmentFk` AS `cl_dep_id`
    FROM
        `vn`.`claim` `c`

-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Triggers

