Files
kls/links/db.go
Kyle Isom 0fa85cb300 migrate to SQLite and prepare for MCP deployment
Switch from PostgreSQL to SQLite (modernc.org/sqlite, pure Go) for
simpler deployment on the MCP platform. Fix URL normalization to
preserve query parameters so sites like YouTube deduplicate correctly.
Add Dockerfile, Makefile, and MCP service definition. Add pg2sqlite
migration tool. Support $PORT env var for MCP port assignment.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-27 16:18:37 -07:00

40 lines
818 B
Go

package links
import (
"context"
"database/sql"
"fmt"
"git.wntrmute.dev/kyle/goutils/config"
_ "modernc.org/sqlite"
)
const (
kDBPath = "DB_PATH"
defaultPath = "kls.db"
)
const createTable = `CREATE TABLE IF NOT EXISTS urls (
id TEXT PRIMARY KEY,
url TEXT NOT NULL,
nurl TEXT NOT NULL,
short TEXT NOT NULL UNIQUE,
created_at TEXT NOT NULL
)`
// Connect opens the SQLite database and ensures the schema exists.
func Connect(ctx context.Context) (*sql.DB, error) {
path := config.GetDefault(kDBPath, defaultPath)
db, err := sql.Open("sqlite", path)
if err != nil {
return nil, fmt.Errorf("database: %w", err)
}
if _, err := db.ExecContext(ctx, createTable); err != nil {
db.Close()
return nil, fmt.Errorf("database: schema init: %w", err)
}
return db, nil
}