Skip to content

Commit bcedbf9

Browse files
authored
Merge pull request #32667 from hashicorp/backport/TF-4390-backport-initial-remote-state-serial-fix/moderately-suited-lab
Backport of Begin cloud remote state with serial > 0 into v1.4
2 parents a6b0426 + c2e7729 commit bcedbf9

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

internal/cloud/state.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ func (s *State) PersistState(schemas *terraform.Schemas) error {
140140
s.mu.Lock()
141141
defer s.mu.Unlock()
142142

143+
log.Printf("[DEBUG] cloud/state: state read serial is: %d; serial is: %d", s.readSerial, s.serial)
144+
log.Printf("[DEBUG] cloud/state: state read lineage is: %s; lineage is: %s", s.readLineage, s.lineage)
145+
143146
if s.readState != nil {
144147
lineageUnchanged := s.readLineage != "" && s.lineage == s.readLineage
145148
serialUnchanged := s.readSerial != 0 && s.serial == s.readSerial
@@ -157,13 +160,16 @@ func (s *State) PersistState(schemas *terraform.Schemas) error {
157160
if err != nil {
158161
return fmt.Errorf("failed checking for existing remote state: %s", err)
159162
}
163+
log.Printf("[DEBUG] cloud/state: after refresh, state read serial is: %d; serial is: %d", s.readSerial, s.serial)
164+
log.Printf("[DEBUG] cloud/state: after refresh, state read lineage is: %s; lineage is: %s", s.readLineage, s.lineage)
165+
160166
if s.lineage == "" { // indicates that no state snapshot is present yet
161167
lineage, err := uuid.GenerateUUID()
162168
if err != nil {
163169
return fmt.Errorf("failed to generate initial lineage: %v", err)
164170
}
165171
s.lineage = lineage
166-
s.serial = 0
172+
s.serial++
167173
}
168174
}
169175

internal/cloud/state_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,23 @@ func TestDelete_SafeDelete(t *testing.T) {
250250
t.Fatalf("workspace %s not deleted", workspaceId)
251251
}
252252
}
253+
254+
func TestState_PersistState(t *testing.T) {
255+
cloudState := testCloudState(t)
256+
257+
t.Run("Initial PersistState", func(t *testing.T) {
258+
if cloudState.readState != nil {
259+
t.Fatal("expected nil initial readState")
260+
}
261+
262+
err := cloudState.PersistState(nil)
263+
if err != nil {
264+
t.Fatalf("expected no error, got %q", err)
265+
}
266+
267+
var expectedSerial uint64 = 1
268+
if cloudState.readSerial != expectedSerial {
269+
t.Fatalf("expected initial state readSerial to be %d, got %d", expectedSerial, cloudState.readSerial)
270+
}
271+
})
272+
}

0 commit comments

Comments
 (0)