From 90ce07881a11a28697d7fc5b35dd39376b2ebf0f Mon Sep 17 00:00:00 2001 From: Juan Ferrer Toribio Date: Mon, 5 Feb 2018 19:34:04 +0100 Subject: [PATCH] Gulpfile refactor, SQL scripts --- gulpfile.js | 11 +++--- services/db/00-trucateAll.sql | 38 +++++++++++++++++++ services/db/changes/1.0.0/01-agencyMode.sql | 25 ++++++++++++ services/db/changes/1.0.0/02-agencyHour.sql | 18 +++++++++ services/db/changes/1.0.0/03-address.sql | 24 ++++++++++++ services/db/changes/1.0.0/04-userRole.sql | 36 ++++++++++++++++++ .../db/changes/1.0.0/05-userHasRoleId.sql | 31 +++++++++++++++ 7 files changed, 178 insertions(+), 5 deletions(-) create mode 100644 services/db/00-trucateAll.sql create mode 100644 services/db/changes/1.0.0/01-agencyMode.sql create mode 100644 services/db/changes/1.0.0/02-agencyHour.sql create mode 100644 services/db/changes/1.0.0/03-address.sql create mode 100644 services/db/changes/1.0.0/04-userRole.sql create mode 100644 services/db/changes/1.0.0/05-userHasRoleId.sql diff --git a/gulpfile.js b/gulpfile.js index 5fa20ad2c..6ef3b3b49 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -1,9 +1,6 @@ require('require-yaml'); const gulp = require('gulp'); const gutil = require('gulp-util'); -const wrap = require('gulp-wrap'); -const concat = require('gulp-concat'); -const merge = require('merge-stream'); const print = require('gulp-print'); const runSequence = require('run-sequence'); const fs = require('fs-extra'); @@ -108,9 +105,8 @@ gulp.task('build', ['clean'], () => { }); gulp.task('docker-compose', async () => { - let compose = await fs.readFile('./docker-compose.tpl.yml', 'utf8'); - const yaml = require('js-yaml'); + let compose = await fs.readFile('./docker-compose.tpl.yml', 'utf8'); let composeYml = yaml.safeLoad(compose); let services = await getServices(); @@ -253,6 +249,8 @@ let localeFiles = `${srcDir}/**/locale/*.json`; gulp.task('locales', function() { const extend = require('gulp-extend'); const yaml = require('gulp-yaml'); + const merge = require('merge-stream'); + let streams = []; for (let mod in modules) @@ -272,6 +270,9 @@ gulp.task('locales', function() { let routeFiles = `${srcDir}/**/routes.json`; gulp.task('routes', function() { + const concat = require('gulp-concat'); + const wrap = require('gulp-wrap'); + return gulp.src(routeFiles) .pipe(concat('routes.js', {newLine: ','})) .pipe(wrap('var routes = [<%=contents%>\n];')) diff --git a/services/db/00-trucateAll.sql b/services/db/00-trucateAll.sql new file mode 100644 index 000000000..f7d98056c --- /dev/null +++ b/services/db/00-trucateAll.sql @@ -0,0 +1,38 @@ +DROP PROCEDURE IF EXISTS mysql.truncateAll; +DELIMITER $$ +CREATE PROCEDURE mysql.truncateAll() +BEGIN + DECLARE vSchema VARCHAR(255); + DECLARE vTable VARCHAR(255); + DECLARE vDone BOOL; + + DECLARE cTables CURSOR FOR + SELECT `TABLE_SCHEMA`, `TABLE_NAME` + FROM `information_schema`.`TABLES` + WHERE `TABLE_TYPE` = 'BASE TABLE' + AND `TABLE_ROWS` > 0 + AND `TABLE_SCHEMA` NOT IN ('information_schema', 'mysql', 'performance_schema'); + + DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE; + + SET FOREIGN_KEY_CHECKS = FALSE; + OPEN cTables; + + l: LOOP + SET vDone = FALSE; + FETCH cTables INTO vSchema, vTable; + + IF vDone THEN + LEAVE l; + END IF; + + SET @stmt = CONCAT('TRUNCATE TABLE `', vSchema, '`.`', vTable, '`'); + PREPARE stmt FROM @stmt; + EXECUTE stmt; + DEALLOCATE PREPARE stmt; + END LOOP; + + CLOSE cTables; + SET FOREIGN_KEY_CHECKS = TRUE; +END$$ +DELIMITER ; \ No newline at end of file diff --git a/services/db/changes/1.0.0/01-agencyMode.sql b/services/db/changes/1.0.0/01-agencyMode.sql new file mode 100644 index 000000000..5190821c5 --- /dev/null +++ b/services/db/changes/1.0.0/01-agencyMode.sql @@ -0,0 +1,25 @@ +CREATE OR REPLACE + ALGORITHM = UNDEFINED + DEFINER = `root`@`%` + SQL SECURITY DEFINER +VIEW `vn`.`agencyMode` AS + SELECT + `a`.`Id_Agencia` AS `id`, + `a`.`Agencia` AS `name`, + `a`.`description` AS `description`, + `a`.`Vista` AS `deliveryMethodFk`, + `a`.`Vista` AS `__deliveryMethod`, + `a`.`Vista` AS `__view`, + `a`.`m3` AS `m3`, + `a`.`cod71` AS `cod71`, + `a`.`web` AS `web`, + `a`.`agency_id` AS `agencyFk`, + `a`.`agency_id` AS `__agency`, + `a`.`agency_service_id` AS `agencyServiceFk`, + `a`.`agency_service_id` AS `__agencyService`, + `a`.`inflacion` AS `inflation`, + `a`.`inflacion` AS `__inflacion`, + `a`.`is_volumetric` AS `isVolumetric`, + `a`.`send_mail` AS `reportMail` + FROM + `vn2008`.`Agencias` `a` \ No newline at end of file diff --git a/services/db/changes/1.0.0/02-agencyHour.sql b/services/db/changes/1.0.0/02-agencyHour.sql new file mode 100644 index 000000000..1c3a743f6 --- /dev/null +++ b/services/db/changes/1.0.0/02-agencyHour.sql @@ -0,0 +1,18 @@ +CREATE OR REPLACE + ALGORITHM = UNDEFINED + DEFINER = `root`@`%` + SQL SECURITY DEFINER +VIEW `vn`.`agencyHour` AS + SELECT + `h`.`agency_hour_id` AS `id`, + `h`.`agency_id` AS `agencyFk`, + `h`.`agency_id` AS `__agency`, + `h`.`week_day` AS `weekDay`, + `h`.`warehouse_id` AS `warehouseFk`, + `h`.`warehouse_id` AS `__warehouse`, + `h`.`province_id` AS `provinceFk`, + `h`.`province_id` AS `__province`, + `h`.`subtract_day` AS `substractDay`, + `h`.`max_hour` AS `maxHour` + FROM + `vn2008`.`agency_hour` `h` \ No newline at end of file diff --git a/services/db/changes/1.0.0/03-address.sql b/services/db/changes/1.0.0/03-address.sql new file mode 100644 index 000000000..bdd27136c --- /dev/null +++ b/services/db/changes/1.0.0/03-address.sql @@ -0,0 +1,24 @@ +CREATE OR REPLACE + ALGORITHM = UNDEFINED + DEFINER = `root`@`%` + SQL SECURITY DEFINER +VIEW `vn`.`address` AS + SELECT + `t`.`id_consigna` AS `id`, + `t`.`Id_cliente` AS `clientFk`, + `t`.`domicilio` AS `street`, + `t`.`poblacion` AS `city`, + `t`.`codPostal` AS `postalCode`, + `t`.`province_id` AS `provinceFk`, + `t`.`telefono` AS `phone`, + `t`.`movil` AS `mobile`, + `t`.`consignatario` AS `nickname`, + `t`.`predeterminada` AS `isDefaultAddress`, + `t`.`longitude` AS `longitude`, + `t`.`latitude` AS `latitude`, + `t`.`warehouse_id` AS `warehouseFk`, + `t`.`Id_Agencia` AS `agencyFk`, + `t`.`isEqualizated` AS `isEqualizated`, + `t`.`active` AS `isActive` + FROM + `vn2008`.`Consignatarios` `t` \ No newline at end of file diff --git a/services/db/changes/1.0.0/04-userRole.sql b/services/db/changes/1.0.0/04-userRole.sql new file mode 100644 index 000000000..b896589bc --- /dev/null +++ b/services/db/changes/1.0.0/04-userRole.sql @@ -0,0 +1,36 @@ +USE `account`; +CREATE + OR REPLACE ALGORITHM = UNDEFINED + DEFINER = `root`@`%` + SQL SECURITY DEFINER +VIEW `account`.`__userRole` AS + SELECT + `r`.`inheritsFrom` AS `id` + FROM + (`account`.`roleRole` `r` + JOIN `account`.`user` `u` ON ((`u`.`role` = `r`.`role`))) + WHERE + (`u`.`id` = USERGETID()); + +DROP VIEW `account`.`userRole`; + +CREATE TABLE `account`.`userRole` ( + `userFk` INT(10) UNSIGNED NOT NULL, + `roleFk` INT(10) UNSIGNED NOT NULL, + PRIMARY KEY (`userFk`, `roleFk`)); + +ALTER TABLE `account`.`userRole` + ADD INDEX `user` (`userFk` ASC), + ADD INDEX `role` (`roleFk` ASC); + +ALTER TABLE `account`.`userRole` + ADD CONSTRAINT `user` + FOREIGN KEY (`userFk`) + REFERENCES `account`.`user` (`id`) + ON DELETE CASCADE + ON UPDATE CASCADE, + ADD CONSTRAINT `role` + FOREIGN KEY (`roleFk`) + REFERENCES `account`.`role` (`id`) + ON DELETE CASCADE + ON UPDATE CASCADE; diff --git a/services/db/changes/1.0.0/05-userHasRoleId.sql b/services/db/changes/1.0.0/05-userHasRoleId.sql new file mode 100644 index 000000000..9d96ff775 --- /dev/null +++ b/services/db/changes/1.0.0/05-userHasRoleId.sql @@ -0,0 +1,31 @@ +DROP function IF EXISTS `userHasRoleId`; +DELIMITER $$ +CREATE DEFINER=`root`@`%` FUNCTION `account`.`userHasRoleId`(vRoleId INT) RETURNS tinyint(1) + DETERMINISTIC +BEGIN +/** + * Comprueba si el usuario actual tiene asociado un rol. + * + * @param vRoleId Identificador del rol a comprobar + * @return %TRUE si tiene el rol, %FALSE en caso contrario + */ + DECLARE vHasRole BOOL DEFAULT FALSE; + + SELECT COUNT(*) > 0 INTO vHasRole + FROM user u + JOIN roleRole r ON r.role = u.role + WHERE u.id = userGetId() + AND r.inheritsFrom = vRoleId; + + IF NOT vHasRole + THEN + SELECT COUNT(*) > 0 INTO vHasRole + FROM userRole + WHERE userFk = userGetId() + AND roleFk = vRoleId; + END IF; + + RETURN vHasRole; +END$$ +DELIMITER ; +