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:
+16
-16
@@ -8,7 +8,7 @@ import (
|
||||
func (a *app) seedSong(t *testing.T, submitter int64, title string) int64 {
|
||||
t.Helper()
|
||||
var id int64
|
||||
err := a.pool.QueryRow(context.Background(), `
|
||||
err := a.db.QueryRowContext(context.Background(), `
|
||||
insert into songs (title, artist, genre, audio_file, duration_seconds, submitted_by)
|
||||
values ($1, 'Testiartisti', 'Metal', 'x.ogg', 120, $2) returning id`,
|
||||
title, submitter).Scan(&id)
|
||||
@@ -21,7 +21,7 @@ func (a *app) seedSong(t *testing.T, submitter int64, title string) int64 {
|
||||
func (a *app) seedReview(t *testing.T, songID, reviewerID int64, score int) int64 {
|
||||
t.Helper()
|
||||
var id int64
|
||||
err := a.pool.QueryRow(context.Background(), `
|
||||
err := a.db.QueryRowContext(context.Background(), `
|
||||
insert into reviews (song_id, reviewer_id, score, text)
|
||||
values ($1, $2, $3, 'sanat') returning id`, songID, reviewerID, score).Scan(&id)
|
||||
if err != nil {
|
||||
@@ -133,8 +133,8 @@ func TestQueueContents(t *testing.T) {
|
||||
|
||||
// Oldest first: a second unreviewed song comes after the first.
|
||||
older := a.seedSong(t, bertta, "Vanhempi")
|
||||
if _, err := a.pool.Exec(ctx,
|
||||
`update songs set created_at = now() - interval '2 days' where id = $1`, older); err != nil {
|
||||
if _, err := a.db.ExecContext(ctx,
|
||||
`update songs set created_at = datetime('now', '-2 days') where id = $1`, older); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
list, err = a.queue(ctx, aino, 0)
|
||||
@@ -165,7 +165,7 @@ func TestSongUnlocksWhenTheLastReviewGoes(t *testing.T) {
|
||||
t.Fatal("a reviewed song is still editable")
|
||||
}
|
||||
|
||||
if _, err := a.pool.Exec(ctx, `delete from reviews where id = $1`, reviewID); err != nil {
|
||||
if _, err := a.db.ExecContext(ctx, `delete from reviews where id = $1`, reviewID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
d, _ = a.song(ctx, aino, songID)
|
||||
@@ -192,8 +192,8 @@ func TestEditWindow(t *testing.T) {
|
||||
}
|
||||
|
||||
// Just inside the window.
|
||||
if _, err := a.pool.Exec(ctx,
|
||||
`update reviews set updated_at = now() - interval '29 minutes' where id = $1`,
|
||||
if _, err := a.db.ExecContext(ctx,
|
||||
`update reviews set updated_at = datetime('now', '-29 minutes') where id = $1`,
|
||||
reviewID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -203,8 +203,8 @@ func TestEditWindow(t *testing.T) {
|
||||
}
|
||||
|
||||
// Past it.
|
||||
if _, err := a.pool.Exec(ctx,
|
||||
`update reviews set updated_at = now() - interval '31 minutes' where id = $1`,
|
||||
if _, err := a.db.ExecContext(ctx,
|
||||
`update reviews set updated_at = datetime('now', '-31 minutes') where id = $1`,
|
||||
reviewID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -215,17 +215,17 @@ func TestEditWindow(t *testing.T) {
|
||||
|
||||
// The database is the authority, not the Go clock: the update and the delete both refuse.
|
||||
var n int64
|
||||
err = a.pool.QueryRow(ctx, `
|
||||
update reviews set score = 1, updated_at = now()
|
||||
where id = $1 and reviewer_id = $2 and updated_at > now() - $3::interval
|
||||
returning id`, reviewID, bertta, editWindow.String()).Scan(&n)
|
||||
err = a.db.QueryRowContext(ctx, `
|
||||
update reviews set score = 1, updated_at = datetime('now')
|
||||
where id = $1 and reviewer_id = $2 and updated_at > datetime('now', $3)
|
||||
returning id`, reviewID, bertta, editWindowAgo).Scan(&n)
|
||||
if err == nil {
|
||||
t.Fatal("an expired review was edited")
|
||||
}
|
||||
err = a.pool.QueryRow(ctx, `
|
||||
err = a.db.QueryRowContext(ctx, `
|
||||
delete from reviews
|
||||
where id = $1 and reviewer_id = $2 and updated_at > now() - $3::interval
|
||||
returning id`, reviewID, bertta, editWindow.String()).Scan(&n)
|
||||
where id = $1 and reviewer_id = $2 and updated_at > datetime('now', $3)
|
||||
returning id`, reviewID, bertta, editWindowAgo).Scan(&n)
|
||||
if err == nil {
|
||||
t.Fatal("an expired review was deleted")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user