@verdnatura/myt 1.5.1

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 (57) hide show
  1. package/.dockerignore +2 -0
  2. package/LICENSE +17 -0
  3. package/README.md +268 -0
  4. package/assets/db.ini +5 -0
  5. package/assets/myvc.default.yml +9 -0
  6. package/assets/structure.sql +24 -0
  7. package/cli.js +4 -0
  8. package/exporters/event.ejs +21 -0
  9. package/exporters/event.js +25 -0
  10. package/exporters/event.sql +19 -0
  11. package/exporters/function.ejs +20 -0
  12. package/exporters/function.js +23 -0
  13. package/exporters/function.sql +16 -0
  14. package/exporters/procedure.ejs +14 -0
  15. package/exporters/procedure.js +23 -0
  16. package/exporters/procedure.sql +14 -0
  17. package/exporters/trigger.ejs +7 -0
  18. package/exporters/trigger.js +5 -0
  19. package/exporters/trigger.sql +13 -0
  20. package/exporters/view.ejs +7 -0
  21. package/exporters/view.js +25 -0
  22. package/exporters/view.sql +12 -0
  23. package/lib/command.js +16 -0
  24. package/lib/docker.js +133 -0
  25. package/lib/exporter-engine.js +112 -0
  26. package/lib/exporter.js +115 -0
  27. package/lib/server.js +71 -0
  28. package/lib/util.js +6 -0
  29. package/myt-clean.js +52 -0
  30. package/myt-dump.js +71 -0
  31. package/myt-fixtures.js +28 -0
  32. package/myt-init.js +25 -0
  33. package/myt-pull.js +129 -0
  34. package/myt-push.js +475 -0
  35. package/myt-run.js +160 -0
  36. package/myt-start.js +51 -0
  37. package/myt-version.js +219 -0
  38. package/myt.js +646 -0
  39. package/package.json +43 -0
  40. package/server/Dockerfile.base +1 -0
  41. package/server/Dockerfile.client +14 -0
  42. package/server/Dockerfile.dump +16 -0
  43. package/server/Dockerfile.server +25 -0
  44. package/server/docker-dump.sh +4 -0
  45. package/server/docker-fixtures.sh +4 -0
  46. package/server/docker-import.sh +6 -0
  47. package/server/docker-init.sh +20 -0
  48. package/server/docker-start.sh +10 -0
  49. package/server/docker.cnf +9 -0
  50. package/template/dump/fixtures.sql +1 -0
  51. package/template/dump/structure.sql +1 -0
  52. package/template/myvc.config.yml +11 -0
  53. package/template/package.json +13 -0
  54. package/template/remotes/local.ini +5 -0
  55. package/template/routines/README.md +1 -0
  56. package/template/versions/00001-firstVersion/00-firstScript.sql +1 -0
  57. package/template/versions/README.md +1 -0
