feat(backend): pg schema + store queries incl. path-keyed progress

This commit is contained in:
2026-09-04 23:48:05 +08:00
parent d4972d6d4c
commit c1761d0f3e
7 changed files with 504 additions and 14 deletions
+28
View File
@@ -0,0 +1,28 @@
package db
import (
"context"
_ "embed"
"fmt"
"github.com/jackc/pgx/v5/pgxpool"
)
//go:embed schema.sql
var schema string
func Connect(ctx context.Context, url string) (*pgxpool.Pool, error) {
cfg, err := pgxpool.ParseConfig(url)
if err != nil {
return nil, err
}
cfg.MaxConns = 10
return pgxpool.NewWithConfig(ctx, cfg)
}
func Migrate(ctx context.Context, p *pgxpool.Pool) error {
if _, err := p.Exec(ctx, schema); err != nil {
return fmt.Errorf("migrate: %w", err)
}
return nil
}
+21
View File
@@ -0,0 +1,21 @@
CREATE TABLE IF NOT EXISTS users (
id BIGSERIAL PRIMARY KEY, username TEXT UNIQUE NOT NULL,
password_hash TEXT NOT NULL, role TEXT NOT NULL CHECK (role IN ('admin','member')),
created_at TIMESTAMPTZ NOT NULL DEFAULT now());
CREATE TABLE IF NOT EXISTS libraries (
id BIGSERIAL PRIMARY KEY, name TEXT NOT NULL, root_path TEXT UNIQUE NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now());
CREATE TABLE IF NOT EXISTS books (
id BIGSERIAL PRIMARY KEY, library_id BIGINT NOT NULL REFERENCES libraries(id),
path TEXT NOT NULL, title TEXT NOT NULL,
format TEXT NOT NULL CHECK (format IN ('cbz','pdf','epub','txt','md')),
file_size BIGINT NOT NULL, mod_ts BIGINT NOT NULL, page_count INT NOT NULL DEFAULT 0,
state TEXT NOT NULL DEFAULT 'ready' CHECK (state IN ('ready','error')),
error_msg TEXT NOT NULL DEFAULT '', added_at TIMESTAMPTZ NOT NULL DEFAULT now(),
UNIQUE (library_id, path));
CREATE TABLE IF NOT EXISTS reading_progress (
user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
library_id BIGINT NOT NULL, book_path TEXT NOT NULL,
locator JSONB NOT NULL DEFAULT '{}', percent DOUBLE PRECISION NOT NULL DEFAULT 0,
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
PRIMARY KEY (user_id, library_id, book_path));