refactor: remove all SQLite code, standardize on PostgreSQL
- Remove database provider switching logic from prisma.ts and auth.ts - Hardcode PostgreSQL as the only supported database - Remove switch-database.js and use-dev-db.js scripts - Remove Python utility scripts that used sqlite3 directly - Update justfile to remove SQLite test targets - Update package.json to remove db:switch script - Update Dockerfile.ci-base to default to PostgreSQL - Update .env.example to remove SQLite mention - Update playwright.config.ts comments - Update .gitignore to remove SQLite file patterns This eliminates the root cause of test failures: the dev server and test Prisma client were using different databases (PostgreSQL vs SQLite).
This commit is contained in:
+14
-28
@@ -3,42 +3,34 @@ import { prismaAdapter } from "better-auth/adapters/prisma";
|
||||
import { prisma } from "./prisma";
|
||||
import { testUtils } from "better-auth/plugins";
|
||||
|
||||
// Detect database provider from environment
|
||||
const databaseProvider = process.env.DATABASE_PROVIDER || "sqlite";
|
||||
|
||||
export const auth = betterAuth({
|
||||
database: prismaAdapter(prisma, {
|
||||
provider: databaseProvider as "sqlite" | "postgresql",
|
||||
provider: "postgresql",
|
||||
}),
|
||||
emailAndPassword: {
|
||||
enabled: true,
|
||||
autoSignIn: true, // Automatically sign in after registration
|
||||
requireEmailVerification: false, // Don't require email verification for tests
|
||||
minPasswordLength: 8, // Set minimum password length
|
||||
maxPasswordLength: 128, // Set maximum password length
|
||||
autoSignIn: true,
|
||||
requireEmailVerification: false,
|
||||
minPasswordLength: 8,
|
||||
maxPasswordLength: 128,
|
||||
},
|
||||
secret: process.env.BETTER_AUTH_SECRET || process.env.NEXTAUTH_SECRET,
|
||||
baseURL: process.env.BETTER_AUTH_URL || process.env.NEXTAUTH_URL || "http://localhost:3000/api/auth",
|
||||
// Configure trusted origins - parse from environment or use defaults
|
||||
trustedOrigins: (() => {
|
||||
const origins = [];
|
||||
|
||||
// Add environment-specified origins
|
||||
|
||||
if (process.env.TRUSTED_ORIGINS) {
|
||||
origins.push(...process.env.TRUSTED_ORIGINS.split(',').map(o => o.trim()));
|
||||
}
|
||||
|
||||
// Add BETTER_AUTH_URL if set
|
||||
|
||||
if (process.env.BETTER_AUTH_URL) {
|
||||
origins.push(process.env.BETTER_AUTH_URL);
|
||||
}
|
||||
|
||||
// Add NEXTAUTH_URL if set
|
||||
|
||||
if (process.env.NEXTAUTH_URL) {
|
||||
origins.push(process.env.NEXTAUTH_URL);
|
||||
}
|
||||
|
||||
// Add defaults
|
||||
|
||||
origins.push(
|
||||
"https://euchre.notsosm.art",
|
||||
"http://euchre.notsosm.art",
|
||||
@@ -47,17 +39,14 @@ export const auth = betterAuth({
|
||||
"http://127.0.0.1:3000",
|
||||
"http://0.0.0.0:3000"
|
||||
);
|
||||
|
||||
// Remove duplicates and empty strings
|
||||
|
||||
return [...new Set(origins.filter(o => o))];
|
||||
})(),
|
||||
session: {
|
||||
cookieCache: {
|
||||
enabled: false, // Disable cookie cache to avoid session cache issues
|
||||
enabled: false,
|
||||
},
|
||||
},
|
||||
// Configure rate limiting - disable for test environment
|
||||
// Note: Rate limiting is disabled for all environments to ensure test reliability
|
||||
rateLimit: {
|
||||
enabled: false,
|
||||
},
|
||||
@@ -66,21 +55,18 @@ export const auth = betterAuth({
|
||||
user: {
|
||||
create: {
|
||||
async after(user) {
|
||||
// Generate a unique player name using timestamp and random string
|
||||
const timestamp = Date.now();
|
||||
const randomId = Math.random().toString(36).substring(2, 8);
|
||||
const baseName = user.name || user.email.split('@')[0];
|
||||
const uniqueName = `${baseName}-${timestamp}-${randomId}`;
|
||||
|
||||
// Create a Player record for the new user
|
||||
|
||||
const newPlayer = await prisma.player.create({
|
||||
data: {
|
||||
name: uniqueName,
|
||||
normalizedName: uniqueName.toLowerCase(),
|
||||
},
|
||||
});
|
||||
|
||||
// Update the User with the playerId
|
||||
|
||||
await prisma.user.update({
|
||||
where: { id: user.id },
|
||||
data: { playerId: newPlayer.id },
|
||||
@@ -93,4 +79,4 @@ export const auth = betterAuth({
|
||||
plugins: [
|
||||
testUtils()
|
||||
]
|
||||
});
|
||||
});
|
||||
|
||||
+7
-25
@@ -1,37 +1,19 @@
|
||||
import { PrismaClient } from '@prisma/client'
|
||||
import { PrismaPg } from '@prisma/adapter-pg'
|
||||
|
||||
const globalForPrisma = globalThis as unknown as {
|
||||
prisma: PrismaClient | undefined
|
||||
}
|
||||
|
||||
// Detect database provider from environment (default to sqlite for local development)
|
||||
// Next.js automatically loads environment variables from .env, .env.development, .env.production
|
||||
const databaseProvider = process.env.DATABASE_PROVIDER || 'sqlite'
|
||||
const databaseUrl = process.env.DATABASE_URL
|
||||
|
||||
// Create PrismaClient with appropriate adapter
|
||||
if (!databaseUrl) {
|
||||
throw new Error('DATABASE_URL environment variable is required.')
|
||||
}
|
||||
|
||||
const createPrismaClient = () => {
|
||||
let client: PrismaClient
|
||||
|
||||
if (databaseProvider === 'postgresql') {
|
||||
// Validate DATABASE_URL is present for PostgreSQL
|
||||
if (!databaseUrl) {
|
||||
throw new Error(
|
||||
'DATABASE_URL environment variable is required when DATABASE_PROVIDER is set to postgresql. ' +
|
||||
'Current DATABASE_PROVIDER: ' + databaseProvider
|
||||
)
|
||||
}
|
||||
|
||||
// Use PrismaPg adapter for PostgreSQL
|
||||
const { PrismaPg } = require('@prisma/adapter-pg')
|
||||
const adapter = new PrismaPg({ connectionString: databaseUrl })
|
||||
client = new PrismaClient({ adapter })
|
||||
} else {
|
||||
// No adapter needed for SQLite
|
||||
client = new PrismaClient()
|
||||
}
|
||||
|
||||
return client
|
||||
const adapter = new PrismaPg({ connectionString: databaseUrl })
|
||||
return new PrismaClient({ adapter })
|
||||
}
|
||||
|
||||
export const prisma = globalForPrisma.prisma ?? createPrismaClient()
|
||||
|
||||
Reference in New Issue
Block a user