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

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