🐛 Database error with readonly mappings (#1420)

* 🐛 Database error with readonly mappings

* ♻️ PR feedback
This commit is contained in:
Manuel
2023-10-08 11:45:30 +02:00
committed by GitHub
parent a6be5fa380
commit a7655b6348

View File

@@ -1,3 +1,5 @@
import Consola from 'consola';
import fs from 'fs/promises';
import { NextRequest, NextResponse } from 'next/server';
import { env } from 'process';
@@ -34,13 +36,32 @@ export async function middleware(req: NextRequest) {
return NextResponse.next();
}
// is only called from when there were no users in the database in this session (Since the app started)
cachedUserCount = await client.user.count.query();
// Do not redirect if there are users in the database
if (cachedUserCount > 0) {
if (!(await shouldRedirectToOnboard())) {
return NextResponse.next();
}
return NextResponse.redirect(getUrl(req) + '/onboard');
}
const shouldRedirectToOnboard = async (): Promise<boolean> => {
const cacheAndGetUserCount = async () => {
cachedUserCount = await client.user.count.query();
return cachedUserCount === 0;
}
if (!env.DATABASE_URL?.startsWith('file:')) {
return await cacheAndGetUserCount();
}
const fileUri = env.DATABASE_URL.substring(4);
try {
await fs.access(fileUri, fs.constants.W_OK);
return await cacheAndGetUserCount();
} catch {
Consola.warn(
`detected that the path ${fileUri} was not readable. Showing onboarding page for setup...`
);
return true;
}
};