Skip to content

Commit 35d7906

Browse files
committed
fix: resolve pr review comments
Signed-off-by: Yeganathan S <63534555+skwowet@users.noreply.github.com>
1 parent abca95d commit 35d7906

6 files changed

Lines changed: 90 additions & 74 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
6+
# shellcheck source=scripts/utils
7+
source "${REPO_ROOT}/scripts/utils"
8+
9+
: "${DB_HOST:?DB_HOST is required}"
10+
: "${DB_PORT:?DB_PORT is required}"
11+
: "${DB_USER:?DB_USER is required}"
12+
: "${DB_PASSWORD:?DB_PASSWORD is required}"
13+
14+
say "Applying Sequin bootstrap SQL..."
15+
docker run --rm --network host \
16+
-v "${REPO_ROOT}/scripts/scaffold/sequin/postgres-docker-entrypoint-initdb.d/create-sequin-database.sql:/bootstrap.sql:ro" \
17+
-e "PGPASSWORD=${DB_PASSWORD}" \
18+
postgres:14-alpine \
19+
psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d postgres -v ON_ERROR_STOP=1 -f /bootstrap.sql
20+
21+
say "Recreating test_template..."
22+
docker run --rm --network host \
23+
-e "PGPASSWORD=${DB_PASSWORD}" \
24+
postgres:14-alpine \
25+
psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d postgres -v ON_ERROR_STOP=1 \
26+
-c "DROP DATABASE IF EXISTS test_template;" \
27+
-c "CREATE DATABASE test_template;"
28+
29+
say "Building flyway image..."
30+
docker build -t crowd_flyway -f "${REPO_ROOT}/backend/src/database/Dockerfile.flyway" "${REPO_ROOT}/backend/src/database"
31+
32+
say "Migrating test_template..."
33+
docker run --rm --network host \
34+
-e "PGHOST=${DB_HOST}" \
35+
-e "PGPORT=${DB_PORT}" \
36+
-e "PGUSER=${DB_USER}" \
37+
-e "PGPASSWORD=${DB_PASSWORD}" \
38+
-e PGDATABASE=test_template \
39+
crowd_flyway
40+
41+
say "Test template database ready."

.github/workflows/server-tests.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ jobs:
2929
services:
3030
postgres:
3131
image: postgres:14-alpine
32+
command: postgres -c wal_level=logical
3233
env:
3334
POSTGRES_USER: postgres
3435
POSTGRES_PASSWORD: example
@@ -40,7 +41,6 @@ jobs:
4041
--health-interval 2s
4142
--health-timeout 5s
4243
--health-retries 15
43-
postgres -c wal_level=logical
4444
4545
env:
4646
NODE_ENV: test
@@ -53,7 +53,7 @@ jobs:
5353
- name: Check out repository code
5454
uses: actions/checkout@v4
5555

56-
- name: Setup Node and cache pnpm
56+
- name: Setup Node
5757
uses: actions/setup-node@v4
5858
with:
5959
node-version: 20
@@ -65,7 +65,7 @@ jobs:
6565
pnpm i --frozen-lockfile
6666
6767
- name: Prepare test database
68-
run: SEQUIN_BOOTSTRAP=1 ./scripts/prepare-test-template-db.sh
68+
run: ./.github/scripts/prepare-test-template-db.sh
6969

7070
- name: Run server tests
7171
run: pnpm test:server

scripts/cli

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -787,11 +787,21 @@ function down_test_scaffold() {
787787
}
788788

789789
function migrate_test() {
790-
set -a
791-
# shellcheck source=/dev/null
792-
source "$CLI_HOME/../.env.test"
793-
set +a
794-
"$CLI_HOME/prepare-test-template-db.sh"
790+
wait_for_postgres test-db crowd-test-db
791+
say "Recreating test_template..."
792+
docker exec crowd-test-db psql -U postgres -d postgres -v ON_ERROR_STOP=1 \
793+
-c "DROP DATABASE IF EXISTS test_template;" \
794+
-c "CREATE DATABASE test_template;"
795+
say "Building crowd flyway migration image..."
796+
docker build $DOCKER_PLATFORM_FLAGS -t crowd_flyway -f $CLI_HOME/../backend/src/database/Dockerfile.flyway $CLI_HOME/../backend/src/database --load
797+
say "Migrating test_template..."
798+
docker run --rm --network "${PROJECT_NAME}-bridge-test" \
799+
-e PGHOST=crowd-test-db \
800+
-e PGPORT=5432 \
801+
-e PGUSER=postgres \
802+
-e PGPASSWORD=example \
803+
-e PGDATABASE=test_template \
804+
crowd_flyway
795805
}
796806

