Skip to content

Commit

Permalink
Make /api/init drop any existing database if it exists.
Browse files Browse the repository at this point in the history
  • Loading branch information
aboodman committed Mar 30, 2021
1 parent 9db0f49 commit f2aa0a0
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 17 deletions.
17 changes: 2 additions & 15 deletions backend/rds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,9 @@ export async function transact(body: TransactionBodyFn) {
);
}

/**
* Creates our databse in RDS if necessary.
*/
export async function ensureDatabase() {
const result = await executeStatementInDatabase(null, "SHOW DATABASES");
if (result.records) {
if (result.records.find((record) => record[0].stringValue === dbName)) {
// TODO: Maybe migrate version
return;
}
}

await createDatabase();
}
export async function createDatabase() {
await executeStatementInDatabase(null, `DROP DATABASE IF EXISTS ${dbName}`);

async function createDatabase() {
await executeStatementInDatabase(
null,
`CREATE DATABASE ${dbName}
Expand Down
4 changes: 2 additions & 2 deletions pages/api/init.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ensureDatabase } from "../../backend/rds";
import { createDatabase } from "../../backend/rds";
import type { NextApiRequest, NextApiResponse } from "next";

export default async (req: NextApiRequest, res: NextApiResponse) => {
await ensureDatabase();
await createDatabase();
res.end("OK");
};

0 comments on commit f2aa0a0

Please sign in to comment.