extract-mysql-schema 0.7.4 → 0.7.6

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 (2) hide show
  1. package/index.js +32 -13
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -74,7 +74,7 @@ const extractSchemas = async function (connection, options) {
74
74
  F.POS,I.TYPE,I.*,
75
75
  CASE WHEN I.TYPE=2 OR I.TYPE=3 THEN 'YES' ELSE 'NO' END AS IS_UNIQUE,
76
76
  CASE WHEN I.TYPE=3 THEN 'YES' ELSE 'NO' END AS IS_PRIMARY,
77
- CASE WHEN I.TYPE=0 THEN 'YES' ELSE 'NO' END AS IS_FK,
77
+ CASE WHEN I.TYPE=0 THEN (CASE WHEN iif.id is not null THEN 'YES' ELSE 'NO' END) ELSE 'NO' END AS IS_FK,
78
78
  CASE WHEN C.EXTRA='auto_increment' THEN 'YES' ELSE 'NO' END AS IS_AUTONUMBER,
79
79
  F2.INDEX_KEYS as FIELD_NAME
80
80
  FROM INFORMATION_SCHEMA.INNODB_INDEXES I
@@ -85,6 +85,7 @@ const extractSchemas = async function (connection, options) {
85
85
  GROUP BY FF.INDEX_ID
86
86
  ) as F2 ON F2.INDEX_ID=F.INDEX_ID
87
87
  JOIN INFORMATION_SCHEMA.INNODB_TABLES T1 ON T1.TABLE_ID=I.TABLE_ID
88
+ LEFT JOIN INFORMATION_SCHEMA.INNODB_FOREIGN as iif on iif.id= concat('${schemaName}','/',I.NAME)
88
89
  LEFT JOIN INFORMATION_SCHEMA.TABLES T2 ON T1.NAME=CONCAT(T2.TABLE_SCHEMA,'/',T2.TABLE_NAME)
89
90
  JOIN information_schema.COLUMNS C on C.TABLE_SCHEMA=T2.TABLE_SCHEMA and C.TABLE_NAME=T2.TABLE_NAME and C.COLUMN_NAME=F.NAME
90
91
  WHERE T2.TABLE_SCHEMA = '${schemaName}' and F.POS=0
@@ -237,14 +238,15 @@ const extractSchemas = async function (connection, options) {
237
238
  let isConstraint = idx['IS_UNIQUE']==='YES' || idx['IS_PRIMARY']==='YES' || idx['IS_FK']==='YES';
238
239
 
239
240
  if(isConstraint) {
240
- if(idx['IS_PRIMARY']==='YES') {
241
+ //console.log('HERE',idx)
242
+ if(idx['IS_PRIMARY']==='YES') {
241
243
  if(idx['FIELD_NAME'].indexOf(',')>0) {
242
244
  definitions.push(`
243
245
  alter table ${idx['TABLE_NAME']}
244
246
  add primary key (${idx['FIELD_NAME']});
245
247
  `)
246
248
  }
247
- } else if(idx['IS_UNIQUE']==='YES') {
249
+ } else if(idx['IS_UNIQUE']==='YES' && idx['IS_FK']==='NO') {
248
250
  definitions.push(`
249
251
  alter table ${idx['TABLE_NAME']}
250
252
  add constraint ${idx['INDEX_NAME']}
@@ -258,13 +260,28 @@ definitions.push(`
258
260
  alter table ${idx['TABLE_NAME']}
259
261
  auto_increment = 1;`);
260
262
  }
261
- }
262
- } else {
263
- definitions.push(`
263
+ } else if(idx['IS_FK']==='YES') {
264
+ let refTable;
265
+ let refCols;
266
+ for (let i = 0; i < queryFkey.length; i++) {
267
+ if(queryFkey[i].ID===schemaName+'/'+idx['INDEX_NAME']) {
268
+ refTable=queryFkey[i].REF_NAME.replace(/.*\//g,'');
269
+ refCols=queryFkey[i].REF_COL_NAME;
270
+ }
271
+ }
272
+
273
+ definitions.push(`
274
+ alter table ${idx['TABLE_NAME']}
275
+ add constraint ${idx['INDEX_NAME']}
276
+ foreign key (${idx['FIELD_NAME']}) references ${refTable} (${refCols});
277
+ `);
278
+ }
279
+ } else {
280
+ definitions.push(`
264
281
  create index ${idx['INDEX_NAME']}
265
- on ${idx['TABLE_NAME']} (${idx['FIELD_NAME']});`)
266
- }
267
- });
282
+ on ${idx['TABLE_NAME']} (${idx['FIELD_NAME']});`)
283
+ }
284
+ });
268
285
 
269
286
  wrapper.definition = definitions.join('\n');
270
287
  });
@@ -276,21 +293,23 @@ create index ${idx['INDEX_NAME']}
276
293
  }
277
294
  }
278
295
  tableOrder.sort();
296
+ let minpos = tableOrder.length;
279
297
 
280
298
  hasParent.forEach((child)=>{
281
299
  let tableColumns = schema[child];
282
- let pos=-1;
300
+ let pos=minpos;
283
301
  tableColumns.forEach((column)=>{
284
302
  column.references.forEach((reference)=>{
285
303
  let pos2 = tableOrder.indexOf(reference.tableName);
286
- //console.log(child,reference.tableName,pos,pos2);
287
- if(pos2<0) { if(tableOrder.indexOf(reference.tableName)<0) { tableOrder.push(reference.tableName); } }
304
+ if(pos2<0) { tableOrder.push(reference.tableName); pos=tableOrder.length }
288
305
  else if(pos2+1>pos) pos=pos2+1;
289
306
  });
290
307
  });
291
308
  if(tableOrder.indexOf(child)<0) {
292
309
  if(pos<0) tableOrder.push(child);
293
- else tableOrder.splice(pos,0,child);
310
+ else {
311
+ tableOrder.splice(pos,0,child);
312
+ }
294
313
  }
295
314
  });
296
315
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "extract-mysql-schema",
3
- "version": "0.7.4",
3
+ "version": "0.7.6",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "bin": "./run.js",