Skip to content

Commit 4d320f3

Browse files
committed
Optimise migrations on startup
The migrations are only started if there are some that have not been applied. Additionally the maintenace task needed after an update are now run after the migrations
1 parent 60428d5 commit 4d320f3

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

docker/docker-entrypoint.sh

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ source /opt/netbox/venv/bin/activate
1515
DB_WAIT_TIMEOUT=${DB_WAIT_TIMEOUT-3}
1616
MAX_DB_WAIT_TIME=${MAX_DB_WAIT_TIME-30}
1717
CUR_DB_WAIT_TIME=0
18-
while ! ./manage.py migrate 2>&1 && [ "${CUR_DB_WAIT_TIME}" -lt "${MAX_DB_WAIT_TIME}" ]; do
18+
while ! ./manage.py showmigrations >/dev/null 2>&1 && [ "${CUR_DB_WAIT_TIME}" -lt "${MAX_DB_WAIT_TIME}" ]; do
1919
echo "⏳ Waiting on DB... (${CUR_DB_WAIT_TIME}s / ${MAX_DB_WAIT_TIME}s)"
2020
sleep "${DB_WAIT_TIMEOUT}"
2121
CUR_DB_WAIT_TIME=$((CUR_DB_WAIT_TIME + DB_WAIT_TIMEOUT))
@@ -24,6 +24,19 @@ if [ "${CUR_DB_WAIT_TIME}" -ge "${MAX_DB_WAIT_TIME}" ]; then
2424
echo "❌ Waited ${MAX_DB_WAIT_TIME}s or more for the DB to become ready."
2525
exit 1
2626
fi
27+
# Check if update is needed
28+
if ! ./manage.py migrate --check >/dev/null 2>&1; then
29+
echo "⚙️ Applying database migrations"
30+
./manage.py migrate --no-input
31+
echo "⚙️ Running trace_paths"
32+
./manage.py trace_paths --no-input
33+
echo "⚙️ Removing stale content types"
34+
./manage.py remove_stale_contenttypes --no-input
35+
echo "⚙️ Removing expired user sessions"
36+
./manage.py clearsessions
37+
echo "⚙️ Clearing cache data"
38+
./manage.py invalidate all
39+
fi
2740

2841
# Create Superuser if required
2942
if [ "$SKIP_SUPERUSER" == "true" ]; then

0 commit comments

Comments
 (0)