migrate to postgres

This commit is contained in:
m
2026-07-12 23:49:25 +02:00
parent e7b418b004
commit e59fde6a48
15 changed files with 151 additions and 125 deletions
+12 -12
View File
@@ -3,22 +3,22 @@ package config
import "os"
type Config struct {
Port string
DBPath string
OCREndpoint string
UploadDir string
HMACSecret string
ServerHost string
Port string
DatabaseURL string
OCREndpoint string
UploadDir string
HMACSecret string
ServerHost string
}
func Load() *Config {
return &Config{
Port: envOr("PORT", "8080"),
DBPath: envOr("DB_PATH", "vaultdrop.db"),
OCREndpoint: envOr("OCR_ENDPOINT", "http://localhost:9090"),
UploadDir: envOr("UPLOAD_DIR", "./uploads"),
HMACSecret: envOr("HMAC_SECRET", "thisismyrandomstring"),
ServerHost: envOr("SERVER_HOST", "http://192.168.1.17:8080"),
Port: envOr("PORT", "8080"),
DatabaseURL: envOr("DATABASE_URL", "postgres://localhost:5432/vaultdrop?sslmode=disable"),
OCREndpoint: envOr("OCR_ENDPOINT", "http://localhost:9090"),
UploadDir: envOr("UPLOAD_DIR", "./uploads"),
HMACSecret: envOr("HMAC_SECRET", "thisismyrandomstring"),
ServerHost: envOr("SERVER_HOST", "http://192.168.1.17:8080"),
}
}
+6 -8
View File
@@ -5,25 +5,23 @@ import (
"fmt"
"os"
_ "modernc.org/sqlite"
_ "github.com/lib/pq"
)
const driver = "sqlite"
const driver = "postgres"
func Connect() (*sql.DB, error) {
path := os.Getenv("DB_PATH")
if path == "" {
path = "vaultdrop.db"
dsn := os.Getenv("DATABASE_URL")
if dsn == "" {
dsn = "postgres://localhost:5432/vaultdrop?sslmode=disable"
}
dsn := fmt.Sprintf("file:%s?_journal_mode=WAL&_busy_timeout=5000&_foreign_keys=on", path)
database, err := sql.Open(driver, dsn)
if err != nil {
return nil, fmt.Errorf("open db: %w", err)
}
database.SetMaxOpenConns(1)
database.SetMaxOpenConns(25)
if err := database.Ping(); err != nil {
return nil, fmt.Errorf("ping db: %w", err)
+10 -8
View File
@@ -7,11 +7,13 @@ package db
import (
"context"
"github.com/lib/pq"
)
const createFile = `-- name: CreateFile :one
INSERT INTO files (id, name, mime_type, size, storage_key, checksum, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
VALUES ($1, $2, $3, $4, $5, $6, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
RETURNING id, name, mime_type, size, storage_key, checksum, ocr_text, created_at, updated_at
`
@@ -50,7 +52,7 @@ func (q *Queries) CreateFile(ctx context.Context, arg CreateFileParams) (File, e
const deleteFile = `-- name: DeleteFile :exec
DELETE FROM files
WHERE id = ?
WHERE id = $1
`
func (q *Queries) DeleteFile(ctx context.Context, id string) error {
@@ -60,7 +62,7 @@ func (q *Queries) DeleteFile(ctx context.Context, id string) error {
const getFile = `-- name: GetFile :one
SELECT id, name, mime_type, size, storage_key, checksum, ocr_text, created_at, updated_at FROM files
WHERE id = ? LIMIT 1
WHERE id = $1 LIMIT 1
`
func (q *Queries) GetFile(ctx context.Context, id string) (File, error) {
@@ -120,12 +122,12 @@ func (q *Queries) ListFiles(ctx context.Context) ([]File, error) {
const listFilesByID = `-- name: ListFilesByID :many
SELECT id, name, mime_type, size, storage_key, checksum, ocr_text, created_at, updated_at FROM files
WHERE id IN (SELECT value FROM json_each(?))
WHERE id = ANY($1::text[])
ORDER BY created_at DESC
`
func (q *Queries) ListFilesByID(ctx context.Context, jsonEach interface{}) ([]File, error) {
rows, err := q.db.QueryContext(ctx, listFilesByID, jsonEach)
func (q *Queries) ListFilesByID(ctx context.Context, dollar_1 []string) ([]File, error) {
rows, err := q.db.QueryContext(ctx, listFilesByID, pq.Array(dollar_1))
if err != nil {
return nil, err
}
@@ -159,8 +161,8 @@ func (q *Queries) ListFilesByID(ctx context.Context, jsonEach interface{}) ([]Fi
const updateFile = `-- name: UpdateFile :exec
UPDATE files
SET name = ?, mime_type = ?, ocr_text = ?, updated_at = CURRENT_TIMESTAMP
WHERE id = ?
SET name = $1, mime_type = $2, ocr_text = $3, updated_at = CURRENT_TIMESTAMP
WHERE id = $4
`
type UpdateFileParams struct {
+2 -2
View File
@@ -13,9 +13,9 @@ const getHealth = `-- name: GetHealth :one
SELECT 1 AS ok
`
func (q *Queries) GetHealth(ctx context.Context) (int64, error) {
func (q *Queries) GetHealth(ctx context.Context) (int32, error) {
row := q.db.QueryRowContext(ctx, getHealth)
var ok int64
var ok int32
err := row.Scan(&ok)
return ok, err
}
+13 -5
View File
@@ -3,14 +3,22 @@ package db
import (
"fmt"
"log"
"os"
"database/sql"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/sqlite"
"github.com/golang-migrate/migrate/v4/database/postgres"
"github.com/golang-migrate/migrate/v4/source/file"
)
func RunMigrations(migrationsURL string) error {
migDB, err := Connect()
dsn := os.Getenv("DATABASE_URL")
if dsn == "" {
dsn = "postgres://localhost:5432/vaultdrop?sslmode=disable"
}
migDB, err := sql.Open("postgres", dsn)
if err != nil {
return fmt.Errorf("open migration db: %w", err)
}
@@ -21,12 +29,12 @@ func RunMigrations(migrationsURL string) error {
return fmt.Errorf("open migrations source: %w", err)
}
dbDriver, err := sqlite.WithInstance(migDB, &sqlite.Config{})
dbDriver, err := postgres.WithInstance(migDB, &postgres.Config{})
if err != nil {
return fmt.Errorf("create sqlite driver: %w", err)
return fmt.Errorf("create postgres driver: %w", err)
}
m, err := migrate.NewWithInstance("file", sourceDriver, "sqlite", dbDriver)
m, err := migrate.NewWithInstance("file", sourceDriver, "postgres", dbDriver)
if err != nil {
return fmt.Errorf("create migrate instance: %w", err)
}
@@ -1,11 +1,12 @@
-- VaultDrop 002: Files table
CREATE TABLE files (
id TEXT PRIMARY KEY NOT NULL,
name TEXT NOT NULL,
mime_type TEXT NOT NULL DEFAULT '',
size INTEGER NOT NULL DEFAULT 0,
id TEXT PRIMARY KEY NOT NULL,
name TEXT NOT NULL,
mime_type TEXT NOT NULL DEFAULT '',
size BIGINT NOT NULL DEFAULT 0,
storage_key TEXT NOT NULL,
checksum TEXT NOT NULL DEFAULT '',
ocr_text TEXT NOT NULL DEFAULT '',
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
checksum TEXT NOT NULL DEFAULT '',
ocr_text TEXT NOT NULL DEFAULT '',
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
+6 -6
View File
@@ -1,6 +1,6 @@
-- name: GetFile :one
SELECT * FROM files
WHERE id = ? LIMIT 1;
WHERE id = $1 LIMIT 1;
-- name: ListFiles :many
SELECT * FROM files
@@ -8,19 +8,19 @@ ORDER BY created_at DESC;
-- name: ListFilesByID :many
SELECT * FROM files
WHERE id IN (SELECT value FROM json_each(?))
WHERE id = ANY($1::text[])
ORDER BY created_at DESC;
-- name: CreateFile :one
INSERT INTO files (id, name, mime_type, size, storage_key, checksum, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
VALUES ($1, $2, $3, $4, $5, $6, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
RETURNING *;
-- name: UpdateFile :exec
UPDATE files
SET name = ?, mime_type = ?, ocr_text = ?, updated_at = CURRENT_TIMESTAMP
WHERE id = ?;
SET name = $1, mime_type = $2, ocr_text = $3, updated_at = CURRENT_TIMESTAMP
WHERE id = $4;
-- name: DeleteFile :exec
DELETE FROM files
WHERE id = ?;
WHERE id = $1;
+2
View File
@@ -1,6 +1,7 @@
package handler
import (
"log"
"net/http"
"path"
"strconv"
@@ -51,6 +52,7 @@ func (h *FileHandler) Upload(c *gin.Context) {
func (h *FileHandler) List(c *gin.Context) {
files, err := h.files.List()
if err != nil {
log.Printf("ERROR List files: %v", err)
api.Error(c, http.StatusInternalServerError, "DB_ERROR", "Failed to list files")
return
}