Skip to content

Commit

Permalink
Merge pull request #32614 from yardbirdsax/fix/30670
Browse files Browse the repository at this point in the history
make remote state initial behavior the same as local state
  • Loading branch information
jbardin committed Feb 7, 2023
2 parents 896f806 + 2d9e3da commit 1307317
Show file tree
Hide file tree
Showing 3 changed files with 206 additions and 99 deletions.
1 change: 1 addition & 0 deletions internal/backend/local/backend_local.go
Expand Up @@ -284,6 +284,7 @@ func (b *Local) localRunForPlanFile(op *backend.Operation, pf *planfile.Reader,
))
return nil, snap, diags
}

if currentStateMeta != nil {
// If the caller sets this, we require that the stored prior state
// has the same metadata, which is an extra safety check that nothing
Expand Down
8 changes: 7 additions & 1 deletion internal/states/remote/state.go
Expand Up @@ -3,6 +3,7 @@ package remote
import (
"bytes"
"fmt"
"log"
"sync"

uuid "github.com/hashicorp/go-uuid"
Expand Down Expand Up @@ -158,6 +159,9 @@ func (s *State) PersistState(schemas *terraform.Schemas) error {
s.mu.Lock()
defer s.mu.Unlock()

log.Printf("[DEBUG] states/remote: state read serial is: %d; serial is: %d", s.readSerial, s.serial)
log.Printf("[DEBUG] states/remote: state read lineage is: %s; lineage is: %s", s.readLineage, s.lineage)

if s.readState != nil {
lineageUnchanged := s.readLineage != "" && s.lineage == s.readLineage
serialUnchanged := s.readSerial != 0 && s.serial == s.readSerial
Expand All @@ -175,13 +179,15 @@ func (s *State) PersistState(schemas *terraform.Schemas) error {
if err != nil {
return fmt.Errorf("failed checking for existing remote state: %s", err)
}
log.Printf("[DEBUG] states/remote: after refresh, state read serial is: %d; serial is: %d", s.readSerial, s.serial)
log.Printf("[DEBUG] states/remote: after refresh, state read lineage is: %s; lineage is: %s", s.readLineage, s.lineage)
if s.lineage == "" { // indicates that no state snapshot is present yet
lineage, err := uuid.GenerateUUID()
if err != nil {
return fmt.Errorf("failed to generate initial lineage: %v", err)
}
s.lineage = lineage
s.serial = 0
s.serial++
}
}

Expand Down

0 comments on commit 1307317

Please sign in to comment.