You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.4 KiB

1 year ago
#!/usr/bin/node
1 year ago
import mysql from 'mysql2/promise'
import { input, password } from '@inquirer/prompts'
1 year ago
import chalk from 'chalk'
const log = console.log
1 year ago
/**
* mariadb-drop.js
*
* create database and generate name, user, password
* and grant this user single priveleges
*
*
*/
1 year ago
const user = {
1 year ago
name: 'root',
1 year ago
password: undefined
}
1 year ago
log(chalk.cyan('Mariadb drop Database...'))
user.password = await password({
message: 'Enter Root Password:',
mask: '*',
1 year ago
async validate(value) {
if (!value) {
return 'Required!'
}
return true
}
})
1 year ago
const database = await input({
message: 'Database:',
1 year ago
async validate(value) {
if (!value) {
return 'Required!'
}
return true
}
})
// create connection, generate name for db and user, and generate password
const connection = await mysql.createConnection({
host: 'localhost',
user: user.name,
password: user.password
})
1 year ago
// drop database
1 year ago
1 year ago
const [results ] = await connection.query("SELECT User FROM mysql.db WHERE Db = '" + database + "'")
1 year ago
1 year ago
results.forEach(async (entity) => {
await connection.query("DROP USER IF EXISTS '" + entity.User + "'@localhost");
await connection.query("DROP USER IS EXISTS '" + entity.User + "'@'%'");
1 year ago
})
1 year ago
await connection.query("DROP DATABASE IF EXISTS " + database);
1 year ago
connection.destroy()
1 year ago
log(chalk.green('Database ' + database + ' droped!'))