Replace Postgres with SQLite

Ten members and a handful of songs a week never needed a database server,
and the server was the last thing making this a two-container deployment.
modernc.org/sqlite is pure Go, so CGO_ENABLED=0 survives and the dependency
count is unchanged: pgx out, sqlite in.

The port stayed small because the driver matches $1-style placeholders
against argument ordinals exactly as pgx does, so no query needed rewriting
for parameters. What did change:

- timestamptz becomes timestamp holding UTC 'YYYY-MM-DD HH:MM:SS'. The
  declared type is what makes the driver return time.Time, and the
  fixed-width UTC string is what makes ordering and comparison against
  datetime('now') mean what they say.
- interval has no equivalent: sessions.idle_ttl is seconds, and the review
  edit window travels as a SQLite date modifier string.
- No stddev_pop, so the divisive and unified boards spell the population
  formula out, guarded with max(0.0, ...) because cancellation returns a
  tiny negative when every score is identical.
- foreign_keys is off by default, so the cascades only exist because the
  pragma is set on every connection.

Drops the postgres service, its healthcheck, the depends_on gate, the
startup retry loop and POSTGRES_PASSWORD. ./storage is now the whole
backup. Tests get a fresh database file per test and run everywhere
instead of skipping without TEST_DATABASE_URL.
This commit is contained in:
Esa Kataja
2026-08-02 20:47:41 +03:00
parent a9776c6dde
commit 1fe5211ae6
28 changed files with 440 additions and 389 deletions
+15 -15
View File
@@ -49,7 +49,7 @@ func makeAudio(t *testing.T, path string) {
func (a *app) readySubmission(t *testing.T, userID int64) *submission {
t.Helper()
var id int64
err := a.pool.QueryRow(context.Background(), `
err := a.db.QueryRowContext(context.Background(), `
insert into submissions (user_id, status, title, artist, genre)
values ($1, 'ready', 'Testikappale', 'Testiartisti', 'Metal') returning id`,
userID).Scan(&id)
@@ -58,7 +58,7 @@ func (a *app) readySubmission(t *testing.T, userID int64) *submission {
}
path := a.tmpPath(id, ".ogg")
makeAudio(t, path)
if _, err := a.pool.Exec(context.Background(),
if _, err := a.db.ExecContext(context.Background(),
`update submissions set tmp_path = $2 where id = $1`, id, path); err != nil {
t.Fatal(err)
}
@@ -96,13 +96,13 @@ func TestPublishIsAllOrNothing(t *testing.T) {
t.Fatalf("publish with an unwritable audio dir: status = %d, want 500", w.Code)
}
var songs, submissions int
if err := a.pool.QueryRow(ctx, `select count(*) from songs`).Scan(&songs); err != nil {
if err := a.db.QueryRowContext(ctx, `select count(*) from songs`).Scan(&songs); err != nil {
t.Fatal(err)
}
if songs != 0 {
t.Fatalf("orphan song row: %d rows with no audio file", songs)
}
if err := a.pool.QueryRow(ctx, `select count(*) from submissions`).Scan(&submissions); err != nil {
if err := a.db.QueryRowContext(ctx, `select count(*) from submissions`).Scan(&submissions); err != nil {
t.Fatal(err)
}
if submissions != 1 {
@@ -120,13 +120,13 @@ func TestPublishIsAllOrNothing(t *testing.T) {
t.Fatalf("publish: status = %d, want 303", w.Code)
}
var songID int64
if err := a.pool.QueryRow(ctx, `select id from songs`).Scan(&songID); err != nil {
if err := a.db.QueryRowContext(ctx, `select id from songs`).Scan(&songID); err != nil {
t.Fatal(err)
}
if _, err := os.Stat(a.audioPath(songID)); err != nil {
t.Fatalf("published song has no audio file: %v", err)
}
if err := a.pool.QueryRow(ctx, `select count(*) from submissions`).Scan(&submissions); err != nil {
if err := a.db.QueryRowContext(ctx, `select count(*) from submissions`).Scan(&submissions); err != nil {
t.Fatal(err)
}
if submissions != 0 {
@@ -154,7 +154,7 @@ func TestSubmissionQuota(t *testing.T) {
check(false, "no submissions")
for range 4 {
if _, err := a.pool.Exec(ctx,
if _, err := a.db.ExecContext(ctx,
`insert into submissions (user_id, status) values ($1, 'ready')`, id); err != nil {
t.Fatal(err)
}
@@ -163,7 +163,7 @@ func TestSubmissionQuota(t *testing.T) {
// Failures never count — yt-dlp rot and bad files are not the submitter's fault.
for range 10 {
if _, err := a.pool.Exec(ctx,
if _, err := a.db.ExecContext(ctx,
`insert into submissions (user_id, status) values ($1, 'failed')`, id); err != nil {
t.Fatal(err)
}
@@ -171,7 +171,7 @@ func TestSubmissionQuota(t *testing.T) {
check(false, "failures do not count")
// A published song still occupies a slot, even though its submission row is gone.
if _, err := a.pool.Exec(ctx, `
if _, err := a.db.ExecContext(ctx, `
insert into songs (title, artist, genre, audio_file, duration_seconds, submitted_by)
values ('T', 'A', 'Metal', '1.ogg', 60, $1)`, id); err != nil {
t.Fatal(err)
@@ -179,8 +179,8 @@ func TestSubmissionQuota(t *testing.T) {
check(true, "four in flight plus one published")
// Yesterday's submissions are outside the window.
if _, err := a.pool.Exec(ctx,
`update submissions set created_at = now() - interval '25 hours' where user_id = $1`,
if _, err := a.db.ExecContext(ctx,
`update submissions set created_at = datetime('now', '-25 hours') where user_id = $1`,
id); err != nil {
t.Fatal(err)
}
@@ -194,17 +194,17 @@ func TestRestartRecovery(t *testing.T) {
id := a.seedMember(t, "[email protected]")
for _, status := range []string{"queued", "downloading", "converting"} {
if _, err := a.pool.Exec(ctx,
if _, err := a.db.ExecContext(ctx,
`insert into submissions (user_id, status) values ($1, $2)`, id, status); err != nil {
t.Fatal(err)
}
}
if err := sweep(ctx, a.pool); err != nil {
if err := sweep(ctx, a.db); err != nil {
t.Fatal(err)
}
var stuck int
if err := a.pool.QueryRow(ctx,
if err := a.db.QueryRowContext(ctx,
`select count(*) from submissions where status <> 'failed'`).Scan(&stuck); err != nil {
t.Fatal(err)
}
@@ -212,7 +212,7 @@ func TestRestartRecovery(t *testing.T) {
t.Fatalf("%d submissions survived the sweep still in flight", stuck)
}
var msg string
if err := a.pool.QueryRow(ctx,
if err := a.db.QueryRowContext(ctx,
`select status_msg from submissions limit 1`).Scan(&msg); err != nil {
t.Fatal(err)
}