add --reset for db

trunk
vilmibm 2023-01-08 00:22:04 -08:00
parent 0c6df57629
commit 0607fe2545
2 changed files with 30 additions and 0 deletions

View File

@ -73,6 +73,16 @@ func newServer() (*gameWorldServer, error) {
return nil, err
}
// TODO --reset flag
reset := flag.Bool("reset", false, "fully reset the database to its initial state")
flag.Parse()
if *reset {
if err = db.Erase(); err != nil {
return nil, fmt.Errorf("failed to reset database: %w", err)
}
}
if err = db.Ensure(); err != nil {
return nil, fmt.Errorf("failed to ensure default entities: %w", err)
}

View File

@ -33,6 +33,7 @@ type DB interface {
// Defaults
Ensure() error
Erase() error
// Presence
SessionIDForAvatar(Object) (string, error)
@ -79,6 +80,25 @@ func NewDB(connURL string) (DB, error) {
return pgdb, nil
}
// Erase fully destroys the database's contents, dropping all tables.
func (db *pgDB) Erase() (err error) {
stmts := []string{
"DROP SCHEMA public CASCADE",
"CREATE SCHEMA public",
"GRANT ALL ON SCHEMA public TO postgres",
"GRANT ALL ON SCHEMA public TO public",
"COMMENT ON SCHEMA public IS 'standard public schema'",
}
for _, stmt := range stmts {
if _, err = db.pool.Exec(context.Background(), stmt); err != nil {
return
}
}
return nil
}
// Ensure checks for and then creates default resources if they do not exist (like the Foyer)
func (db *pgDB) Ensure() error {
// TODO this is sloppy but shrug