Add initial database setup and user/artist management functionality

This commit is contained in:
zardzul
2026-03-13 20:42:50 +01:00
parent 5fa23b0d1c
commit f81b28417e
26 changed files with 585 additions and 64 deletions
+35
View File
@@ -0,0 +1,35 @@
package database
import (
"context"
"fmt"
"os"
"time"
db "zardzul/music-index/sqlc"
"github.com/jackc/pgx/v5/pgxpool"
)
func Connect() (*pgxpool.Pool, *db.Queries, error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
databaseURL := os.Getenv("DATABASE_URL")
if databaseURL == "" {
databaseURL = "postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable"
}
pool, err := pgxpool.New(ctx, databaseURL)
if err != nil {
return nil, nil, fmt.Errorf("create pool: %w", err)
}
if err := pool.Ping(ctx); err != nil {
pool.Close()
return nil, nil, fmt.Errorf("ping database: %w", err)
}
queries := db.New(pool)
return pool, queries, nil
}