Skip to content

Commit

Permalink
Merge pull request #32667 from hashicorp/backport/TF-4390-backport-in…
Browse files Browse the repository at this point in the history
…itial-remote-state-serial-fix/moderately-suited-lab

Backport of Begin cloud remote state with serial > 0 into v1.4
  • Loading branch information
brandonc committed Feb 10, 2023
2 parents a6b0426 + c2e7729 commit bcedbf9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
8 changes: 7 additions & 1 deletion internal/cloud/state.go
Expand Up @@ -140,6 +140,9 @@ func (s *State) PersistState(schemas *terraform.Schemas) error {
s.mu.Lock()
defer s.mu.Unlock()

log.Printf("[DEBUG] cloud/state: state read serial is: %d; serial is: %d", s.readSerial, s.serial)
log.Printf("[DEBUG] cloud/state: 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 @@ -157,13 +160,16 @@ 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] cloud/state: after refresh, state read serial is: %d; serial is: %d", s.readSerial, s.serial)
log.Printf("[DEBUG] cloud/state: 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
20 changes: 20 additions & 0 deletions internal/cloud/state_test.go
Expand Up @@ -250,3 +250,23 @@ func TestDelete_SafeDelete(t *testing.T) {
t.Fatalf("workspace %s not deleted", workspaceId)
}
}

func TestState_PersistState(t *testing.T) {
cloudState := testCloudState(t)

t.Run("Initial PersistState", func(t *testing.T) {
if cloudState.readState != nil {
t.Fatal("expected nil initial readState")
}

err := cloudState.PersistState(nil)
if err != nil {
t.Fatalf("expected no error, got %q", err)
}

var expectedSerial uint64 = 1
if cloudState.readSerial != expectedSerial {
t.Fatalf("expected initial state readSerial to be %d, got %d", expectedSerial, cloudState.readSerial)
}
})
}

0 comments on commit bcedbf9

Please sign in to comment.