@verdnatura/myt 1.5.24 → 1.5.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -250,7 +250,7 @@ $ myt create [-t <type>] <schema>.<name>
250
250
  Cleans all already applied versions older than *maxOldVersions*.
251
251
 
252
252
  ```text
253
- $ myt clean
253
+ $ myt clean [-p|--purge]
254
254
  ```
255
255
 
256
256
  ## Local server commands
@@ -276,8 +276,7 @@ $ myt fixtures [<remote>]
276
276
  ### run
277
277
 
278
278
  Builds and starts local database server container. It only rebuilds the image
279
- when fixtures have been modified or when the day on which the image was built
280
- is different to today.
279
+ dump has been modified.
281
280
 
282
281
  ```text
283
282
  $ myt run [-c|--ci] [-r|--random]
package/myt-clean.js CHANGED
@@ -8,26 +8,36 @@ const path = require('path');
8
8
  */
9
9
  class Clean extends Command {
10
10
  static usage = {
11
- description: 'Cleans old applied versions'
11
+ description: 'Cleans old applied versions',
12
+ params: {
13
+ purge: 'Wether to remove non-existent scripts from DB log'
14
+ }
12
15
  };
13
16
 
14
17
  static opts = {
18
+ alias: {
19
+ purge: 'p'
20
+ },
21
+ boolean: [
22
+ 'purge'
23
+ ],
15
24
  default: {
16
25
  remote: 'production'
17
26
  }
18
27
  };
19
28
 
20
29
  async run(myt, opts) {
21
- await myt.dbConnect();
30
+ const conn = await myt.dbConnect();
31
+ const versionDirs = await fs.readdir(opts.versionsDir);
32
+ const archiveDir = path.join(opts.versionsDir, '.archive');
33
+
22
34
  const dbVersion = await myt.fetchDbVersion() || {};
23
35
  const number = parseInt(dbVersion.number);
24
36
 
25
37
  const oldVersions = [];
26
- const versionDirs = await fs.readdir(opts.versionsDir);
27
38
  for (const versionDir of versionDirs) {
28
39
  const version = await myt.loadVersion(versionDir);
29
40
  const shouldArchive = version
30
- && version.matchRegex
31
41
  && !version.apply
32
42
  && parseInt(version.number) < number;
33
43
 
@@ -39,19 +49,85 @@ class Clean extends Command {
39
49
  && oldVersions.length > opts.maxOldVersions) {
40
50
  oldVersions.splice(-opts.maxOldVersions);
41
51
 
42
- const archiveDir = path.join(opts.versionsDir, '.archive');
43
52
  if (!await fs.pathExists(archiveDir))
44
53
  await fs.mkdir(archiveDir);
45
54
 
46
- for (const oldVersion of oldVersions)
47
- await fs.move(
48
- path.join(opts.versionsDir, oldVersion),
49
- path.join(archiveDir, oldVersion)
50
- );
55
+ for (const oldVersion of oldVersions) {
56
+ const srcDir = path.join(opts.versionsDir, oldVersion);
57
+ const dstDir = path.join(archiveDir, oldVersion);
58
+
59
+ if (!await fs.pathExists(dstDir))
60
+ await fs.mkdir(dstDir);
61
+
62
+ const scripts = await fs.readdir(srcDir);
63
+ for (const script of scripts) {
64
+ await fs.move(
65
+ path.join(srcDir, script),
66
+ path.join(dstDir, script),
67
+ {overwrite: true}
68
+ );
69
+ }
70
+
71
+ await fs.rmdir(srcDir);
72
+ }
51
73
 
52
74
  console.log(`Old versions archived: ${oldVersions.length}`);
53
75
  } else
54
76
  console.log(`No versions to archive.`);
77
+
78
+ if (opts.purge) {
79
+ const versionDb = new VersionDb(myt, opts.versionsDir);
80
+ versionDb.load();
81
+
82
+ const archiveDb = new VersionDb(myt, archiveDir);
83
+ archiveDb.load();
84
+
85
+ const [res] = await conn.query(
86
+ `SELECT number, file FROM versionLog
87
+ WHERE code = ?
88
+ ORDER BY number, file`,
89
+ [opts.code]
90
+ );
91
+
92
+ for (const script of res) {
93
+ const hasVersion = await versionDb.hasScript(script);
94
+ const hasArchive = await archiveDb.hasScript(script);
95
+
96
+ if (!hasVersion && !hasArchive) {
97
+ await conn.query(
98
+ `DELETE FROM versionLog
99
+ WHERE code = ? AND number = ? AND file = ?`,
100
+ [opts.code, script.number, script.file]
101
+ );
102
+ }
103
+ }
104
+ }
105
+ }
106
+ }
107
+
108
+ class VersionDb {
109
+ constructor(myt, baseDir) {
110
+ Object.assign(this, {myt, baseDir});
111
+ }
112
+
113
+ async load() {
114
+ const versionMap = this.versionMap = new Map();
115
+ if (await fs.pathExists(this.baseDir)) {
116
+ const dirs = await fs.readdir(this.baseDir);
117
+ for (const dir of dirs) {
118
+ const version = this.myt.parseVersionDir(dir);
119
+ if (!version) continue;
120
+ versionMap.set(version.number, dir);
121
+ }
122
+ }
123
+ return versionMap;
124
+ }
125
+
126
+ async hasScript(script) {
127
+ const dir = this.versionMap.get(script.number);
128
+ if (!dir) return false;
129
+ const scriptPath = path.join(this.baseDir, dir, script.file);
130
+ return await fs.pathExists(scriptPath);
55
131
  }
56
132
  }
57
133
 
package/myt-run.js CHANGED
@@ -9,10 +9,9 @@ const connExt = require('./lib/conn');
9
9
  const SqlString = require('sqlstring');
10
10
 
11
11
  /**
12
- * Builds the database image and runs a container. It only rebuilds the
13
- * image when fixtures have been modified or when the day on which the
14
- * image was built is different to today. Some workarounds have been used
15
- * to avoid a bug with OverlayFS driver on MacOS.
12
+ * Builds the database image and runs a container. It only rebuilds the image
13
+ * when dump has been modified. Some workarounds have been used to avoid a bug
14
+ * with OverlayFS driver on MacOS.
16
15
  */
17
16
  class Run extends Command {
18
17
  static usage = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@verdnatura/myt",
3
- "version": "1.5.24",
3
+ "version": "1.5.26",
4
4
  "author": "Verdnatura Levante SL",
5
5
  "description": "MySQL version control",
6
6
  "license": "GPL-3.0",