Import SQL changes

This commit is contained in:
Juan Ferrer 2019-03-20 12:21:32 +01:00
parent 21c1b8f56f
commit 1f3117e7e5
5 changed files with 92 additions and 93 deletions

31
Jenkinsfile vendored
View File

@ -6,11 +6,13 @@ pipeline {
disableConcurrentBuilds() disableConcurrentBuilds()
} }
environment { environment {
PROJECT_NAME = 'salix' PROJECT_NAME = 'salix'
REGISTRY = 'registry.verdnatura.es' REGISTRY = 'registry.verdnatura.es'
DOCKER_HOST_1 = 'vch1.verdnatura.es' DOCKER_HOST_1 = 'vch1.verdnatura.es'
DOCKER_HOST_2 = 'vch2.verdnatura.es' DOCKER_HOST_2 = 'vch2.verdnatura.es'
TAG = "${env.BRANCH_NAME}" PORT_MASTER = '5002'
PORT_TEST = '5001'
TAG = "${env.BRANCH_NAME}"
} }
stages { stages {
stage('Checkout') { stage('Checkout') {
@ -29,10 +31,10 @@ pipeline {
switch (env.BRANCH_NAME) { switch (env.BRANCH_NAME) {
case 'master': case 'master':
env.PORT = 5002 env.PORT = PORT_MASTER
break break
case 'test': case 'test':
env.PORT = 5001 env.PORT = PORT_TEST
break break
} }
switch (env.BRANCH_NAME) { switch (env.BRANCH_NAME) {
@ -126,6 +128,21 @@ pipeline {
} }
} }
} }
stage('Database') {
when { anyOf {
branch 'test'
}}
steps {
configFileProvider([
configFile(fileId: "config.${env.NODE_ENV}.ini",
variable: 'MYSQL_CONFIG')
]) {
sh 'cp $MYSQL_CONFIG db/config.$NODE_ENV.ini'
}
sh 'db/import-changes.sh -f $NODE_ENV'
}
}
stage('Cleanup') { stage('Cleanup') {
when { anyOf { when { anyOf {
branch 'test' branch 'test'

2
db/.gitignore vendored
View File

@ -1 +1 @@
connect.ini config.*.ini

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;
}

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

@ -0,0 +1,67 @@
#!/bin/bash
FORCE=FALSE
usage() {
echo "Usage: $0 [-f] [environment]"
exit 1
}
while getopts ":f" option
do
case $option in
f)
FORCE=TRUE
;;
\?|:)
usage
;;
esac
done
shift $(($OPTIND - 1))
ENV=$1
if [ "$ENV" == "production" ]; then
echo ""
echo " ( ( ) ( ( ) ) "
echo " )\ ))\ ) ( /( )\ ) ( * ))\ ) ( /( ( /( "
echo "(()/(()/( )\()|()/( ( )\ ) /(()/( )\()) )\())"
echo " /(_))(_)|(_)\ /(_)) )\ (((_) ( )(_))(_)|(_)\ ((_)\ "
echo "(_))(_)) ((_|_))_ _ ((_))\___(_(_()|_)) ((_) _((_)"
echo "| _ \ _ \ / _ \| \| | | ((/ __|_ _|_ _| / _ \| \| |"
echo "| _/ /| (_) | |) | |_| || (__ | | | | | (_) | . |"
echo "|_| |_|_\ \___/|___/ \___/ \___| |_| |___| \___/|_|\_|"
echo ""
if [ "$FORCE" != "TRUE" ]; then
read -p "Are you sure? (Default: no) [yes|no]: " ANSWER
if [ "$ANSWER" != "yes" ]; then
echo "Aborting"
exit;
fi
fi
fi
if [ -z "$ENV" ]; then
ENV="test"
fi
DIR="$(dirname "${BASH_SOURCE[0]}")"
INI_FILE="$DIR/config.$ENV.ini"
if [ ! -f "$INI_FILE" ]; then
echo "Config file doesn't exists: $INI_FILE"
exit 2
fi
echo "[INFO] Config file: $INI_FILE"
echo "[INFO] Applying changes"
# Import changes
for file in $DIR/install/changes/*.sql; do
echo "[INFO] -> $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);
}
})();