improve db
This commit is contained in:
@@ -42,25 +42,42 @@ function ensureDevCerts() {
|
||||
}
|
||||
|
||||
async function globalSetup() {
|
||||
// Ensure dev certificates exist
|
||||
ensureDevCerts()
|
||||
|
||||
// Create test admin user if it doesn't exist
|
||||
const { get, run } = await getDb()
|
||||
const existingUser = await get('SELECT id FROM users WHERE identifier = ?', [TEST_ADMIN.identifier])
|
||||
let retries = 3
|
||||
let lastError = null
|
||||
while (retries > 0) {
|
||||
try {
|
||||
const { get, run } = await getDb()
|
||||
const existingUser = await get('SELECT id FROM users WHERE identifier = ?', [TEST_ADMIN.identifier])
|
||||
|
||||
if (!existingUser) {
|
||||
const id = crypto.randomUUID()
|
||||
const now = new Date().toISOString()
|
||||
await run(
|
||||
'INSERT INTO users (id, identifier, password_hash, role, created_at, auth_provider, oidc_issuer, oidc_sub) VALUES (?, ?, ?, ?, ?, ?, ?, ?)',
|
||||
[id, TEST_ADMIN.identifier, hashPassword(TEST_ADMIN.password), TEST_ADMIN.role, now, 'local', null, null],
|
||||
)
|
||||
console.log(`[test] Created test admin user: ${TEST_ADMIN.identifier}`)
|
||||
}
|
||||
else {
|
||||
console.log(`[test] Test admin user already exists: ${TEST_ADMIN.identifier}`)
|
||||
if (!existingUser) {
|
||||
const id = crypto.randomUUID()
|
||||
const now = new Date().toISOString()
|
||||
await run(
|
||||
'INSERT INTO users (id, identifier, password_hash, role, created_at, auth_provider, oidc_issuer, oidc_sub) VALUES (?, ?, ?, ?, ?, ?, ?, ?)',
|
||||
[id, TEST_ADMIN.identifier, hashPassword(TEST_ADMIN.password), TEST_ADMIN.role, now, 'local', null, null],
|
||||
)
|
||||
console.log(`[test] Created test admin user: ${TEST_ADMIN.identifier}`)
|
||||
}
|
||||
else {
|
||||
console.log(`[test] Test admin user already exists: ${TEST_ADMIN.identifier}`)
|
||||
}
|
||||
return
|
||||
}
|
||||
catch (error) {
|
||||
lastError = error
|
||||
if (error.message?.includes('SQLITE_BUSY') || error.message?.includes('database is locked')) {
|
||||
retries--
|
||||
if (retries > 0) {
|
||||
await new Promise(resolve => setTimeout(resolve, 100 * (4 - retries)))
|
||||
continue
|
||||
}
|
||||
}
|
||||
throw error
|
||||
}
|
||||
}
|
||||
throw lastError
|
||||
}
|
||||
|
||||
export default globalSetup
|
||||
|
||||
Reference in New Issue
Block a user