fix(auth): singleflight access schema initialization (#1953)

This commit is contained in:
Lyon
2026-06-23 06:33:07 +08:00
committed by GitHub
parent 6e68cacd9b
commit bcb3a4d02e
+17 -2
View File
@@ -175,6 +175,11 @@ class AccessController {
this.openFga = openFgaAuthorizer ?? createOpenFgaAuthorizer({ env, fetchImpl, now, configStore: store });
this.userBilling = userBillingClient ?? createUserBillingClient({ env, fetchImpl });
this.bootstrapAttempted = false;
if (typeof this.store?.ensureSchema === "function") {
this.store.ensureSchema().catch((error) => {
console.warn("access store schema warmup failed", error?.message ?? error);
});
}
}
configureCodeAgentWorkspaceContext({ env = null, fetchImpl = null, traceStore = null, codeAgentChatResults = null } = {}) {
@@ -1370,12 +1375,22 @@ class PostgresAccessStore extends MemoryAccessStore {
super({ now });
this.query = query;
this.schemaReady = false;
this.schemaReadyPromise = null;
}
async ensureSchema() {
if (this.schemaReady) return;
for (const sql of ACCESS_SCHEMA_STATEMENTS) await this.query(sql, []);
this.schemaReady = true;
if (!this.schemaReadyPromise) {
this.schemaReadyPromise = (async () => {
for (const sql of ACCESS_SCHEMA_STATEMENTS) await this.query(sql, []);
this.schemaReady = true;
})();
}
try {
await this.schemaReadyPromise;
} finally {
if (!this.schemaReady) this.schemaReadyPromise = null;
}
}
async countUsers() { await this.ensureSchema(); const result = await this.query("SELECT COUNT(*)::int AS count FROM users", []); return Number(result.rows?.[0]?.count ?? 0); }