Skip to content

Commit fa6fa6c

Browse files
committed
Improve develpment.sh start up
There is a race condition between with the Tram infralinks dump and Hasura migrations. Wait for the conflicting Hasura migration to finish before trying to insert the tram links.
1 parent d5a691b commit fa6fa6c

1 file changed

Lines changed: 25 additions & 5 deletions

File tree

scripts/development.sh

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,33 @@ login() {
6262
fi
6363
}
6464

65+
where_exists_table() {
66+
echo "SELECT EXISTS (SELECT FROM pg_tables WHERE schemaname = '$1' AND tablename = '$2');"
67+
}
68+
69+
where_exists_external_source() {
70+
echo "SELECT EXISTS (SELECT FROM infrastructure_network.external_source WHERE value = '$1');"
71+
}
72+
6573
wait_for_database() {
6674
SUCCESS=false
6775
while ! $SUCCESS; do
68-
echo "$1: Checking if schema $2 and table $3 exist..."
69-
if [[ $(docker exec "$1" psql $ROUTES_DB_CONNECTION_STRING -AXqtc "SELECT EXISTS (SELECT FROM pg_tables WHERE schemaname = '$2' AND tablename = '$3');" 2> /dev/null) = "t" ]]; then
76+
echo "$1: Checking if SQL query is true: $2"
77+
if [[ $(docker exec "$1" psql $ROUTES_DB_CONNECTION_STRING -AXqtc "$2" 2> /dev/null) = "t" ]]; then
7078
SUCCESS=true
7179
fi
7280
sleep 2
7381
done
7482
}
7583

84+
wait_for_database_table() {
85+
wait_for_database "$1" "$(where_exists_table "$2" "$3")"
86+
}
87+
88+
wait_for_external_source() {
89+
wait_for_database "$1" "$(where_exists_external_source "$2")"
90+
}
91+
7692
download_bus_infralinks() {
7793
if [ -f "infraLinks.sql" ]; then
7894
echo "infraLinks.sql already exists, skipping download."
@@ -101,7 +117,7 @@ seed_bus_infra_links() {
101117

102118
echo "$1: Seeding Bus infrastructure links..."
103119

104-
wait_for_database "$1" infrastructure_network infrastructure_link
120+
wait_for_database_table "$1" infrastructure_network infrastructure_link
105121

106122
echo "$1: infraLinks.sql..."
107123
docker exec -i "$1" psql $ROUTES_DB_CONNECTION_STRING < "infraLinks.sql";
@@ -114,7 +130,11 @@ seed_tram_infra_links() {
114130

115131
echo "$1: Seeding Tram infrastructure links..."
116132

117-
wait_for_database "$1" infrastructure_network infrastructure_link
133+
# hsl_tram eventually gets populated by Hasura migrations.
134+
# We need to explicitly wait for the Hasura migration to happen before
135+
# we can populate the dump in, as the Hasura migration breaks if there
136+
# is already tram data in the table when it starts up.
137+
wait_for_external_source "$1" hsl_tram
118138

119139
echo "$1: tram_infraLinks.sql..."
120140
docker exec -i "$1" psql $ROUTES_DB_CONNECTION_STRING < "tram_infraLinks.sql";
@@ -347,7 +367,7 @@ setup_environment() {
347367

348368
start_docker_containers $DOCKER_TESTDB_IMAGE
349369

350-
wait_for_database testdb topology topology
370+
wait_for_database_table testdb topology topology
351371
import_dump $DUMP_ROUTES_FILENAME jore4e2e
352372
import_dump $DUMP_TIMETABLES_FILENAME timetablesdb
353373
import_dump $DUMP_STOPS_FILENAME stopdb

0 commit comments

Comments
 (0)