Skip to content

Commit

Permalink
Merge pull request #5 from oslabs-beta/ian/dbtype-setting
Browse files Browse the repository at this point in the history
Ian/dbtype setting
  • Loading branch information
fred-jeong authored Jul 21, 2022
2 parents 5198175 + f33e6d9 commit e9a245c
Show file tree
Hide file tree
Showing 20 changed files with 267 additions and 20,068 deletions.
2 changes: 1 addition & 1 deletion backend/channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ interface Feedback {
}

// Listen for request from front-end and send back the DB List upon request
ipcMain.on('return-db-list', (event, dbType: DBType) => {
ipcMain.on('return-db-list', (event, dbType: DBType = DBType.Postgres) => {
db.getLists(dbType)
.then((data: DBList) => {
event.sender.send('db-lists', data);
Expand Down
62 changes: 50 additions & 12 deletions backend/helperFunctions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable object-shorthand */
import { DBType } from './BE_types';
const { exec } = require('child_process'); // Child_Process: Importing Node.js' child_process API
const { dialog } = require('electron'); // Dialog: display native system dialogs for opening and saving files, alerting, etc
import { DBType } from './BE_types';
// ************************************** CLI COMMANDS & SQL Queries TO CREATE, DELETE, COPY DB SCHEMA, etc. ************************************** //

// Generate SQL queries & CLI commands to be executed in pg and child process respectively
Expand All @@ -22,40 +23,77 @@ interface HelperFunctions {
runHollowCopyFunc: CreateCommand;
promExecute: (cmd: string) => Promise<{ stdout: string; stderr: string }>;
}
const helperFunctions:HelperFunctions = {
// PG = Postgres - Query necessary to run PG Query/Command
// MYSQL = MySQL - Query necessary to run MySQL Query/Command
const helperFunctions: HelperFunctions = {
// create a database
createDBFunc: function (name, dbType: DBType) {
return `CREATE DATABASE "${name}"`;
},

createDBFunc: function (name, dbType: DBType) {
const PG = `CREATE DATABASE "${name}"`;
// might need to use the USE keyword after creating database
const MYSQL = `CREATE DATABASE "${name}"`;
// const mySQLUse = `USE DATABASE "${name}"`;

return dbType === DBType.Postgres ? PG : MYSQL;
// if (dbType === DBType.Postgres) {
// return `CREATE DATABASE "${name}"`;
// }
// if (dbType === DBType.MySQL) {
// return `CREATE DATABASE "${name}"`;
},

// drop provided database
dropDBFunc: function (dbName, dbType: DBType) {
return `DROP DATABASE "${dbName}"`;
const PG = `DROP DATABASE "${dbName}"`;
const MYSQL = `DROP DATABASE "${dbName}"`;

return dbType === DBType.Postgres ? PG : MYSQL;
// return `DROP DATABASE "${dbName}"`;
},

// run explain on query
explainQuery: function (sqlString, dbType: DBType) {
return `BEGIN; EXPLAIN (FORMAT JSON, ANALYZE, VERBOSE, BUFFERS) ${sqlString}; ROLLBACK;`;
const PG = `BEGIN; EXPLAIN (FORMAT JSON, ANALYZE, VERBOSE, BUFFERS) "${sqlString}"; ROLLBACK;`;

// this should work but is limited to only select, update, delete and table statements
const MYSQL = `EXPLAIN FORMAT=JSON ${sqlString}`;

return dbType === DBType.Postgres ? PG : MYSQL;
// return `BEGIN; EXPLAIN (FORMAT JSON, ANALYZE, VERBOSE, BUFFERS) ${sqlString}; ROLLBACK;`;
},

// import SQL file into new DB created
runSQLFunc: function (dbName, file, dbType: DBType){
return `psql -U postgres -d ${dbName} -f "${file}"`;
runSQLFunc: function (dbName, file, dbType: DBType) {
const PG = `psql -U postgres -d "${dbName}" -f "${file}"`;
// need variable to store username. Typed into comamnd line but none of options below worked for me.

const MYSQL = `mysql -u username -p "${dbName}" < "${file}"`;
// -u root -p DATABASENAME < FILETOBEIMPORTED.sql;
// mysql -u root -p"Hello123!" dish < ~/Desktop/mysqlsamp.sql
// SET autocommit=0 ; source d /Users/fryer/Downloads/mysqlsamp.sql ; COMMIT ;

return dbType === DBType.Postgres ? PG : MYSQL;
},

// import TAR file into new DB created
runTARFunc: function (dbName, file, dbType: DBType) {
return `pg_restore -U postgres -d ${dbName} "${file}"`
const PG = `pg_restore -U postgres -d "${dbName}" "${file}"`;
const MYSQL = `mysqldump -u username -p "${dbName}" > "${file}"`;

return dbType === DBType.Postgres ? PG : MYSQL;
},

// make a full copy of the schema
runFullCopyFunc: function (dbCopyName, newFile, dbType: DBType) {
return `pg_dump -U postgres -F p -d ${dbCopyName} > "${newFile}"`
return `pg_dump -U postgres -F p -d "${dbCopyName}" > "${newFile}"`;
},

// make a hollow copy of the schema
runHollowCopyFunc: function (dbCopyName, file, dbType: DBType) {
return `pg_dump -s -U postgres -F p -d ${dbCopyName} > "${file}"`
const PG = `pg_dump -s -U postgres -F p -d "${dbCopyName}" > "${file}"`;
const MYSQL = `mysqldump -h localhost -u root -p --no-data "${dbCopyName}" > "${file}"`;

return dbType === DBType.Postgres ? PG : MYSQL;
},

// promisified execute to execute commands in the child process
Expand Down
1 change: 0 additions & 1 deletion backend/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ fixPath();
// Keep a global reference of the window objects, if you don't, the window will be closed automatically when the JavaScript object is garbage collected.
let mainWindow: BrowserWindow | null;


function createWindow() {
mainWindow = new BrowserWindow({
width: 1800,
Expand Down
Loading

0 comments on commit e9a245c

Please sign in to comment.