Skip to content

Commit c246d97

Browse files
committed
fix: covering migrations for wrappers across all versions`
1 parent 69b35ff commit c246d97

File tree

2 files changed

+60
-11
lines changed

2 files changed

+60
-11
lines changed

ansible/vars.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ postgres_major:
1010

1111
# Full version strings for each major version
1212
postgres_release:
13-
postgresorioledb-17: "17.5.1.051-orioledb"
14-
postgres17: "17.6.1.030"
15-
postgres15: "15.14.1.030"
13+
postgresorioledb-17: "17.5.1.052-orioledb-wmig-1"
14+
postgres17: "17.6.1.031-wmig-1"
15+
postgres15: "15.14.1.031-wmig-1"
1616

1717
# Non Postgres Extensions
1818
pgbouncer_release: 1.19.0

nix/ext/wrappers/default.nix

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,12 @@ let
171171
};
172172
}
173173
);
174-
previouslyPackagedVersions = [
174+
# All versions that were previously packaged (historical list)
175+
allPreviouslyPackagedVersions = [
176+
"0.4.3"
177+
"0.4.2"
178+
"0.4.1"
179+
"0.3.0"
175180
"0.2.0"
176181
"0.1.19"
177182
"0.1.18"
@@ -191,14 +196,19 @@ let
191196
"0.1.1"
192197
"0.1.0"
193198
];
194-
numberOfPreviouslyPackagedVersions = builtins.length previouslyPackagedVersions;
195199
allVersions = (builtins.fromJSON (builtins.readFile ../versions.json)).wrappers;
196200
supportedVersions = lib.filterAttrs (
197201
_: value: builtins.elem (lib.versions.major postgresql.version) value.postgresql
198202
) allVersions;
199203
versions = lib.naturalSort (lib.attrNames supportedVersions);
200204
latestVersion = lib.last versions;
201205
numberOfVersions = builtins.length versions;
206+
# Filter out previously packaged versions that are actually built for this PG version
207+
# This prevents double-counting when a version appears in both lists
208+
previouslyPackagedVersions = builtins.filter (
209+
v: !(builtins.elem v versions)
210+
) allPreviouslyPackagedVersions;
211+
numberOfPreviouslyPackagedVersions = builtins.length previouslyPackagedVersions;
202212
packages = builtins.attrValues (
203213
lib.mapAttrs (name: value: build name value.hash value.rust value.pgrx) supportedVersions
204214
);
@@ -231,26 +241,64 @@ buildEnv {
231241
}
232242
233243
create_migration_sql_files() {
244+
# buildEnv creates symlinks in $out/share/postgresql/extension
245+
# But we need to write new migration files there
246+
# The Nix store is immutable at the path level, so we need to:
247+
# 1. Save all the symlinked files to a temporary location
248+
# 2. Remove the entire symlinked share/postgresql/extension tree
249+
# 3. Recreate it as real directories with actual files (not symlinks)
250+
# 4. Then we can add our migration files
251+
252+
TEMP_DIR=$(mktemp -d)
253+
254+
# Copy all existing SQL and control files, dereferencing symlinks
255+
if [ -d "$out/share/postgresql/extension" ]; then
256+
cp -rL $out/share/postgresql/extension/* $TEMP_DIR/ 2>/dev/null || true
257+
# Need to remove from parent and recreate to avoid immutable symlinked structure
258+
chmod -R u+w $out/share/postgresql 2>/dev/null || true
259+
rm -rf $out/share/postgresql/extension
260+
fi
261+
262+
# Recreate the entire path as real directories
263+
mkdir -p $out/share/postgresql/extension
264+
265+
# Copy everything back
266+
if [ "$(ls -A $TEMP_DIR 2>/dev/null)" ]; then
267+
cp -r $TEMP_DIR/* $out/share/postgresql/extension/
268+
fi
269+
234270
PREVIOUS_VERSION=""
235271
while IFS= read -r i; do
236272
FILENAME=$(basename "$i")
237-
DIRNAME=$(dirname "$i")
238273
VERSION="$(grep -oE '[0-9]+\.[0-9]+\.[0-9]+' <<< $FILENAME)"
239274
if [[ "$PREVIOUS_VERSION" != "" ]]; then
240-
echo "Processing $i"
241-
MIGRATION_FILENAME="$DIRNAME/''${FILENAME/$VERSION/$PREVIOUS_VERSION--$VERSION}"
242-
cp "$i" "$MIGRATION_FILENAME"
275+
# Always write to $out/share/postgresql/extension, not $DIRNAME
276+
# because $DIRNAME might be a symlinked read-only path from the Nix store
277+
# We use -L with cp to dereference symlinks (copy the actual file content, not the symlink)
278+
MIGRATION_FILENAME="$out/share/postgresql/extension/''${FILENAME/$VERSION/$PREVIOUS_VERSION--$VERSION}"
279+
cp -L "$i" "$MIGRATION_FILENAME"
243280
fi
244281
PREVIOUS_VERSION="$VERSION"
245282
done < <(find $out -name '*.sql' | sort -V)
246283
284+
# Create empty SQL files for previously packaged versions that don't exist
285+
# This compensates for versions that failed to produce SQL files in the past
286+
for prev_version in ${lib.concatStringsSep " " previouslyPackagedVersions}; do
287+
sql_file="$out/share/postgresql/extension/wrappers--$prev_version.sql"
288+
if [ ! -f "$sql_file" ]; then
289+
echo "-- Empty migration file for previously packaged version $prev_version" > "$sql_file"
290+
fi
291+
done
292+
247293
# Create migration SQL files from previous versions to newer versions
294+
# Skip if the migration file already exists (to avoid conflicts with the first loop)
248295
for prev_version in ${lib.concatStringsSep " " previouslyPackagedVersions}; do
249296
for curr_version in ${lib.concatStringsSep " " versions}; do
250297
if [[ "$(printf '%s\n%s' "$prev_version" "$curr_version" | sort -V | head -n1)" == "$prev_version" ]] && [[ "$prev_version" != "$curr_version" ]]; then
251298
main_sql_file="$out/share/postgresql/extension/wrappers--$curr_version.sql"
252-
if [ -f "$main_sql_file" ]; then
253-
new_file="$out/share/postgresql/extension/wrappers--$prev_version--$curr_version.sql"
299+
new_file="$out/share/postgresql/extension/wrappers--$prev_version--$curr_version.sql"
300+
# Only create if it doesn't already exist (first loop may have created it)
301+
if [ -f "$main_sql_file" ] && [ ! -f "$new_file" ]; then
254302
cp "$main_sql_file" "$new_file"
255303
sed -i 's|$libdir/wrappers-[0-9.]*|$libdir/wrappers|g' "$new_file"
256304
fi
@@ -263,6 +311,7 @@ buildEnv {
263311
create_lib_files
264312
create_migration_sql_files
265313
314+
# Verify library count matches expected
266315
(test "$(ls -A $out/lib/${pname}*${postgresql.dlSuffix} | wc -l)" = "${
267316
toString (numberOfVersions + numberOfPreviouslyPackagedVersions + 1)
268317
}")

0 commit comments

Comments
 (0)