Skip to content
Closed
2 changes: 1 addition & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
"baseBranch": "main",
"updateInternalDependencies": "patch",
"ignore": ["create-db-worker", "claim-db-worker"],
"linked": [["create-db", "create-pg", "create-postgres"]]
"fixed": [["create-db", "create-pg", "create-postgres"]]
}
47 changes: 41 additions & 6 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,58 @@ jobs:
echo "Using CLAIM_DB_WORKER_URL=${CLAIM_DB_WORKER_URL}"
echo "Using POSTHOG_API_HOST=${POSTHOG_API_HOST}"

# create-pg and create-postgres pin the create-db version they were
# published with, so the three CLIs must move to a new version together
versions=""
for pkg in ${{ env.WORKSPACES }}; do
versions="$versions $pkg@$(node -p "require('./${{ env.PACKAGES_DIR }}/$pkg/package.json').version")"
done
if [ "$(echo $versions | tr ' ' '\n' | sed 's/.*@//' | sort -u | wc -l)" -ne 1 ]; then
echo "::error::The CLIs must share one version, got:$versions"
exit 1
fi

for pkg in ${{ env.WORKSPACES }}; do
echo "Publishing $pkg to npm..."
cd "${{ env.PACKAGES_DIR }}/$pkg"
export POSTHOG_API_HOST="${POSTHOG_API_HOST}"
export POSTHOG_API_KEY="${POSTHOG_API_KEY}"
export CREATE_DB_WORKER_URL="${CREATE_DB_WORKER_URL}"
export CLAIM_DB_WORKER_URL="${CLAIM_DB_WORKER_URL}"

# First try to publish
if ! pnpm publish --access public --no-git-checks; then
echo "Publish failed, trying to bump version and retry..."
npm version patch --no-git-tag-version
pnpm publish --access public --no-git-checks || echo "Publish failed again for $pkg"
version=$(node -p "require('./package.json').version")

# Merges that don't bump the version have nothing to publish
if npm view "$pkg@$version" version >/dev/null 2>&1; then
echo "$pkg@$version is already on npm, skipping."
else
echo "Publishing $pkg@$version to npm..."
pnpm publish --access public --no-git-checks
fi
cd - >/dev/null
done

- name: ✅ Verify Published Versions
run: |
for pkg in ${{ env.WORKSPACES }}; do
version=$(node -p "require('./${{ env.PACKAGES_DIR }}/$pkg/package.json').version")

# The registry can take a few seconds to serve a new version
published=""
for attempt in 1 2 3 4 5 6; do
published=$(npm view "$pkg@$version" version 2>/dev/null || true)
if [ "$published" = "$version" ]; then
break
fi
sleep 10
done

if [ "$published" != "$version" ]; then
echo "::error::$pkg@$version is not on npm"
exit 1
fi
echo "$pkg@$version is on npm."
done

