Skip to content

Commit

Permalink
db: run creation code only once on start up
Browse files Browse the repository at this point in the history
  • Loading branch information
LoV432 committed Nov 9, 2023
1 parent 5914a1c commit 650400a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 42 deletions.
7 changes: 6 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,18 @@ RUN mkdir .next
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static
COPY ./create-db.js ./create-db.js


COPY ./entrypoint.sh ./entrypoint.sh
RUN chmod +x ./entrypoint.sh

EXPOSE 3000

ENV PORT 3000
# set hostname to localhost
ENV HOSTNAME "0.0.0.0"

CMD ["node", "server.js"]
ENTRYPOINT [ "/app/entrypoint.sh" ]

# TODO: Run as non-root user
52 changes: 52 additions & 0 deletions create-db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const Database = require('better-sqlite3');
const fs = require('fs');

if (!fs.existsSync('./db')) {
fs.mkdirSync('./db');
}
const db = new Database('./db/next-openwrt-stats.db');

// TODO: How to handle DB migrations?

db.transaction(() => {
const createUserTable = db.prepare(`CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
index_number INTEGER,
name TEXT,
display_name TEXT,
ip TEXT,
mac_address TEXT,
last_updated INTEGER,
device_type TEXT,
last_event_type TEXT
)`);
createUserTable.run();

const createConnectionLogsTable =
db.prepare(`CREATE TABLE IF NOT EXISTS connectionlogs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
status TEXT,
time INTEGER
)`);
createConnectionLogsTable.run();

const checkForEmptyConnectionsTable = db
.prepare(`SELECT * FROM connectionlogs`)
.get();
if (!checkForEmptyConnectionsTable) {
db.prepare(
'INSERT INTO connectionlogs (id, status, time) VALUES (?, ?, ?)'
).run(1, 'connected', Date.now());
}

const rpcdTokenTable = db.prepare(`CREATE TABLE IF NOT EXISTS rpcdtoken (
id INTEGER PRIMARY KEY AUTOINCREMENT,
token TEXT
)`);
rpcdTokenTable.run();

const checkToken = db.prepare(`SELECT * FROM rpcdtoken WHERE id = 1`).get();
if (!checkToken) {
db.prepare('INSERT INTO rpcdtoken (token) VALUES (?)').run('0');
}
})();
4 changes: 4 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh

node create-db.js
node server.js
41 changes: 0 additions & 41 deletions lib/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,3 @@ if (!fs.existsSync('./db')) {
fs.mkdirSync('./db');
}
export const db = new Database('./db/next-openwrt-stats.db');

const createUserTable = db.prepare(`CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
index_number INTEGER,
name TEXT,
display_name TEXT,
ip TEXT,
mac_address TEXT,
last_updated INTEGER,
device_type TEXT,
last_event_type TEXT
)`);
createUserTable.run();

const createConnectionLogsTable =
db.prepare(`CREATE TABLE IF NOT EXISTS connectionlogs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
status TEXT,
time INTEGER
)`);
createConnectionLogsTable.run();

const checkForEmptyConnectionsTable = db
.prepare(`SELECT * FROM connectionlogs WHERE id = 1`)
.get();
if (!checkForEmptyConnectionsTable) {
db.prepare(
'INSERT INTO connectionlogs (id, status, time) VALUES (? ,?, ?)'
).run(1, 'connected', Date.now());
}

const rpcdTokenTable = db.prepare(`CREATE TABLE IF NOT EXISTS rpcdtoken (
id INTEGER PRIMARY KEY AUTOINCREMENT,
token TEXT
)`);
rpcdTokenTable.run();

const checkToken = db.prepare(`SELECT * FROM rpcdtoken WHERE id = 1`).get();
if (!checkToken) {
db.prepare('INSERT INTO rpcdtoken (token) VALUES (?)').run('0');
}

0 comments on commit 650400a

Please sign in to comment.