Skip to content

Commit 48696db

Browse files
Merge pull request #4 from Pix4D/reduce-diff-noise
real-life experience: reduce diff noise, do not take state lock
2 parents 8ee268c + e514b06 commit 48696db

File tree

4 files changed

+16
-18
lines changed

4 files changed

+16
-18
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1818

1919
### Changes
2020

21+
- Migration script: do not print any more the count `>>> 1/N`, because each time N changed, this was causing N spurious diffs, hiding the real elements that changed. The `terravalet_output_format` is now 2.
22+
- Migration script: do not take a lock; it is useless as long as the operations are strictly on a local state file. This speeds up the runtime.
23+
2124
### New
2225

2326
- Generate also the DOWN migration script.

main.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,15 @@ func match(create, destroy []string) (map[string]string, map[string]string, erro
170170
func script(matches map[string]string, statePath string, out io.Writer) error {
171171
fmt.Fprintf(out, "#! /usr/bin/sh\n")
172172
fmt.Fprintf(out, "# DO NOT EDIT. Generated by terravalet.\n")
173-
fmt.Fprintf(out, "# terravalet_output_format=1\n")
173+
fmt.Fprintf(out, "# terravalet_output_format=2\n")
174174
fmt.Fprintf(out, "#\n")
175175
fmt.Fprintf(out, "# This script will move %d items.\n\n", len(matches))
176176
fmt.Fprintf(out, "set -e\n\n")
177177

178-
cmd := fmt.Sprintf("terraform state mv -state=%s", statePath)
179-
total := len(matches)
178+
// -lock=false greatly speeds up operations when the state has many elements
179+
// and is safe as long as we use -state=FILE, since this keeps operations
180+
// strictly local, without considering the configured backend.
181+
cmd := fmt.Sprintf("terraform state mv -lock=false -state=%s", statePath)
180182

181183
// Go maps are unordered. We want instead a stable iteration order, to make it
182184
// possible to compare scripts.
@@ -188,7 +190,6 @@ func script(matches map[string]string, statePath string, out io.Writer) error {
188190

189191
i := 1
190192
for _, d := range destroys {
191-
fmt.Fprintf(out, "echo \">>> %d/%d\"\n", i, total)
192193
fmt.Fprintf(out, "%s \\\n '%s' \\\n '%s'\n\n", cmd, d, matches[d])
193194
i++
194195
}

testdata/001_synthetic.down.sh

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
#! /usr/bin/sh
22
# DO NOT EDIT. Generated by terravalet.
3-
# terravalet_output_format=1
3+
# terravalet_output_format=2
44
#
55
# This script will move 3 items.
66

77
set -e
88

9-
echo ">>> 1/3"
10-
terraform state mv -state=local.tfstate \
9+
terraform state mv -lock=false -state=local.tfstate \
1110
'aws_batch_compute_environment.concourse_gpu_batch' \
1211
'module.ci.aws_batch_compute_environment.concourse_gpu_batch'
1312

14-
echo ">>> 2/3"
15-
terraform state mv -state=local.tfstate \
13+
terraform state mv -lock=false -state=local.tfstate \
1614
'aws_instance.bar' \
1715
'module.ci.aws_instance.bar'
1816

19-
echo ">>> 3/3"
20-
terraform state mv -state=local.tfstate \
17+
terraform state mv -lock=false -state=local.tfstate \
2118
'aws_instance.foo["cloud"]' \
2219
'module.ci.aws_instance.foo["cloud"]'
2320

testdata/001_synthetic.up.sh

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
#! /usr/bin/sh
22
# DO NOT EDIT. Generated by terravalet.
3-
# terravalet_output_format=1
3+
# terravalet_output_format=2
44
#
55
# This script will move 3 items.
66

77
set -e
88

9-
echo ">>> 1/3"
10-
terraform state mv -state=local.tfstate \
9+
terraform state mv -lock=false -state=local.tfstate \
1110
'module.ci.aws_batch_compute_environment.concourse_gpu_batch' \
1211
'aws_batch_compute_environment.concourse_gpu_batch'
1312

14-
echo ">>> 2/3"
15-
terraform state mv -state=local.tfstate \
13+
terraform state mv -lock=false -state=local.tfstate \
1614
'module.ci.aws_instance.bar' \
1715
'aws_instance.bar'
1816

19-
echo ">>> 3/3"
20-
terraform state mv -state=local.tfstate \
17+
terraform state mv -lock=false -state=local.tfstate \
2118
'module.ci.aws_instance.foo["cloud"]' \
2219
'aws_instance.foo["cloud"]'
2320

0 commit comments

Comments
 (0)