Skip to content

Commit dc94660

Browse files
fix(postgres): render the declared databases in a stable order
keys returns a map's keys in Go's randomized iteration order, so a release with several databases rendered DEFINED_DBS in a different order on each upgrade. The init-script Secret, and the checksum the init Job carries, then changed on upgrades that left the databases untouched, which makes a no-op upgrade look like a real change in the release history. Assisted-by: LLM Signed-off-by: Mattia Eleuteri <[email protected]>
1 parent 55aad6f commit dc94660

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

‎packages/apps/postgres/templates/init-script.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ stringData:
337337
RESTORE_MARK={{ with .Values.bootstrap.newServerName }}$(echo {{ b64enc . }} | base64 -d){{ end }}
338338
ADOPTED_MARK=$(psql -v ON_ERROR_STOP=1 -t -A --dbname postgres -c "SELECT current_setting('cozystack.adopted_restore', true)")
339339
MANAGED_DBS=$(psql -v ON_ERROR_STOP=1 -t -A -c "SELECT datname FROM pg_database d JOIN pg_shdescription s ON d.oid = s.objoid WHERE s.description = 'database managed by helm'")
340-
DEFINED_DBS="{{ join " " (keys .Values.databases) }}"
340+
DEFINED_DBS="{{ join " " (keys .Values.databases | sortAlpha) }}"
341341
DELETE_DBS=$(for db in $MANAGED_DBS; do case " $DEFINED_DBS " in *" $db "*) :;; *) echo $db;; esac; done)
342342
343343
if [ -n "$RESTORE_MARK" ] && [ "$ADOPTED_MARK" != "$RESTORE_MARK" ]; then
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
suite: init-script renders the same for the same values
2+
3+
# keys returns a map's keys in Go's randomized iteration order, so a release with
4+
# several databases rendered DEFINED_DBS in a different order on each upgrade. The
5+
# init-script Secret then changed on upgrades that changed nothing in the
6+
# databases, and so did the checksum the init Job carries.
7+
8+
templates:
9+
- templates/init-script.yaml
10+
11+
release:
12+
name: pg-test
13+
namespace: tenant-test
14+
15+
set:
16+
_cluster:
17+
scheduling: {}
18+
19+
tests:
20+
- it: lists the declared databases in a stable order
21+
set:
22+
databases:
23+
hotel: {}
24+
alpha: {}
25+
golf: {}
26+
charlie: {}
27+
foxtrot: {}
28+
bravo: {}
29+
echo: {}
30+
delta: {}
31+
documentSelector:
32+
path: metadata.name
33+
value: pg-test-init-script
34+
asserts:
35+
- matchRegex:
36+
path: stringData["init.sh"]
37+
pattern: 'DEFINED_DBS="alpha bravo charlie delta echo foxtrot golf hotel"'

0 commit comments

Comments
 (0)