first working version

This commit is contained in:
Dmitry Fedotov
2025-05-03 21:35:32 +03:00
parent efc5bea070
commit 3c602c7679
9 changed files with 420 additions and 1 deletions

37
init_db.go Normal file
View File

@@ -0,0 +1,37 @@
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
}