From fca2d471b1e47fd6e2957a532d2bec16e35e08ea Mon Sep 17 00:00:00 2001 From: Juan Ferrer Toribio Date: Sat, 19 Dec 2020 02:06:06 +0100 Subject: [PATCH] Pull only overwrites modified routines --- README.md | 8 +++++--- myvc-pull.js | 36 +++++++++++++++++++++++++++++++----- package.json | 2 +- 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 2bb9a74..af1f9c2 100644 --- a/README.md +++ b/README.md @@ -153,9 +153,11 @@ the following steps: 6. Merge the new branch into. 7. Let the user deal with merge conflicts. -Furthermore, migrate all possible tools and code from shell scripts to native -Javascript, dealing with dependencies and compatibility issues between various -OS that this may cause. +Use a custom *Dockerfile* for local database container. + +Allow to specify a custom workspece subdirectory inside project directory. + +Create a lock during push to avoid collisions. ## Built With diff --git a/myvc-pull.js b/myvc-pull.js index 9d565f3..4130a03 100755 --- a/myvc-pull.js +++ b/myvc-pull.js @@ -11,9 +11,14 @@ class Pull { await exporter.init(); const exportDir = `${opts.workspace}/routines`; - if (await fs.pathExists(exportDir)) - await fs.remove(exportDir, {recursive: true}); - await fs.mkdir(exportDir); + if (!await fs.pathExists(exportDir)) + await fs.mkdir(exportDir); + + const schemas = await fs.readdir(exportDir); + for (const schema of schemas) { + if (opts.schemas.indexOf(schema) == -1) + await fs.remove(`${exportDir}/${schema}`, {recursive: true}); + } for (const schema of opts.schemas) { let schemaDir = `${exportDir}/${schema}`; @@ -52,13 +57,34 @@ class Exporter { if (!await fs.pathExists(routineDir)) await fs.mkdir(routineDir); + const routineSet = new Set(); + for (const params of res) + routineSet.add(params.name); + + const routines = await fs.readdir(routineDir); + for (const routineFile of routines) { + const match = routineFile.match(/^(.*)\.sql$/); + if (!match) continue; + const routine = match[1]; + if (!routineSet.has(routine)) + await fs.remove(`${routineDir}/${routine}.sql`); + } + for (const params of res) { if (this.formatter) this.formatter(params, schema) params.schema = schema; - let sql = this.template(params); - await fs.writeFile(`${routineDir}/${params.name}.sql`, sql); + const sql = this.template(params); + const routineFile = `${routineDir}/${params.name}.sql`; + let changed = true; + + if (await fs.pathExists(routineFile)) { + const currentSql = await fs.readFile(routineFile, 'utf8'); + changed = currentSql !== sql; + } + if (changed) + await fs.writeFile(routineFile, sql); } } } diff --git a/package.json b/package.json index b4e5f00..8448239 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "myvc", - "version": "1.1.6", + "version": "1.1.7", "author": "Verdnatura Levante SL", "description": "MySQL Version Control", "license": "GPL-3.0",