Skip to content

Commit 79ca17b

Browse files
committed
fix: postgres CI tests with service container and test pool setup
1 parent 13b7c46 commit 79ca17b

2 files changed

Lines changed: 79 additions & 13 deletions

File tree

.github/workflows/ci.yml

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,8 @@ on:
66
pull_request:
77

88
jobs:
9-
test:
10-
strategy:
11-
matrix:
12-
include:
13-
- features: sqlite
14-
os: ubuntu-latest
15-
- features: postgres
16-
os: ubuntu-latest
17-
runs-on: ${{ matrix.os }}
9+
test-sqlite:
10+
runs-on: ubuntu-latest
1811
steps:
1912
- uses: actions/checkout@v5
2013

@@ -23,12 +16,55 @@ jobs:
2316
components: clippy
2417

2518
- uses: Swatinem/rust-cache@v2
19+
with:
20+
key: sqlite
21+
22+
- name: Check
23+
run: cargo check --no-default-features --features sqlite
24+
25+
- name: Clippy
26+
run: cargo clippy --no-default-features --features sqlite -- -D warnings
27+
28+
- name: Test
29+
run: cargo test --no-default-features --features sqlite
30+
31+
test-postgres:
32+
runs-on: ubuntu-latest
33+
34+
services:
35+
postgres:
36+
image: postgres:16
37+
env:
38+
POSTGRES_USER: stackpit
39+
POSTGRES_PASSWORD: stackpit
40+
POSTGRES_DB: stackpit_test
41+
ports:
42+
- 5432:5432
43+
options: >-
44+
--health-cmd pg_isready
45+
--health-interval 5s
46+
--health-timeout 5s
47+
--health-retries 5
48+
49+
env:
50+
DATABASE_URL: postgres://stackpit:stackpit@localhost:5432/stackpit_test
51+
52+
steps:
53+
- uses: actions/checkout@v5
54+
55+
- uses: dtolnay/rust-toolchain@stable
56+
with:
57+
components: clippy
58+
59+
- uses: Swatinem/rust-cache@v2
60+
with:
61+
key: postgres
2662

2763
- name: Check
28-
run: cargo check --no-default-features --features ${{ matrix.features }}
64+
run: cargo check --no-default-features --features postgres
2965

3066
- name: Clippy
31-
run: cargo clippy --no-default-features --features ${{ matrix.features }} -- -D warnings
67+
run: cargo clippy --no-default-features --features postgres -- -D warnings
3268

3369
- name: Test
34-
run: cargo test --no-default-features --features ${{ matrix.features }}
70+
run: cargo test --no-default-features --features postgres -- --test-threads=1

src/db/mod.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,38 @@ pub(crate) struct EventRow {
158158

159159
#[cfg(test)]
160160
pub(crate) async fn open_test_pool() -> DbPool {
161-
let pool = pool::create_write_pool("sqlite::memory:").await.unwrap();
161+
#[cfg(all(feature = "sqlite", not(feature = "postgres")))]
162+
let url = "sqlite::memory:";
163+
164+
#[cfg(all(feature = "postgres", not(feature = "sqlite")))]
165+
let url_owned = std::env::var("DATABASE_URL")
166+
.unwrap_or_else(|_| "postgres://stackpit:stackpit@localhost:5432/stackpit_test".into());
167+
#[cfg(all(feature = "postgres", not(feature = "sqlite")))]
168+
let url = url_owned.as_str();
169+
170+
let pool = pool::create_write_pool(url).await.unwrap();
162171
pool::run_migrations(&pool).await.unwrap();
172+
173+
// Postgres tests share a real database -- clean all data between tests.
174+
// TRUNCATE CASCADE handles foreign key ordering for us.
175+
#[cfg(all(feature = "postgres", not(feature = "sqlite")))]
176+
{
177+
sqlx::query(
178+
"TRUNCATE events, issues, logs, spans, metrics, attachments, \
179+
issue_tag_values, integrations, project_integrations, \
180+
alert_rules, alert_state, digest_schedules, \
181+
project_keys, projects, releases, sourcemaps, upload_chunks, \
182+
discard_stats, discarded_fingerprints, inbound_filters, \
183+
message_filters, rate_limits, environment_filters, \
184+
release_filters, user_agent_filters, filter_rules, \
185+
ip_blocklist, project_repos, sync_state \
186+
CASCADE",
187+
)
188+
.execute(&pool)
189+
.await
190+
.unwrap();
191+
}
192+
163193
pool
164194
}
165195

0 commit comments

Comments
 (0)