slite-change
Advanced tools
Comparing version 1.1.1 to 1.1.2
{ | ||
"name": "slite-change", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "A simple change management system for SQLite3 written in pure JavaScript/TypeScript.", | ||
@@ -5,0 +5,0 @@ "main": "dist/slite-change.js", |
@@ -9,2 +9,10 @@ "use strict"; | ||
type CallStack = { | ||
id: number; | ||
previous: number; | ||
author: string; | ||
sql: string; | ||
file: string; | ||
}; | ||
/** | ||
@@ -21,3 +29,3 @@ * While creating a moder Angular web service to run on Raspberry Pi, a DB change control was needed. | ||
private ChangeLogDir: string; | ||
private ChangeId: number; | ||
private LastCompletedChangeId: number; | ||
private ErrorMessage: string; | ||
@@ -27,2 +35,4 @@ private ErrorFile: string; | ||
private ChangesCompleted: number; | ||
private callStack: any; | ||
private sourceFiles: number; | ||
@@ -33,3 +43,3 @@ constructor(Sql: typeof sqlite.Database, ChangeLog: string, Callback: any) { | ||
this.Sql = Sql; | ||
this.ChangeId = 0; | ||
this.LastCompletedChangeId = 0; | ||
this.ErrorMessage = ''; | ||
@@ -39,2 +49,4 @@ this.ErrorFile = '' | ||
this.ChangesCompleted = 0; | ||
this.callStack = new Array(); | ||
this.sourceFiles = 0; | ||
@@ -46,6 +58,6 @@ this.ChangeLogTable((errMsg: string) => { | ||
this.ReadChangeFiles((errorMsg: string) => { | ||
// console.log('main err "%s", Issued %d, Completed %d', | ||
// errorMsg, this.ChangesIssued, this.ChangesCompleted); | ||
if((errorMsg != '') || ( (this.ChangesIssued > 0) && (this.ChangesCompleted === this.ChangesIssued))) { | ||
Callback(errorMsg); | ||
// console.log('main err "%s", files %d, Issued %d, Completed %d, callStack length %d', | ||
// errorMsg, this.sourceFiles, this.ChangesIssued, this.ChangesCompleted, this.callStack.length); | ||
if((errorMsg != '') || ( (this.sourceFiles === this.ChangesIssued))) { | ||
Callback(errorMsg); | ||
} | ||
@@ -59,3 +71,3 @@ }); | ||
* Hold the first failure | ||
* @param NewError | ||
* @param NewError | ||
*/ | ||
@@ -82,13 +94,15 @@ private SetError(NewError: string, Callback: any) { | ||
private ReadChangeFiles(Callback: any) { | ||
let PreviousStack = 0; | ||
// ChangeAuthor, ChangeNumber, Buffer should be a type. | ||
// And the values should be added to an array that sends off in the end. | ||
fs.readdir(this.ChangeLogDir, (err: any, files: any ) => { | ||
fs.readdir(this.ChangeLogDir, (err: any, files: any ) => { | ||
if (err) { | ||
this.SetError('Unable to scan directory: ' + err, Callback); | ||
} | ||
files.sort().forEach( (file: string) => { | ||
this.sourceFiles = files.length; | ||
files.sort().forEach( (file: string) => { | ||
fs.readFile(this.ChangeLogDir + file, (err: any, data: any) => { | ||
if(err) { | ||
this.SetError('Error in change file "'+file+'": '+err.message, Callback); | ||
} else { | ||
} else { | ||
let Buffer = ''; | ||
@@ -101,8 +115,2 @@ let ChangeAuthor = ''; | ||
if(line.startsWith('--changeset ')) { | ||
if(Buffer.length > 0) { | ||
this.CommitChange(ChangeAuthor, ChangeNumber, Buffer, (ErrMsg: string) => { | ||
Callback(ErrMsg); | ||
}); | ||
Buffer = ''; | ||
} | ||
ChangeAuthor = line.split(/[ :]+/)[1]; | ||
@@ -119,5 +127,18 @@ ChangeNumber = line.split(/[ :]+/)[2]; | ||
if(Buffer.length > 0) { | ||
this.CommitChange(ChangeAuthor, ChangeNumber, Buffer, (ErrMsg: string) => { | ||
Callback(ErrMsg); | ||
let Pre = parseInt(ChangeNumber) - 1; | ||
this.callStack.push({ | ||
id: ChangeNumber, | ||
previous: Pre, | ||
author: ChangeAuthor, | ||
sql: Buffer, | ||
file: file | ||
}); | ||
// console.log(" id %d, \npre %s, \nauthor %s, \nsql %s, \nfile %s,\nfiles %d,\ncallStack %d", | ||
// ChangeNumber, Pre, ChangeAuthor, Buffer.substring(0,40), file, files.length, this.callStack.length); | ||
if(this.callStack.length === files.length) { | ||
this.CommitChange(this.callStack.find( (cB: CallStack) => cB.previous == 0), (ErrMsg: string) => { | ||
Callback(ErrMsg); | ||
}); | ||
} | ||
} | ||
@@ -127,36 +148,48 @@ } | ||
}); | ||
// Done each file and no errors needs to call the Callback | ||
Callback(this.ErrorMessage); | ||
}) | ||
} | ||
private CommitChange(Author: string, ChangeNumber: string, ChangeBlock: string, Callback: any) { | ||
if(this.ErrorMessage) { return; } | ||
private CommitChange(callStack: CallStack, Callback: any) { | ||
this.ChangesIssued++; | ||
if(Number(this.ChangeId) >= Number(ChangeNumber)) return; | ||
let DateTime = date.format(new Date(), 'YYYY-MM-DD HH:mm:ss'); | ||
let hash = md5(ChangeBlock); | ||
let hash = md5(callStack.sql); | ||
if(this.ErrorMessage == '') { | ||
this.Sql.exec(ChangeBlock, (err: any) => { | ||
this.ChangesCompleted++; | ||
if(err) { | ||
this.SetError('Error in change number '+ChangeNumber+'\n'+ err.message, Callback); | ||
this.Sql.get('SELECT COUNT(*) AS c FROM CHANGELOG WHERE id = '+callStack.id, (err: any, row: any) => { | ||
if(row['c'] === 0) { | ||
this.Sql.exec(callStack.sql, (err: any) => { | ||
this.ChangesCompleted++; | ||
if(err) { | ||
this.SetError('Error in change number '+callStack.id+'\n'+ err.message, Callback); | ||
} else { | ||
console.log('SQL ['+callStack.id+']: %s\n', callStack.sql); | ||
this.LastCompletedChangeId++; | ||
// An error can close the SQL just before this runs. | ||
if(this.Sql) { | ||
this.Sql.run("INSERT INTO CHANGELOG \ | ||
(id, changeID, author, datetime, md5sum, size) \ | ||
VALUES ((SELECT IIF(COUNT(id) == 0, 1, MAX(id)+1) AS id FROM CHANGELOG), ?, ?, ?, ?, ?);", | ||
callStack.id, | ||
callStack.author, | ||
DateTime, | ||
hash, | ||
callStack.sql.length | ||
); | ||
} | ||
let Next = this.callStack.find( (cB: CallStack) => cB.previous == callStack.id); | ||
if(Next) { | ||
this.CommitChange(Next, Callback); | ||
} else { | ||
// This the all done callback | ||
Callback(this.ErrorMessage); | ||
} | ||
} | ||
}); | ||
} else { | ||
console.log('SQL ['+ChangeNumber+']: %s\n', ChangeBlock); | ||
this.ChangeId++; | ||
// An error can close the SQL just before this runs. | ||
if(this.Sql) { | ||
this.Sql.run("INSERT INTO CHANGELOG \ | ||
(id, changeID, author, datetime, md5sum, size) \ | ||
VALUES ((SELECT IIF(COUNT(id) == 0, 1, MAX(id)+1) AS id FROM CHANGELOG), ?, ?, ?, ?, ?);", | ||
this.ChangeId, | ||
Author, | ||
DateTime, | ||
hash, | ||
ChangeBlock.length | ||
); | ||
let Next = this.callStack.find( (cB: CallStack) => { return cB.previous == callStack.id}); | ||
if(Next) { | ||
this.CommitChange(Next, Callback); | ||
} else { | ||
// This the all done callback | ||
Callback(this.ErrorMessage); | ||
} | ||
Callback(this.ErrorMessage); | ||
} | ||
@@ -171,3 +204,3 @@ }); | ||
* Get the value highest change ID | ||
*/ | ||
*/ | ||
private ChangeLogTable(Callback: any) { | ||
@@ -183,4 +216,3 @@ this.Sql.get("SELECT COUNT(*) AS COUNT FROM sqlite_master WHERE type='table' AND name='CHANGELOG';", (err: any, row: any) => { | ||
} | ||
this.ChangeId = row.id; | ||
console.log('This ChangeId = '+this.ChangeId); | ||
this.LastCompletedChangeId = row.id; | ||
}); | ||
@@ -196,8 +228,8 @@ } else { | ||
PRIMARY KEY ('id'));"); | ||
this.ChangeId = 0; | ||
console.log('Created change log table'); | ||
this.LastCompletedChangeId = 0; | ||
console.log('Created change log table'); | ||
} | ||
Callback(this.ErrorMessage); | ||
} | ||
}); | ||
}); | ||
} | ||
@@ -204,0 +236,0 @@ } |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
42577
424