pollo/lib/db.go
2024-06-28 14:23:35 -06:00

55 lines
1.3 KiB
Go

package lib
import (
"context"
"fmt"
"github.com/jackc/pgx/v4/pgxpool"
)
var dbPool *pgxpool.Pool
// Initializes the global database connection pool.
func InitializeDBPool(host, user, password, dbname string, port int) error {
// Construct the connection string using the provided parameters
connString := fmt.Sprintf("postgres://%s:%s@%s:%d/%s", user, password, host, port, dbname)
var err error
dbPool, err = pgxpool.Connect(context.Background(), connString)
if err != nil {
return err
}
return nil
}
func InitializeSchema(dbPool *pgxpool.Pool) error {
const schemaSQL = `
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL
);
CREATE TABLE IF NOT EXISTS rooms (
id TEXT NOT NULL PRIMARY KEY,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
userId TEXT NOT NULL,
roomName TEXT,
topicName TEXT,
visible BOOLEAN NOT NULL DEFAULT false,
scale TEXT NOT NULL DEFAULT '0.5,1,2,3,5'
);
`
// TODO: Add more tables to the schema
_, err := dbPool.Exec(context.Background(), schemaSQL)
if err != nil {
return err
}
return nil
}
// Returns the initialized database connection pool.
func GetDBPool() *pgxpool.Pool {
return dbPool
}