38 lines
746 B
Go
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
|
||
|
}
|