Files
repo/init_db.go
Dmitry Fedotov b6dc6d3fce working version
2025-05-04 21:13:19 +03:00

38 lines
746 B
Go

package repo
import (
"context"
"database/sql"
_ "embed"
"errors"
"strings"
)
var initStatement = `
CREATE TABLE IF NOT EXISTS -- (
id text PRIMARY KEY,
created_at timestamp with time zone,
updated_at timestamp with time zone,
deleted_at timestamp with time zone,
payload jsonb
)`
func initDB(ctx context.Context, db *sql.DB, tablename string) error {
if tablename == "" {
return errors.Join(ErrInitRepo, errors.New("tablename may not be empty"))
}
if db == nil {
return errors.Join(ErrInitRepo, errors.New("db instance is nil"))
}
query := strings.Replace(initStatement, "--", tablename, 1)
_, err := db.ExecContext(ctx, query)
if err != nil {
return errors.Join(ErrInitRepo, err)
}
return nil
}