-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate.js
31 lines (29 loc) · 857 Bytes
/
migrate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import 'dotenv/config';
import fs from 'node:fs';
import mysql from 'mysql2/promise';
const migrate = async () =>
{
const { DB_HOST, DB_PORT, DB_USER, DB_PASSWORD, DB_NAME } = process.env,
connection = await mysql.createConnection
({
host: DB_HOST,
port: DB_PORT,
user: DB_USER,
password: DB_PASSWORD,
multipleStatements: true
}),
sql = fs.readFileSync('./database.sql', 'utf-8');
await connection.query(`DROP DATABASE IF EXISTS ${DB_NAME}`);
await connection.query(`CREATE DATABASE ${DB_NAME}`);
await connection.query(`USE ${DB_NAME}`);
await connection.query(sql);
connection.end();
};
try
{
migrate();
}
catch(err)
{
console.error(err);
}