Skip to content

Commit

Permalink
Merge pull request #5 from tyr-liu/master
Browse files Browse the repository at this point in the history
fix bugs : backup and excuting sql
  • Loading branch information
novrain committed Dec 14, 2015
2 parents 0415496 + fa50bd1 commit a10703b
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 29,280 deletions.
2 changes: 1 addition & 1 deletion build-package.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ css-loader
jsx-loader
react
react-router
react-dropzone
react-dropzone@1.2.4
superagent
sweetalert
q
Expand Down
93 changes: 44 additions & 49 deletions freesun/services/updateservice.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/**
* Created by rain on 2015/8/25.
*/
/*jslint node:true */
Expand Down Expand Up @@ -173,21 +173,21 @@ Us.prototype.install = function (pkg) {
.then(function () {
return that.loadInstallScript(projWrokspace);
}).then(function (flow) {
return that.flow.install(flow)
.then(function (flow) {
return that.newProject(flow);
})
.then(function (flow) {
that.finish();
})
.catch(function (flow) {
that.failed();
return that.flow.uninstall(flow);
});
}).catch(function (err) {
that.failed();
//logger.error(err);
});
return that.flow.install(flow)
.then(function (flow) {
return that.newProject(flow);
})
.then(function (flow) {
that.finish();
})
.catch(function (flow) {
that.failed();
return that.flow.uninstall(flow);
});
}).catch(function (err) {
that.failed();
//logger.error(err);
});
} catch (e) {
that.failed();
throw new Error(ustring.sprintf('This project does not exist or can not be updated. e: %s', e.stack));
Expand Down Expand Up @@ -259,25 +259,31 @@ Us.prototype.update = function (pjid, pkg) {
oldProj = merge(oldProj, projWorkspace);
return that.loadUpdateScript(oldProj, projWorkspace);
}).then(function (flow) {
return that.flow.newFlow.update(flow)
.then(function () {
return that.updateProject(flow.newFlow);
})
.then(function () {
that.finish();
//return Q();
})
.catch(function () {
that.failed();
return that.flow.newFlow.rollback(that.flow);
});
//.then(function () {
//});
}).catch(function (err) {
that.failed();
return Q();
//logger.error(err);
});
that.flow.oldFlow.project.workspace.keepOldBackup = that.flow.newFlow.project.workspace.keepOldBackup;
return that.flow.newFlow.update(flow)
.then(function () {
return that.updateProject(flow.newFlow);
})
.then(function () {
that.finish();
if(!flow.newFlow.project.workspace.keepOldBackup) {
var p = path.join(flow.oldFlow.project.workspace.backup, 'temp', flow.oldFlow.project.name);
fs.emptyDirSync(p);
fs.rmdirSync(p);
}
//return Q();
})
.catch(function () {
that.failed();
return that.flow.newFlow.rollback(that.flow);
});
//.then(function () {
//});
}).catch(function (err) {
that.failed();
return Q();
//logger.error(err);
});
} catch (e) {
that.failed();
throw new Error(ustring.sprintf('This project does not exist or can not be updated. e: %s', e.stack));
Expand Down Expand Up @@ -349,19 +355,8 @@ Us.prototype.begin = function (projWorkspace) {
this.updateStep(flowStep, new StepLog('Begin...', 1, 'info'));
this.updateStep(flowStep, new StepLog(ustring.sprintf('Package: %s', pkgPath), 1, 'info'));
Project.pauseWatching();
(function () {
if (projWorkspace.workspace.keepOldBackup) {
var now = new Date();
projWorkspace.workspace.backup = path.join(projWorkspace.workspace.backupRoot,
ustring.sprintf('%s-%s-%s %s-%s-%s', now.getFullYear(), now.getMonth() + 1,
now.getDate(), now.getHours(), now.getMinutes(), now.getSeconds()));
} else {
projWorkspace.workspace.backup = projWorkspace.workspace.backupRoot;
}
return Q.nfcall(fs.emptyDir, projWorkspace.workspace.backup);
}()).then(function () {
return Q.nfcall(fs.emptyDir, decompressPath);
})
projWorkspace.workspace.backup = projWorkspace.workspace.backupRoot;
Q.nfcall(fs.emptyDir, decompressPath)
.then(function () {
var reader = fs.createReadStream(pkgPath);
reader.pipe(unzip.Extract({path: decompressPath}));
Expand Down Expand Up @@ -407,7 +402,7 @@ Us.prototype.selfUpdate = function (pkg) {
setTimeout(function () {
exec("npm install", {cwd: Us.ROOT}, function (errIn, stdOut, stdErr) {
var err = errIn || stdErr.trim();
if (err) {
if (/npm ERR!/g.test(err)) {
that.updateStep(flowStep, new StepLog(ustring.sprintf('Failed: %s', err), 1, 'error'));
} else {
that.updateStep(flowStep, new StepLog(ustring.sprintf('Success: %s', stdOut), 1, 'info'));
Expand Down
56 changes: 48 additions & 8 deletions freesun/upgrade/copyutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,43 @@ var StepStatus = require('./flowStep').StepStatus;

var CopyUtil = {};

function getBackupDir(flow, createNew){
if(!flow.project.workspace.keepOldBackup){
return path.join(flow.project.workspace.backup, 'temp', flow.project.name);
} else{
var number = 0;
var p = path.join(flow.project.workspace.backup, flow.project.name);
if(fs.existsSync(p)){
var files = fs.readdirSync(p);
files.forEach(function (f){
if(Number(f) > number){
number = Number(f);
}
});
}
if(createNew){
number = number + 1;
}
p = path.join(p, number.toString());
return number === 0 ? null : p;
}
}

var backup = function (flow, targetArray, desc) {
var deferred = Q.defer(),
flowStep,
stepLog,
targetDir;
flowStep = new FlowStep(ustring.sprintf('Backup %s.', desc));
targetDir = flow.project.workspace.backup;
flow.service.newStep(flowStep);
flow.service.updateStep(flowStep, new StepLog('Begin...', 1, 'info'));
targetDir = getBackupDir(flow, true);
if(!targetDir){
stepLog = new StepLog(ustring.sprintf('Fail, reason: backup dir not found or created error'), 2, 'error');
flowStep.status = StepStatus.Failed;
flow.service.updateStep(flowStep, stepLog);
deferred.reject(flow);
}
if (Array.isArray(targetArray)) {
targetArray.reduce(function (prev, next, i) {
return prev.then(function () {
Expand Down Expand Up @@ -149,7 +177,12 @@ var migrate = function (flow, targetArray, targetDir, desc) {
flow.service.newStep(step);
flow.service.updateStep(step, new StepLog('Begin...', 1, 'info'));

var backup = flow.project.workspace.backup;
var backup = getBackupDir(flow, false);
if(!backup){
step.status = StepStatus.Failed;
flow.service.updateStep(step, new StepLog('Fail. reason: backup dir not found', 2, 'error'));
deferred.reject(flow);
}

var internalCopy = function (p, c) {
var source = path.join(backup, p.path, c.from);
Expand All @@ -170,10 +203,10 @@ var migrate = function (flow, targetArray, targetDir, desc) {
} else {
flow.service.updateStep(step, new StepLog('migrate ' + p.name + '..', 2, 'info'));
return copy.reduce(function (prev, next) {
return prev.then(function () {
return internalCopy(p, next);
});
}, Q())
return prev.then(function () {
return internalCopy(p, next);
});
}, Q())
.then(function () {
flow.service.updateStep(step, new StepLog(p.name + ' migrate completed.', 2, 'info'));
return Q();
Expand Down Expand Up @@ -204,17 +237,24 @@ var restore = function (flow, targetArray, desc) {
stepLog,
targetDir;
flowStep = new FlowStep(ustring.sprintf('Restore %s.', desc), 'warn');
targetDir = flow.project.workspace.backup;
flow.service.newStep(flowStep);
flow.service.updateStep(flowStep, new StepLog('Begin...', 1, 'warn'));

if (!flow.service.findStep(ustring.sprintf('Backup %s.', desc)) || flow.service.findStep(ustring.sprintf('Backup %s.', desc)).status !== StepStatus.Success) {
flowStep.status = StepStatus.Failed;
flow.service.updateStep(flowStep, new StepLog('skipped. reason: there is no backup found', 1, 'error'));
flow.service.updateStep(flowStep, new StepLog('skipped. reason: there is no backup found', 2, 'error'));
deferred.resolve(flow);
return;
}

targetDir = getBackupDir(flow, false);
if(!targetDir){
flowStep.status = StepStatus.Failed;
flow.service.updateStep(flowStep, new StepLog('skipped. reason: backup dir not found', 2, 'error'));
deferred.resolve(flow);
}


if (Array.isArray(targetArray)) {
targetArray.reduce(function (prev, next, i) {
return prev.then(function () {
Expand Down
73 changes: 66 additions & 7 deletions freesun/upgrade/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,79 @@ var restore = function (flow, check) {
};
Database.restore = restore;

function fillZero(str) {
var strs = str.split('.');
var rs = [];
strs.forEach(function (s) {
s = '0000' + s;
rs.push(s.substring(s.length - 4, s.length));
});

return rs.join('.');
}

function getCompareStr(v) {
var vs = v.split('-');
var from = fillZero(vs[0]);
var to = fillZero(vs[1]);
return from + '-' + to;
}

function compareVersion(v1, v2) {
v1 = getCompareStr(v1);
v2 = getCompareStr(v2);

if (v1 < v2) return -1;
if (v1 == v2) return 0;
if (v1 > v2) return 1;
}

function getMinVersion(versions) {
var min = versions[0];
if (versions.length > 1) {
for (var i = 1; i < versions.length; i++) {
if (compareVersion(min, versions[i]) > 0) {
min = versions[i];
}
}
}

return min;
}

var executeSqls = function (flow) {
var sqlCmd = flow.project.sqlCmd,
deferred = Q.defer(),
SCHEMA = 'schema',
DATA = 'data',
flowStep,
stepLog,
executeOptions = extend({}, sqlCmd.loginTemplate, sqlCmd.ioTemplate),
sqlFiles;
sqlSchemaFiles,
sqlDataFiles,
sqlFiles = [];
flowStep = new FlowStep('Execute sql scripts.');
flow.service.newStep(flowStep);
flow.service.updateStep(flowStep, new StepLog('Begin...', 1, 'info'));
try {
sqlFiles = fs.readdirSync(sqlCmd.inputFiles);
var versions = fs.readdirSync(sqlCmd.inputFiles);
while (versions.length > 0) {
var minVersion = getMinVersion(versions);
sqlSchemaFiles = fs.readdirSync(path.join(sqlCmd.inputFiles, minVersion, SCHEMA));
sqlDataFiles = fs.readdirSync(path.join(sqlCmd.inputFiles, minVersion, DATA));
sqlFiles = sqlFiles.concat(!sqlSchemaFiles ? [] : sqlSchemaFiles.filter(function (file) {
return path.extname(file) === '.sql';
}).map(function (file) {
return path.join(sqlCmd.inputFiles, minVersion, SCHEMA, file);
}));
sqlFiles = sqlFiles.concat(!sqlDataFiles ? [] : sqlDataFiles.filter(function (file) {
return path.extname(file) === '.sql';
}).map(function (file) {
return path.join(sqlCmd.inputFiles, minVersion, DATA, file);
}));

versions.splice(versions.indexOf(minVersion), 1);
}
} catch (err) {
stepLog = new StepLog('Error. reason:' + err, 1, 'warn');
stepLog = new StepLog('End...', 1, 'info');
Expand All @@ -117,11 +178,9 @@ var executeSqls = function (flow) {
deferred.resolve(flow);
return deferred.promise;
}
executeOptions.inputFiles = !sqlFiles ? [] : sqlFiles.filter(function (file) {
return path.extname(file) === '.sql';
}).map(function (file) {
return path.join(sqlCmd.inputFiles, file);
});
executeOptions.inputFiles = sqlFiles;
console.log(executeOptions.inputFiles);

if (executeOptions.inputFiles && executeOptions.inputFiles.length > 0) {
executeOptions.outputFile = sqlCmd.outputFile;
sqlcmd(executeOptions, function (data) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"passport-local": "^1.0.0",
"q": "^1.4.1",
"react": "^0.13.3",
"react-dropzone": "^1.3.0",
"react-dropzone": "=1.2.4",
"react-router": "^0.13.4",
"readable-stream": "^1.0.33",
"serve-favicon": "^2.3.0",
Expand Down
Loading

0 comments on commit a10703b

Please sign in to comment.