797807
function source_edition() {

scripts/prepare-test-template-db.sh

Lines changed: 0 additions & 53 deletions
This file was deleted.

services/libs/test-kit/src/postgres.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import pgPromise from 'pg-promise'
22

3-
import { IS_TEST_ENV } from '@crowd/common'
4-
import { DEFAULT_TENANT_ID } from '@crowd/common/src/env'
3+
import { DEFAULT_TENANT_ID, IS_TEST_ENV } from '@crowd/common'
54
import { type DbConnection, type DbInstance, type IDatabaseConfig } from '@crowd/database'
65
import type { QueryExecutor } from '@crowd/database'
76

@@ -60,7 +59,7 @@ export async function resetTestDatabase(qx: QueryExecutor): Promise<void> {
6059
const { name } = await qx.selectOne('SELECT current_database() AS name')
6160

6261
if (!/^test_[a-z0-9_]+$/.test(name)) {
63-
throw new Error(`Not a worker test database: ${name}`)
62+
throw new Error(`Expected worker test database (got ${name})`)
6463
}
6564

6665
await qx.selectNone(`
@@ -137,21 +136,19 @@ async function dropDatabase(catalog: DbConnection, name: string): Promise<void>
137136
*/
138137
function getTestPostgres(): TestPostgres {
139138
if (!IS_TEST_ENV) {
140-
throw new Error(`NODE_ENV=test required (got ${process.env.NODE_ENV ?? 'unset'})`)
139+
throw new Error(`Expected NODE_ENV=test (got ${process.env.NODE_ENV ?? 'unset'})`)
141140
}
142141

143142
const { DB_HOST: host, DB_PORT, DB_USER: user, DB_PASSWORD: password } = process.env
144143

145144
if (!host || !DB_PORT || !user || !password) {
146-
throw new Error(
147-
'DB_HOST, DB_PORT, DB_USER, and DB_PASSWORD are required — run ./scripts/cli scaffold up-test and use .env.test',
148-
)
145+
throw new Error('Missing required database environment variables')
149146
}
150147

151148
const port = Number(DB_PORT)
152149

153150
if (!['localhost', '127.0.0.1', '::1'].includes(host)) {
154-
throw new Error(`Security safeguard: DB_HOST must be localhost (got: ${host})`)
151+
throw new Error(`Expected local DB_HOST (got ${host})`)
155152
}
156153

157154
return { host, port, user, password }

vitest.config.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,25 @@ import { defineConfig } from 'vitest/config'
33

44
const root = __dirname
55

6-
const testEnv = loadEnv('test', root, '')
6+
/** Load .env.${mode} and let existing process env vars override file values. */
7+
function resolveTestEnv(mode: string): Record<string, string> {
8+
const env = loadEnv(mode, root, '')
9+
const resolved = { ...env }
710

8-
export default defineConfig({
11+
for (const key in env) {
12+
const value = process.env[key]
13+
14+
if (value !== undefined) {
15+
resolved[key] = value
16+
}
17+
}
18+
19+
return resolved
20+
}
21+
22+
export default defineConfig(({ mode }) => ({
923
test: {
10-
env: testEnv,
24+
env: resolveTestEnv(mode),
1125
server: {
1226
deps: { inline: [/@crowd\//] },
1327
},
@@ -17,12 +31,19 @@ export default defineConfig({
1731
test: {
1832
name: 'server',
1933
include: ['backend/**/*.test.ts', 'services/**/*.test.ts'],
20-
exclude: ['**/node_modules/**', '**/dist/**', 'services/cronjobs/**'],
34+
exclude: [
35+
'**/node_modules/**',
36+
'**/dist/**',
37+
'services/cronjobs/**',
38+
// TODO: packages_worker has its own vitest config and packages-db; it was landed in another PR
39+
// alongside the test foundation. excluding this for now, will refactor it later!
40+
'services/apps/packages_worker/**',
41+
],
2142
pool: 'forks',
2243
hookTimeout: 300_000,
2344
testTimeout: 30_000,
2445
},
2546
},
2647
],
2748
},
28-
})
49+
}))

0 commit comments

Comments
 (0)