DB Initialisation: try named parameters

Should be tested on Windows, as that's where this code will be used most
often. As of now, untested.
This commit is contained in:
Sybren A. Stüvel 2022-02-22 12:06:54 +01:00
parent 77f1e02c75
commit 80df8fa6e4

View File

@ -22,6 +22,7 @@ package persistence
import ( import (
"bufio" "bufio"
"database/sql"
"errors" "errors"
"fmt" "fmt"
"os" "os"
@ -56,23 +57,16 @@ func InitialSetup() error {
// Has to be used by the regular Flamenco Manager runs as well, though. // Has to be used by the regular Flamenco Manager runs as well, though.
username := "flamenco" username := "flamenco"
userPass := "flamenco" userPass := "flamenco"
// tx := db.Exec("CREATE USER flamenco PASSWORD ? NOSUPERUSER NOCREATEDB NOCREATEROLE INHERIT LOGIN", userPass)
// if tx.Error != nil { tx := db.Exec("CREATE USER @user PASSWORD @pass NOSUPERUSER NOCREATEDB NOCREATEROLE INHERIT LOGIN",
// return fmt.Errorf("unable to create database user '%s': %w", username, tx.Error) sql.Named("user", username),
// } sql.Named("pass", userPass))
{ if tx.Error != nil {
sqlDB, err := db.DB() return fmt.Errorf("unable to create database user '%s': %w", username, tx.Error)
if err != nil {
panic(err)
}
_, err = sqlDB.Exec("CREATE USER flamenco WITH PASSWORD $1::string NOSUPERUSER NOCREATEDB NOCREATEROLE INHERIT LOGIN", userPass)
if err != nil {
panic(err)
}
} }
// Create the databases. // Create the databases.
tx := db.Debug().Exec("CREATE DATABASE flamenco OWNER ? ENCODING 'utf8'", username) tx = db.Debug().Exec("CREATE DATABASE flamenco OWNER ? ENCODING 'utf8'", username)
if tx.Error != nil { if tx.Error != nil {
return fmt.Errorf("unable to create database 'flamenco': %w", tx.Error) return fmt.Errorf("unable to create database 'flamenco': %w", tx.Error)
} }