@verdnatura/myt 1.5.19 → 1.5.20

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.
Files changed (64) hide show
  1. package/.dockerignore +2 -2
  2. package/LICENSE +17 -17
  3. package/README.md +278 -278
  4. package/assets/db.ini +5 -5
  5. package/assets/myt.default.yml +11 -11
  6. package/assets/structure.sql +24 -24
  7. package/cli.js +4 -4
  8. package/exporters/event.ejs +18 -18
  9. package/exporters/event.js +31 -31
  10. package/exporters/event.sql +19 -19
  11. package/exporters/function.ejs +17 -17
  12. package/exporters/function.js +27 -27
  13. package/exporters/function.sql +16 -16
  14. package/exporters/procedure.ejs +11 -11
  15. package/exporters/procedure.js +23 -23
  16. package/exporters/procedure.sql +14 -14
  17. package/exporters/trigger.ejs +4 -4
  18. package/exporters/trigger.js +10 -10
  19. package/exporters/trigger.sql +13 -13
  20. package/exporters/view.ejs +7 -7
  21. package/exporters/view.js +29 -29
  22. package/exporters/view.sql +12 -12
  23. package/lib/command.js +13 -13
  24. package/lib/conn.js +141 -141
  25. package/lib/docker.js +133 -133
  26. package/lib/dumper.js +83 -83
  27. package/lib/exporter-engine.js +112 -112
  28. package/lib/exporter.js +86 -86
  29. package/lib/repo.js +29 -29
  30. package/lib/server.js +71 -71
  31. package/lib/util.js +7 -7
  32. package/myt-clean.js +58 -58
  33. package/myt-create.js +74 -74
  34. package/myt-dump.js +75 -75
  35. package/myt-fixtures.js +29 -29
  36. package/myt-init.js +36 -36
  37. package/myt-pull.js +183 -183
  38. package/myt-push.js +568 -568
  39. package/myt-run.js +177 -177
  40. package/myt-start.js +54 -54
  41. package/myt-version.js +218 -218
  42. package/myt.js +361 -361
  43. package/package.json +41 -41
  44. package/server/Dockerfile.base +1 -1
  45. package/server/Dockerfile.client +14 -14
  46. package/server/Dockerfile.dump +16 -16
  47. package/server/Dockerfile.server +25 -25
  48. package/server/docker-dump.sh +4 -4
  49. package/server/docker-fixtures.sh +4 -4
  50. package/server/docker-import.sh +6 -6
  51. package/server/docker-init.sh +20 -20
  52. package/server/docker-start.sh +10 -10
  53. package/server/docker.cnf +9 -9
  54. package/template/dump/Dockerfile.base +1 -1
  55. package/template/dump/afterDump.sql +1 -1
  56. package/template/dump/beforeDump.sql +1 -1
  57. package/template/dump/fixtures.sql +1 -1
  58. package/template/myt.config.yml +11 -11
  59. package/template/package.json +13 -13
  60. package/template/remotes/docker.ini +4 -4
  61. package/template/remotes/local.ini +5 -5
  62. package/template/routines/README.md +1 -1
  63. package/template/versions/00001-firstVersion/00-firstScript.sql +1 -1
  64. package/template/versions/README.md +1 -1
package/myt-init.js CHANGED
@@ -1,36 +1,36 @@
1
- const Myt = require('./myt');
2
- const Command = require('./lib/command');
3
- const fs = require('fs-extra');
4
- const path = require('path');
5
-
6
- class Init extends Command {
7
- static usage = {
8
- description: 'Initialize an empty workspace'
9
- };
10
-
11
- async run(myt, opts) {
12
- const packageFile = path.join(opts.mytDir, 'package.json');
13
- const packageExists = await fs.pathExists(packageFile);
14
-
15
- const templateDir = path.join(__dirname, 'template');
16
- const templates = await fs.readdir(templateDir);
17
- for (let template of templates) {
18
- const dst = path.join(opts.mytDir, template);
19
- if (!await fs.pathExists(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, ' '));
29
- }
30
- }
31
- }
32
-
33
- module.exports = Init;
34
-
35
- if (require.main === module)
36
- new Myt().run(Init);
1
+ const Myt = require('./myt');
2
+ const Command = require('./lib/command');
3
+ const fs = require('fs-extra');
4
+ const path = require('path');
5
+
6
+ class Init extends Command {
7
+ static usage = {
8
+ description: 'Initialize an empty workspace'
9
+ };
10
+
11
+ async run(myt, opts) {
12
+ const packageFile = path.join(opts.mytDir, 'package.json');
13
+ const packageExists = await fs.pathExists(packageFile);
14
+
15
+ const templateDir = path.join(__dirname, 'template');
16
+ const templates = await fs.readdir(templateDir);
17
+ for (let template of templates) {
18
+ const dst = path.join(opts.mytDir, template);
19
+ if (!await fs.pathExists(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, ' '));
29
+ }
30
+ }
31
+ }
32
+
33
+ module.exports = Init;
34
+
35
+ if (require.main === module)
36
+ new Myt().run(Init);
package/myt-pull.js CHANGED
@@ -1,183 +1,183 @@
1
- const Myt = require('./myt');
2
- const Command = require('./lib/command');
3
- const fs = require('fs-extra');
4
- const nodegit = require('nodegit');
5
- const ExporterEngine = require('./lib/exporter-engine');
6
- const repoExt = require('./lib/repo');
7
-
8
- class Pull extends Command {
9
- static usage = {
10
- description: 'Incorporate database routine changes into workspace',
11
- params: {
12
- force: 'Do it even if there are local changes',
13
- checkout: 'Move to same database commit before pull',
14
- update: 'Update all routines',
15
- sums: 'Save SHA sums of all objects'
16
- },
17
- operand: 'remote'
18
- };
19
-
20
- static opts = {
21
- alias: {
22
- force: 'f',
23
- checkout: 'c',
24
- update: 'u',
25
- sums: 's'
26
- },
27
- boolean: [
28
- 'force',
29
- 'checkout',
30
- 'update',
31
- 'sums'
32
- ]
33
- };
34
-
35
- async run(myt, opts) {
36
- const conn = await myt.dbConnect();
37
- const repo = await myt.openRepo();
38
-
39
- if (!opts.force) {
40
- async function hasChanges(diff) {
41
- if (diff)
42
- for (const patch of await diff.patches()) {
43
- const match = patch
44
- .newFile()
45
- .path()
46
- .match(/^routines\/(.+)\.sql$/);
47
- if (match) return true;
48
- }
49
-
50
- return false;
51
- }
52
-
53
- // Check for unstaged changes
54
-
55
- const unstagedDiff = await repoExt.getUnstaged(repo);
56
-
57
- if (await hasChanges(unstagedDiff))
58
- throw new Error('You have unstaged changes, save them before pull');
59
-
60
- // Check for staged changes
61
-
62
- const stagedDiff = await repoExt.getStaged(repo);
63
-
64
- if (await hasChanges(stagedDiff))
65
- throw new Error('You have staged changes, save them before pull');
66
- }
67
-
68
- // Checkout to remote commit
69
-
70
- if (opts.checkout) {
71
- const version = await myt.fetchDbVersion();
72
-
73
- if (version && version.gitCommit) {
74
- const now = parseInt(new Date().toJSON());
75
- const branchName = `myt-pull_${now}`;
76
- console.log(`Creating branch '${branchName}' from database commit.`);
77
- const commit = await repo.getCommit(version.gitCommit);
78
- const branch = await nodegit.Branch.create(repo,
79
- `myt-pull_${now}`, commit, () => {});
80
- await repo.checkoutBranch(branch);
81
- }
82
- }
83
-
84
- // Export routines to SQL files
85
-
86
- console.log(`Incorporating routine changes.`);
87
-
88
- const engine = new ExporterEngine(conn, opts);
89
- await engine.init();
90
- const shaSums = engine.shaSums;
91
-
92
- const routinesDir = opts.routinesDir;
93
- if (!await fs.pathExists(routinesDir))
94
- await fs.mkdir(routinesDir);
95
-
96
- // Delete old schemas
97
-
98
- const schemas = await fs.readdir(routinesDir);
99
- for (const schema of schemas) {
100
- if (opts.schemas.indexOf(schema) == -1)
101
- await fs.remove(`${routinesDir}/${schema}`, {recursive: true});
102
- }
103
-
104
- for (const schema in shaSums) {
105
- if (!await fs.pathExists(`${routinesDir}/${schema}`))
106
- engine.deleteSchemaSums(schema);
107
- }
108
-
109
- // Export objects to SQL files
110
-
111
- for (const schema of opts.schemas) {
112
- let schemaDir = `${routinesDir}/${schema}`;
113
- if (!await fs.pathExists(schemaDir))
114
- await fs.mkdir(schemaDir);
115
-
116
- for (const exporter of engine.exporters)
117
- await this.export(conn, engine, exporter, schema);
118
- }
119
-
120
- await engine.refreshPullDate();
121
- await engine.saveInfo();
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 type = exporter.objectType;
131
-
132
- const routineDir = `${opts.routinesDir}/${schema}/${type}s`;
133
- if (!await fs.pathExists(routineDir))
134
- await fs.mkdir(routineDir);
135
-
136
- const routineSet = new Set();
137
- for (const params of res)
138
- routineSet.add(params.name);
139
-
140
- const routines = await fs.readdir(routineDir);
141
- for (const routineFile of routines) {
142
- const match = routineFile.match(/^(.*)\.sql$/);
143
- if (!match) continue;
144
- const routine = match[1];
145
- if (!routineSet.has(routine)) {
146
- await fs.remove(`${routineDir}/${routine}.sql`);
147
- engine.deleteShaSum(type, schema, routine)
148
- }
149
- }
150
-
151
- for (const params of res) {
152
- const routineName = params.name;
153
- const sql = exporter.format(params);
154
- const routineFile = `${routineDir}/${routineName}.sql`;
155
- let update = opts.update;
156
-
157
- const oldSum = engine.getShaSum(type, schema, routineName);
158
- if (oldSum || opts.sums || (opts.sumViews && type === 'view')) {
159
- const shaSum = engine.shaSum(sql);
160
- if (oldSum !== shaSum) {
161
- engine.setShaSum(type, schema, routineName, shaSum);
162
- update = true;
163
- }
164
- } else if (params.modified && engine.lastPull) {
165
- if (params.modified > engine.lastPull)
166
- update = true;
167
- } else if (await fs.pathExists(routineFile)) {
168
- const currentSql = await fs.readFile(routineFile, 'utf8');
169
- if (sql != currentSql)
170
- update = true;
171
- } else
172
- update = true;
173
-
174
- if (update)
175
- await fs.writeFile(routineFile, sql);
176
- }
177
- }
178
- }
179
-
180
- module.exports = Pull;
181
-
182
- if (require.main === module)
183
- new Myt().run(Pull);
1
+ const Myt = require('./myt');
2
+ const Command = require('./lib/command');
3
+ const fs = require('fs-extra');
4
+ const nodegit = require('nodegit');
5
+ const ExporterEngine = require('./lib/exporter-engine');
6
+ const repoExt = require('./lib/repo');
7
+
8
+ class Pull extends Command {
9
+ static usage = {
10
+ description: 'Incorporate database routine changes into workspace',
11
+ params: {
12
+ force: 'Do it even if there are local changes',
13
+ checkout: 'Move to same database commit before pull',
14
+ update: 'Update all routines',
15
+ sums: 'Save SHA sums of all objects'
16
+ },
17
+ operand: 'remote'
18
+ };
19
+
20
+ static opts = {
21
+ alias: {
22
+ force: 'f',
23
+ checkout: 'c',
24
+ update: 'u',
25
+ sums: 's'
26
+ },
27
+ boolean: [
28
+ 'force',
29
+ 'checkout',
30
+ 'update',
31
+ 'sums'
32
+ ]
33
+ };
34
+
35
+ async run(myt, opts) {
36
+ const conn = await myt.dbConnect();
37
+ const repo = await myt.openRepo();
38
+
39
+ if (!opts.force) {
40
+ async function hasChanges(diff) {
41
+ if (diff)
42
+ for (const patch of await diff.patches()) {
43
+ const match = patch
44
+ .newFile()
45
+ .path()
46
+ .match(/^routines\/(.+)\.sql$/);
47
+ if (match) return true;
48
+ }
49
+
50
+ return false;
51
+ }
52
+
53
+ // Check for unstaged changes
54
+
55
+ const unstagedDiff = await repoExt.getUnstaged(repo);
56
+
57
+ if (await hasChanges(unstagedDiff))
58
+ throw new Error('You have unstaged changes, save them before pull');
59
+
60
+ // Check for staged changes
61
+
62
+ const stagedDiff = await repoExt.getStaged(repo);
63
+
64
+ if (await hasChanges(stagedDiff))
65
+ throw new Error('You have staged changes, save them before pull');
66
+ }
67
+
68
+ // Checkout to remote commit
69
+
70
+ if (opts.checkout) {
71
+ const version = await myt.fetchDbVersion();
72
+
73
+ if (version && version.gitCommit) {
74
+ const now = parseInt(new Date().toJSON());
75
+ const branchName = `myt-pull_${now}`;
76
+ console.log(`Creating branch '${branchName}' from database commit.`);
77
+ const commit = await repo.getCommit(version.gitCommit);
78
+ const branch = await nodegit.Branch.create(repo,
79
+ `myt-pull_${now}`, commit, () => {});
80
+ await repo.checkoutBranch(branch);
81
+ }
82
+ }
83
+
84
+ // Export routines to SQL files
85
+
86
+ console.log(`Incorporating routine changes.`);
87
+
88
+ const engine = new ExporterEngine(conn, opts);
89
+ await engine.init();
90
+ const shaSums = engine.shaSums;
91
+
92
+ const routinesDir = opts.routinesDir;
93
+ if (!await fs.pathExists(routinesDir))
94
+ await fs.mkdir(routinesDir);
95
+
96
+ // Delete old schemas
97
+
98
+ const schemas = await fs.readdir(routinesDir);
99
+ for (const schema of schemas) {
100
+ if (opts.schemas.indexOf(schema) == -1)
101
+ await fs.remove(`${routinesDir}/${schema}`, {recursive: true});
102
+ }
103
+
104
+ for (const schema in shaSums) {
105
+ if (!await fs.pathExists(`${routinesDir}/${schema}`))
106
+ engine.deleteSchemaSums(schema);
107
+ }
108
+
109
+ // Export objects to SQL files
110
+
111
+ for (const schema of opts.schemas) {
112
+ let schemaDir = `${routinesDir}/${schema}`;
113
+ if (!await fs.pathExists(schemaDir))
114
+ await fs.mkdir(schemaDir);
115
+
116
+ for (const exporter of engine.exporters)
117
+ await this.export(conn, engine, exporter, schema);
118
+ }
119
+
120
+ await engine.refreshPullDate();
121
+ await engine.saveInfo();
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 type = exporter.objectType;
131
+
132
+ const routineDir = `${opts.routinesDir}/${schema}/${type}s`;
133
+ if (!await fs.pathExists(routineDir))
134
+ await fs.mkdir(routineDir);
135
+
136
+ const routineSet = new Set();
137
+ for (const params of res)
138
+ routineSet.add(params.name);
139
+
140
+ const routines = await fs.readdir(routineDir);
141
+ for (const routineFile of routines) {
142
+ const match = routineFile.match(/^(.*)\.sql$/);
143
+ if (!match) continue;
144
+ const routine = match[1];
145
+ if (!routineSet.has(routine)) {
146
+ await fs.remove(`${routineDir}/${routine}.sql`);
147
+ engine.deleteShaSum(type, schema, routine)
148
+ }
149
+ }
150
+
151
+ for (const params of res) {
152
+ const routineName = params.name;
153
+ const sql = exporter.format(params);
154
+ const routineFile = `${routineDir}/${routineName}.sql`;
155
+ let update = opts.update;
156
+
157
+ const oldSum = engine.getShaSum(type, schema, routineName);
158
+ if (oldSum || opts.sums || (opts.sumViews && type === 'view')) {
159
+ const shaSum = engine.shaSum(sql);
160
+ if (oldSum !== shaSum) {
161
+ engine.setShaSum(type, schema, routineName, shaSum);
162
+ update = true;
163
+ }
164
+ } else if (params.modified && engine.lastPull) {
165
+ if (params.modified > engine.lastPull)
166
+ update = true;
167
+ } else if (await fs.pathExists(routineFile)) {
168
+ const currentSql = await fs.readFile(routineFile, 'utf8');
169
+ if (sql != currentSql)
170
+ update = true;
171
+ } else
172
+ update = true;
173
+
174
+ if (update)
175
+ await fs.writeFile(routineFile, sql);
176
+ }
177
+ }
178
+ }
179
+
180
+ module.exports = Pull;
181
+
182
+ if (require.main === module)
183
+ new Myt().run(Pull);