Skip to content
Merged
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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"format:typ": "typstyle -i career",
"format:typ:check": "typstyle --check career",
"db:generate": "drizzle-kit generate",
"db:migrate": "drizzle-kit migrate && bun scripts/migrate-daily-history.ts",
"db:migrate": "drizzle-kit migrate",
"db:studio": "drizzle-kit studio",
"codex:backfill": "bun scripts/backfill-codex.ts",
"codex:account": "bun scripts/configure-codex-account.ts",
Expand Down
52 changes: 0 additions & 52 deletions scripts/migrate-daily-history.ts

This file was deleted.

11 changes: 10 additions & 1 deletion src/db/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ export class DatabaseConfigurationError extends Error {
let client: ReturnType<typeof postgres> | null = null;
let database: ReturnType<typeof drizzle<typeof schema>> | null = null;

export const databaseConnectionUrl = (value: string) => {
const url = new URL(value);
// Postgres.js pipelines concurrent queries; Supavisor transaction mode can hang them.
if (url.hostname.endsWith(".pooler.supabase.com") && url.port === "6543") {
url.port = "5432";
}
return url.toString();
};

const readDatabaseUrl = () => {
const value = env.DATABASE_URL?.trim();
return value === undefined || value.length === 0 ? null : value;
Expand All @@ -31,7 +40,7 @@ export const getDatabase = () => {
throw new DatabaseConfigurationError();
}

client = postgres(databaseUrl, {
client = postgres(databaseConnectionUrl(databaseUrl), {
// Keep repeated outages from adding up to 20 seconds before each reconnect.
backoff: () => 1,
connect_timeout: 10,
Expand Down
5 changes: 3 additions & 2 deletions src/lib/codex/public-stats.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import "server-only";
import { and, gte, lt } from "drizzle-orm";
import { unstable_cache } from "next/cache";
import { cache } from "react";

import { tokenPreferences } from "@/content/tokens";
import { getDatabase, isDatabaseConfigured } from "@/db/client";
Expand Down Expand Up @@ -84,7 +85,7 @@ const readPublicViews = unstable_cache(
{ revalidate: 900, tags: ["public-codex-stats"] }
);

const getViews = async () => {
const getViews = cache(async () => {
if (!tokenPreferences.enabled || !isDatabaseConfigured()) {
return null;
}
Expand All @@ -97,7 +98,7 @@ const getViews = async () => {
reportOperationalError("public_codex_stats", error);
return null;
}
};
});

export const getPublicCodexStats = async () =>
(await getViews())?.stats ?? null;
Expand Down
23 changes: 9 additions & 14 deletions src/lib/github-activity-snapshots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ export const fillSavedGitHubSummaries = async (transaction: Transaction) => {

export const publishGitHubActivitySnapshots = async (
repositoryIds?: readonly string[],
bootstrap = false,
preserveTodayRepositories: readonly string[] = []
) =>
// oxlint-disable-next-line eslint/complexity -- One locked publication transaction keeps bootstrap, immutable history and today-only replacement atomic.
Expand All @@ -125,18 +124,12 @@ export const publishGitHubActivitySnapshots = async (
.select({ initialized: githubPublicFeedHead.historyInitializedAt })
.from(githubPublicFeedHead)
.where(eq(githubPublicFeedHead.id, true));
if (bootstrap && head?.initialized) {
return { changed: false, workUnits: 0, issues: 0 };
}
if (!bootstrap && !head?.initialized) {
throw new Error(
"Daily GitHub history must be initialized before publication."
);
}
const bootstrap = !head?.initialized;
const publicationRepositoryIds = bootstrap ? undefined : repositoryIds;
const now = new Date();
const today = dateKey(now, WORK_LOG_TIME_ZONE);
const scope = repositoryIds
? inArray(githubWorkUnits.repositoryId, [...repositoryIds])
const scope = publicationRepositoryIds
? inArray(githubWorkUnits.repositoryId, [...publicationRepositoryIds])
: undefined;
const units = await transaction
.select({
Expand All @@ -156,8 +149,10 @@ export const publishGitHubActivitySnapshots = async (
.selectDistinctOn([githubActivitySnapshots.identityKey])
.from(githubActivitySnapshots)
.where(
repositoryIds
? inArray(githubActivitySnapshots.repositoryId, [...repositoryIds])
publicationRepositoryIds
? inArray(githubActivitySnapshots.repositoryId, [
...publicationRepositoryIds,
])
: undefined
)
.orderBy(
Expand All @@ -166,7 +161,7 @@ export const publishGitHubActivitySnapshots = async (
);
const previous = new Map(latest.map((row) => [row.identityKey, row]));
const rows = await readCurrentPublicGitHubRows(transaction, {
repositoryIds,
repositoryIds: publicationRepositoryIds,
exactSummary: !bootstrap,
});
let changed = false;
Expand Down
1 change: 0 additions & 1 deletion src/lib/github-work-unit-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2213,7 +2213,6 @@ export const refreshGitHubWorkUnitProjection = async (
);
const publication = await publishGitHubActivitySnapshots(
scopes.map(({ id }) => id),
false,
[
...new Set([
...snapshot.excludedChanges
Expand Down
18 changes: 18 additions & 0 deletions tests/database-client.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { expect, test } from "bun:test";

import { databaseConnectionUrl } from "../src/db/client";

test("Postgres.js uses Supabase session pooling without changing credentials or other endpoints", () => {
const transaction =
"postgresql://postgres.project:p%40ss@aws-0-region.pooler.supabase.com:6543/postgres?sslmode=require";
const session = transaction.replace(":6543/", ":5432/");
expect(databaseConnectionUrl(transaction)).toBe(session);
for (const url of [
session,
"postgresql://user:password@db.project.supabase.co:5432/postgres",
"postgresql://user:password@localhost:6543/postgres",
"postgresql://user:password@pooler.supabase.com.example:6543/postgres",
]) {
expect(databaseConnectionUrl(url)).toBe(url);
}
});
Loading