@verdnatura/myt 1.5.8 → 1.5.11

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.
@@ -1,6 +1,7 @@
1
1
  versionSchema: myt
2
2
  versionDigits: 5
3
- maxOldVersions: 30
3
+ maxOldVersions: 20
4
+ sumViews: true
4
5
  schemas:
5
6
  - myt
6
7
  fixtures:
@@ -1,21 +1,18 @@
1
- DROP EVENT IF EXISTS <%- schema %>.<%- name %>;
2
- DELIMITER $$
3
- CREATE DEFINER=<%- definer %> EVENT <%- schema %>.<%- name %><%
4
- if (locals.type == 'RECURRING') { %>
5
- ON SCHEDULE EVERY <%- intervalValue %> <%- intervalField %><%
6
- if (locals.starts) { %>
7
- STARTS <%- starts %><%
1
+ <%- begin %> <%- typeAndName %><%
2
+ if (params.type == 'RECURRING') { %>
3
+ ON SCHEDULE EVERY <%- params.intervalValue %> <%- params.intervalField %><%
4
+ if (params.starts) { %>
5
+ STARTS <%- params.starts %><%
8
6
  }
9
- if (locals.ends) { %>
10
- ENDS <%- ends %><%
7
+ if (params.ends) { %>
8
+ ENDS <%- params.ends %><%
11
9
  }
12
10
  } else { %>
13
- ON SCHEDULE AT <%- executeAt %><%
11
+ ON SCHEDULE AT <%- params.executeAt %><%
14
12
  } %>
15
- ON COMPLETION <%- onCompletion %>
16
- <%- status %><%
17
- if (locals.comment) { %>
18
- COMMENT <%- comment %><%
13
+ ON COMPLETION <%- params.onCompletion %>
14
+ <%- params.status %><%
15
+ if (params.comment) { %>
16
+ COMMENT <%- params.comment %><%
19
17
  } %>
20
- DO <%- body %>$$
21
- DELIMITER ;
18
+ DO <%- params.body %><%- end %>
@@ -1,20 +1,17 @@
1
- DROP FUNCTION IF EXISTS <%- schema %>.<%- name %>;
2
- DELIMITER $$
3
- CREATE DEFINER=<%- definer %> FUNCTION <%- schema %>.<%- name %>(<%- locals.paramList %>)
4
- RETURNS <%- returns %><%
5
- if (locals.isDeterministic == 'NO') { %>
1
+ <%- begin %> <%- typeAndName %>(<%- params.paramList %>)
2
+ RETURNS <%- params.returns %><%
3
+ if (params.isDeterministic == 'NO') { %>
6
4
  NOT DETERMINISTIC<%
7
5
  } else { %>
8
6
  DETERMINISTIC<%
9
7
  }
10
- if (locals.dataAccess) { %>
11
- <%- dataAccess %><%
8
+ if (params.dataAccess) { %>
9
+ <%- params.dataAccess %><%
12
10
  }
13
- if (locals.securityType == 'INVOKER') { %>
14
- SQL SECURITY <%- securityType %><%
11
+ if (params.securityType == 'INVOKER') { %>
12
+ SQL SECURITY <%- params.securityType %><%
15
13
  }
16
- if (locals.comment) { %>
17
- COMMENT <%- comment %><%
14
+ if (params.comment) { %>
15
+ COMMENT <%- params.comment %><%
18
16
  } %>
19
- <%- body %>$$
20
- DELIMITER ;
17
+ <%- params.body %><%- end %>
@@ -1,14 +1,11 @@
1
- DROP PROCEDURE IF EXISTS <%- schema %>.<%- name %>;
2
- DELIMITER $$
3
- CREATE DEFINER=<%- definer %> PROCEDURE <%- schema %>.<%- name %>(<%- locals.paramList %>)<%
4
- if (locals.dataAccess) { %>
5
- <%- dataAccess %><%
1
+ <%- begin %> <%- typeAndName %>(<%- params.paramList %>)<%
2
+ if (params.dataAccess) { %>
3
+ <%- params.dataAccess %><%
6
4
  }
7
- if (locals.securityType == 'INVOKER') { %>
8
- SQL SECURITY <%- securityType %><%
5
+ if (params.securityType == 'INVOKER') { %>
6
+ SQL SECURITY <%- params.securityType %><%
9
7
  }
10
- if (locals.comment) { %>
11
- COMMENT <%- comment %><%
8
+ if (params.comment) { %>
9
+ COMMENT <%- params.comment %><%
12
10
  } %>
13
- <%- body %>$$
14
- DELIMITER ;
11
+ <%- params.body %><%- end %>
@@ -1,7 +1,4 @@
1
- DROP TRIGGER IF EXISTS <%- schema %>.<%- name %>;
2
- DELIMITER $$
3
- CREATE DEFINER=<%- definer %> TRIGGER <%- schema %>.<%- name %>
4
- <%- actionTiming %> <%- actionType %> ON `<%- table %>`
1
+ <%- begin %> <%- typeAndName %>
2
+ <%- params.actionTiming %> <%- params.actionType %> ON `<%- params.table %>`
5
3
  FOR EACH ROW
6
- <%- body %>$$
7
- DELIMITER ;
4
+ <%- params.body %><%- end %>
@@ -1,7 +1,7 @@
1
- CREATE OR REPLACE DEFINER=<%- definer %>
2
- SQL SECURITY <%- securityType %>
3
- VIEW <%- schema %>.<%- name %>
4
- AS <%- definition %><%
5
- if (locals.checkOption != 'NONE') { %>
6
- WITH <%- checkOption %> CHECK OPTION<%
1
+ <%- createOrReplace %>
2
+ SQL SECURITY <%- params.securityType %>
3
+ <%- typeAndName %>
4
+ AS <%- params.definition %><%
5
+ if (params.checkOption != 'NONE') { %>
6
+ WITH <%- params.checkOption %> CHECK OPTION<%
7
7
  } %>
@@ -3,9 +3,10 @@ const fs = require('fs-extra');
3
3
  const Exporter = require('./exporter');
4
4
 
5
5
  module.exports = class ExporterEngine {
6
- constructor(conn, mytDir) {
6
+ constructor(conn, opts) {
7
7
  this.conn = conn;
8
- this.pullFile = `${mytDir}/.pullinfo.json`;
8
+ this.opts = opts;
9
+ this.pullFile = `${opts.mytDir}/.pullinfo.json`;
9
10
  this.exporters = [];
10
11
  this.exporterMap = {};
11
12
  }
@@ -35,7 +36,7 @@ module.exports = class ExporterEngine {
35
36
  ];
36
37
 
37
38
  for (const type of types) {
38
- const exporter = new Exporter(type);
39
+ const exporter = new Exporter(type, this.opts.replace);
39
40
  await exporter.init();
40
41
 
41
42
  this.exporters.push(exporter);
package/lib/exporter.js CHANGED
@@ -3,8 +3,11 @@ const fs = require('fs-extra');
3
3
  const SqlString = require('sqlstring');
4
4
 
5
5
  module.exports = class Exporter {
6
- constructor(objectType) {
7
- this.objectType = objectType;
6
+ constructor(objectType, replace) {
7
+ Object.assign(this, {
8
+ objectType,
9
+ replace
10
+ });
8
11
  }
9
12
 
10
13
  async init() {
@@ -38,7 +41,8 @@ module.exports = class Exporter {
38
41
 
39
42
  format(params) {
40
43
  const {attrs} = this;
41
- params = Object.assign({}, attrs.defaults, params);
44
+ params = Object.assign({}, attrs.defaults, params)
45
+ const data = {params};
42
46
 
43
47
  if (attrs.formatter)
44
48
  attrs.formatter(params);
@@ -50,12 +54,33 @@ module.exports = class Exporter {
50
54
  }
51
55
 
52
56
  const split = params.definer.split('@');
53
- params.schema = SqlString.escapeId(params.schema, true);
54
- params.name = SqlString.escapeId(params.name, true);
55
- params.definer =
57
+ data.definer =
56
58
  SqlString.escapeId(split[0], true) + '@' +
57
59
  SqlString.escapeId(split[1], true);
58
60
 
59
- return this.template(params);
61
+ data.name =
62
+ SqlString.escapeId(params.schema, true) + '.' +
63
+ SqlString.escapeId(params.name, true);
64
+
65
+ const objectType = this.objectType.toUpperCase();
66
+ const delimiter = '$$';
67
+ const replace = `CREATE OR REPLACE`;
68
+
69
+ let create;
70
+ if (this.replace) {
71
+ create = `DELIMITER ${delimiter}\n${replace}`
72
+ } else {
73
+ create =
74
+ `DROP ${objectType} IF EXISTS ${data.name};\n`
75
+ +`DELIMITER ${delimiter}\n`
76
+ +`CREATE`;
77
+ }
78
+
79
+ data.createOrReplace = `${replace} DEFINER=${data.definer}`
80
+ data.begin = `${create} DEFINER=${data.definer}`;
81
+ data.typeAndName = `${objectType} ${data.name}`;
82
+ data.end = `${delimiter}\nDELIMITER ;`;
83
+
84
+ return this.template(data);
60
85
  }
61
86
  }
package/myt-clean.js CHANGED
@@ -1,6 +1,7 @@
1
1
  const Myt = require('./myt');
2
2
  const Command = require('./lib/command');
3
3
  const fs = require('fs-extra');
4
+ const path = require('path');
4
5
 
5
6
  /**
6
7
  * Cleans old applied versions.
@@ -35,9 +36,15 @@ class Clean extends Command {
35
36
  && oldVersions.length > opts.maxOldVersions) {
36
37
  oldVersions.splice(-opts.maxOldVersions);
37
38
 
39
+ const archiveDir = path.join(opts.versionsDir, '.archive');
40
+ if (!await fs.pathExists(archiveDir))
41
+ await fs.mkdir(archiveDir);
42
+
38
43
  for (const oldVersion of oldVersions)
39
- await fs.remove(`${opts.versionsDir}/${oldVersion}`,
40
- {recursive: true});
44
+ await fs.move(
45
+ path.join(opts.versionsDir, oldVersion),
46
+ path.join(archiveDir, oldVersion)
47
+ );
41
48
 
42
49
  console.log(`Old versions deleted: ${oldVersions.length}`);
43
50
  } else
package/myt-create.js CHANGED
@@ -53,7 +53,7 @@ class Create extends Command {
53
53
  break;
54
54
  }
55
55
 
56
- const exporter = new Exporter(opts.type);
56
+ const exporter = new Exporter(opts.type, opts.replace);
57
57
  await exporter.init();
58
58
  const sql = exporter.format(params);
59
59
 
package/myt-dump.js CHANGED
@@ -52,16 +52,6 @@ class Dump extends Command {
52
52
  }
53
53
 
54
54
  await dumper.end();
55
-
56
- console.log('Saving version.');
57
- await myt.dbConnect();
58
- const version = await myt.fetchDbVersion();
59
- if (version) {
60
- await fs.writeFile(
61
- `${opts.dumpDir}/.dump.json`,
62
- JSON.stringify(version)
63
- );
64
- }
65
55
  }
66
56
  }
67
57
 
package/myt-init.js CHANGED
@@ -1,6 +1,7 @@
1
1
  const Myt = require('./myt');
2
2
  const Command = require('./lib/command');
3
3
  const fs = require('fs-extra');
4
+ const path = require('path');
4
5
 
5
6
  class Init extends Command {
6
7
  static usage = {
@@ -8,12 +9,23 @@ class Init extends Command {
8
9
  };
9
10
 
10
11
  async run(myt, opts) {
11
- const templateDir = `${__dirname}/template`;
12
+ const packageFile = path.join(opts.mytDir, 'package.json');
13
+ const packageExists = await fs.pathExists(packageFile);
14
+
15
+ const templateDir = path.join(__dirname, 'template');
12
16
  const templates = await fs.readdir(templateDir);
13
17
  for (let template of templates) {
14
- const dst = `${opts.mytDir}/${template}`;
18
+ const dst = path.join(opts.mytDir, template);
15
19
  if (!await fs.pathExists(dst))
16
- await fs.copy(`${templateDir}/${template}`, dst);
20
+ await fs.copy(path.join(templateDir, template), dst);
21
+ }
22
+
23
+ if (!packageExists) {
24
+ const packageJson = require(packageFile);
25
+ packageJson.dependencies[myt.packageJson.name] =
26
+ `^${myt.packageJson.version}`;
27
+ await fs.writeFile(packageFile,
28
+ JSON.stringify(packageJson, null, ' '));
17
29
  }
18
30
  }
19
31
  }
package/myt-pull.js CHANGED
@@ -85,7 +85,7 @@ class Pull extends Command {
85
85
 
86
86
  console.log(`Incorporating routine changes.`);
87
87
 
88
- const engine = new ExporterEngine(conn, opts.mytDir);
88
+ const engine = new ExporterEngine(conn, opts);
89
89
  await engine.init();
90
90
  const shaSums = engine.shaSums;
91
91
 
@@ -125,9 +125,11 @@ class Pull extends Command {
125
125
  const {opts} = this;
126
126
 
127
127
  const res = await exporter.query(conn, schema);
128
- if (!res.length) return;
128
+ if (!res.length) return;
129
129
 
130
- const routineDir = `${opts.routinesDir}/${schema}/${exporter.objectType}s`;
130
+ const type = exporter.objectType;
131
+
132
+ const routineDir = `${opts.routinesDir}/${schema}/${type}s`;
131
133
  if (!await fs.pathExists(routineDir))
132
134
  await fs.mkdir(routineDir);
133
135
 
@@ -140,8 +142,10 @@ class Pull extends Command {
140
142
  const match = routineFile.match(/^(.*)\.sql$/);
141
143
  if (!match) continue;
142
144
  const routine = match[1];
143
- if (!routineSet.has(routine))
145
+ if (!routineSet.has(routine)) {
144
146
  await fs.remove(`${routineDir}/${routine}.sql`);
147
+ engine.deleteShaSum(type, schema, routine)
148
+ }
145
149
  }
146
150
 
147
151
  for (const params of res) {
@@ -150,12 +154,11 @@ class Pull extends Command {
150
154
  const routineFile = `${routineDir}/${routineName}.sql`;
151
155
  let update = opts.update;
152
156
 
153
- const oldSum = engine.getShaSum(routineName);
154
- if (oldSum || opts.sums) {
157
+ const oldSum = engine.getShaSum(type, schema, routineName);
158
+ if (oldSum || opts.sums || (opts.sumViews && type === 'view')) {
155
159
  const shaSum = engine.shaSum(sql);
156
160
  if (oldSum !== shaSum) {
157
- engine.setShaSum(
158
- exporter.objectType, schema, routineName, shaSum);
161
+ engine.setShaSum(type, schema, routineName, shaSum);
159
162
  update = true;
160
163
  }
161
164
  } else if (params.modified && engine.lastPull) {
package/myt-push.js CHANGED
@@ -288,7 +288,7 @@ class Push extends Command {
288
288
  );
289
289
  }
290
290
 
291
- const engine = new ExporterEngine(conn, opts.mytDir);
291
+ const engine = new ExporterEngine(conn, opts);
292
292
  await engine.init();
293
293
 
294
294
  async function finalize() {
@@ -349,7 +349,7 @@ class Push extends Command {
349
349
  );
350
350
  }
351
351
 
352
- if (opts.sums || oldSum)
352
+ if (opts.sums || oldSum || (opts.sumViews && type === 'view'))
353
353
  await engine.fetchShaSum(type, schema, name);
354
354
  } else {
355
355
  const escapedName =
package/myt-run.js CHANGED
@@ -42,25 +42,30 @@ class Run extends Command {
42
42
 
43
43
  // Build base image
44
44
 
45
- let serverDockerfile = path.join(dumpDir, 'Dockerfile');
46
- if (!await fs.pathExists(serverDockerfile))
47
- serverDockerfile = path.join(serverDir, 'Dockerfile.base');
45
+ let basePath = dumpDir;
46
+ let baseDockerfile = path.join(dumpDir, 'Dockerfile');
48
47
 
49
- await docker.build(__dirname, {
48
+ if (!await fs.pathExists(baseDockerfile)) {
49
+ basePath = serverDir;
50
+ baseDockerfile = path.join(serverDir, 'Dockerfile.base');
51
+ }
52
+
53
+ await docker.build(basePath, {
50
54
  tag: 'myt/base',
51
- file: serverDockerfile
55
+ file: baseDockerfile
52
56
  }, opts.debug);
53
57
 
54
58
  // Build server image
55
59
 
56
- await docker.build(__dirname, {
60
+ await docker.build(serverDir, {
57
61
  tag: 'myt/server',
58
62
  file: path.join(serverDir, 'Dockerfile.server')
59
63
  }, opts.debug);
60
64
 
61
65
  // Build dump image
62
66
 
63
- await docker.build(opts.mytDir, {
67
+ const dumpContext = path.join(opts.mytDir, 'dump');
68
+ await docker.build(dumpContext, {
64
69
  tag: opts.code,
65
70
  file: path.join(serverDir, 'Dockerfile.dump')
66
71
  }, opts.debug);
package/myt.js CHANGED
@@ -45,6 +45,7 @@ class Myt {
45
45
  'Myt'.green,
46
46
  `v${packageJson.version}`.magenta
47
47
  );
48
+ this.packageJson = packageJson;
48
49
 
49
50
  let baseOpts = this.constructor.opts;
50
51
  baseOpts.default = Object.assign(baseOpts.default || {}, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@verdnatura/myt",
3
- "version": "1.5.8",
3
+ "version": "1.5.11",
4
4
  "author": "Verdnatura Levante SL",
5
5
  "description": "MySQL version control",
6
6
  "license": "GPL-3.0",
@@ -14,7 +14,7 @@
14
14
  "dependencies": {
15
15
  "@sqltools/formatter": "^1.2.5",
16
16
  "colors": "^1.4.0",
17
- "ejs": "^3.1.6",
17
+ "ejs": "^3.1.8",
18
18
  "fs-extra": "^8.1.0",
19
19
  "getopts": "^2.3.0",
20
20
  "ini": "^1.3.8",
@@ -3,9 +3,9 @@ FROM myt/server
3
3
  USER root
4
4
 
5
5
  COPY \
6
- dump/.dump.sql \
7
- dump/beforeDump.sql \
8
- dump/afterDump.sql \
6
+ .dump.sql \
7
+ beforeDump.sql \
8
+ afterDump.sql \
9
9
  dump/
10
10
 
11
11
  RUN gosu mysql docker-init.sh
@@ -8,11 +8,11 @@ RUN mkdir /mysql-data \
8
8
 
9
9
  WORKDIR /workspace
10
10
 
11
- COPY server/docker.cnf /etc/mysql/conf.d/
11
+ COPY docker.cnf /etc/mysql/conf.d/
12
12
  COPY \
13
- server/docker-init.sh \
14
- server/docker-import.sh \
15
- server/docker-start.sh \
13
+ docker-init.sh \
14
+ docker-import.sh \
15
+ docker-start.sh \
16
16
  /usr/local/bin/
17
17
 
18
18
  USER mysql
@@ -0,0 +1 @@
1
+ FROM mariadb:10.7
@@ -0,0 +1 @@
1
+ -- Executed after data dump
@@ -0,0 +1 @@
1
+ -- Executed before data dump
@@ -8,6 +8,6 @@
8
8
  "type": "git"
9
9
  },
10
10
  "dependencies": {
11
- "@verdnatura/myt": "^1.5.8"
11
+ "@verdnatura/myt": "^1.0.0"
12
12
  }
13
13
  }
@@ -1 +0,0 @@
1
- -- Place your local structure changes here