extract-mysql-schema 0.7.3 → 0.7.5
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/index.js +26 -9
- package/package.json +1 -1
- package/run.js +3 -3
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
263
|
-
|
|
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
|
-
|
|
266
|
-
|
|
267
|
-
|
|
282
|
+
on ${idx['TABLE_NAME']} (${idx['FIELD_NAME']});`)
|
|
283
|
+
}
|
|
284
|
+
});
|
|
268
285
|
|
|
269
286
|
wrapper.definition = definitions.join('\n');
|
|
270
287
|
});
|
package/package.json
CHANGED
package/run.js
CHANGED
|
@@ -26,8 +26,8 @@ async function main(options) {
|
|
|
26
26
|
}
|
|
27
27
|
};
|
|
28
28
|
|
|
29
|
-
tableContent.push(`USE ${config.connection.database};`);
|
|
30
29
|
const tablesPath=path.join(process.cwd(),"tables");
|
|
30
|
+
tableContent.push(`USE ${config.connection.database};`);
|
|
31
31
|
if(result[config.connection.database].tables.length>0) {
|
|
32
32
|
// write table sql
|
|
33
33
|
if (!fs.existsSync(tablesPath)){
|
|
@@ -49,7 +49,7 @@ async function main(options) {
|
|
|
49
49
|
processFolder(tablesPath,tableContent); // add files not in the database
|
|
50
50
|
|
|
51
51
|
const proceduresPath=path.join(process.cwd(),"procedures");
|
|
52
|
-
|
|
52
|
+
sprocContent.push(`USE ${config.connection.database};`);
|
|
53
53
|
if(result[config.connection.database].procedures.length>0) {
|
|
54
54
|
// write routines
|
|
55
55
|
if (!fs.existsSync(proceduresPath)){
|
|
@@ -66,7 +66,7 @@ async function main(options) {
|
|
|
66
66
|
processFolder(proceduresPath,sprocContent); // add files not in the database
|
|
67
67
|
|
|
68
68
|
const seedPath = path.join(process.cwd(),"seed");
|
|
69
|
-
|
|
69
|
+
seedContent.push(`USE ${config.connection.database};`);
|
|
70
70
|
if(fs.existsSync(seedPath)) {
|
|
71
71
|
result[config.connection.database].tableOrder.forEach(table => {
|
|
72
72
|
let seedfile = path.join(seedPath,table+'.sql');
|