Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ RUN set -x \
&& npm install -g pnpm

# Script dependencies
RUN pnpm --allow-build='@prisma/engines' add npm-run-all dotenv chalk semver \
RUN pnpm --allow-build='@prisma/engines' add npm-run-all dotenv chalk semver bcryptjs \
prisma@${PRISMA_VERSION} \
@prisma/adapter-pg@${PRISMA_VERSION}

Expand Down
22 changes: 21 additions & 1 deletion scripts/check-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import 'dotenv/config';
import { execSync } from 'node:child_process';
import { PrismaPg } from '@prisma/adapter-pg';
import bcrypt from 'bcryptjs';
import chalk from 'chalk';
import semver from 'semver';
import { PrismaClient } from '../generated/prisma/client.js';
Expand Down Expand Up @@ -73,9 +74,28 @@ async function applyMigration() {
}
}

async function createDefaultUser() {
const username = process.env.DEFAULT_ADMIN_USERNAME;
const password = process.env.DEFAULT_ADMIN_PASSWORD;

if (!username && !password) {
return;
}

const admin = await prisma.user.findFirst({ where: { role: 'admin' } });

if (admin) {
const data = {};
if (username) data.username = username;
if (password) data.password = bcrypt.hashSync(password, 10);
await prisma.user.update({ where: { id: admin.id }, data });
success('Admin user credentials updated.');
}
}

(async () => {
let err = false;
for (const fn of [checkEnv, checkConnection, checkDatabaseVersion, applyMigration]) {
for (const fn of [checkEnv, checkConnection, checkDatabaseVersion, applyMigration, createDefaultUser]) {
try {
await fn();
} catch (e) {
Expand Down