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

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