import-changes
gitea/salix/dev This commit looks good Details

This commit is contained in:
Bernat 2019-03-12 12:20:14 +01:00
parent fb75ff0eae
commit 55fd1c233e
7 changed files with 45 additions and 126 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

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