2020-11-24 01:13:09 +01:00
|
|
|
const path = require('path')
|
|
|
|
const envPath = path.resolve(process.cwd(), '.env.local')
|
|
|
|
|
|
|
|
console.log({ envPath })
|
|
|
|
|
|
|
|
require('dotenv').config({ path: envPath })
|
|
|
|
|
|
|
|
const mysql = require('serverless-mysql')
|
|
|
|
|
|
|
|
const db = mysql({
|
|
|
|
config: {
|
|
|
|
host: process.env.MYSQL_HOST,
|
|
|
|
database: process.env.MYSQL_DATABASE,
|
|
|
|
user: process.env.MYSQL_USERNAME,
|
|
|
|
password: process.env.MYSQL_PASSWORD,
|
2021-02-22 14:32:23 +01:00
|
|
|
port: process.env.MYSQL_PORT,
|
2020-11-24 01:13:09 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
async function query(q) {
|
|
|
|
try {
|
|
|
|
const results = await db.query(q)
|
|
|
|
await db.end()
|
|
|
|
return results
|
|
|
|
} catch (e) {
|
|
|
|
throw Error(e.message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create "entries" table if doesn't exist
|
|
|
|
async function migrate() {
|
|
|
|
try {
|
|
|
|
await query(`
|
|
|
|
CREATE TABLE IF NOT EXISTS entries (
|
|
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
|
|
title TEXT NOT NULL,
|
|
|
|
content TEXT NOT NULL,
|
|
|
|
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
updated_at
|
|
|
|
TIMESTAMP
|
|
|
|
NOT NULL
|
|
|
|
DEFAULT CURRENT_TIMESTAMP
|
|
|
|
ON UPDATE CURRENT_TIMESTAMP
|
|
|
|
)
|
|
|
|
`)
|
|
|
|
console.log('migration ran successfully')
|
|
|
|
} catch (e) {
|
|
|
|
console.error('could not run migration, double check your credentials.')
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
migrate().then(() => process.exit())
|