@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 93 lines 3.11 KB
Raw
1 import {
2 MigrationError,
3 runMigrations,
4 withPgExecutor,
5 type NormalizedMigrationsInput,
6 type SqlExecutor,
7 type StampedMigrationsState,
8 } from "@alchemy.run/sql/Migrations";
9 import { stripSslQueryParams } from "@alchemy.run/sql/PostgresDriver";
10 import { applySqlImports } from "@alchemy.run/sql/SqlFile";
11 import * as Effect from "effect/Effect";
12 import * as Redacted from "effect/Redacted";
13
14 export { stripSslQueryParams };
15
16 const TRANSIENT_CONNECT_CODES = new Set([
17 "ENOTFOUND",
18 "EAI_AGAIN",
19 "ECONNREFUSED",
20 "ECONNRESET",
21 "ETIMEDOUT",
22 ]);
23
24 const isTransientConnectError = (cause: unknown): boolean => {
25 const code = (cause as { code?: unknown } | null | undefined)?.code;
26 return typeof code === "string" && TRANSIENT_CONNECT_CODES.has(code);
27 };
28
29 /**
30 * A verified-TLS `pg` session on a Fly Managed Postgres URI for the scope
31 * of `use`. A fresh cluster's hostname takes a moment to resolve, so
32 * connection failures are retried; one that persists is almost always a
33 * missing route — MPG hostnames only exist on the org's private network.
34 */
35 const withFlyPgExecutor = <A, E, R>(
36 connectionUri: Redacted.Redacted<string>,
37 use: (executor: SqlExecutor) => Effect.Effect<A, E, R>,
38 ): Effect.Effect<A, MigrationError | E, R> =>
39 withPgExecutor(
40 {
41 connectionString: connectionUri.pipe(Redacted.value, stripSslQueryParams),
42 ssl: { rejectUnauthorized: true },
43 },
44 use,
45 {
46 retryConnect: { while: isTransientConnectError, times: 15, interval: "3 seconds" },
47 onConnectFailure: (error) =>
48 isTransientConnectError(error.cause)
49 ? new MigrationError({
50 message:
51 `${error.message} — Fly Managed Postgres is only reachable ` +
52 "on the org's private network. Run the deploy behind a " +
53 "WireGuard peer or `fly mpg proxy`, or from a machine on " +
54 "6PN. See https://fly.io/docs/mpg/create-and-connect/",
55 cause: error.cause,
56 })
57 : error,
58 },
59 );
60
61 /**
62 * Fly Managed Postgres's migration adaptation is the shared pipeline
63 * with a connection-URI-scoped pg client as its executor. Use the
64 * direct (non-PgBouncer) URI so DDL and advisory locks work.
65 */
66 export const runPgMigrations = (options: {
67 connectionUri: Redacted.Redacted<string>;
68 input: NormalizedMigrationsInput;
69 stamped: StampedMigrationsState;
70 }) =>
71 runMigrations({
72 input: options.input,
73 stamped: options.stamped,
74 withExecutor: (apply) => withFlyPgExecutor(options.connectionUri, apply),
75 });
76
77 /** Run a single SQL script against the database (used for `importFiles`). */
78 export const runSql = (connectionUri: Redacted.Redacted<string>, sql: string) =>
79 withFlyPgExecutor(connectionUri, (executor) => executor.query(sql)).pipe(Effect.asVoid);
80
81 /** Apply `importFiles`, each once and again only when it changes. */
82 export const runImports = (
83 connectionUri: Redacted.Redacted<string>,
84 importFiles: ReadonlyArray<string>,
85 rootDir: string,
86 previous: Record<string, string>,
87 ) =>
88 applySqlImports({
89 importFiles,
90 rootDir,
91 previous,
92 apply: (file) => runSql(connectionUri, file.sql),
93 });