feat: drop sqlite support
All checks were successful
Deploy / build-and-deploy (push) Successful in 1m27s
All checks were successful
Deploy / build-and-deploy (push) Successful in 1m27s
This commit is contained in:
parent
5e00060421
commit
da718427bd
27 changed files with 1296 additions and 115 deletions
|
|
@ -3,25 +3,16 @@ package db
|
|||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
_ "github.com/jackc/pgx/v5/stdlib"
|
||||
"github.com/jmoiron/sqlx"
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
func Init(driver, connection string) (*sqlx.DB, error) {
|
||||
if driver == "sqlite" {
|
||||
dir := filepath.Dir(connection)
|
||||
err := os.MkdirAll(dir, 0755)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create data directory: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
db, err := sqlx.Connect(driver, connection)
|
||||
// Init opens a PostgreSQL connection pool. The connection string must be a
|
||||
// libpq-style URL or DSN supported by the pgx stdlib driver.
|
||||
func Init(connection string) (*sqlx.DB, error) {
|
||||
db, err := sqlx.Connect("pgx", connection)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to connect: %w", err)
|
||||
}
|
||||
|
|
@ -30,10 +21,9 @@ func Init(driver, connection string) (*sqlx.DB, error) {
|
|||
db.SetMaxIdleConns(5)
|
||||
db.SetConnMaxLifetime(5 * time.Minute)
|
||||
|
||||
slog.Info("database connected", "driver", driver)
|
||||
slog.Info("database connected", "driver", "pgx")
|
||||
|
||||
err = db.Ping()
|
||||
if err != nil {
|
||||
if err := db.Ping(); err != nil {
|
||||
return nil, fmt.Errorf("failed to ping database: %w", err)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -9,22 +9,8 @@ import (
|
|||
"github.com/pressly/goose/v3"
|
||||
)
|
||||
|
||||
var dialectMap = map[string]string{
|
||||
"sqlite": "sqlite3",
|
||||
"pgx": "postgres",
|
||||
}
|
||||
|
||||
func getDialect(driver string) string {
|
||||
dialect, ok := dialectMap[driver]
|
||||
if ok {
|
||||
return dialect
|
||||
}
|
||||
return driver
|
||||
}
|
||||
|
||||
func setupGoose(driver string) error {
|
||||
err := goose.SetDialect(getDialect(driver))
|
||||
if err != nil {
|
||||
func setupGoose() error {
|
||||
if err := goose.SetDialect("postgres"); err != nil {
|
||||
return fmt.Errorf("failed to set dialect: %w", err)
|
||||
}
|
||||
|
||||
|
|
@ -34,22 +20,18 @@ func setupGoose(driver string) error {
|
|||
}
|
||||
|
||||
goose.SetBaseFS(migrationsDir)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func RunMigrations(db *sql.DB, driver string) error {
|
||||
err := setupGoose(driver)
|
||||
if err != nil {
|
||||
func RunMigrations(db *sql.DB) error {
|
||||
if err := setupGoose(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = goose.Up(db, ".")
|
||||
if err != nil {
|
||||
if err := goose.Up(db, "."); err != nil {
|
||||
return fmt.Errorf("failed to run migrations: %w", err)
|
||||
}
|
||||
|
||||
slog.Info("migrations completed successfully")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ CREATE TABLE space_audit_logs (
|
|||
action TEXT NOT NULL,
|
||||
target_user_id TEXT REFERENCES users(id) ON DELETE SET NULL,
|
||||
target_email TEXT,
|
||||
metadata JSONB NOT NULL DEFAULT '{}',
|
||||
metadata JSONB NOT NULL DEFAULT '{}'::jsonb,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ CREATE TABLE transaction_audit_logs (
|
|||
transaction_id TEXT NOT NULL,
|
||||
actor_id TEXT REFERENCES users(id) ON DELETE SET NULL,
|
||||
action TEXT NOT NULL,
|
||||
metadata JSONB NOT NULL DEFAULT '{}',
|
||||
metadata JSONB NOT NULL DEFAULT '{}'::jsonb,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,22 @@
|
|||
-- +goose Up
|
||||
-- +goose StatementBegin
|
||||
-- The account-scoped activity feeds filter audit rows by metadata->>'account_id'.
|
||||
-- A partial expression index is the right shape for this access pattern in
|
||||
-- PostgreSQL 17: it is small (only the rows where the field exists), uses a
|
||||
-- standard B-tree (cheap equality + ORDER BY created_at), and avoids the bloat
|
||||
-- of a full GIN over the metadata document.
|
||||
|
||||
CREATE INDEX idx_space_audit_logs_account_id
|
||||
ON space_audit_logs ((metadata->>'account_id'), created_at DESC)
|
||||
WHERE action LIKE 'account.%';
|
||||
|
||||
CREATE INDEX idx_transaction_audit_logs_account_id
|
||||
ON transaction_audit_logs ((metadata->>'account_id'), created_at DESC)
|
||||
WHERE metadata ? 'account_id';
|
||||
-- +goose StatementEnd
|
||||
|
||||
-- +goose Down
|
||||
-- +goose StatementBegin
|
||||
DROP INDEX IF EXISTS idx_space_audit_logs_account_id;
|
||||
DROP INDEX IF EXISTS idx_transaction_audit_logs_account_id;
|
||||
-- +goose StatementEnd
|
||||
Loading…
Add table
Add a link
Reference in a new issue