first working version
This commit is contained in:
37
init_db.go
Normal file
37
init_db.go
Normal 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
|
||||
}
|
Reference in New Issue
Block a user