dbtasker 2.5.1 → 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/dbop.js CHANGED
@@ -33,7 +33,7 @@ async function alterDatabaseCharsetCollate(config, databaseName, characterSet, c
33
33
  `;
34
34
 
35
35
  await connection.query(query);
36
- console.log(`${cstyler.purple('Database:')} '${cstyler.blue(databaseName)}' character set changed to '${cstyler.yellow(characterSet)}' and collation to '${cstyler.yellow(collate)}'.`);
36
+ console.log(`${cstyler.blue('Database:')} '${cstyler.hex("#00d9ffff")(databaseName)}' character set changed to '${cstyler.yellow(characterSet)}' and collation to '${cstyler.yellow(collate)}'.`);
37
37
  return true;
38
38
  } catch (err) {
39
39
  console.error("Error altering database:", err.message);
@@ -73,7 +73,7 @@ async function createDatabase(config, databaseName, characterSet = null, collate
73
73
  await connection.query(query);
74
74
 
75
75
  console.log(
76
- `${cstyler.purple('Database:')} '${cstyler.blue(databaseName)}'` +
76
+ `${cstyler.blue('Database:')} '${cstyler.hex("#00d9ffff")(databaseName)}'` +
77
77
  (characterSet ? ` created with CHARACTER SET '${cstyler.yellow(characterSet)}'` : "") +
78
78
  (collate ? ` and COLLATE '${cstyler.yellow(collate)}'` : "")
79
79
  );
@@ -85,42 +85,11 @@ async function createDatabase(config, databaseName, characterSet = null, collate
85
85
  if (connection) await connection.end();
86
86
  }
87
87
  }
88
- async function dropTable(config, json_data, seperator = "_") {
89
- try {
90
- console.log("Starting dropping the table.");
91
- for (const jsondb of Object.keys(json_data)) {
92
- let dbname = fncs.perseDatabaseNameWithLoop(jsondb, seperator);
93
- if (dbname === false) {
94
- console.error("There must be some mistake. Please re install the module.");
95
- }
96
- const alltables = await fncs.getTableNames(config, dbname.loopname);
97
- if (alltables === null) {
98
- console.error("Having problem getting all the table name of the Database: ", cstyler.yellow(dbname.loopname), ". Please re-install the module.");
99
- return null;
100
- }
101
- let tables = {};
102
- for (const tableName of (alltables)) {
103
- const revlpnm = fncs.reverseLoopName(tableName);
104
- if (!Object.keys(json_data[jsondb]).includes(revlpnm[0]) && !Object.keys(json_data[jsondb]).includes(revlpnm[1])) {
105
- const droptable = await fncs.dropTable(config, dbname.loopname, tableName);
106
- if (droptable === null) {
107
- console.error("Having problem dropping table. Please check database connection.");
108
- return null;
109
- }
110
- console.log(cstyler.purple("Database: "), cstyler.blue(dbname.loopname), cstyler.purple("Table: "), cstyler.blue(tableName), "- has dropped successfully.")
111
- }
112
- }
113
- console.log(cstyler.green("Successfully dropped all unlisted tables."));
114
- return true;
115
- }
116
- } catch (err) {
117
- console.error(err.message);
118
- return null;
119
- }
120
- }
121
- async function databaseAddDeleteAlter(allconfig, jsondata, dropdb = false, donttouchdb = [], seperator = "_") {
88
+
89
+ async function databaseAddDeleteAlter(allconfig, jsondata, dropdb = false, donttouchdb = [], separator = "_") {
122
90
  try {
123
91
  // lets add databases and drop databases
92
+ console.log(cstyler.bold.yellow("Starting database add/drop/alter process..."));
124
93
  let config;
125
94
  if (fncs.isValidMySQLConfig(allconfig)) {
126
95
  config = { "port": allconfig.port, "host": allconfig.host, "user": allconfig.user, "password": allconfig.password }
@@ -137,7 +106,7 @@ async function databaseAddDeleteAlter(allconfig, jsondata, dropdb = false, dontt
137
106
  // Lets add databases
138
107
  for (const jsondb of jsondbnames) {
139
108
  let data = {};
140
- data.name = fncs.perseDatabaseNameWithLoop(jsondb, seperator).loopname;
109
+ data.name = fncs.perseDatabaseNameWithLoop(jsondb, separator).loopname;
141
110
  if (fncs.isJsonObject(jsondata[jsondb])) {
142
111
  if (jsondata[jsondb].hasOwnProperty("_collate_")) {
143
112
  data.collate = jsondata[jsondb]._collate_;
@@ -155,14 +124,14 @@ async function databaseAddDeleteAlter(allconfig, jsondata, dropdb = false, dontt
155
124
  }
156
125
  if (avldblist.includes(data.name)) {
157
126
  // Let's Alter database if needed
158
- console.log(cstyler.purple("Database Name: "), cstyler.blue(data.name), cstyler.green(" is exist. Checking for charactar set and collate configuration"));
127
+ console.log(cstyler.blue("Database Name: "), cstyler.hex("#00d9ffff")(data.name), " is exist. Checking for charactar set and collate configuration");
159
128
  const dbdetails = await fncs.getDatabaseCharsetAndCollation(config, data.name);
160
129
  if (!fncs.isJsonObject(dbdetails)) {
161
130
  console.error(cstyler.bold("Having problem getting database character set and collate."));
162
131
  return null;
163
132
  } else {
164
133
  if ((data.charset === null || dbdetails.characterSet === data.charset) && (data.collate === null || dbdetails.collation === data.collate)) {
165
- console.log(cstyler.purple("Database: "), cstyler.blue(data.name), " no changes needed.");
134
+ console.log(cstyler.blue("Database: "), cstyler.hex("#00d9ffff")(data.name), " no changes needed.");
166
135
  } else {
167
136
  // lets alter the database charset and collate
168
137
  if (data.charset === null) {
@@ -180,14 +149,14 @@ async function databaseAddDeleteAlter(allconfig, jsondata, dropdb = false, dontt
180
149
 
181
150
  } else {
182
151
  // Let's Create database
183
- console.log(cstyler.purple("Database Name: "), cstyler.blue(data.name), " do not exist.");
152
+ console.log(cstyler.blue("Database Name: "), cstyler.hex("#00d9ffff")(data.name), " do not exist.");
184
153
  console.log("Lets create Database: ", cstyler.yellow(jsondb));
185
154
  const createdb = await createDatabase(config, data.name, data.charset, data.collate);
186
155
  if (createdb === true) {
187
- console.log(cstyler.purple("Database Name: "), cstyler.blue(jsondb), cstyler.green(" have created successfully"));
156
+ console.log(cstyler.blue("Database Name: "), cstyler.hex("#00d9ffff")(jsondb), cstyler.green(" have created successfully"));
188
157
  } else if (createdb === false) {
189
- console.error("Trying to create this ", cstyler.blue(jsondb), " database when it do not exist on the list all existing database. But when creating server says it already exist. There must be a database problem.");
190
- console.log(cstyler.purple("All available Database names are: "), cstyler.blue(avldblist.join(", ")));
158
+ console.error("Trying to create this ", cstyler.hex("#00d9ffff")(jsondb), " database when it do not exist on the list all existing database. But when creating server says it already exist. There must be a database problem.");
159
+ console.log(cstyler.blue("All available Database names are: "), cstyler.hex("#00d9ffff")(avldblist.join(", ")));
191
160
  return null;
192
161
  } else {
193
162
  return null;
@@ -197,6 +166,7 @@ async function databaseAddDeleteAlter(allconfig, jsondata, dropdb = false, dontt
197
166
  // Lets drop database
198
167
  if (dropdb) {
199
168
  // Lets get all database name
169
+ console.log(cstyler.bold.yellow("Initiating dropping the databases"));
200
170
  const avldblist = await fncs.getAllDatabaseNames(config);
201
171
  if (!Array.isArray(avldblist)) {
202
172
  console.error(cstyler.red.bold("There is a problem connecting to the database. Please check database info or connection."));
@@ -205,23 +175,44 @@ async function databaseAddDeleteAlter(allconfig, jsondata, dropdb = false, dontt
205
175
  // Let's arrange database names
206
176
  let arrngdbnms = {};
207
177
  for (const dbnms of avldblist) {
208
- if ([...defaultdb, ...donttouchdb].includes(dbnms)) { continue }
178
+ if ([...defaultdb, ...donttouchdb].includes(dbnms)) {
179
+ continue;
180
+ }
209
181
  const getrev = fncs.reverseLoopName(dbnms);
210
- if (arrngdbnms.hasOwnProperty(getrev)[0]) {
211
- arrngdbnms[getrev[0]].push(dbnms);
212
- } else if (arrngdbnms.hasOwnProperty(getrev)[1]) {
213
- arrngdbnms[getrev[1]].push(dbnms);
182
+ if (Array.isArray(getrev)) {
183
+ for(const item of getrev) {
184
+ if(arrngdbnms.hasOwnProperty(item)) {
185
+ arrngdbnms[item].push(dbnms);
186
+ break;
187
+ }
188
+ }
214
189
  } else {
215
190
  arrngdbnms[getrev] = [dbnms];
216
191
  }
217
192
  }
193
+ console.log("Lets start dropping");
194
+ let count = 0;
218
195
  for (const databaseName of Object.keys(arrngdbnms)) {
219
196
  if (!jsondbnames.includes(databaseName)) {
220
197
  for (const items of arrngdbnms[databaseName]) {
221
- await fncs.dropDatabase(config, items);
198
+ console.log('dropping now', items);
199
+ const isdropdb = await fncs.dropDatabase(config, items);
200
+ if (isdropdb) {
201
+ count += 1;
202
+ console.log(`${cstyler.blue('Database')} '${cstyler.yellow(databaseName)}' ${cstyler.red('dropped')} ${cstyler.green('successfully.')}`);
203
+ } else if (isdropdb === false) {
204
+ console.log(`${cstyler.blue('Database')} '${cstyler.hex("#00d9ffff")(databaseName)}' does not exist.`);
205
+ } else if (isdropdb === null) {
206
+ return null;
207
+ }
222
208
  }
223
209
  }
224
210
  }
211
+ if (count > 0) {
212
+ console.log(cstyler.green("All useless database have dropped"));
213
+ } else {
214
+ console.log(cstyler.green("No database found to be dropped"));
215
+ }
225
216
  }
226
217
  return true;
227
218
  } catch (err) {
@@ -231,6 +222,5 @@ async function databaseAddDeleteAlter(allconfig, jsondata, dropdb = false, dontt
231
222
  }
232
223
 
233
224
  module.exports = {
234
- databaseAddDeleteAlter,
235
- dropTable
225
+ databaseAddDeleteAlter
236
226
  }
package/dropcolumn.js ADDED
@@ -0,0 +1,174 @@
1
+ const fncs = require("./function");
2
+ const cstyler = require("cstyler");
3
+
4
+
5
+
6
+
7
+
8
+
9
+ async function dropcolumn(config, tableJson, forceDropColumn, separator = "_") {
10
+ try {
11
+ console.log(cstyler.bold.yellow("Initiating drop column operation"));
12
+
13
+ if (!fncs.isJsonObject(tableJson)) {
14
+ return false;
15
+ }
16
+
17
+ for (const jsDb of Object.keys(tableJson)) {
18
+ const parsedDb = fncs.perseDatabaseNameWithLoop(jsDb, separator);
19
+ if (!parsedDb) {
20
+ console.error(
21
+ cstyler.bold.red("Cannot parse database name."),
22
+ jsDb,
23
+ parsedDb
24
+ );
25
+ return null;
26
+ }
27
+
28
+ const databaseName = parsedDb.loopname;
29
+ config.database = databaseName;
30
+
31
+ for (const jsTable of Object.keys(tableJson[jsDb])) {
32
+ const tableDef = tableJson[jsDb][jsTable];
33
+ if (!fncs.isJsonObject(tableDef)) continue;
34
+
35
+ const parsedTable = fncs.perseTableNameWithLoop(jsTable, separator);
36
+ if (!parsedTable) {
37
+ console.error(
38
+ cstyler.bold.red("Cannot parse table name."),
39
+ jsTable
40
+ );
41
+ return null;
42
+ }
43
+
44
+ const tableName = parsedTable.loopname;
45
+
46
+ const existingColumns = await fncs.getColumnNames(
47
+ config,
48
+ databaseName,
49
+ tableName
50
+ );
51
+
52
+ if (!existingColumns) {
53
+ console.error(
54
+ cstyler.bold.red("Failed to fetch columns for table:"),
55
+ cstyler.hex("#00d9ffff")(tableName),
56
+ cstyler.bold.red("from database:"),
57
+ cstyler.hex("#00d9ffff")(databaseName)
58
+ );
59
+ return null;
60
+ }
61
+
62
+ for (const column of existingColumns) {
63
+ const definedInJson = Object.prototype.hasOwnProperty.call(
64
+ tableDef,
65
+ column
66
+ );
67
+
68
+ const isValidDefinition =
69
+ definedInJson && fncs.isJsonObject(tableDef[column]);
70
+
71
+ if (isValidDefinition) continue;
72
+
73
+ console.log(
74
+ cstyler.bold.purple("Database:"),
75
+ cstyler.hex("#00d9ffff")(databaseName),
76
+ cstyler.bold.purple("Table:"),
77
+ cstyler.hex("#00d9ffff")(tableName),
78
+ cstyler.bold.yellow("Dropping column:"),
79
+ cstyler.yellow(column)
80
+ );
81
+
82
+ const referencingColumns =
83
+ await fncs.findReferencingFromColumns(
84
+ config,
85
+ databaseName,
86
+ tableName,
87
+ column
88
+ );
89
+
90
+ if (referencingColumns === null) {
91
+ console.error(
92
+ cstyler.bold.red("Failed to resolve FK references for column:"),
93
+ cstyler.hex("#00d9ffff")(column)
94
+ );
95
+ return null;
96
+ }
97
+
98
+ if (referencingColumns.length > 0) {
99
+ if (!forceDropColumn) {
100
+ console.error(
101
+ cstyler.bold.red("Column is referenced by other tables:"),
102
+ cstyler.hex("#00d9ffff")(column),
103
+ cstyler.bold.red("Enable forceDropColumn to proceed.")
104
+ );
105
+ continue;
106
+ }
107
+
108
+ let allFkRemoved = true;
109
+
110
+ for (const ref of referencingColumns) {
111
+ const removed = await fncs.removeForeignKeyFromColumn(
112
+ config,
113
+ ref.child_schema,
114
+ ref.child_table,
115
+ ref.child_columns[0]
116
+ );
117
+
118
+ if (removed === null) {
119
+ console.error(
120
+ cstyler.bold.red("Failed to drop FK from:"),
121
+ cstyler.hex("#00d9ffff")(ref.child_table),
122
+ cstyler.bold.red("column:"),
123
+ cstyler.hex("#00d9ffff")(ref.child_columns[0])
124
+ );
125
+ allFkRemoved = false;
126
+ }
127
+ }
128
+
129
+ if (!allFkRemoved) {
130
+ console.error(
131
+ cstyler.bold.red("Aborting column drop due to FK failures:"),
132
+ cstyler.hex("#00d9ffff")(column)
133
+ );
134
+ continue;
135
+ }
136
+ }
137
+
138
+ const dropped = await fncs.dropColumn(
139
+ config,
140
+ databaseName,
141
+ tableName,
142
+ column
143
+ );
144
+
145
+ if (dropped === null) {
146
+ console.error(
147
+ cstyler.bold.red("Failed to drop column:"),
148
+ cstyler.hex("#00d9ffff")(column)
149
+ );
150
+ return null;
151
+ }
152
+
153
+ console.log(
154
+ cstyler.bold.green("Successfully dropped column:"),
155
+ cstyler.hex("#00d9ffff")(column),
156
+ cstyler.bold.green("from table:"),
157
+ cstyler.hex("#00d9ffff")(tableName)
158
+ );
159
+ }
160
+ }
161
+ }
162
+
163
+ return true;
164
+ } catch (err) {
165
+ console.error(err?.message || err);
166
+ return null;
167
+ }
168
+ }
169
+
170
+
171
+
172
+ module.exports = {
173
+ dropcolumn
174
+ }