This commit is contained in:
Carlos Jimenez Ruiz 2019-03-12 14:21:10 +01:00
commit 4e51584b78
22 changed files with 203 additions and 215 deletions

3
db/.gitignore vendored
View File

@ -1 +1,2 @@
connect.ini
config.production.ini
config.test.ini

View File

@ -1,7 +1,7 @@
[client]
enable_cleartext_plugin = ON
host = localhost
port = 3306
user = root
password = password
ssl-mode = DISABLED
enable_cleartext_plugin = ON

View File

@ -1,66 +0,0 @@
const mysql = require('mysql2/promise');
const fs = require('fs-extra');
(async () => {
try {
const connection = await mysql.createConnection({
host: 'localhost',
user: 'root',
multipleStatements: true
});
let changesDir = './install/changes';
let results = await connection.query("SELECT dbVersion FROM util.config");
if (results[0].length != 1)
throw new Error('There must be exactly one row in the configuration table');
let version = results[0][0].dbVersion;
if (!version)
throw new Error('Database version not defined');
let dirs = await fs.readdir(changesDir);
dirs.sort(compareVersions);
let lastVersion;
for (let dir of dirs) {
if (compareVersions(dir, version) <= 0) continue;
if (/^\./.test(dir)) continue;
let path = `${changesDir}/${dir}`;
let files = await fs.readdir(path);
files.sort();
console.log(dir);
for (let file of files) {
let sql = await fs.readFile(`${path}/${file}`, 'utf8');
if (/^\s*$/.test(sql)) continue;
await connection.query(sql);
console.log(` - ${file}`);
}
lastVersion = dir;
}
if (lastVersion) {
await connection.query("UPDATE util.config SET dbVersion = ?", [lastVersion]);
console.log(`Database upgraded successfully to version ${lastVersion}`);
} else {
console.log("Database is alredy in the last version");
}
await connection.end();
process.exit();
} catch (err) {
console.error(err);
process.exit(1);
}
})();
function compareVersions(ver1, ver2) {
let diff;
ver1 = ver1.split('.');
ver2 = ver2.split('.');
diff = parseInt(ver1[0]) - parseInt(ver2[0]);
if (diff !== 0) return diff;
diff = parseInt(ver1[1]) - parseInt(ver2[1]);
if (diff !== 0) return diff;
diff = parseInt(ver1[2]) - parseInt(ver2[2]);
if (diff !== 0) return diff;
return 0;
}

42
db/import-changes.sh Executable file
View File

@ -0,0 +1,42 @@
#!/bin/bash
ENV=$1
if [ "$ENV" == "production" ]; then
echo ""
echo " ( ( ) ( ( ) ) "
echo " )\ ))\ ) ( /( )\ ) ( * ))\ ) ( /( ( /( "
echo "(()/(()/( )\()|()/( ( )\ ) /(()/( )\()) )\())"
echo " /(_))(_)|(_)\ /(_)) )\ (((_) ( )(_))(_)|(_)\ ((_)\ "
echo "(_))(_)) ((_|_))_ _ ((_))\___(_(_()|_)) ((_) _((_)"
echo "| _ \ _ \ / _ \| \| | | ((/ __|_ _|_ _| / _ \| \| |"
echo "| _/ /| (_) | |) | |_| || (__ | | | | | (_) | . |"
echo "|_| |_|_\ \___/|___/ \___/ \___| |_| |___| \___/|_|\_|"
echo ""
read -p "Are you sure? (Default: no) [yes|no]: " ANSWER
if [ "$ANSWER" != "yes" ]; then
echo "Aborting"
exit;
fi
fi
if [ -z "$ENV" ]; then
ENV="test"
fi
INI_FILE="config.$ENV.ini"
if [ ! -f "$INI_FILE" ]; then
echo "File $INI_FILE doesn't exists"
exit 1
fi
echo "[INFO] Config file: $INI_FILE"
echo "[INFO] Importing changes."
# Import changes
for file in install/changes/*.sql; do
echo "[INFO] -> Applying $file"
mysql --defaults-file="$INI_FILE" < $file
done

View File

@ -1,19 +0,0 @@
const mysql = require('mysql2/promise');
const fs = require('fs-extra');
(async () => {
try {
const connection = await mysql.createConnection({
host: 'localhost',
user: 'root',
multipleStatements: true
});
sql = await fs.readFile(`install/dump/fixtures.sql`, 'utf8');
await connection.query(sql);
await connection.end();
process.exit();
} catch (err) {
console.error(err);
process.exit(1);
}
})();

View File

@ -2,8 +2,6 @@
export MYSQL_PWD=root
# Dump structure
echo "[INFO] -> Imported ./dump/truncateAll.sql"
mysql -u root -f < ./dump/truncateAll.sql
echo "[INFO] -> Imported ./dump/structure.sql"
mysql -u root -f < ./dump/structure.sql
echo "[INFO] -> Imported ./dump/mysqlPlugins.sql"

View File

@ -0,0 +1,21 @@
DROP procedure IF EXISTS `hedera`.`basketGetTax`;
DELIMITER $$
CREATE DEFINER=`root`@`%` PROCEDURE `hedera`.`basketGetTax`()
READS SQL DATA
BEGIN
/**
* Returns the taxes for the current client basket.
*
* @treturn tmp.orderTax
*/
DROP TEMPORARY TABLE IF EXISTS tmp.order;
CREATE TEMPORARY TABLE tmp.order
ENGINE = MEMORY
SELECT myBasketGetId() orderFk;
CALL orderGetTax();
DROP TEMPORARY TABLE IF EXISTS tmp.order;
END$$
DELIMITER ;

