@verdnatura/myt 1.6.8 → 1.6.10

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.
@@ -6,6 +6,11 @@ mockFunctions:
6
6
  - mockTime
7
7
  - mockUtcTime
8
8
  sumViews: true
9
+ defaultDefiner: root@localhost
10
+ deprecMarkRegex: __$
11
+ deprecCommentRegex: ^@deprecated [0-9]{4}-[0-9]{2}-[0-9]{2}
12
+ deprecDateRegex: '[0-9]{4}-[0-9]{2}-[0-9]{2}'
13
+ deprecRetentionPeriod: 60 # Days
9
14
  privileges:
10
15
  userTable: global_priv
11
16
  userWhere: >-
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
package/myt-clean.js CHANGED
@@ -10,7 +10,7 @@ class Clean extends Command {
10
10
  static usage = {
11
11
  description: 'Cleans old applied versions',
12
12
  params: {
13
- purge: 'Wether to remove non-existent scripts from DB log'
13
+ purge: 'Whether to remove non-existent scripts from DB log'
14
14
  }
15
15
  };
16
16
 
package/myt-create.js CHANGED
@@ -43,7 +43,7 @@ class Create extends Command {
43
43
  const params = {
44
44
  schema,
45
45
  name,
46
- definer: 'root@localhost'
46
+ definer: opts.defaultDefiner
47
47
  };
48
48
 
49
49
  switch (opts.type) {
package/myt-dump.js CHANGED
@@ -9,7 +9,7 @@ class Dump extends Command {
9
9
  description: 'Dumps structure and fixtures from remote',
10
10
  params: {
11
11
  lock: 'Whether to lock tables on dump',
12
- triggers: 'Wether to include triggers into dump'
12
+ triggers: 'Whether to include triggers into dump'
13
13
  },
14
14
  operand: 'remote'
15
15
  };
package/myt-init.js CHANGED
File without changes
package/myt-pull.js CHANGED
File without changes
package/myt-push.js CHANGED
@@ -15,9 +15,9 @@ class Push extends Command {
15
15
  description: 'Apply changes into database',
16
16
  params: {
17
17
  force: 'Answer yes to all questions',
18
- commit: 'Wether to save the commit SHA into database',
18
+ commit: 'Whether to save the commit SHA into database',
19
19
  sums: 'Save SHA sums of pushed objects',
20
- triggers: 'Wether to exclude triggers, used to generate local DB'
20
+ triggers: 'Whether to exclude triggers, used to generate local DB'
21
21
  },
22
22
  operand: 'remote'
23
23
  };
@@ -156,7 +156,17 @@ class Push extends Command {
156
156
  throw new Error(`Cannot obtain exclusive lock, used by connection ${isUsed}`);
157
157
  }
158
158
 
159
+ const [[scheduler]] = await conn.query(`SELECT @@event_scheduler state`);
160
+ if (scheduler.state === 'ON') await eventScheduler(false);
161
+
162
+ async function eventScheduler(isActive) {
163
+ await conn.query(
164
+ `SET GLOBAL event_scheduler = ${isActive ? 'ON' : 'OFF'}`
165
+ );
166
+ }
167
+
159
168
  async function releaseLock() {
169
+ if (scheduler.state === 'ON') await eventScheduler(true);
160
170
  await conn.query(`DO RELEASE_LOCK('myt_push')`);
161
171
  }
162
172
 
package/myt-version.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 SqlString = require('sqlstring');
4
5
 
5
6
  /**
6
7
  * Creates a new version.
@@ -9,18 +10,23 @@ class Version extends Command {
9
10
  static usage = {
10
11
  description: 'Creates a new version',
11
12
  params: {
12
- name: 'Name for the new version'
13
+ name: 'Name for the new version',
14
+ deprecate: 'Whether to generate sql to delete deprecated objects'
13
15
  },
14
16
  operand: 'name'
15
17
  };
16
18
 
17
19
  static opts = {
18
20
  alias: {
19
- name: 'n'
21
+ name: 'n',
22
+ deprecate: 'p'
20
23
  },
21
24
  string: [
22
25
  'name'
23
26
  ],
27
+ boolean: [
28
+ 'deprecate'
29
+ ],
24
30
  default: {
25
31
  remote: 'production'
26
32
  }
@@ -36,7 +42,8 @@ class Version extends Command {
36
42
  },
37
43
  versionCreated: function(versionName) {
38
44
  console.log(`New version created: ${versionName}`);
39
- }
45
+ },
46
+ deprecate: 'Generating SQL for deprecated objects deletion.'
40
47
  };
41
48
 
42
49
  async run(myt, opts) {
@@ -121,10 +128,16 @@ class Version extends Command {
121
128
  [opts.code, newVersion]
122
129
  );
123
130
  await fs.mkdir(newVersionDir);
124
- await fs.writeFile(
125
- `${newVersionDir}/00-firstScript.sql`,
126
- '-- Place your SQL code here\n'
127
- );
131
+
132
+ if (opts.deprecate) {
133
+ this.emit('deprecate');
134
+ await deprecate(conn, opts, newVersionDir);
135
+ } else
136
+ await fs.writeFile(
137
+ `${newVersionDir}/00-firstScript.sql`,
138
+ '-- Place your SQL code here\n'
139
+ );
140
+
128
141
  this.emit('versionCreated', versionFolder);
129
142
 
130
143
  await conn.query('COMMIT');
@@ -137,6 +150,105 @@ class Version extends Command {
137
150
  }
138
151
  }
139
152
 
153
+ async function deprecate(conn, opts, newVersionDir) {
154
+ const now = new Date();
155
+ const minDeprecDate = new Date(now.getTime() - opts.deprecRetentionPeriod * 24 * 60 * 60 * 1000);
156
+ const deprecMarkRegex = opts.deprecMarkRegex;
157
+ const deprecCommentRegex = opts.deprecCommentRegex;
158
+ const deprecDateRegex = opts.deprecDateRegex;
159
+ const filePath = `${newVersionDir}/00-deprecate.sql`;
160
+
161
+ // Generate the drops of the primary keys
162
+ const [primaryKeys] = await conn.query(`
163
+ SELECT c.TABLE_SCHEMA 'schema', c.TABLE_NAME 'table'
164
+ FROM information_schema.COLUMNS c
165
+ LEFT JOIN information_schema.VIEWS v ON v.TABLE_SCHEMA = c.TABLE_SCHEMA
166
+ AND v.TABLE_NAME = c.TABLE_NAME
167
+ JOIN information_schema.STATISTICS s ON s.TABLE_SCHEMA = c.TABLE_SCHEMA
168
+ AND s.TABLE_NAME = c.TABLE_NAME
169
+ AND s.COLUMN_NAME = c.COLUMN_NAME
170
+ WHERE c.COLUMN_NAME REGEXP ? COLLATE utf8mb4_unicode_ci
171
+ AND c.COLUMN_COMMENT REGEXP ? COLLATE utf8mb4_unicode_ci
172
+ AND REGEXP_SUBSTR(c.COLUMN_COMMENT, ? COLLATE utf8mb4_unicode_ci) < ?
173
+ AND v.TABLE_NAME IS NULL
174
+ AND s.INDEX_NAME = 'PRIMARY'
175
+ `, [deprecMarkRegex, deprecCommentRegex, deprecDateRegex, minDeprecDate]);
176
+
177
+ primaryKeys.map(async row => {
178
+ await fs.appendFile(
179
+ filePath,
180
+ 'ALTER TABLE ' + SqlString.escapeId(row.schema, true) + '.' +
181
+ SqlString.escapeId(row.table, true) + ' DROP PRIMARY KEY;\n'
182
+ );
183
+ });
184
+
185
+ // Generate the drops of the foreign keys
186
+ const [foreignKeys] = await conn.query(`
187
+ SELECT c.TABLE_SCHEMA 'schema', c.TABLE_NAME 'table', kcu.CONSTRAINT_NAME 'constraint'
188
+ FROM information_schema.COLUMNS c
189
+ LEFT JOIN information_schema.VIEWS v ON v.TABLE_SCHEMA = c.TABLE_SCHEMA
190
+ AND v.TABLE_NAME = c.TABLE_NAME
191
+ JOIN information_schema.KEY_COLUMN_USAGE kcu ON kcu.TABLE_SCHEMA = c.TABLE_SCHEMA
192
+ AND kcu.TABLE_NAME = c.TABLE_NAME
193
+ AND kcu.COLUMN_NAME = c.COLUMN_NAME
194
+ WHERE c.COLUMN_NAME REGEXP ? COLLATE utf8mb4_unicode_ci
195
+ AND c.COLUMN_COMMENT REGEXP ? COLLATE utf8mb4_unicode_ci
196
+ AND REGEXP_SUBSTR(c.COLUMN_COMMENT, ? COLLATE utf8mb4_unicode_ci) < ?
197
+ AND v.TABLE_NAME IS NULL
198
+ AND kcu.REFERENCED_COLUMN_NAME IS NOT NULL
199
+ `, [deprecMarkRegex, deprecCommentRegex, deprecDateRegex, minDeprecDate]);
200
+
201
+ foreignKeys.map(async row => {
202
+ await fs.appendFile(
203
+ filePath,
204
+ 'ALTER TABLE ' + SqlString.escapeId(row.schema, true) + '.' +
205
+ SqlString.escapeId(row.table, true) + ' DROP FOREIGN KEY ' +
206
+ SqlString.escapeId(row.constraint, true) + ';\n'
207
+ );
208
+ });
209
+
210
+ // Generate the drops of the columns
211
+ const [columns] = await conn.query(`
212
+ SELECT c.TABLE_SCHEMA 'schema', c.TABLE_NAME 'table', c.COLUMN_NAME 'column'
213
+ FROM information_schema.COLUMNS c
214
+ LEFT JOIN information_schema.VIEWS v ON v.TABLE_SCHEMA = c.TABLE_SCHEMA
215
+ AND v.TABLE_NAME = c.TABLE_NAME
216
+ LEFT JOIN information_schema.KEY_COLUMN_USAGE kcu ON kcu.TABLE_SCHEMA = c.TABLE_SCHEMA
217
+ AND kcu.TABLE_NAME = c.TABLE_NAME
218
+ AND kcu.COLUMN_NAME = c.COLUMN_NAME
219
+ WHERE c.COLUMN_NAME REGEXP ? COLLATE utf8mb4_unicode_ci
220
+ AND c.COLUMN_COMMENT REGEXP ? COLLATE utf8mb4_unicode_ci
221
+ AND REGEXP_SUBSTR(c.COLUMN_COMMENT, ? COLLATE utf8mb4_unicode_ci) <?
222
+ AND v.TABLE_NAME IS NULL
223
+ `, [deprecMarkRegex, deprecCommentRegex, deprecDateRegex, minDeprecDate]);
224
+
225
+ columns.map(async row => {
226
+ await fs.appendFile(
227
+ filePath,
228
+ 'ALTER TABLE ' + SqlString.escapeId(row.schema, true) + '.' +
229
+ SqlString.escapeId(row.table, true) + ' DROP COLUMN ' +
230
+ SqlString.escapeId(row.column, true) + ';\n'
231
+ );
232
+ });
233
+
234
+ // Generate the drops of the tables
235
+ const [tables] = await conn.query(`
236
+ SELECT TABLE_SCHEMA 'schema', TABLE_NAME 'table'
237
+ FROM information_schema.TABLES
238
+ WHERE TABLE_NAME REGEXP ? COLLATE utf8mb4_unicode_ci
239
+ AND TABLE_COMMENT REGEXP ? COLLATE utf8mb4_unicode_ci
240
+ AND REGEXP_SUBSTR(TABLE_COMMENT, ? COLLATE utf8mb4_unicode_ci) < ?
241
+ `, [deprecMarkRegex, deprecCommentRegex, deprecDateRegex, minDeprecDate]);
242
+
243
+ tables.map(async row => {
244
+ await fs.appendFile(
245
+ filePath,
246
+ 'DROP TABLE ' + SqlString.escapeId(row.schema, true) + '.' +
247
+ SqlString.escapeId(row.table, true) + ';\n'
248
+ );
249
+ });
250
+ }
251
+
140
252
  function randomName() {
141
253
  const color = random(colors);
142
254
  let plant = random(plants);
package/myt.js CHANGED
@@ -19,7 +19,7 @@ class Myt {
19
19
  params: {
20
20
  remote: 'Name of remote to use',
21
21
  workspace: 'The base directory of the project',
22
- debug: 'Wether to enable debug mode',
22
+ debug: 'Whether to enable debug mode',
23
23
  version: 'Display the version number and exit',
24
24
  help: 'Display this help message'
25
25
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@verdnatura/myt",
3
- "version": "1.6.8",
3
+ "version": "1.6.10",
4
4
  "author": "Verdnatura Levante SL",
5
5
  "description": "MySQL version control",
6
6
  "license": "GPL-3.0",
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes