Skip to content

Commit

Permalink
Also remove wal directory when destroying data
Browse files Browse the repository at this point in the history
If a wal directory is supplied then it's important to clean-out our wals
along with our data directory. This commit renames the existing
RemoveAll method to RemoveAllIfInitialized, which better represents what
it does, and has it rely on a RemoveAll method that cleans-up both data
directory and wal.
  • Loading branch information
lawrencejones committed May 10, 2019
1 parent 77cd0b7 commit b634e53
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
8 changes: 4 additions & 4 deletions cmd/keeper/cmd/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,7 @@ func (p *PostgresKeeper) resync(db, followedDB *cluster.DB, tryPgrewind bool) er
replSlot = common.StolonName(db.UID)
}

if err := pgm.RemoveAll(); err != nil {
if err := pgm.RemoveAllIfInitialized(); err != nil {
return fmt.Errorf("failed to remove the postgres data dir: %v", err)
}
if slog.IsDebug() {
Expand Down Expand Up @@ -1023,7 +1023,7 @@ func (p *PostgresKeeper) postgresKeeperSM(pctx context.Context) {
}

// Clean up cluster db datadir
if err = pgm.RemoveAll(); err != nil {
if err = pgm.RemoveAllIfInitialized(); err != nil {
log.Errorw("failed to remove the postgres data dir", zap.Error(err))
return
}
Expand Down Expand Up @@ -1082,7 +1082,7 @@ func (p *PostgresKeeper) postgresKeeperSM(pctx context.Context) {
log.Errorw("failed to stop pg instance", zap.Error(err))
return
}
if err = pgm.RemoveAll(); err != nil {
if err = pgm.RemoveAllIfInitialized(); err != nil {
log.Errorw("failed to remove the postgres data dir", zap.Error(err))
return
}
Expand Down Expand Up @@ -1144,7 +1144,7 @@ func (p *PostgresKeeper) postgresKeeperSM(pctx context.Context) {
log.Errorw("failed to stop pg instance", zap.Error(err))
return
}
if err = pgm.RemoveAll(); err != nil {
if err = pgm.RemoveAllIfInitialized(); err != nil {
log.Errorw("failed to remove the postgres data dir", zap.Error(err))
return
}
Expand Down
17 changes: 14 additions & 3 deletions internal/postgresql/postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func (p *Manager) Init(initConfig *InitConfig) error {
}
// remove the dataDir, so we don't end with an half initialized database
if err != nil {
os.RemoveAll(p.dataDir)
p.RemoveAll()
return err
}
return nil
Expand Down Expand Up @@ -237,7 +237,7 @@ func (p *Manager) Restore(command string) error {
// On every error remove the dataDir, so we don't end with an half initialized database
out:
if err != nil {
os.RemoveAll(p.dataDir)
p.RemoveAll()
return err
}
return nil
Expand Down Expand Up @@ -853,7 +853,7 @@ func (p *Manager) SyncFromFollowed(followedConnParams ConnParams, replSlot strin
return nil
}

func (p *Manager) RemoveAll() error {
func (p *Manager) RemoveAllIfInitialized() error {
initialized, err := p.IsInitialized()
if err != nil {
return fmt.Errorf("failed to retrieve instance state: %v", err)
Expand All @@ -869,6 +869,17 @@ func (p *Manager) RemoveAll() error {
if started {
return fmt.Errorf("cannot remove postregsql database. Instance is active")
}

return p.RemoveAll()
}

// RemoveAll entirely cleans up the data directory, including any wal directory if that
// exists outside of the data directory.
func (p *Manager) RemoveAll() error {
if p.walDir != "" {
os.RemoveAll(p.walDir)
}

return os.RemoveAll(p.dataDir)
}

Expand Down

0 comments on commit b634e53

Please sign in to comment.