- name: 📝 Ensure Changeset Exists
run: |
if [ -z "$(ls -A .changeset 2>/dev/null)" ]; then
Expand Down
4 changes: 2 additions & 2 deletions apps/claim-db-worker/app/claim/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ function ClaimContent() {
<p className="mt-10 max-w-[48ch] text-pretty text-muted-foreground">
*Your database will be{" "}
<strong className="font-semibold text-foreground">
deleted 24 hours after creation
deleted 72 hours after creation
Comment thread
gregory-boch-prisma marked this conversation as resolved.
Resolved
</strong>{" "}
unless you claim it.
(sooner if it was created with a shorter lifetime) unless you claim it.
</p>
</StatePanel>
);
Expand Down
4 changes: 2 additions & 2 deletions apps/claim-db-worker/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const steps = [
description: (
<>
Claim your database to your Prisma account to keep it permanently. Unclaimed databases
expire after 24 hours.
expire after 72 hours.
</>
),
},
Expand Down Expand Up @@ -168,7 +168,7 @@ async function HomePageContent({ searchParams }: PageProps) {
</div>

<p className="relative mt-6 text-sm text-muted-foreground">
Databases are deleted after 24 hours unless claimed.
Databases are deleted after 72 hours unless claimed.
</p>
</div>
</div>
Expand Down
5 changes: 3 additions & 2 deletions apps/claim-db-worker/app/web/DatabaseContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ interface CreateDatabaseResponse {
}

const saveToCookie = (data: DatabaseInfo) => {
cookieUtils.set(DB_KEY, JSON.stringify(data), 1);
// Unclaimed databases live 72 hours; keep the cookie as long.
cookieUtils.set(DB_KEY, JSON.stringify(data), 3);
};

const loadFromCookie = (): Partial<DatabaseInfo> | null => {
Expand Down Expand Up @@ -111,7 +112,7 @@ export const DatabaseProvider = ({ children }: DatabaseProviderProps) => {
projectId: result.projectId ?? "",
connectionString: db.connectionString,
directConnectionString: directConnString,
expirationTime: Date.now() + 24 * 60 * 60 * 1000,
expirationTime: Date.now() + 72 * 60 * 60 * 1000,
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
databaseId: result.databaseId ?? "",
};

Expand Down
2 changes: 1 addition & 1 deletion apps/claim-db-worker/components/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function Navbar() {
{formatRemaining(timeRemaining!)}
</span>
<p className="hidden text-sm text-muted-foreground xl:block">
Deleted after 24 hours unless claimed.{" "}
Deleted after 72 hours unless claimed.{" "}
<strong className="font-semibold text-foreground">
Do not store sensitive data.
</strong>
Expand Down
64 changes: 46 additions & 18 deletions apps/create-db-worker/src/delete-stale-workflow.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { WorkflowEntrypoint, WorkflowEvent, WorkflowStep } from 'cloudflare:workers';
import { STALE_PROJECT_AGE_MS } from './ttl';

type Params = Record<string, never>;

Expand All @@ -20,35 +21,62 @@ type ProjectsResponse = {
};
};

const PROJECTS_PAGE_SIZE = 1000;

export class DeleteStaleProjectsWorkflow extends WorkflowEntrypoint<Env, Params> {
async run(event: WorkflowEvent<Params>, step: WorkflowStep): Promise<void> {
const res = await step.do('fetch-projects', async () => {
const response = await fetch('https://api.prisma.io/v1/projects?limit=1000', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${this.env.INTEGRATION_TOKEN}`,
},
const projects: Project[] = [];
const seenCursors = new Set<string>();
let cursor: string | null = null;
let page = 0;

do {
const pageCursor: string | null = cursor;
const res: string = await step.do(`fetch-projects-${page}`, async () => {
const url = new URL('https://api.prisma.io/v1/projects');
url.searchParams.set('limit', String(PROJECTS_PAGE_SIZE));
if (pageCursor) {
url.searchParams.set('cursor', pageCursor);
}

const response = await fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${this.env.INTEGRATION_TOKEN}`,
},
});

if (!response.ok) {
throw new Error(`Failed to fetch projects: ${response.statusText}`);
}

const data = await response.text();
return data;
});

if (!response.ok) {
throw new Error(`Failed to fetch projects: ${response.statusText}`);
}
const body: ProjectsResponse = JSON.parse(res);
projects.push(...body.data);

const data = await response.text();
return data;
});
const { hasMore, nextCursor } = body.pagination;
if (hasMore && (!nextCursor || seenCursors.has(nextCursor))) {
throw new Error(`Projects API reported more pages without a new cursor after page ${page}`);
}
cursor = hasMore ? nextCursor : null;
if (cursor) {
seenCursors.add(cursor);
}
page += 1;
} while (cursor);

const projects: ProjectsResponse = JSON.parse(res);
const now = Date.now();
const twentyFourHours = 24 * 60 * 60 * 1000;