package/.dockerignore ADDED
@@ -0,0 +1,2 @@
1
+ node_modules
2
+ .git
package/LICENSE ADDED
@@ -0,0 +1,17 @@
1
+ Copyright (C) 2020 - Verdnatura Levante S.L.
2
+
3
+ This package is free software; you can redistribute it and/or modify
4
+ it under the terms of the GNU General Public License as published by
5
+ the Free Software Foundation, either version 3 of the License, or
6
+ (at your option) any later version.
7
+
8
+ This program is distributed in the hope that it will be useful,
9
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ GNU General Public License for more details.
12
+
13
+ You should have received a copy of the GNU General Public License
14
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
15
+
16
+ On Debian systems, the complete text of the GNU General Public
17
+ License can be found in "/usr/share/common-licenses/GPL-3".
package/README.md ADDED
@@ -0,0 +1,268 @@
1
+ # Myt (MySQL Version Control)
2
+
3
+ Utilities to ease the maintenance of MySQL or MariaDB database versioning using
4
+ a Git repository.
5
+
6
+ This project is just to bring an idea to life and is still in an early stage of
7
+ development, so any help is welcomed! Feel free to contribute.
8
+
9
+ ## Requirements
10
+
11
+ * Git
12
+ * Docker (Optional, used to manage local server)
13
+
14
+ ## Installation
15
+
16
+ It's recommended to install the package globally.
17
+
18
+ ```text
19
+ # npm install -g myt
20
+ $ myt <command>
21
+ ```
22
+
23
+ You can also install locally and use the *npx* command to execute it.
24
+
25
+ ```text
26
+ $ npm install myt
27
+ $ npx myt <command>
28
+ ```
29
+
30
+ ## How to use
31
+
32
+ Execute *myt* with the desired command.
33
+
34
+ ```text
35
+ $ [npx] myt [-w|--workspace <string>] [-r|--remote <string>] [-d|--debug]
36
+ [-h|--help] <command> [<args>]
37
+ ```
38
+
39
+ The default workspace directory is the current working directory and unless
40
+ otherwise indicated, the default remote is *local*.
41
+
42
+ Database versioning commands:
43
+
44
+ * **init**: Initialize an empty workspace.
45
+ * **pull**: Incorporate database routine changes into workspace.
46
+ * **push**: Apply changes into database.
47
+ * **version**: Creates a new version.
48
+
49
+ Local server management commands:
50
+
51
+ * **dump**: Export database structure and fixtures.
52
+ * **fixtures**: Export local database fixtures.
53
+ * **run**: Build and start local database server container.
54
+ * **start**: Start local database server container.
55
+
56
+ Each command can have its own specific commandline options.
57
+
58
+ ## Basic information
59
+
60
+ First of all you have to initalize the workspace.
61
+
62
+ ```text
63
+ $ myt init
64
+ ```
65
+
66
+ Now you can configure Myt using *myt.config.yml* file, located at the root of
67
+ your workspace. This file should include the project codename and schemas/tables
68
+ wich are exported when you use *pull* or *dump* commands.
69
+
70
+ Don't forget to initialize git (if it isn't initialized yet).
71
+
72
+ ```text
73
+ $ git init
74
+ ```
75
+
76
+ ### Remotes
77
+
78
+ Create database connection configuration for each environment at *remotes*
79
+ folder using standard MySQL *ini* configuration files. The convention remote
80
+ names are *local*, *production* and *test*.
81
+
82
+ ```text
83
+ remotes/[remote].ini
84
+ ```
85
+ ### Startup
86
+
87
+ Once the basic configuration is done, routines can be imported from the
88
+ database into the project, it is recommended to use the *production* remote.
89
+
90
+ ```text
91
+ $ myt pull production
92
+ ```
93
+
94
+ From now on, you can use the project as if it were a standard git repository
95
+ (since it is). To apply changes to the database run the *push* command on the
96
+ desired remote.
97
+
98
+ ```text
99
+ $ myt push [<remote>] [--commit]
100
+ ```
101
+
102
+ ### Routines
103
+
104
+ Routines are placed inside *routines* folder. All objects that have PL/SQL code
105
+ are considered routines. It includes events, functions, procedures, triggers
106
+ and views with the following structure.
107
+
108
+ ```text
109
+ routines
110
+ `- schema
111
+ |- events
112
+ | `- eventName.sql
113
+ |- functions
114
+ | `- functionName.sql
115
+ |- procedures
116
+ | `- procedureName.sql
117
+ |- triggers
118
+ | `- triggerName.sql
119
+ `- views
120
+ `- viewName.sql
121
+ ```
122
+
123
+ ### Versions
124
+
125
+ Versions are placed inside *versions* folder with the following structure.
126
+ Don't place your PL/SQL objects here, use the routines folder!
127
+
128
+ ```text
129
+ versions
130
+ |- 00001-firstVersionCodeName
131
+ | |- 00-firstExecutedScript.sql
132
+ | |- 01-secondScript.sql
133
+ | `- 99-lastScript.sql
134
+ `- 00002-secondVersion
135
+ |- 00-firstExecutedScript.sql
136
+ `- 00-sameNumbers.sql
137
+ ```
138
+
139
+ ### Local server
140
+
141
+ The local server is created as a MariaDB Docker container using the base dump
142
+ created with the *dump* command plus pushing local versions and changed
143
+ routines.
144
+
145
+ ### Dumps
146
+
147
+ You can create your local fixture and structure files.
148
+
149
+ * *dump/beforeDump.sql*
150
+ * *dump/afterDump.sql*
151
+ * *dump/fixtures.sql*
152
+
153
+ ## Versioning commands
154
+
155
+ ### init
156
+
157
+ Initializes an empty workspace.
158
+
159
+ ```text
160
+ $ myt init
161
+ ```
162
+
163
+ ### pull
164
+
165
+ Incorporates database routine changes into workspace.
166
+
167
+ ```text
168
+ $ myt pull [remote] [-f|--force] [-c|--checkout] [-u|--update] [-s|--sums]
169
+ ```
170
+
171
+ When *--checkout* option is provided, it does the following before export:
172
+
173
+ 1. Get the last database push commit (saved in versioning tables).
174
+ 2. Creates and checkout to a new branch based in database commit.
175
+
176
+ ### push
177
+
178
+ Applies versions and routine changes into database.
179
+
180
+ ```text
181
+ $ myt push [<remote>] [-f|--force] [-c|--commit] [-s|--sums]
182
+ ```
183
+
184
+ Commit is saved into database only if *--commit* option is provided, it
185
+ prevents from accidentally saving local commits into shared servers, causing
186
+ subsequent pushes from other clients to fail because they can't get that
187
+ commit from the git tree in order to get differences.
188
+
189
+ ### version
190
+
191
+ Creates a new version folder, when name is not specified it generates a random
192
+ name mixing a color with a plant name.
193
+
194
+ ```text
195
+ $ myt version [<name>]
196
+ ```
197
+
198
+ ### clean
199
+
200
+ Cleans all already applied versions older than *maxOldVersions*.
201
+
202
+ ```text
203
+ $ myt clean
204
+ ```
205
+
206
+ ## Local server commands
207
+
208
+ ### dump
209
+
210
+ Exports database structure and fixtures from remote into hidden files located
211
+ in *dump* folder. If no remote is specified *production* is used.
212
+
213
+ ```text
214
+ $ myt dump [<remote>]
215
+ ```
216
+
217
+ ### fixtures
218
+
219
+ Exports local database fixtures into *dump/fixtures.sql* files. If no remote is
220
+ specified *local* is used.
221
+
222
+ ```text
223
+ $ myt fixtures [<remote>]
224
+ ```
225
+
226
+ ### run
227
+
228
+ Builds and starts local database server container. It only rebuilds the image
229
+ when fixtures have been modified or when the day on which the image was built
230
+ is different to today.
231
+
232
+ ```text
233
+ $ myt run [-c|--ci] [-r|--random]
234
+ ```
235
+
236
+ ### start
237
+
238
+ Starts local database server container. It does the minium effort, if it
239
+ doesn't exists calls the run command, if it is started does nothing. Keep in
240
+ mind that when you do not rebuild the docker you may be using an outdated
241
+ version of it.
242
+
243
+ ```text
244
+ $ myt start
245
+ ```
246
+
247
+ ## Why
248
+
249
+ The main reason for starting this project it's because there are no fully free
250
+ and open source migration tools available that allow versioning database
251
+ routines with an standard CVS system as if they were normal application code.
252
+
253
+ Also, the existing tools are too complex and require too much knowledge to
254
+ start a small project.
255
+
256
+ ## ToDo
257
+
258
+ * Undo changes when there is an error applying a version using "undo" files.
259
+ * Console logging via events.
260
+ * Lock version table row when pushing.
261
+ * Preserve all characteristics on pull: SQL mode, character set, algorithm...
262
+
263
+ ## Built With
264
+
265
+ * [Git](https://git-scm.com/)
266
+ * [nodejs](https://nodejs.org/)
267
+ * [NodeGit](https://www.nodegit.org/)
268
+ * [docker](https://www.docker.com/)
package/assets/db.ini ADDED
@@ -0,0 +1,5 @@
1
+ [client]
2
+ host = localhost
3
+ port = 3306
4
+ user = root
5
+ password = root
@@ -0,0 +1,9 @@
1
+ versionSchema: myt
2
+ versionDigits: 5
3
+ maxOldVersions: 30
4
+ schemas:
5
+ - myt
6
+ fixtures:
7
+ myt:
8
+ - version
9
+ - versionLog
@@ -0,0 +1,24 @@
1
+
2
+ CREATE TABLE `version` (
3
+ `code` VARCHAR(50) NOT NULL,
4
+ `number` CHAR(11) NULL DEFAULT NULL,
5
+ `gitCommit` VARCHAR(255) NULL DEFAULT NULL,
6
+ `updated` DATETIME NOT NULL,
7
+ `lastNumber` CHAR(11) NULL DEFAULT NULL
8
+ ) ENGINE=InnoDB;
9
+
10
+ ALTER TABLE `version`
11
+ ADD PRIMARY KEY (`code`);
12
+
13
+ CREATE TABLE `versionLog` (
14
+ `code` VARCHAR(50) NOT NULL,
15
+ `number` CHAR(11) NOT NULL,
16
+ `file` VARCHAR(255) NOT NULL,
17
+ `user` VARCHAR(255) NULL,
18
+ `updated` DATETIME NOT NULL,
19
+ `errorNumber` INT(10) UNSIGNED DEFAULT NULL,
20
+ `errorMessage` VARCHAR(255) DEFAULT NULL
21
+ ) ENGINE=InnoDB;
22
+
23
+ ALTER TABLE `versionLog`
24
+ ADD PRIMARY KEY (`code`,`number`,`file`);
package/cli.js ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env node
2
+
3
+ const Myt = require('./myt');
4
+ new Myt().run();
@@ -0,0 +1,21 @@
1
+ DROP EVENT IF EXISTS <%- schema %>.<%- name %>;
2
+ DELIMITER $$
3
+ CREATE DEFINER=<%- definer %> EVENT <%- schema %>.<%- name %><%
4
+ if (type == 'RECURRING') { %>
5
+ ON SCHEDULE EVERY <%- intervalValue %> <%- intervalField %><%
6
+ if (starts) { %>
7
+ STARTS <%- starts %><%
8
+ }
9
+ if (ends) { %>
10
+ ENDS <%- ends %><%
11
+ }
12
+ } else { %>
13
+ ON SCHEDULE AT <%- executeAt %><%
14
+ } %>
15
+ ON COMPLETION <%- onCompletion %>
16
+ <%- status %><%
17
+ if (comment) { %>
18
+ COMMENT <%- comment %><%
19
+ } %>
20
+ DO <%- body %>$$
21
+ DELIMITER ;
@@ -0,0 +1,25 @@
1
+
2
+ module.exports = {
3
+ schemaCol: 'EVENT_SCHEMA',
4
+ nameCol: 'EVENT_NAME',
5
+ escapeCols: [
6
+ 'starts',
7
+ 'ends',
8
+ 'executeAt',
9
+ 'comment'
10
+ ],
11
+ formatter(params) {
12
+ let status;
13
+ switch(params.status){
14
+ case 'DISABLED':
15
+ status = 'DISABLE';
16
+ break;
17
+ case 'SLAVESIDE_DISABLED':
18
+ status = 'DISABLE ON SLAVE';
19
+ break;
20
+ default:
21
+ status = 'ENABLE'
22
+ }
23
+ params.status = status;
24
+ }
25
+ };
@@ -0,0 +1,19 @@
1
+
2
+ SELECT
3
+ `EVENT_SCHEMA` AS `schema`,
4
+ `EVENT_NAME` AS `name`,
5
+ `DEFINER` AS `definer`,
6
+ `EVENT_DEFINITION` AS `body`,
7
+ `EVENT_TYPE` AS `type`,
8
+ `EXECUTE_AT` AS `executeAt`,
9
+ `INTERVAL_VALUE` AS `intervalValue`,
10
+ `INTERVAL_FIELD` AS `intervalField`,
11
+ `STARTS` AS `starts`,
12
+ `ENDS` AS `ends`,
13
+ `STATUS` AS `status`,
14
+ `ON_COMPLETION` AS `onCompletion`,
15
+ `EVENT_COMMENT` AS `comment`,
16
+ `LAST_ALTERED` AS `modified`
17
+ FROM `information_schema`.`EVENTS`
18
+ WHERE ?
19
+ ORDER BY `name`
@@ -0,0 +1,20 @@
1
+ DROP FUNCTION IF EXISTS <%- schema %>.<%- name %>;
2
+ DELIMITER $$
3
+ CREATE DEFINER=<%- definer %> FUNCTION <%- schema %>.<%- name %>(<%- paramList %>)
4
+ RETURNS <%- returns %><%
5
+ if (isDeterministic == 'NO') { %>
6
+ NOT DETERMINISTIC<%
7
+ } else { %>
8
+ DETERMINISTIC<%
9
+ }
10
+ if (dataAccess) { %>
11
+ <%- dataAccess %><%
12
+ }
13
+ if (securityType == 'INVOKER') { %>
14
+ SQL SECURITY <%- securityType %><%
15
+ }
16
+ if (comment) { %>
17
+ COMMENT <%- comment %><%
18
+ } %>
19
+ <%- body %>$$
20
+ DELIMITER ;
@@ -0,0 +1,23 @@
1
+
2
+ module.exports = {
3
+ schemaCol: 'db',
4
+ nameCol: 'name',
5
+ escapeCols: [
6
+ 'comment'
7
+ ],
8
+ formatter(params) {
9
+ let dataAccess;
10
+ switch(params.dataAccess) {
11
+ case 'NO_SQL':
12
+ dataAccess = 'NO SQL';
13
+ break;
14
+ case 'READS_SQL_DATA':
15
+ dataAccess = 'READS SQL DATA';
16
+ break;
17
+ case 'MODIFIES_SQL_DATA':
18
+ dataAccess = 'MODIFIES SQL DATA';
19
+ break;
20
+ }
21
+ params.dataAccess = dataAccess;
22
+ }
23
+ };
@@ -0,0 +1,16 @@
1
+
2
+ SELECT
3
+ `db` AS `schema`,
4
+ `name`,
5
+ `definer`,
6
+ `param_list` AS `paramList`,
7
+ `body`,
8
+ `sql_data_access` AS `dataAccess`,
9
+ `security_type` AS `securityType`,
10
+ `comment`,
11
+ `modified`,
12
+ `is_deterministic` AS `isDeterministic`,
13
+ `returns`
14
+ FROM `mysql`.`proc`
15
+ WHERE ? AND `type` = 'FUNCTION'
16
+ ORDER BY `name`
@@ -0,0 +1,14 @@
1
+ DROP PROCEDURE IF EXISTS <%- schema %>.<%- name %>;
2
+ DELIMITER $$
3
+ CREATE DEFINER=<%- definer %> PROCEDURE <%- schema %>.<%- name %>(<%- paramList %>)<%
4
+ if (dataAccess) { %>
5
+ <%- dataAccess %><%
6
+ }
7
+ if (securityType == 'INVOKER') { %>
8
+ SQL SECURITY <%- securityType %><%
9
+ }
10
+ if (comment) { %>
11
+ COMMENT <%- comment %><%
12
+ } %>
13
+ <%- body %>$$
14
+ DELIMITER ;
@@ -0,0 +1,23 @@
1
+
2
+ module.exports = {
3
+ schemaCol: 'db',
4
+ nameCol: 'name',
5
+ escapeCols: [
6
+ 'comment'
7
+ ],
8
+ formatter(params) {
9
+ let dataAccess;
10
+ switch(params.dataAccess) {
11
+ case 'NO_SQL':
12
+ dataAccess = 'NO SQL';
13
+ break;
14
+ case 'READS_SQL_DATA':
15
+ dataAccess = 'READS SQL DATA';
16
+ break;
17
+ case 'MODIFIES_SQL_DATA':
18
+ dataAccess = 'MODIFIES SQL DATA';
19
+ break;
20
+ }
21
+ params.dataAccess = dataAccess;
22
+ }
23
+ };
@@ -0,0 +1,14 @@
1
+
2
+ SELECT
3
+ `db` AS `schema`,
4
+ `name`,
5
+ `definer`,
6
+ `param_list` AS `paramList`,
7
+ `body`,
8
+ `sql_data_access` AS `dataAccess`,
9
+ `security_type` AS `securityType`,
10
+ `comment`,
11
+ `modified`
12
+ FROM `mysql`.`proc`
13
+ WHERE ? AND `type` = 'PROCEDURE'
14
+ ORDER BY `name`
@@ -0,0 +1,7 @@
1
+ DROP TRIGGER IF EXISTS <%- schema %>.<%- name %>;
2
+ DELIMITER $$
3
+ CREATE DEFINER=<%- definer %> TRIGGER <%- schema %>.<%- name %>
4
+ <%- actionTiming %> <%- actionType %> ON `<%- table %>`
5
+ FOR EACH ROW
6
+ <%- body %>$$
7
+ DELIMITER ;
@@ -0,0 +1,5 @@
1
+
2
+ module.exports = {
3
+ schemaCol: 'TRIGGER_SCHEMA',
4
+ nameCol: 'TRIGGER_NAME'
5
+ };
@@ -0,0 +1,13 @@
1
+
2
+ SELECT
3
+ `TRIGGER_SCHEMA` AS `schema`,
4
+ `TRIGGER_NAME` AS `name`,
5
+ `DEFINER` AS `definer`,
6
+ `ACTION_TIMING` AS `actionTiming`,
7
+ `EVENT_MANIPULATION` AS `actionType`,
8
+ `EVENT_OBJECT_TABLE` AS `table`,
9
+ `ACTION_STATEMENT` AS `body`,
10
+ `CREATED` AS `modified`
11
+ FROM `information_schema`.`TRIGGERS`
12
+ WHERE ?
13
+ ORDER BY `name`
@@ -0,0 +1,7 @@
1
+ CREATE OR REPLACE DEFINER=<%- definer %>
2
+ SQL SECURITY <%- securityType %>
3
+ VIEW <%- schema %>.<%- name %>
4
+ AS <%- definition %><%
5
+ if (checkOption != 'NONE') { %>
6
+ WITH <%- checkOption %> CHECK OPTION<%
7
+ } %>
@@ -0,0 +1,25 @@
1
+
2
+ const sqlFormatter = require('@sqltools/formatter');
3
+
4
+ module.exports = {
5
+ schemaCol: 'TABLE_SCHEMA',
6
+ nameCol: 'TABLE_NAME',
7
+ formatter(params) {
8
+ params.definition = sqlFormatter.format(params.definition, {
9
+ indent: '\t',
10
+ reservedWordCase: 'upper'
11
+ });
12
+
13
+ let algorithm;
14
+ switch (params.isUpdatable) {
15
+ case 'YES':
16
+ algorithm = 'MERGE';
17
+ break;
18
+ case 'NO':
19
+ algorithm = 'TEMPTABLE';
20
+ break;
21
+ default:
22
+ algorithm = 'UNDEFINED';
23
+ }
24
+ }
25
+ };
@@ -0,0 +1,12 @@
1
+
2
+ SELECT
3
+ `TABLE_SCHEMA` AS `schema`,
4
+ `TABLE_NAME` AS `name`,
5
+ `VIEW_DEFINITION` AS `definition`,
6
+ `CHECK_OPTION` AS `checkOption`,
7
+ `IS_UPDATABLE` AS `isUpdatable`,
8
+ `DEFINER` AS `definer`,
9
+ `SECURITY_TYPE` AS `securityType`
10
+ FROM `information_schema`.`VIEWS`
11
+ WHERE ?
12
+ ORDER BY `name`
package/lib/command.js ADDED
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Base class for Myt commands.
3
+ */
4
+ module.exports = class MytCommand {
5
+ get usage() {
6
+ return {};
7
+ }
8
+
9
+ get localOpts() {
10
+ return {};
11
+ }
12
+
13
+ async run(myt, opts) {
14
+ throw new Error('run command not defined');
15
+ }
16
+ }