dbtasker 2.5.0 → 3.0.0
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/README.md +14 -1
- package/addcolumn.js +264 -0
- package/altercolumn.js +595 -0
- package/dbop.js +41 -51
- package/dropcolumn.js +174 -0
- package/function.js +567 -104
- package/index.js +98 -32
- package/package.json +1 -1
- package/tableop.js +273 -0
- package/validation.js +224 -159
- package/columnop.js +0 -748
- package/tables.js +0 -0
package/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const fncs = require("./function");
|
|
2
|
-
const recordedjson = require("./
|
|
2
|
+
const recordedjson = require("./tableop");
|
|
3
3
|
const cstyler = require("cstyler");
|
|
4
4
|
const checker = require("./validation");
|
|
5
5
|
|
|
@@ -8,10 +8,11 @@ const checker = require("./validation");
|
|
|
8
8
|
|
|
9
9
|
const moduleName = "dbtasker";
|
|
10
10
|
const truers = [true, 1, "1", "true", "True", "TRUE"];
|
|
11
|
+
const falsers = [false, 0, "0", "false", "False", "FALSE"];
|
|
11
12
|
|
|
12
|
-
module.exports = async function(allconfig, table_json) {
|
|
13
|
+
module.exports = async function (allconfig, table_json) {
|
|
13
14
|
try {
|
|
14
|
-
console.log(cstyler.
|
|
15
|
+
console.log(cstyler.hex("#00d9ffff")("Initializing DBTASKER..."))
|
|
15
16
|
// check if enough database available on table json
|
|
16
17
|
const databaseNames = Object.keys(table_json);
|
|
17
18
|
if (databaseNames.length === 0) {
|
|
@@ -31,6 +32,29 @@ module.exports = async function(allconfig, table_json) {
|
|
|
31
32
|
'password': allconfig.password,
|
|
32
33
|
'port': allconfig.port
|
|
33
34
|
}
|
|
35
|
+
// lets check database type
|
|
36
|
+
const ifmysqldatabase = await fncs.isMySQLDatabase(config);
|
|
37
|
+
if (ifmysqldatabase === false) {
|
|
38
|
+
console.error("My SQL database is required to run ", moduleName, " module. Please install mysql2 to use this module. To install run this code on the terminal > npm install mysql2");
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const isvalidmysqlversion = await fncs.isMySQL578OrAbove(config);
|
|
42
|
+
if (isvalidmysqlversion === false) {
|
|
43
|
+
console.error("My SQL version 5.7.8 or above is required. Please check if you have installed mysql2. To install: npm install mysql2");
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
// Declare separator
|
|
47
|
+
let separator = "_";
|
|
48
|
+
const sepkeys = ['sep', 'separator'];
|
|
49
|
+
for (const item of Object.keys(allconfig)) {
|
|
50
|
+
if (sepkeys.includes(item.toLowerCase())) {
|
|
51
|
+
separator = allconfig[item];
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (!fncs.isValidMySQLIdentifier(separator)) {
|
|
56
|
+
separator = "_";
|
|
57
|
+
}
|
|
34
58
|
// get don't touch database
|
|
35
59
|
let donttouchdb = [];
|
|
36
60
|
const donttouchkeys = ['donttouch', 'donottouch', 'donttouchdb', 'donottouchdb', 'donttouchdatabase', 'donottouchdatabase', 'dontdelete', 'donotdelete', 'dontdeletedb', 'donotdeletedb', 'dontdeletedatabase', 'donotdeletedatabase', 'dont_touch', 'do_not_touch', 'dont_touch_db', 'do_not_touch_db', 'dont_touch_database', 'do_not_touch_database', 'dont_delete', 'do_not_delete', 'dont_delete_db', 'do_not_delete_db', 'dont_delete_database', 'do_not_delete_database', 'reserveddb', 'reserved_db'];
|
|
@@ -50,18 +74,6 @@ module.exports = async function(allconfig, table_json) {
|
|
|
50
74
|
}
|
|
51
75
|
}
|
|
52
76
|
}
|
|
53
|
-
// Declare seperator
|
|
54
|
-
let seperator = "_";
|
|
55
|
-
const sepkeys = ['sep', 'seperator'];
|
|
56
|
-
for (const item of Object.keys(allconfig)) {
|
|
57
|
-
if (sepkeys.includes(item.toLowerCase())) {
|
|
58
|
-
seperator = allconfig[item];
|
|
59
|
-
break;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
if (!fncs.isValidMySQLIdentifier(seperator)) {
|
|
63
|
-
seperator = "_";
|
|
64
|
-
}
|
|
65
77
|
let dropdatabase;
|
|
66
78
|
const dropdbkeys = ['dropdb', 'dropdatabase', 'deletedb', 'deletedatabase', 'drop_db', 'drop_database', 'delete_db', 'delete_database', 'removedb', 'removedatabase', 'remove_db', 'remove_database'];
|
|
67
79
|
for (const item of Object.keys(allconfig)) {
|
|
@@ -95,23 +107,42 @@ module.exports = async function(allconfig, table_json) {
|
|
|
95
107
|
}
|
|
96
108
|
if (truers.includes(dropcolumn)) {
|
|
97
109
|
dropcolumn = true;
|
|
98
|
-
} else {
|
|
110
|
+
} else if (falsers.includes(dropcolumn)) {
|
|
99
111
|
dropcolumn = false;
|
|
112
|
+
} else {
|
|
113
|
+
dropcolumn = true;
|
|
100
114
|
}
|
|
101
|
-
|
|
102
|
-
const
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
115
|
+
let forcedropcolumn;
|
|
116
|
+
const frocedropcolkey = ['forcedropcol', 'forcedropcolumn', 'forcedeletecol', 'forcedeletecolumn', 'forceremovecol', 'forceremovecolumn', 'force_drop_col', 'force_drop_column', 'force_delete_col', 'force_delete_column', 'force_remove_col', 'force_remove_column'];
|
|
117
|
+
for (const item of Object.keys(allconfig)) {
|
|
118
|
+
if (frocedropcolkey.includes(item.toLowerCase())) {
|
|
119
|
+
forcedropcolumn = allconfig[item];
|
|
120
|
+
}
|
|
106
121
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
122
|
+
if (truers.includes(forcedropcolumn)) {
|
|
123
|
+
forcedropcolumn = true;
|
|
124
|
+
} else if (falsers.includes(forcedropcolumn)) {
|
|
125
|
+
forcedropcolumn = false;
|
|
126
|
+
} else {
|
|
127
|
+
forcedropcolumn = false;
|
|
128
|
+
}
|
|
129
|
+
let forceupdatecolumn;
|
|
130
|
+
const forceupdatecolkey = ['forceupdatecol', 'forcemodifycol', 'forceupdatecolumn', 'forcemodifycolumn', 'force_update_col', 'force_modify_col', 'force_update_column', 'force_modify_column', 'forcealtercol', 'forcealtercolumn', 'force_alter_col', 'force_alter_column'];
|
|
131
|
+
for (const item of Object.keys(allconfig)) {
|
|
132
|
+
if (forceupdatecolkey.includes(item.toLowerCase())) {
|
|
133
|
+
forceupdatecolumn = allconfig[item];
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
if (truers.includes(forceupdatecolumn)) {
|
|
137
|
+
forceupdatecolumn = true;
|
|
138
|
+
} else if (falsers.includes(forceupdatecolumn)) {
|
|
139
|
+
forceupdatecolumn = false;
|
|
140
|
+
} else {
|
|
141
|
+
forceupdatecolumn = true;
|
|
111
142
|
}
|
|
112
143
|
console.log(cstyler.bold.underline.yellow("Lets check if the table need an upgrade"))
|
|
113
144
|
// lets check all table name and column name
|
|
114
|
-
const checking = await checker.JSONchecker(table_json, config,
|
|
145
|
+
const checking = await checker.JSONchecker(table_json, config, separator);
|
|
115
146
|
if (checking.status === false) {
|
|
116
147
|
console.log(cstyler.bold.underline.red("Please correct those information and try again."))
|
|
117
148
|
return;
|
|
@@ -119,25 +150,60 @@ module.exports = async function(allconfig, table_json) {
|
|
|
119
150
|
const jsondata = checking.data;
|
|
120
151
|
console.log(cstyler.bold.purple("Lets start operation on databases."));
|
|
121
152
|
const dbop = require("./dbop");
|
|
122
|
-
const databaseop = await dbop.databaseAddDeleteAlter(config, jsondata, dropdatabase, donttouchdb,
|
|
153
|
+
const databaseop = await dbop.databaseAddDeleteAlter(config, jsondata, dropdatabase, donttouchdb, separator);
|
|
123
154
|
if (databaseop === null) {
|
|
155
|
+
console.log(cstyler.bold.underline.red("Error occurred during database operation."));
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
// lets create tables if needed
|
|
159
|
+
const tableop = require("./tableop");
|
|
160
|
+
const createtable = await tableop.createTableifNeeded(config, jsondata, separator);
|
|
161
|
+
if (createtable === null) {
|
|
162
|
+
console.log(cstyler.bold.underline.red("Error occurred during creating tables."));
|
|
124
163
|
return;
|
|
125
164
|
}
|
|
126
165
|
// Drop tables
|
|
127
166
|
if (droptable) {
|
|
128
167
|
console.log(cstyler.bold.purple("Lets drop unlisted table if needed."));
|
|
129
|
-
const droptableifneeded = await
|
|
168
|
+
const droptableifneeded = await tableop.dropTable(config, jsondata, separator);
|
|
130
169
|
if (droptableifneeded === null) {
|
|
170
|
+
console.log(cstyler.bold.underline.red("Error occurred during dropping tables."));
|
|
131
171
|
return;
|
|
132
172
|
}
|
|
133
173
|
}
|
|
134
|
-
|
|
135
174
|
console.log(cstyler.bold.purple("Lets start working on columns"));
|
|
136
|
-
const colop = require("./
|
|
137
|
-
|
|
138
|
-
|
|
175
|
+
const colop = require("./dropcolumn");
|
|
176
|
+
|
|
177
|
+
// lets drop columns if needed
|
|
178
|
+
console.log(cstyler.bold.purple("Lets drop unlisted columns if needed."));
|
|
179
|
+
if (dropcolumn) {
|
|
180
|
+
const dropcolifneeded = await colop.dropcolumn(config, jsondata, forcedropcolumn, separator);
|
|
181
|
+
if (dropcolifneeded === null) {
|
|
182
|
+
console.log(cstyler.bold.underline.red("Error occurred during dropping columns."));
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
// lets add columns if needed
|
|
187
|
+
const addcolumn = require("./addcolumn");
|
|
188
|
+
console.log(cstyler.bold.purple("Lets add columns if needed."));
|
|
189
|
+
const addcolifneeded = await addcolumn.addColumnIfNeeded(config, jsondata, separator);
|
|
190
|
+
if (addcolifneeded === null) {
|
|
191
|
+
console.log(cstyler.bold.underline.red("Error occurred during adding columns."));
|
|
139
192
|
return;
|
|
140
193
|
}
|
|
194
|
+
// lets alter columns if needed
|
|
195
|
+
const altercolop = require("./altercolumn");
|
|
196
|
+
console.log(cstyler.bold.purple("Lets alter columns if needed."));
|
|
197
|
+
const altercolifneeded = await altercolop.alterColumnIfNeeded(config, jsondata, forceupdatecolumn, separator);
|
|
198
|
+
if (altercolifneeded === null) {
|
|
199
|
+
console.log(cstyler.bold.underline.red("Error occurred during altering columns."));
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
|
|
141
207
|
console.log(cstyler.bold.underline.green("<<<All database work is done perfectly>>>"));
|
|
142
208
|
} catch (err) {
|
|
143
209
|
console.error(err.message);
|
package/package.json
CHANGED
package/tableop.js
ADDED
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
const fncs = require("./function");
|
|
2
|
+
const cstyler = require("cstyler");
|
|
3
|
+
|
|
4
|
+
const moduleName = "dbtasker";
|
|
5
|
+
const truers = [true, 1, "1", "true", "True", "TRUE"];
|
|
6
|
+
const falsers = [false, 0, "0", "false", "False", "FALSE"];
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
async function createTableQuery(config, tabledata, tableName, dbname) {
|
|
13
|
+
try {
|
|
14
|
+
//let queryText = `CREATE TABLE ${tableName} ( `;
|
|
15
|
+
let quries = [];
|
|
16
|
+
let foreignkeys = {};
|
|
17
|
+
for (const columnName of Object.keys(tabledata)) {
|
|
18
|
+
let queryText = "";
|
|
19
|
+
if (["_engine_", "_charset_", "_collate_"].includes(columnName)) {
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
queryText += `\`${columnName}\``;
|
|
23
|
+
if (tabledata[columnName].hasOwnProperty("columntype")) {
|
|
24
|
+
queryText += ` ${tabledata[columnName].columntype}`
|
|
25
|
+
}
|
|
26
|
+
if (tabledata[columnName].hasOwnProperty("length_value")) {
|
|
27
|
+
const lengthval = tabledata[columnName].length_value;
|
|
28
|
+
|
|
29
|
+
// INT, VARCHAR, CHAR, BIT, etc.
|
|
30
|
+
if (typeof lengthval === "number") {
|
|
31
|
+
queryText += `(${lengthval})`;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// DECIMAL, FLOAT, DOUBLE → [precision, scale]
|
|
35
|
+
else if (
|
|
36
|
+
Array.isArray(lengthval) &&
|
|
37
|
+
lengthval.length === 2 &&
|
|
38
|
+
lengthval.every(v => typeof v === "number")
|
|
39
|
+
) {
|
|
40
|
+
queryText += `(${lengthval[0]},${lengthval[1]})`;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// ENUM / SET → ['a','b','c']
|
|
44
|
+
else if (
|
|
45
|
+
Array.isArray(lengthval) &&
|
|
46
|
+
lengthval.every(v => typeof v === "string")
|
|
47
|
+
) {
|
|
48
|
+
const escaped = lengthval.map(v => `'${v.replace(/'/g, "''")}'`);
|
|
49
|
+
queryText += `(${escaped.join(",")})`;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
queryText += " ";
|
|
53
|
+
if (tabledata[columnName].hasOwnProperty("unsigned") && tabledata[columnName].unsigned === true) {
|
|
54
|
+
queryText += `UNSIGNED `
|
|
55
|
+
}
|
|
56
|
+
if (tabledata[columnName].zerofill === true) {
|
|
57
|
+
queryText += `ZEROFILL `
|
|
58
|
+
}
|
|
59
|
+
if (tabledata[columnName].autoincrement === true) {
|
|
60
|
+
queryText += `AUTO_INCREMENT `
|
|
61
|
+
}
|
|
62
|
+
if (tabledata[columnName].hasOwnProperty("index")) {
|
|
63
|
+
queryText += `${tabledata[columnName].index} `
|
|
64
|
+
}
|
|
65
|
+
if (tabledata[columnName].hasOwnProperty("_charset_")) {
|
|
66
|
+
queryText += `CHARACTER SET ${tabledata[columnName]._charset_} `
|
|
67
|
+
}
|
|
68
|
+
if (tabledata[columnName].hasOwnProperty("_collate_")) {
|
|
69
|
+
queryText += `COLLATE ${tabledata[columnName]._collate_} `
|
|
70
|
+
}
|
|
71
|
+
if (tabledata[columnName].hasOwnProperty("nulls")) {
|
|
72
|
+
if (tabledata[columnName].nulls === true) {
|
|
73
|
+
queryText += `NULL `
|
|
74
|
+
} else {
|
|
75
|
+
queryText += `NOT NULL `
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if (tabledata[columnName].hasOwnProperty("defaults")) {
|
|
79
|
+
const d = tabledata[columnName].defaults;
|
|
80
|
+
if (d === null) queryText += "DEFAULT NULL ";
|
|
81
|
+
else if (typeof d === "number") queryText += `DEFAULT ${d} `;
|
|
82
|
+
else if (/^CURRENT_TIMESTAMP$/i.test(d)) queryText += `DEFAULT ${d} `;
|
|
83
|
+
else queryText += `DEFAULT '${d.replace(/'/g, "''")}' `;
|
|
84
|
+
}
|
|
85
|
+
if (tabledata[columnName].hasOwnProperty("comment")) {
|
|
86
|
+
queryText += `COMMENT '${tabledata[columnName].comment}' `
|
|
87
|
+
}
|
|
88
|
+
quries.push(queryText);
|
|
89
|
+
// lets sotore foreing keys
|
|
90
|
+
if (tabledata[columnName].hasOwnProperty("foreign_key")) {
|
|
91
|
+
foreignkeys[columnName] = tabledata[columnName].foreign_key;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
// foreign keys
|
|
95
|
+
let fkquery = [];
|
|
96
|
+
let keyidx = [];
|
|
97
|
+
if (Object.keys(foreignkeys).length > 0) {
|
|
98
|
+
for (const fks in foreignkeys) {
|
|
99
|
+
const ifexist = await fncs.columnExists(config, dbname, tabledata[fks].foreign_key.table, tabledata[fks].foreign_key.column);
|
|
100
|
+
if (ifexist === false) {
|
|
101
|
+
console.log(cstyler.red("Foreign key column do not exist."));
|
|
102
|
+
} else if (ifexist === true) {
|
|
103
|
+
let fktext = "";
|
|
104
|
+
fktext +=
|
|
105
|
+
`CONSTRAINT fk_${tableName}_${foreignkeys[fks].table}_${foreignkeys[fks].column} ` +
|
|
106
|
+
`FOREIGN KEY (\`${fks}\`) REFERENCES \`${foreignkeys[fks].table}\`(\`${foreignkeys[fks].column}\`) `;
|
|
107
|
+
|
|
108
|
+
if (foreignkeys[fks].hasOwnProperty("deleteOption")) {
|
|
109
|
+
fktext += `ON DELETE ${foreignkeys[fks].deleteOption} `
|
|
110
|
+
}
|
|
111
|
+
if (foreignkeys[fks].hasOwnProperty("updateOption")) {
|
|
112
|
+
console.log(cstyler.red("has update option"), foreignkeys[fks].updateOption)
|
|
113
|
+
fktext += `ON UPDATE ${foreignkeys[fks].updateOption} `
|
|
114
|
+
}
|
|
115
|
+
fkquery.push(fktext);
|
|
116
|
+
keyidx.push(`KEY \`idx_${tableName}_${fks}\` (\`${fks}\`)`);
|
|
117
|
+
// lets delete used item from the foreign key
|
|
118
|
+
delete foreignkeys[fks];
|
|
119
|
+
} else {
|
|
120
|
+
console.error("Having problem connecting to database.");
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
let lastqueryText = ``;
|
|
126
|
+
if (tabledata.hasOwnProperty("_engine_")) {
|
|
127
|
+
lastqueryText += `ENGINE=${tabledata._engine_}\n`;
|
|
128
|
+
}
|
|
129
|
+
if (tabledata.hasOwnProperty("_charset_")) {
|
|
130
|
+
lastqueryText += `DEFAULT CHARSET=${tabledata._charset_}\n`;
|
|
131
|
+
}
|
|
132
|
+
if (tabledata.hasOwnProperty("_collate_")) {
|
|
133
|
+
lastqueryText += `COLLATE=${tabledata._collate_}\n`;
|
|
134
|
+
}
|
|
135
|
+
if (tabledata.hasOwnProperty("_comment_")) {
|
|
136
|
+
lastqueryText += `COMMENT=${tabledata._comment_}\n`;
|
|
137
|
+
}
|
|
138
|
+
const fullqueryText = `
|
|
139
|
+
CREATE TABLE IF NOT EXISTS \`${tableName}\` (
|
|
140
|
+
${[...quries, ...keyidx, ...fkquery].join(",\n ")}
|
|
141
|
+
) ${lastqueryText};
|
|
142
|
+
`;
|
|
143
|
+
console.log("Running query: ", cstyler.green(fullqueryText));
|
|
144
|
+
const runquery = await fncs.runQuery(config, dbname, fullqueryText);
|
|
145
|
+
if (runquery === null) {
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
console.log(cstyler.green("Successfully created "), cstyler.blue("Table: "), cstyler.hex("#00d9ffff")(tableName), " on ", cstyler.blue("Database: "), cstyler.hex("#00d9ffff")(dbname));
|
|
149
|
+
return foreignkeys;
|
|
150
|
+
} catch (err) {
|
|
151
|
+
console.error(err.message);
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
async function createTableifNeeded(config, jsondata, separator) {
|
|
156
|
+
try {
|
|
157
|
+
if (!fncs.isJsonObject(jsondata)) {
|
|
158
|
+
return false;
|
|
159
|
+
}
|
|
160
|
+
let foreignkeys = {};
|
|
161
|
+
console.log(cstyler.bold.purple("Lets start creating unlisted tables if needed."));
|
|
162
|
+
// Lets check config
|
|
163
|
+
for (const jsdb of Object.keys(jsondata)) {
|
|
164
|
+
let dbname = fncs.perseDatabaseNameWithLoop(jsdb, separator);
|
|
165
|
+
if (dbname === false) {
|
|
166
|
+
console.error(cstyler.bold.red("There must be some mistake. Please re install the module."));
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
const getalltables = await fncs.getTableNames(config, dbname.loopname);
|
|
170
|
+
if (getalltables === null) {
|
|
171
|
+
console.error(cstyler.bold.red("Having problem getting all the table names of Database: ", dbname.loopname, ". Please re-install the module."));
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
for (const dbtableName of Object.keys(jsondata[jsdb])) {
|
|
175
|
+
// check if table data is json object
|
|
176
|
+
if (fncs.isJsonObject(jsondata[jsdb][dbtableName]) === false) { continue }
|
|
177
|
+
const tableName = fncs.perseTableNameWithLoop(dbtableName, separator);
|
|
178
|
+
if (tableName === false) {
|
|
179
|
+
console.error(cstyler.bold.red("Can not parse table name from json. There must be some mistake in table name. Please re install the module."));
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
if (getalltables.includes(tableName.loopname)) {
|
|
183
|
+
console.log(cstyler.blue("Database:"), cstyler.hex("#00d9ffff")(dbname.loopname), cstyler.blue("Table: "), cstyler.hex("#00d9ffff")(tableName.loopname), cstyler.green(" already exists in Database"));
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
const createtable = await createTableQuery(config, jsondata[jsdb][dbtableName], tableName.loopname, dbname.loopname);
|
|
187
|
+
if (createtable === null) {
|
|
188
|
+
console.error(cstyler.bold.red("Having problem creating table: ", tableName.loopname, " on Database: ", dbname.loopname, ". Please check database connection."));
|
|
189
|
+
return null;
|
|
190
|
+
}
|
|
191
|
+
if (createtable && Object.keys(createtable).length > 0) {
|
|
192
|
+
foreignkeys = fncs.JoinJsonObjects(foreignkeys, createtable);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
// lets create foreign keys if any
|
|
196
|
+
if (Object.keys(foreignkeys).length > 0) {
|
|
197
|
+
for (const fkcol of Object.keys(foreignkeys)) {
|
|
198
|
+
const addfk = await fncs.addForeignKeyWithIndex(config, dbname.loopname, tableName.loopname, fkcol, foreignkeys[fkcol].table, foreignkeys[fkcol].column, { onDelete: foreignkeys[fkcol].deleteOption, onUpdate: foreignkeys[fkcol].updateOption });
|
|
199
|
+
if (addfk === null) {
|
|
200
|
+
console.error(cstyler.bold.red("Having problem adding foreign key constraint on column: ", fkcol, " on Database: ", dbname.loopname, ". Please check database connection."));
|
|
201
|
+
return null;
|
|
202
|
+
}
|
|
203
|
+
else if (addfk === true) {
|
|
204
|
+
console.log(cstyler.green("Successfully added foreign key constraint on column: "), cstyler.hex("#00d9ffff")(fkcol), cstyler.green(" on Table: "), cstyler.hex("#00d9ffff")(tableName.loopname), cstyler.green(" on Database: "), cstyler.hex("#00d9ffff")(dbname.loopname));
|
|
205
|
+
}
|
|
206
|
+
else if (addfk === false) {
|
|
207
|
+
console.log(cstyler.blue("Foreign key constraint on column: "), cstyler.hex("#00d9ffff")(fkcol), cstyler.blue(" on Table: "), cstyler.hex("#00d9ffff")(tableName.loopname), cstyler.blue(" on Database: "), cstyler.hex("#00d9ffff")(dbname.loopname), cstyler.blue(" already exists. So, skipping."));
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return true;
|
|
213
|
+
} catch (err) {
|
|
214
|
+
console.error(cstyler.bold.red("Error occurred in createTableifNeeded function of ", moduleName, " module. Error details: "), err);
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
async function dropTable(config, json_data, separator = "_") {
|
|
219
|
+
try {
|
|
220
|
+
console.log(cstyler.bold.yellow("Initiating drop table operation"));
|
|
221
|
+
let count = 0;
|
|
222
|
+
for (const jsondb of Object.keys(json_data)) {
|
|
223
|
+
let dbname = fncs.perseDatabaseNameWithLoop(jsondb, separator);
|
|
224
|
+
if (dbname === false) {
|
|
225
|
+
console.error("There must be some mistake. Please re install the module.");
|
|
226
|
+
}
|
|
227
|
+
const alltables = await fncs.getTableNames(config, dbname.loopname);
|
|
228
|
+
if (alltables === null) {
|
|
229
|
+
console.error("Having problem getting all the table name of the Database: ", cstyler.blue(dbname.loopname), ". Please re-install the module.");
|
|
230
|
+
return null;
|
|
231
|
+
}
|
|
232
|
+
for (const tableName of (alltables)) {
|
|
233
|
+
const revlpnm = fncs.reverseLoopName(tableName);
|
|
234
|
+
if (Array.isArray(revlpnm)) {
|
|
235
|
+
if (!Object.keys(json_data[jsondb]).includes(revlpnm[0]) && !Object.keys(json_data[jsondb]).includes(revlpnm[1]) && !Object.keys(json_data[jsondb]).includes(revlpnm[2]) && !Object.keys(json_data[jsondb]).includes(revlpnm[3])) {
|
|
236
|
+
const droptable = await fncs.dropTable(config, dbname.loopname, tableName);
|
|
237
|
+
if (droptable === null) {
|
|
238
|
+
console.error("Having problem dropping table. Please check database connection.");
|
|
239
|
+
return null;
|
|
240
|
+
} else if (droptable === true) {
|
|
241
|
+
count += 1;
|
|
242
|
+
console.log(cstyler.blue("Database: "), cstyler.hex("#00d9ffff")(dbname.loopname), cstyler.blue("Table: "), cstyler.hex("#00d9ffff")(tableName), "- has dropped successfully.");
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
} else if (!Object.keys(json_data[jsondb]).includes(revlpnm)) {
|
|
246
|
+
const droptable = await fncs.dropTable(config, dbname.loopname, tableName);
|
|
247
|
+
if (droptable === null) {
|
|
248
|
+
console.error("Having problem dropping table. Please check database connection.");
|
|
249
|
+
return null;
|
|
250
|
+
} else if (droptable === true) {
|
|
251
|
+
count += 1;
|
|
252
|
+
console.log(cstyler.blue("Database: "), cstyler.hex("#00d9ffff")(dbname.loopname), cstyler.blue("Table: "), cstyler.hex("#00d9ffff")(tableName), "- has dropped successfully.");
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
if (count > 0) {
|
|
257
|
+
console.log(cstyler.green("Successfully dropped ", count, " unlisted tables"));
|
|
258
|
+
} else {
|
|
259
|
+
console.log(cstyler.underline("No table found to be dropped"));
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
return true;
|
|
263
|
+
} catch (err) {
|
|
264
|
+
console.error(err.message);
|
|
265
|
+
return null;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
module.exports = {
|
|
271
|
+
createTableifNeeded,
|
|
272
|
+
dropTable
|
|
273
|
+
};
|