Jack commited on
Commit
dcbdcef
·
1 Parent(s): f3845d0

Fixed login with seeded accounts

Browse files
Files changed (2) hide show
  1. package.json +2 -0
  2. scripts/bootstrap-db.mjs +73 -0
package.json CHANGED
@@ -3,7 +3,9 @@
3
  "version": "0.1.0",
4
  "private": true,
5
  "scripts": {
 
6
  "dev": "next dev",
 
7
  "build": "next build",
8
  "start": "next start",
9
  "lint": "next lint",
 
3
  "version": "0.1.0",
4
  "private": true,
5
  "scripts": {
6
+ "predev": "node scripts/bootstrap-db.mjs",
7
  "dev": "next dev",
8
+ "prestart": "node scripts/bootstrap-db.mjs",
9
  "build": "next build",
10
  "start": "next start",
11
  "lint": "next lint",
scripts/bootstrap-db.mjs ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { readFile } from "node:fs/promises";
2
+ import mysql from "mysql2/promise";
3
+
4
+ const DATABASE_URL = process.env.DATABASE_URL;
5
+ const BOOTSTRAP_SQL_PATH = new URL("../db/init/00-bootstrap.sql", import.meta.url);
6
+ const RETRY_DELAY_MS = 2000;
7
+ const MAX_ATTEMPTS = 30;
8
+
9
+ if (!DATABASE_URL) {
10
+ console.error("DATABASE_URL is required to bootstrap the database.");
11
+ process.exit(1);
12
+ }
13
+
14
+ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
15
+
16
+ async function connectWithRetry() {
17
+ let lastError;
18
+
19
+ for (let attempt = 1; attempt <= MAX_ATTEMPTS; attempt += 1) {
20
+ try {
21
+ const connection = await mysql.createConnection({
22
+ uri: DATABASE_URL,
23
+ multipleStatements: true,
24
+ });
25
+
26
+ await connection.query("SELECT 1");
27
+ return connection;
28
+ } catch (error) {
29
+ lastError = error;
30
+ console.log(
31
+ `Database connection attempt ${attempt}/${MAX_ATTEMPTS} failed. Retrying in ${RETRY_DELAY_MS}ms.`
32
+ );
33
+ await sleep(RETRY_DELAY_MS);
34
+ }
35
+ }
36
+
37
+ throw lastError;
38
+ }
39
+
40
+ async function tableExists(connection, tableName) {
41
+ const [rows] = await connection.query("SHOW TABLES LIKE ?", [tableName]);
42
+ return Array.isArray(rows) && rows.length > 0;
43
+ }
44
+
45
+ function stripUseStatement(sql) {
46
+ return sql.replace(/^\s*USE\s+[^;]+;\s*/i, "");
47
+ }
48
+
49
+ async function bootstrapIfNeeded() {
50
+ const connection = await connectWithRetry();
51
+
52
+ try {
53
+ const hasUsers = await tableExists(connection, "users");
54
+ const hasSessions = await tableExists(connection, "sessions");
55
+ const hasStores = await tableExists(connection, "stores");
56
+
57
+ if (hasUsers && hasSessions && hasStores) {
58
+ console.log("Database bootstrap skipped because required tables already exist.");
59
+ return;
60
+ }
61
+
62
+ const sql = await readFile(BOOTSTRAP_SQL_PATH, "utf8");
63
+ await connection.query(stripUseStatement(sql));
64
+ console.log("Database bootstrap completed from db/init/00-bootstrap.sql.");
65
+ } finally {
66
+ await connection.end();
67
+ }
68
+ }
69
+
70
+ bootstrapIfNeeded().catch((error) => {
71
+ console.error("Database bootstrap failed.", error);
72
+ process.exit(1);
73
+ });