Files
repo/init_db.go

38 lines
706 B
Go
Raw Normal View History

2025-05-03 21:35:32 +03:00
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,
payload jsonb
2025-08-30 10:30:53 +03:00
)
`
2025-05-03 21:35:32 +03:00
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
}