Skip to content

Commit 0f2384d

Browse files
committed
ENG-681 first trial
1 parent 91fe58c commit 0f2384d

7 files changed

Lines changed: 130 additions & 21 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: Database tests
2+
on:
3+
pull_request:
4+
paths:
5+
- "packages/database/**"
6+
env:
7+
SUPABASE_USE_DB: local
8+
SUPABASE_PROJECT_ID: test
9+
GITHUB_TEST: test
10+
11+
jobs:
12+
build:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v6
16+
- name: Install pnpm
17+
uses: pnpm/action-setup@v4
18+
with:
19+
version: 10.15.1
20+
run_install: false
21+
- name: Setup node
22+
uses: actions/setup-node@v4
23+
with:
24+
node-version: "20"
25+
cache: "pnpm"
26+
- name: Install Dependencies
27+
run: pnpm install --frozen-lockfile
28+
- name: Get supabase version
29+
working-directory: ./packages/database
30+
run: echo "SUPABASE_VERSION=$(./node_modules/.bin/supabase --version)" >> $GITHUB_ENV
31+
- name: Cache Docker images.
32+
uses: ScribeMD/docker-cache@0.5.0
33+
with:
34+
key: docker-${{ runner.os }}-${{ env.SUPABASE_VERSION }}
35+
- name: Setup database
36+
working-directory: ./packages/database
37+
run: pnpm run setup
38+
- name: Serve and run tests
39+
working-directory: ./packages/database
40+
run: pnpm run test:withserve

packages/database/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"lint:fix": "eslint --fix . && tsx scripts/lintSchemas.ts -f && tsx scripts/lintFunctions.ts",
3434
"migrate": "tsx scripts/migrate.ts",
3535
"test": "pnpm run build && cucumber-js",
36+
"test:withserve": "pnpm run build && tsx scripts/serve_and_test.ts",
3637
"genenv": "tsx scripts/createEnv.mts",
3738
"gentypes": "tsx scripts/genTypes.ts",
3839
"dbdiff": "supabase stop && supabase db diff --use-pg-schema",

packages/database/scripts/createEnv.mts

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,17 @@ const getVercelToken = () => {
2929
};
3030

3131
const makeFnEnv = (envTxt: string): string => {
32-
return envTxt.split('\n').filter(l=>l.match(/^SUPABASE_\w+_KEY/)).map((l)=> l.replace('SUPABASE_', 'SB_')).join('\n');
33-
}
32+
return envTxt
33+
.split("\n")
34+
.filter((l) => l.match(/^SUPABASE_\w+_KEY/))
35+
.map((l) => l.replace("SUPABASE_", "SB_"))
36+
.join("\n");
37+
};
3438

3539
const makeLocalEnv = () => {
3640
execSync("supabase start", {
37-
cwd: projectRoot, stdio: "inherit"
41+
cwd: projectRoot,
42+
stdio: "inherit",
3843
});
3944
const stdout = execSync("supabase status -o env", {
4045
encoding: "utf8",
@@ -54,8 +59,8 @@ const makeLocalEnv = () => {
5459
);
5560
writeFileSync(
5661
join(projectRoot, "supabase/functions/.env"),
57-
makeFnEnv(prefixed)
58-
)
62+
makeFnEnv(prefixed),
63+
);
5964
};
6065

6166
const makeBranchEnv = async (vercel: Vercel, vercelToken: string) => {
@@ -94,11 +99,11 @@ const makeBranchEnv = async (vercel: Vercel, vercelToken: string) => {
9499
throw err;
95100
}
96101
appendFileSync(".env.branch", `NEXT_API_ROOT="https://${url}/api"\n`);
97-
const fromVercel = readFileSync('.env.branch').toString();
102+
const fromVercel = readFileSync(".env.branch").toString();
98103
writeFileSync(
99104
join(projectRoot, "supabase/functions/.env"),
100-
makeFnEnv(fromVercel)
101-
)
105+
makeFnEnv(fromVercel),
106+
);
102107
};
103108

104109
const makeProductionEnv = async (vercel: Vercel, vercelToken: string) => {
@@ -117,33 +122,37 @@ const makeProductionEnv = async (vercel: Vercel, vercelToken: string) => {
117122
`vercel -t ${vercelToken} env pull --environment production .env.production`,
118123
);
119124
appendFileSync(".env.production", `NEXT_API_ROOT="https://${url}/api"\n`);
120-
const fromVercel = readFileSync('.env.production').toString();
125+
const fromVercel = readFileSync(".env.production").toString();
121126
writeFileSync(
122127
join(projectRoot, "supabase/functions/.env"),
123-
makeFnEnv(fromVercel)
124-
)
128+
makeFnEnv(fromVercel),
129+
);
125130
};
126131

