Skip to content

Commit 6dd7b9e

Browse files
committed
fix(schema): make migration 058 and runner resilient to AGE cold start
058 uses AGE Cypher DDL (CREATE VLABEL/ELABEL) which can fail on cold start when AGE isn't fully initialized. Wrap each label creation in EXCEPTION handler so failures log a notice instead of aborting — labels get created on first use anyway. Also add ON_ERROR_STOP=1 to psql in migrate-db.sh so failed migrations don't self-register via the INSERT at the end of the file. Previously, a mid-file failure would still record the migration as applied, then the runner would exit, blocking all subsequent migrations.
1 parent be408ba commit 6dd7b9e

2 files changed

Lines changed: 32 additions & 17 deletions

File tree

operator/database/migrate-db.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ if [ -f "/.dockerenv" ] || grep -q docker /proc/1/cgroup 2>/dev/null; then
4343
PGPASSWORD="${POSTGRES_PASSWORD}" psql -h "$DB_HOST" -U "$DB_USER" -d "$DB_NAME" "$@"
4444
}
4545
run_psql_file() {
46-
PGPASSWORD="${POSTGRES_PASSWORD}" psql -h "$DB_HOST" -U "$DB_USER" -d "$DB_NAME" -f "$1"
46+
PGPASSWORD="${POSTGRES_PASSWORD}" psql -h "$DB_HOST" -U "$DB_USER" -d "$DB_NAME" -v ON_ERROR_STOP=1 -f "$1"
4747
}
4848
else
4949
INSIDE_CONTAINER=false
@@ -52,7 +52,7 @@ else
5252
docker exec "$CONTAINER" psql -U "$DB_USER" -d "$DB_NAME" "$@"
5353
}
5454
run_psql_file() {
55-
docker exec -i "$CONTAINER" psql -U "$DB_USER" -d "$DB_NAME" < "$1"
55+
docker exec -i "$CONTAINER" psql -U "$DB_USER" -d "$DB_NAME" -v ON_ERROR_STOP=1 < "$1"
5656
}
5757
fi
5858

schema/migrations/058_precreate_graph_labels.sql

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
-- Fix: explicitly create all known vertex and edge labels at schema init time.
99
-- Uses DO blocks to check ag_catalog.ag_label before creating (AGE has no
1010
-- CREATE VLABEL IF NOT EXISTS syntax).
11+
--
12+
-- Note: Uses EXCEPTION handlers so label creation failures (e.g., AGE not fully
13+
-- initialized on cold start) are logged as warnings rather than aborting the migration.
1114

1215
LOAD 'age';
1316
SET search_path = ag_catalog, "$user", public;
@@ -27,11 +30,15 @@ BEGIN
2730
JOIN ag_catalog.ag_graph g ON l.graph = g.graphid
2831
WHERE g.name = 'knowledge_graph' AND l.name = lbl AND l.kind = 'v'
2932
) THEN
30-
EXECUTE format(
31-
'SELECT * FROM cypher(''knowledge_graph'', $$ CREATE VLABEL %I $$) as (a agtype)',
32-
lbl
33-
);
34-
RAISE NOTICE 'Created vertex label: %', lbl;
33+
BEGIN
34+
EXECUTE format(
35+
'SELECT * FROM cypher(''knowledge_graph'', $$ CREATE VLABEL %I $$) as (a agtype)',
36+
lbl
37+
);
38+
RAISE NOTICE 'Created vertex label: %', lbl;
39+
EXCEPTION WHEN OTHERS THEN
40+
RAISE NOTICE 'Could not create vertex label % (will be created on first use): %', lbl, SQLERRM;
41+
END;
3542
END IF;
3643
END LOOP;
3744
END $$;
@@ -55,11 +62,15 @@ BEGIN
5562
JOIN ag_catalog.ag_graph g ON l.graph = g.graphid
5663
WHERE g.name = 'knowledge_graph' AND l.name = lbl AND l.kind = 'e'
5764
) THEN
58-
EXECUTE format(
59-
'SELECT * FROM cypher(''knowledge_graph'', $$ CREATE ELABEL %I $$) as (a agtype)',
60-
lbl
61-
);
62-
RAISE NOTICE 'Created edge label: %', lbl;
65+
BEGIN
66+
EXECUTE format(
67+
'SELECT * FROM cypher(''knowledge_graph'', $$ CREATE ELABEL %I $$) as (a agtype)',
68+
lbl
69+
);
70+
RAISE NOTICE 'Created edge label: %', lbl;
71+
EXCEPTION WHEN OTHERS THEN
72+
RAISE NOTICE 'Could not create edge label % (will be created on first use): %', lbl, SQLERRM;
73+
END;
6374
END IF;
6475
END LOOP;
6576
END $$;
@@ -98,11 +109,15 @@ BEGIN
98109
JOIN ag_catalog.ag_graph g ON l.graph = g.graphid
99110
WHERE g.name = 'knowledge_graph' AND l.name = lbl AND l.kind = 'e'
100111
) THEN
101-
EXECUTE format(
102-
'SELECT * FROM cypher(''knowledge_graph'', $$ CREATE ELABEL %I $$) as (a agtype)',
103-
lbl
104-
);
105-
RAISE NOTICE 'Created edge label: %', lbl;
112+
BEGIN
113+
EXECUTE format(
114+
'SELECT * FROM cypher(''knowledge_graph'', $$ CREATE ELABEL %I $$) as (a agtype)',
115+
lbl
116+
);
117+
RAISE NOTICE 'Created edge label: %', lbl;
118+
EXCEPTION WHEN OTHERS THEN
119+
RAISE NOTICE 'Could not create edge label % (will be created on first use): %', lbl, SQLERRM;
120+
END;
106121
END IF;
107122
END LOOP;
108123
END $$;

0 commit comments

Comments
 (0)