const staleProjects = projects.data.filter((project) => {
const staleProjects = projects.filter((project) => {
const createdAt = new Date(project.createdAt).getTime();
return now - createdAt > twentyFourHours;
return now - createdAt > STALE_PROJECT_AGE_MS;
});

console.log(`Total projects: ${projects.data.length}, Stale projects: ${staleProjects.length}`);
console.log(`Total projects: ${projects.length}, Stale projects: ${staleProjects.length}`);

for (const project of staleProjects) {
await step.do(`delete-project-${project.id}`, async () => {
Expand Down
10 changes: 9 additions & 1 deletion apps/create-db-worker/src/ttl.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
export const MIN_TTL_MS = 30 * 60 * 1000;
export const MAX_TTL_MS = 24 * 60 * 60 * 1000;
/** Default and maximum lifetime of an unclaimed database: 72 hours. */
export const MAX_TTL_MS = 72 * 60 * 60 * 1000;
Comment thread
gregory-boch-prisma marked this conversation as resolved.
Resolved

/**
* The daily sweep deletes unclaimed projects older than this. It is the maximum
* lifetime plus a margin, so the sweep never deletes a database before the
* deletion time the CLI printed; the per-database workflow does that.
*/
export const STALE_PROJECT_AGE_MS = MAX_TTL_MS + 60 * 60 * 1000;

export function parseTtlMsInput(value: unknown): number | undefined {
if (typeof value !== 'number' || !Number.isFinite(value)) {
Expand Down
115 changes: 115 additions & 0 deletions apps/create-db-worker/test/delete-stale-workflow.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { DeleteStaleProjectsWorkflow } from '../src/delete-stale-workflow';

const HOUR_MS = 60 * 60 * 1000;

function hoursAgo(hours: number) {
return new Date(Date.now() - hours * HOUR_MS).toISOString();
}

function page(data: { id: string; createdAt: string }[], nextCursor: string | null) {
return new Response(
JSON.stringify({
data: data.map((project) => ({ ...project, name: project.id })),
pagination: { hasMore: nextCursor !== null, nextCursor },
}),
);
}

async function runWorkflow() {
const stepNames: string[] = [];
const step = {
do: async (name: string, callback: () => Promise<unknown>) => {
stepNames.push(name);
return callback();
},
};
// workerd only constructs a WorkflowEntrypoint itself, so run the method on a stand-in `this`
await DeleteStaleProjectsWorkflow.prototype.run.call(
{ env: { INTEGRATION_TOKEN: 'test-token' } } as any,
{} as any,
step as any,
);
return stepNames;
}

describe('DeleteStaleProjectsWorkflow', () => {
let fetchMock: ReturnType<typeof vi.fn>;

function requestsTo(method: string) {
return fetchMock.mock.calls.filter(([, init]) => init?.method === method).map(([url]) => new URL(url));
}

beforeEach(() => {
fetchMock = vi.fn();
vi.spyOn(globalThis, 'fetch').mockImplementation(fetchMock);
});

it('walks every page and deletes stale projects from all of them', async () => {
fetchMock
.mockResolvedValueOnce(
page(
[
{ id: 'stale-1', createdAt: hoursAgo(80) },
{ id: 'fresh-1', createdAt: hoursAgo(2) },
{ id: 'two-days-old', createdAt: hoursAgo(48) },
],
'fresh-1',
),
)
.mockResolvedValueOnce(page([{ id: 'stale-2', createdAt: hoursAgo(75) }], null))
.mockImplementation(async () => new Response(null, { status: 200 }));

const stepNames = await runWorkflow();

const lists = requestsTo('GET');
expect(lists).toHaveLength(2);
expect(lists[0].searchParams.get('cursor')).toBeNull();
expect(lists[1].searchParams.get('cursor')).toBe('fresh-1');

expect(requestsTo('DELETE').map((url) => url.pathname)).toEqual([
'/v1/projects/stale-1',
'/v1/projects/stale-2',
]);

expect(new Set(stepNames).size).toBe(stepNames.length);
});

it('stops after one page when there are no more projects', async () => {
fetchMock.mockResolvedValueOnce(page([{ id: 'fresh-1', createdAt: hoursAgo(1) }], null));

await runWorkflow();

expect(requestsTo('GET')).toHaveLength(1);
expect(requestsTo('DELETE')).toHaveLength(0);
});

it('fails when the API reports more pages without advancing the cursor', async () => {
fetchMock
.mockResolvedValueOnce(page([{ id: 'stale-1', createdAt: hoursAgo(30) }], 'stale-1'))
.mockResolvedValueOnce(page([{ id: 'stale-1', createdAt: hoursAgo(30) }], 'stale-1'));

await expect(runWorkflow()).rejects.toThrow('without a new cursor');
expect(requestsTo('DELETE')).toHaveLength(0);
});

it('fails when the API reports more pages with no cursor', async () => {
fetchMock.mockResolvedValueOnce(
new Response(
JSON.stringify({
data: [{ id: 'stale-1', name: 'stale-1', createdAt: hoursAgo(30) }],
pagination: { hasMore: true, nextCursor: null },
}),
),
);

await expect(runWorkflow()).rejects.toThrow('without a new cursor');
expect(requestsTo('DELETE')).toHaveLength(0);
});

it('fails when a page cannot be fetched', async () => {
fetchMock.mockResolvedValueOnce(new Response(null, { status: 500, statusText: 'Server Error' }));

await expect(runWorkflow()).rejects.toThrow('Failed to fetch projects');
});
});
14 changes: 13 additions & 1 deletion apps/create-db-worker/test/ttl.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import { describe, it, expect } from 'vitest';
import { parseTtlMsInput, isTtlMsInRange, clampTtlMs, MIN_TTL_MS, MAX_TTL_MS } from '../src/ttl';
import { parseTtlMsInput, isTtlMsInRange, clampTtlMs, MIN_TTL_MS, MAX_TTL_MS, STALE_PROJECT_AGE_MS } from '../src/ttl';

describe('lifetime constants', () => {
it('allows up to 72 hours', () => {
expect(MAX_TTL_MS).toBe(72 * 60 * 60 * 1000);
expect(isTtlMsInRange(72 * 60 * 60 * 1000)).toBe(true);
expect(isTtlMsInRange(48 * 60 * 60 * 1000)).toBe(true);
});

it('sweeps only after the maximum lifetime has passed', () => {
expect(STALE_PROJECT_AGE_MS).toBeGreaterThan(MAX_TTL_MS);
});
});

describe('parseTtlMsInput', () => {
it('returns the value for a valid finite number', () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/create-db/__tests__/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const runCli = async (

describe("CLI database creation", () => {
it("creates database with default command", async () => {
const result = await runCli([]);
const result = await runCli(["--ttl", "30m"]);
if (result.exitCode !== 0) {
console.error("CLI failed with exit code:", result.exitCode);
console.error("stdout:", result.stdout);
Expand All @@ -43,7 +43,7 @@ describe("CLI database creation", () => {
}, 30000);

it("creates database with --json flag", async () => {
const result = await runCli(["--json"]);
const result = await runCli(["--json", "--ttl", "30m"]);
if (result.exitCode !== 0) {
console.error("CLI failed with exit code:", result.exitCode);
console.error("stdout:", result.stdout);
Expand All @@ -68,7 +68,7 @@ describe("CLI database creation", () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "create-db-json-env-"));
const envPath = path.join(dir, ".env");
try {
const result = await runCli(["--json", "--env", envPath]);
const result = await runCli(["--json", "--env", envPath, "--ttl", "30m"]);
expect(result.exitCode).toBe(0);
const parsed = JSON.parse(result.stdout.trim());
expect(parsed.success).toBe(true);
Expand Down
Loading