View File

@ -1,37 +0,0 @@
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_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 ;

View File

@ -4,7 +4,12 @@ describe('item new()', () => {
let item;
afterAll(async done => {
await app.models.Item.destroyById(item.id);
let sql = 'DELETE FROM vn.itemLog WHERE originFk = ?';
await app.models.Item.rawSql(sql, [item.id]);
sql = 'DELETE FROM vn.item WHERE id = ?';
await app.models.Item.rawSql(sql, [item.id]);
done();
});

View File

@ -27,7 +27,7 @@ module.exports = Self => {
if (!tax.taxClassFk)
throw new UserError('Tax class cannot be blank');
promises.push(Self.app.models.ItemTaxCountry.updateAll(
promises.push(Self.app.models.ItemTaxCountry.update(
{id: tax.id},
{taxClassFk: tax.taxClassFk}
));

View File

@ -1,6 +1,11 @@
{
"name": "ItemBarcode",
"base": "VnModel",
"base": "Loggable",
"log": {
"model": "ItemLog",
"relation": "item",
"changedModelValue": "code"
},
"options": {
"mysql": {
"table": "itemBarcode"

View File

@ -1,6 +1,11 @@
{
"name": "ItemBotanical",
"base": "VnModel",
"base": "Loggable",
"log": {
"model": "ItemLog",
"relation": "item",
"changedModelValue": "botanical"
},
"options": {
"mysql": {
"table": "itemBotanical"

View File

@ -2,44 +2,54 @@
"name": "ItemLog",
"base": "VnModel",
"options": {
"mysql": {
"table": "itemLog"
}
"mysql": {
"table": "itemLog"
}
},
"properties": {
"id": {
"type": "Number",
"id": true,
"description": "Identifier"
"type": "Number",
"forceId": false
},
"originFk": {
"type": "Number",
"required": true
},
"userFk": {
"type": "Number"
},
"action": {
"type": "String",
"required": true
},
"changedModel": {
"type": "String"
},
"oldInstance": {
"type": "Object"
},
"newInstance": {
"type": "Object"
},
"creationDate": {
"type": "Date"
},
"description": {
"changedModelId": {
"type": "Number"
},
"changedModelValue": {
"type": "String"
},
"action": {
"description": {
"type": "String"
}
},
"relations": {
"item": {
"type": "belongsTo",
"model": "Item",
"foreignKey": "originFk"
},
"user": {
"type": "belongsTo",
"model": "Account",
"foreignKey": "userFk"
}
},
"acls": [
{
"accessType": "READ",
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "ALLOW"
}
]
}
}
}

View File

@ -1,6 +1,11 @@
{
"name": "ItemNiche",
"base": "VnModel",
"base": "Loggable",
"log": {
"model": "ItemLog",
"relation": "item",
"changedModelValue": "code"
},
"options": {
"mysql": {
"table": "itemPlacement"

View File

@ -1,6 +1,11 @@
{
"name": "ItemTag",
"base": "VnModel",
"base": "Loggable",
"log": {
"model": "ItemLog",
"relation": "item",
"changedModelValue": "value"
},
"options": {
"mysql": {
"table": "itemTag"

View File

@ -1,6 +1,9 @@
{
"name": "Item",
"base": "VnModel",
"base": "Loggable",
"log": {
"model":"ItemLog"
},
"options": {
"mysql": {
"table": "item"

View File

@ -1,33 +0,0 @@
<vn-crud-model
vn-id="model"
url="/item/api/ItemLogs"
filter="::$ctrl.filter"
link="{originFk: $ctrl.$stateParams.id}"
limit="20"
data="logs" auto-load="false">
</vn-crud-model>
<vn-vertical>
<vn-card pad-large>
<vn-vertical>
<vn-table model="model">
<vn-thead>
<vn-tr>
<vn-th field="description">Description</vn-th>
<vn-th field="action">Action</vn-th>
<vn-th field="userFk">Changed by</vn-th>
<vn-th field="creationDate" default-order="DESC">Date</vn-th>
</vn-tr>
</vn-thead>
<vn-tbody>
<vn-tr ng-repeat="itemLog in logs">
<vn-td>{{::itemLog.description}}</vn-td>
<vn-td>{{::itemLog.action}}</vn-td>
<vn-td>{{::itemLog.user.name}}</vn-td>
<vn-td>{{::itemLog.creationDate | dateTime:'dd/MM/yyyy HH:mm'}}</vn-td>
</vn-tr>
</vn-tbody>
</vn-table>
</vn-vertical>
<vn-pagination model="model"></vn-pagination>
</vn-card>
</vn-vertical>

View File

@ -1,25 +0,0 @@
import ngModule from '../module';
class Controller {
constructor($stateParams) {
this.$stateParams = $stateParams;
this.filter = {
include: [{
relation: "item"
},
{
relation: "user",
scope: {
fields: ["name"]
}
}]
};
}
}
Controller.$inject = ['$stateParams'];
ngModule.component('vnItemHistory', {
template: require('./index.html'),
controller: Controller
});

View File

@ -11,7 +11,7 @@ import './data';
import './fetched-tags';
import './tags';
import './tax';
// import './history';
import './log';
import './last-entries';
import './niche';
import './botanical';

View File

@ -0,0 +1,9 @@
<vn-crud-model
vn-id="model"
url="/api/ItemLogs"
link="{originFk: $ctrl.$stateParams.id}"
filter="$ctrl.filter"
limit="20"
data="$ctrl.logs" auto-load="false">
</vn-crud-model>
<vn-log model="model"></vn-log>

View File

@ -0,0 +1,53 @@
import ngModule from '../module';
class Controller {
constructor($scope, $stateParams) {
this.$scope = $scope;
this.$stateParams = $stateParams;
this.filter = {
include: [{
relation: 'user',
scope: {
fields: ['name'],
},
}],
};
}
get logs() {
return this._logs;
}
set logs(value) {
this._logs = value;
if (this.logs) {
this.logs.forEach(log => {
log.oldProperties = this.getInstance(log.oldInstance);
log.newProperties = this.getInstance(log.newInstance);
});
}
}
getInstance(instance) {
let validDate = /^(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[01]|0[1-9]|[12][0-9])T(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(.[0-9]+)?(Z)?$/;
const properties = [];
if (typeof instance == 'object' && instance != null) {
Object.keys(instance).forEach(property => {
if (validDate.test(instance[property]))
instance[property] = new Date(instance[property]).toLocaleString('es-ES');
properties.push({key: property, value: instance[property]});
});
return properties;
}
return null;
}
}
Controller.$inject = ['$scope', '$stateParams'];
ngModule.component('vnItemLog', {
template: require('./index.html'),
controller: Controller,
});

View File

@ -12,7 +12,8 @@
{"state": "item.card.botanical", "icon": "local_florist"},
{"state": "item.card.itemBarcode", "icon": "icon-barcode"},
{"state": "item.card.diary", "icon": "icon-transaction"},
{"state": "item.card.last-entries", "icon": "icon-regentry"}
{"state": "item.card.last-entries", "icon": "icon-regentry"},
{"state": "item.card.log", "icon": "history"}
],
"keybindings": [
{"key": "a", "state": "item.index"}
@ -116,6 +117,11 @@
"item": "$ctrl.item"
},
"acl": ["employee"]
}, {
"url" : "/log",
"state": "item.card.log",
"component": "vn-item-log",
"description": "Log"
}
]
}