@verdnatura/myt 1.5.6 → 1.5.8

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/myt-dump.js CHANGED
@@ -1,7 +1,7 @@
1
-
2
1
  const Myt = require('./myt');
3
2
  const Command = require('./lib/command');
4
3
  const fs = require('fs-extra');
4
+ const Dumper = require('./lib/dumper');
5
5
 
6
6
  class Dump extends Command {
7
7
  static usage = {
@@ -9,14 +9,15 @@ class Dump extends Command {
9
9
  operand: 'remote'
10
10
  };
11
11
 
12
- static localOpts = {
12
+ static opts = {
13
13
  default: {
14
14
  remote: 'production'
15
15
  }
16
16
  };
17
17
 
18
18
  async run(myt, opts) {
19
- const dumpStream = await myt.initDump('.dump.sql');
19
+ const dumper = new Dumper(opts);
20
+ await dumper.init('.dump.sql');
20
21
 
21
22
  console.log('Dumping structure.');
22
23
  let dumpArgs = [
@@ -29,10 +30,10 @@ class Dump extends Command {
29
30
  '--databases'
30
31
  ];
31
32
  dumpArgs = dumpArgs.concat(opts.schemas);
32
- await myt.runDump('docker-dump.sh', dumpArgs, dumpStream);
33
+ await dumper.runDump('docker-dump.sh', dumpArgs);
33
34
 
34
35
  console.log('Dumping fixtures.');
35
- await myt.dumpFixtures(dumpStream, opts.fixtures);
36
+ await dumper.dumpFixtures(opts.fixtures);
36
37
 
37
38
  console.log('Dumping privileges.');
38
39
  const privs = opts.privileges;
@@ -45,12 +46,12 @@ class Dump extends Command {
45
46
  ];
46
47
  if (privs.where) args.push('--where', privs.where);
47
48
  args = args.concat(['mysql'], privs.tables);
48
-
49
- await dumpStream.write('USE `mysql`;\n', 'utf8');
50
- await myt.runDump('mysqldump', args, dumpStream);
49
+
50
+ await dumper.use('mysql');
51
+ await dumper.runDump('mysqldump', args);
51
52
  }
52
53
 
53
- await dumpStream.end();
54
+ await dumper.end();
54
55
 
55
56
  console.log('Saving version.');
56
57
  await myt.dbConnect();
package/myt-fixtures.js CHANGED
@@ -1,6 +1,6 @@
1
-
2
1
  const Myt = require('./myt');
3
2
  const Command = require('./lib/command');
3
+ const Dumper = require('./lib/dumper');
4
4
 
5
5
  class Fixtures extends Command {
6
6
  static usage = {
@@ -8,16 +8,17 @@ class Fixtures extends Command {
8
8
  operand: 'remote'
9
9
  };
10
10
 
11
- static localOpts = {
11
+ static opts = {
12
12
  default: {
13
13
  remote: 'docker'
14
14
  }
15
15
  };
16
16
 
17
17
  async run(myt, opts) {
18
- const dumpStream = await myt.initDump('fixtures.sql');
19
- await myt.dumpFixtures(dumpStream, opts.localFixtures, true);
20
- await dumpStream.end();
18
+ const dumper = new Dumper(opts);
19
+ await dumper.init('fixtures.sql');
20
+ await dumper.dumpFixtures(opts.localFixtures, true);
21
+ await dumper.end();
21
22
  }
22
23
  }
23
24
 
package/myt-init.js CHANGED
@@ -1,4 +1,3 @@
1
-
2
1
  const Myt = require('./myt');
3
2
  const Command = require('./lib/command');
4
3
  const fs = require('fs-extra');
package/myt-pull.js CHANGED
@@ -1,9 +1,9 @@
1
-
2
1
  const Myt = require('./myt');
3
2
  const Command = require('./lib/command');
4
3
  const fs = require('fs-extra');
5
4
  const nodegit = require('nodegit');
6
5
  const ExporterEngine = require('./lib/exporter-engine');
6
+ const repoExt = require('./lib/repo');
7
7
 
8
8
  class Pull extends Command {
9
9
  static usage = {
@@ -17,7 +17,7 @@ class Pull extends Command {
17
17
  operand: 'remote'
18
18
  };
19
19
 
20
- static localOpts = {
20
+ static opts = {
21
21
  alias: {
22
22
  force: 'f',
23
23
  checkout: 'c',
@@ -52,14 +52,14 @@ class Pull extends Command {
52
52
 
53
53
  // Check for unstaged changes
54
54
 
55
- const unstagedDiff = await myt.getUnstaged(repo);
55
+ const unstagedDiff = await repoExt.getUnstaged(repo);
56
56
 
57
57
  if (await hasChanges(unstagedDiff))
58
58
  throw new Error('You have unstaged changes, save them before pull');
59
59
 
60
60
  // Check for staged changes
61
61
 
62
- const stagedDiff = await myt.getStaged(repo);
62
+ const stagedDiff = await repoExt.getStaged(repo);
63
63
 
64
64
  if (await hasChanges(stagedDiff))
65
65
  throw new Error('You have staged changes, save them before pull');
@@ -114,13 +114,64 @@ class Pull extends Command {
114
114
  await fs.mkdir(schemaDir);
115
115
 
116
116
  for (const exporter of engine.exporters)
117
- await exporter.export(routinesDir,
118
- schema, opts.update, opts.sums);
117
+ await this.export(conn, engine, exporter, schema);
119
118
  }
120
119
 
121
120
  await engine.refreshPullDate();
122
121
  await engine.saveInfo();
123
122
  }
123
+
124
+ async export(conn, engine, exporter, schema) {
125
+ const {opts} = this;
126
+
127
+ const res = await exporter.query(conn, schema);
128
+ if (!res.length) return;
129
+
130
+ const routineDir = `${opts.routinesDir}/${schema}/${exporter.objectType}s`;
131
+ if (!await fs.pathExists(routineDir))
132
+ await fs.mkdir(routineDir);
133
+
134
+ const routineSet = new Set();
135
+ for (const params of res)
136
+ routineSet.add(params.name);
137
+
138
+ const routines = await fs.readdir(routineDir);
139
+ for (const routineFile of routines) {
140
+ const match = routineFile.match(/^(.*)\.sql$/);
141
+ if (!match) continue;
142
+ const routine = match[1];
143
+ if (!routineSet.has(routine))
144
+ await fs.remove(`${routineDir}/${routine}.sql`);
145
+ }
146
+
147
+ for (const params of res) {
148
+ const routineName = params.name;
149
+ const sql = exporter.format(params);
150
+ const routineFile = `${routineDir}/${routineName}.sql`;
151
+ let update = opts.update;
152
+
153
+ const oldSum = engine.getShaSum(routineName);
154
+ if (oldSum || opts.sums) {
155
+ const shaSum = engine.shaSum(sql);
156
+ if (oldSum !== shaSum) {
157
+ engine.setShaSum(
158
+ exporter.objectType, schema, routineName, shaSum);
159
+ update = true;
160
+ }
161
+ } else if (params.modified && engine.lastPull) {
162
+ if (params.modified > engine.lastPull)
163
+ update = true;
164
+ } else if (await fs.pathExists(routineFile)) {
165
+ const currentSql = await fs.readFile(routineFile, 'utf8');
166
+ if (sql != currentSql)
167
+ update = true;
168
+ } else
169
+ update = true;
170
+
171
+ if (update)
172
+ await fs.writeFile(routineFile, sql);
173
+ }
174
+ }
124
175
  }
125
176
 
126
177
  module.exports = Pull;
package/myt-push.js CHANGED
@@ -1,9 +1,11 @@
1
-
2
1
  const Myt = require('./myt');
3
2
  const Command = require('./lib/command');
4
3
  const fs = require('fs-extra');
5
4
  const nodegit = require('nodegit');
6
5
  const ExporterEngine = require('./lib/exporter-engine');
6
+ const connExt = require('./lib/conn');
7
+ const repoExt = require('./lib/repo');
8
+ const SqlString = require('sqlstring');
7
9
 
8
10
  /**
9
11
  * Pushes changes to remote.
@@ -20,7 +22,7 @@ class Push extends Command {
20
22
  operand: 'remote'
21
23
  };
22
24
 
23
- static localOpts = {
25
+ static opts = {
24
26
  alias: {
25
27
  force: 'f',
26
28
  commit: 'c',
@@ -217,7 +219,7 @@ class Push extends Command {
217
219
 
218
220
  let err;
219
221
  try {
220
- await myt.queryFromFile(pushConn,
222
+ await connExt.queryFromFile(pushConn,
221
223
  `${scriptsDir}/${script}`);
222
224
  } catch (e) {
223
225
  err = e;
@@ -261,7 +263,7 @@ class Push extends Command {
261
263
  const gitExists = await fs.pathExists(`${opts.workspace}/.git`);
262
264
 
263
265
  let nRoutines = 0;
264
- let changes = await myt.changedRoutines(version.gitCommit);
266
+ let changes = await this.changedRoutines(version.gitCommit);
265
267
  changes = this.parseChanges(changes);
266
268
 
267
269
  const routines = [];
@@ -328,13 +330,13 @@ class Push extends Command {
328
330
  console.log('', actionMsg.bold, typeMsg.bold, change.fullName);
329
331
 
330
332
  if (!isEqual) {
331
- const scapedSchema = pushConn.escapeId(schema, true);
333
+ const scapedSchema = SqlString.escapeId(schema, true);
332
334
 
333
335
  if (exists) {
334
336
  if (change.type.name === 'VIEW')
335
337
  await pushConn.query(`USE ${scapedSchema}`);
336
338
 
337
- await myt.multiQuery(pushConn, newSql);
339
+ await connExt.multiQuery(pushConn, newSql);
338
340
 
339
341
  if (change.isRoutine) {
340
342
  await conn.query(
@@ -352,7 +354,7 @@ class Push extends Command {
352
354
  } else {
353
355
  const escapedName =
354
356
  scapedSchema + '.' +
355
- pushConn.escapeId(name, true);
357
+ SqlString.escapeId(name, true);
356
358
 
357
359
  const query = `DROP ${change.type.name} IF EXISTS ${escapedName}`;
358
360
  await pushConn.query(query);
@@ -400,7 +402,7 @@ class Push extends Command {
400
402
  }
401
403
 
402
404
  async updateVersion(column, value) {
403
- column = this.conn.escapeId(column, true);
405
+ column = SqlString.escapeId(column, true);
404
406
  await this.conn.query(
405
407
  `INSERT INTO version
406
408
  SET code = ?,
@@ -416,6 +418,68 @@ class Push extends Command {
416
418
  );
417
419
  }
418
420
 
421
+ async changedRoutines(commitSha) {
422
+ const repo = await this.myt.openRepo();
423
+ const changes = [];
424
+ const changesMap = new Map();
425
+
426
+ async function pushChanges(diff) {
427
+ if (!diff) return;
428
+ const patches = await diff.patches();
429
+
430
+ for (const patch of patches) {
431
+ const path = patch.newFile().path();
432
+ const match = path.match(/^routines\/(.+)\.sql$/);
433
+ if (!match) continue;
434
+
435
+ let change = changesMap.get(match[1]);
436
+ if (!change) {
437
+ change = {path: match[1]};
438
+ changes.push(change);
439
+ changesMap.set(match[1], change);
440
+ }
441
+ change.mark = patch.isDeleted() ? '-' : '+';
442
+ }
443
+ }
444
+
445
+ const head = await repo.getHeadCommit();
446
+
447
+ if (head && commitSha) {
448
+ let commit;
449
+ let notFound;
450
+
451
+ try {
452
+ commit = await repo.getCommit(commitSha);
453
+ notFound = false;
454
+ } catch (err) {
455
+ if (err.errorFunction == 'Commit.lookup')
456
+ notFound = true;
457
+ else
458
+ throw err;
459
+ }
460
+
461
+ if (notFound) {
462
+ console.warn(`Database commit not found, trying git fetch`.yellow);
463
+ await repo.fetchAll();
464
+ commit = await repo.getCommit(commitSha);
465
+ }
466
+
467
+ const commitTree = await commit.getTree();
468
+
469
+ const headTree = await head.getTree();
470
+ const diff = await headTree.diff(commitTree);
471
+ await pushChanges(diff);
472
+ }
473
+
474
+ await pushChanges(await repoExt.getUnstaged(repo));
475
+ await pushChanges(await repoExt.getStaged(repo));
476
+
477
+ return changes.sort((a, b) => {
478
+ if (b.mark != a.mark)
479
+ return b.mark == '-' ? 1 : -1;
480
+ return a.path.localeCompare(b.path);
481
+ });
482
+ }
419
483
  }
420
484
 
421
485
  const typeMap = {
package/myt-run.js CHANGED
@@ -1,4 +1,3 @@
1
-
2
1
  const Myt = require('./myt');
3
2
  const Command = require('./lib/command');
4
3
  const Push = require('./myt-push');
@@ -6,6 +5,7 @@ const docker = require('./lib/docker');
6
5
  const fs = require('fs-extra');
7
6
  const path = require('path');
8
7
  const Server = require('./lib/server');
8
+ const connExt = require('./lib/conn');
9
9
 
10
10
  /**
11
11
  * Builds the database image and runs a container. It only rebuilds the
@@ -22,7 +22,7 @@ class Run extends Command {
22
22
  }
23
23
  };
24
24
 
25
- static localOpts = {
25
+ static opts = {
26
26
  alias: {
27
27
  ci: 'c',
28
28
  random: 'r'
@@ -144,7 +144,7 @@ class Run extends Command {
144
144
 
145
145
  const triggersDir = await fs.readdir(triggersPath);
146
146
  for (const triggerFile of triggersDir)
147
- await myt.queryFromFile(conn, `${triggersPath}/${triggerFile}`);
147
+ await connExt.queryFromFile(conn, `${triggersPath}/${triggerFile}`);
148
148
  }
149
149
 
150
150
  return server;
package/myt-start.js CHANGED
@@ -1,4 +1,3 @@
1
-
2
1
  const Myt = require('./myt');
3
2
  const Command = require('./lib/command');
4
3
  const Container = require('./lib/docker').Container;
package/myt-version.js CHANGED
@@ -1,4 +1,3 @@
1
-
2
1
  const Myt = require('./myt');
3
2
  const Command = require('./lib/command');
4
3
  const fs = require('fs-extra');
@@ -15,7 +14,7 @@ class Version extends Command {
15
14
  operand: 'name'
16
15
  };
17
16
 
18
- static localOpts = {
17
+ static opts = {
19
18
  alias: {
20
19
  name: 'n'
21
20
  },