add --reset for db
parent
0c6df57629
commit
0607fe2545
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue