Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Begin cloud remote state with serial > 0 #32647

Merged
merged 1 commit into from Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
}
})
}