127132
const main = async (variant: Variant) => {
128133
if (process.env.ROAM_BUILD_SCRIPT) {
129134
// special case: production build
130135
try {
131-
const response = execSync('curl https://discoursegraphs.com/api/supabase/env');
136+
const response = execSync(
137+
"curl https://discoursegraphs.com/api/supabase/env",
138+
);
132139
const asJson = JSON.parse(response.toString()) as Record<string, string>;
133140
writeFileSync(
134141
join(projectRoot, ".env"),
135-
Object.entries(asJson).map(([k,v])=>`${k}=${v}`).join('\n')
142+
Object.entries(asJson)
143+
.map(([k, v]) => `${k}=${v}`)
144+
.join("\n"),
136145
);
137146
return;
138147
} catch (e) {
139148
if (process.env.SUPABASE_URL && process.env.SUPABASE_PUBLISHABLE_KEY)
140149
return;
141150
throw new Error("Could not get environment from site");
142151
}
143-
}
144-
else if (
152+
} else if (
145153
process.env.HOME === "/vercel" ||
146-
process.env.GITHUB_ACTIONS !== undefined
154+
(process.env.GITHUB_ACTIONS !== undefined &&
155+
process.env.GITHUB_TEST !== "test")
147156
)
148157
// Do not execute in deployment or github action.
149158
return;

packages/database/scripts/migrate.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import { getVariant } from "@repo/database/dbDotEnv";
66
const __dirname = dirname(__filename);
77
const projectRoot = join(__dirname, "..");
88

9-
if (process.env.HOME === "/vercel" || process.env.GITHUB_ACTIONS === "true") {
9+
if (
10+
process.env.HOME === "/vercel" ||
11+
(process.env.GITHUB_ACTIONS === "true" && process.env.GITHUB_TEST !== "test")
12+
) {
1013
console.log("Skipping in production environment");
1114
process.exit(0);
1215
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { spawn, execSync } from "node:child_process";
2+
import { join, dirname } from "path";
3+
4+
const __dirname = dirname(__filename);
5+
const projectRoot = join(__dirname, "..");
6+
7+
if (process.env.GITHUB_TEST !== "test") {
8+
}
9+
10+
const serve = spawn("supabase", ["functions", "serve"], {
11+
cwd: projectRoot,
12+
detached: true,
13+
});
14+
15+
let resolveCallback: ((v: unknown) => void) | undefined = undefined;
16+
let rejectCallback: ((v: unknown) => void) | undefined = undefined;
17+
18+
const servingReady = new Promise((rsc, rjc) => {
19+
resolveCallback = rsc;
20+
rejectCallback = rjc;
21+
});
22+
23+
serve.stdout.on("data", (data: string) => {
24+
console.log(`stdout: ${data}`);
25+
if (data.includes("Serving functions ")) {
26+
console.log("Found serving functions");
27+
if (resolveCallback === undefined) throw new Error("did not get callback");
28+
resolveCallback(null);
29+
}
30+
});
31+
32+
const doTest = async () => {
33+
await servingReady;
34+
try {
35+
execSync("cucumber-js", {
36+
cwd: projectRoot,
37+
stdio: "inherit",
38+
});
39+
// will throw on failure
40+
} finally {
41+
if (serve.pid) process.kill(-serve.pid);
42+
}
43+
};
44+
45+
doTest()
46+
.then(() => {
47+
console.log("success");
48+
})
49+
.catch((err) => {
50+
console.error(err);
51+
process.exit(-1);
52+
});

packages/database/src/dbDotEnv.mjs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ export const getVariant = () => {
2323
: process.env["SUPABASE_USE_DB"];
2424
if (variant === undefined) {
2525
dotenv.config();
26-
const dbGlobalEnv = join(findRoot(),'.env');
27-
if (existsSync(dbGlobalEnv))
28-
dotenv.config({path: dbGlobalEnv});
26+
const dbGlobalEnv = join(findRoot(), ".env");
27+
if (existsSync(dbGlobalEnv)) dotenv.config({ path: dbGlobalEnv });
2928
variant = process.env["SUPABASE_USE_DB"];
3029
}
3130
const processHasVars =
@@ -39,7 +38,11 @@ export const getVariant = () => {
3938
throw new Error("Invalid variant: " + variant);
4039
}
4140

42-
if (process.env.HOME === "/vercel" || process.env.GITHUB_ACTIONS === "true") {
41+
if (
42+
process.env.HOME === "/vercel" ||
43+
(process.env.GITHUB_ACTIONS === "true" &&
44+
process.env.GITHUB_TEST !== "test")
45+
) {
4346
// deployment should have variables
4447
if (!processHasVars) {
4548
console.error("Missing SUPABASE variables in deployment");

turbo.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"GEMINI_API_KEY",
3030
"GH_CLIENT_SECRET_PROD",
3131
"GITHUB_ACTIONS",
32+
"GITHUB_TEST",
3233
"HOME",
3334
"OPENAI_API_KEY",
3435
"POSTGRES_PASSWORD",

0 commit comments

Comments
 (0)