Neon / portal template support (#713)

TODOs:
- [x] Do restart when checkout / restore if there is a DB
- [x] List all branches (branch id, name, date)
- [x] Allow checking out versions with no DB
- [x] safeguard to never delete main branches
- [x] create app hook for neon template
- [x] weird UX with connector on configure panel
- [x] tiny neon logo in connector
- [x] deploy to vercel
- [x] build forgot password page
- [x] what about email setup
- [x] lots of imgix errors
- [x] edit file - db snapshot
- [x] DYAD_DISABLE_DB_PUSH
- [ ] update portal doc
- [x] switch preview branch to be read-only endpoint
- [x] disable supabase sys prompt if neon is enabled
- [ ] https://payloadcms.com/docs/upload/storage-adapters
- [x] need to use main branch...

Phase 2?
- [x] generate DB migrations
This commit is contained in:
Will Chen
2025-08-04 16:36:09 -07:00
committed by GitHub
parent 0f1a5c5c77
commit b0f08eaf15
50 changed files with 3525 additions and 205 deletions

View File

@@ -0,0 +1,134 @@
import { db } from "../../db";
import { versions, apps } from "../../db/schema";
import { eq, and } from "drizzle-orm";
import fs from "node:fs";
import git from "isomorphic-git";
import { getDyadAppPath } from "../../paths/paths";
import { neon } from "@neondatabase/serverless";
import log from "electron-log";
import { getNeonClient } from "@/neon_admin/neon_management_client";
const logger = log.scope("neon_timestamp_utils");
/**
* Retrieves the current timestamp from a Neon database
*/
async function getLastUpdatedTimestampFromNeon({
neonConnectionUri,
}: {
neonConnectionUri: string;
}): Promise<string> {
try {
const sql = neon(neonConnectionUri);
const [{ current_timestamp }] = await sql`
SELECT TO_CHAR(NOW() AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS"Z') AS current_timestamp
`;
return current_timestamp;
} catch (error) {
logger.error("Error retrieving timestamp from Neon:", error);
throw new Error(`Failed to retrieve timestamp from Neon: ${error}`);
}
}
/**
* Stores a Neon database timestamp for the current git commit hash
* and stores it in the versions table
* @param appId - The app ID
* @param neonConnectionUri - The Neon connection URI to get the timestamp from
*/
export async function storeDbTimestampAtCurrentVersion({
appId,
}: {
appId: number;
}): Promise<{ timestamp: string }> {
try {
logger.info(`Storing DB timestamp for current version - app ${appId}`);
// 1. Get the app to find the path
const app = await db.query.apps.findFirst({
where: eq(apps.id, appId),
});
if (!app) {
throw new Error(`App with ID ${appId} not found`);
}
if (!app.neonProjectId || !app.neonDevelopmentBranchId) {
throw new Error(`App with ID ${appId} has no Neon project or branch`);
}
// 2. Get the current commit hash
const appPath = getDyadAppPath(app.path);
const currentCommitHash = await git.resolveRef({
fs,
dir: appPath,
ref: "HEAD",
});
logger.info(`Current commit hash: ${currentCommitHash}`);
const neonClient = await getNeonClient();
const connectionUri = await neonClient.getConnectionUri({
projectId: app.neonProjectId,
branch_id: app.neonDevelopmentBranchId,
database_name: "neondb",
role_name: "neondb_owner",
});
// 3. Get the current timestamp from Neon
const currentTimestamp = await getLastUpdatedTimestampFromNeon({
neonConnectionUri: connectionUri.data.uri,
});
logger.info(`Current timestamp from Neon: ${currentTimestamp}`);
// 4. Check if a version with this commit hash already exists
const existingVersion = await db.query.versions.findFirst({
where: and(
eq(versions.appId, appId),
eq(versions.commitHash, currentCommitHash),
),
});
if (existingVersion) {
// Update existing version with the new timestamp
await db
.update(versions)
.set({
neonDbTimestamp: currentTimestamp,
updatedAt: new Date(),
})
.where(
and(
eq(versions.appId, appId),
eq(versions.commitHash, currentCommitHash),
),
);
logger.info(
`Updated existing version record with timestamp ${currentTimestamp}`,
);
} else {
// Create new version record
await db.insert(versions).values({
appId,
commitHash: currentCommitHash,
neonDbTimestamp: currentTimestamp,
});
logger.info(
`Created new version record for commit ${currentCommitHash} with timestamp ${currentTimestamp}`,
);
}
logger.info(
`Successfully stored timestamp for commit ${currentCommitHash} in app ${appId}`,
);
return { timestamp: currentTimestamp };
} catch (error) {
logger.error("Error in storeDbTimestampAtCurrentVersion:", error);
throw error;
}
}