diff --git a/myvc-dump.js b/myvc-dump.js index 2b5a4d1..b8cc9fd 100644 --- a/myvc-dump.js +++ b/myvc-dump.js @@ -38,7 +38,7 @@ class Dump { await docker.build(__dirname, { tag: 'myvc/client', - file: path.join(__dirname, 'server', 'Dockerfile') + file: path.join(__dirname, 'server', 'Dockerfile.client') }, opts.debug); let dumpArgs = [ diff --git a/myvc-push.js b/myvc-push.js index 11d6230..58aa20f 100644 --- a/myvc-push.js +++ b/myvc-push.js @@ -31,10 +31,10 @@ class Push { + `\n -> Commit: ${version.gitCommit}` ); - if (!/^[0-9]*$/.test(version.number)) - throw new Error('Wrong database version'); if (!version.number) version.number = '00000'; + if (!/^[0-9]*$/.test(version.number)) + throw new Error('Wrong database version'); if (opts.user) { const [[user]] = conn.query( @@ -217,8 +217,9 @@ class Push { parseChanges(changes) { const routines = []; - for (const change of changes) - routines.push(new Routine(change)); + if (changes) + for (const change of changes) + routines.push(new Routine(change)); return routines; } diff --git a/myvc-run.js b/myvc-run.js index 8165a4a..9b0b840 100644 --- a/myvc-run.js +++ b/myvc-run.js @@ -27,6 +27,10 @@ class Run { async run(myvc, opts) { const dumpDir = `${opts.workspace}/dump`; + + if (!await fs.pathExists(`${dumpDir}/.dump.sql`)) + throw new Error('To run local database you have to create a dump first'); + const dumpInfo = `${dumpDir}/.dump.json`; if (await fs.pathExists(dumpInfo)) { @@ -46,7 +50,6 @@ class Run { } if (!isEqual) { - console.log('not equal'); const fd = await fs.open(`${dumpDir}/.changes`, 'w+'); for (const change of changes) fs.write(fd, change.mark + change.path + '\n'); diff --git a/myvc.js b/myvc.js index bb3453a..632ceb4 100755 --- a/myvc.js +++ b/myvc.js @@ -131,6 +131,7 @@ class MyVC { user: iniConfig.user, password: iniConfig.password, database: opts.versionSchema, + multipleStatements: true, authPlugins: { mysql_clear_password() { return () => iniConfig.password + '\0'; @@ -194,28 +195,34 @@ class MyVC { return version; } - async changedRoutines(commit) { - const repo = await nodegit.Repository.open(this.opts.workspace); + async changedRoutines(commitSha) { + const {opts} = this; - const from = await repo.getCommit(commit); - const fromTree = await from.getTree(); - - const to = await repo.getHeadCommit(); - const toTree = await to.getTree(); - - const diff = await toTree.diff(fromTree); - const patches = await diff.patches(); + if (!await fs.pathExists(`${opts.workspace}/.git`)) + throw new Error ('Git not initialized'); const changes = []; - for (const patch of patches) { - const path = patch.newFile().path(); - const match = path.match(/^routines\/(.+)\.sql$/); - if (!match) continue; + const repo = await nodegit.Repository.open(opts.workspace); + const head = await repo.getHeadCommit(); - changes.push({ - mark: patch.isDeleted() ? '-' : '+', - path: match[1] - }); + if (head && commitSha) { + const commit = await repo.getCommit(commitSha); + const commitTree = await commit.getTree(); + + const headTree = await head.getTree(); + const diff = await headTree.diff(commitTree); + const patches = await diff.patches(); + + for (const patch of patches) { + const path = patch.newFile().path(); + const match = path.match(/^routines\/(.+)\.sql$/); + if (!match) continue; + + changes.push({ + mark: patch.isDeleted() ? '-' : '+', + path: match[1] + }); + } } return changes.sort( @@ -224,7 +231,6 @@ class MyVC { } async cachedChanges() { - const changes = []; const dumpDir = `${this.opts.workspace}/dump`; const dumpChanges = `${dumpDir}/.changes`; @@ -238,13 +244,13 @@ class MyVC { console: false }); + const changes = []; for await (const line of rl) { changes.push({ mark: line.charAt(0), path: line.substr(1) }); } - return changes; } } diff --git a/package.json b/package.json index c44b9ee..f23135b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "myvc", - "version": "1.1.4", + "version": "1.1.5", "author": "Verdnatura Levante SL", "description": "MySQL Version Control", "license": "GPL-3.0", diff --git a/structure.sql b/structure.sql index 082856c..d5bf51a 100644 --- a/structure.sql +++ b/structure.sql @@ -1,8 +1,8 @@ CREATE TABLE `version` ( `code` varchar(255) NOT NULL, - `number` char(11) NOT NULL, - `gitCommit` varchar(255) NOT NULL, + `number` char(11) NULL, + `gitCommit` varchar(255) NULL, `updated` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ) ENGINE=InnoDB; @@ -12,8 +12,8 @@ ALTER TABLE `version` CREATE TABLE `versionUser` ( `code` varchar(255) NOT NULL, `user` varchar(255) NOT NULL, - `number` char(11) NOT NULL, - `gitCommit` varchar(255) NOT NULL, + `number` char(11) NULL, + `gitCommit` varchar(255) NULL, `updated` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ) ENGINE=InnoDB;