From 63e25d9e8a583d1b430d4cdcea94f9ee48bfa48d Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Mon, 20 Jan 2025 14:52:20 -0800 Subject: [PATCH 1/3] Avoid accessing user when running migrations --- bin/migrate | 3 +++ posthog/apps.py | 25 +++++++++++++------------ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/bin/migrate b/bin/migrate index 76645d6ae3714..82fafb2634d01 100755 --- a/bin/migrate +++ b/bin/migrate @@ -3,6 +3,7 @@ set -e SCRIPT_DIR=$(dirname "$(readlink -f "$0")") +export POSTHOG_RUNNING_MIGRATION=true # NOTE when running in docker, rust might not exist so we need to check for it if [ -d "$SCRIPT_DIR/../rust" ]; then bash $SCRIPT_DIR/../rust/bin/migrate-cyclotron @@ -26,3 +27,5 @@ python manage.py run_async_migrations --complete-noop-migrations python manage.py run_async_migrations --check wait $(jobs -p) # Make sure CH migrations are done before we exit + +unset POSTHOG_RUNNING_MIGRATION diff --git a/posthog/apps.py b/posthog/apps.py index 0329fc316eb4f..1d0ff094bce54 100644 --- a/posthog/apps.py +++ b/posthog/apps.py @@ -48,18 +48,19 @@ def ready(self): {"git_rev": get_git_commit_short(), "git_branch": get_git_branch()}, ) - try: - user = User.objects.filter(last_login__isnull=False).order_by("-last_login").first() - except: - user = None - - local_api_key = get_self_capture_api_token(user) - - if SELF_CAPTURE and local_api_key: - posthoganalytics.api_key = local_api_key - posthoganalytics.host = settings.SITE_URL - else: - posthoganalytics.disabled = True + if not os.getenv("POSTHOG_RUNNING_MIGRATION"): + try: + user = User.objects.filter(last_login__isnull=False).order_by("-last_login").first() + except: + user = None + + local_api_key = get_self_capture_api_token(user) + + if SELF_CAPTURE and local_api_key: + posthoganalytics.api_key = local_api_key + posthoganalytics.host = settings.SITE_URL + else: + posthoganalytics.disabled = True # load feature flag definitions if not already loaded if not posthoganalytics.disabled and posthoganalytics.feature_flag_definitions() is None: From 6364cf1031744f2d085d001e4001959fdddfbf01 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Mon, 20 Jan 2025 15:23:48 -0800 Subject: [PATCH 2/3] Move `get_self_capture_api_token` inside of try, catch --- bin/migrate | 3 --- posthog/apps.py | 23 +++++++++++------------ 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/bin/migrate b/bin/migrate index 82fafb2634d01..76645d6ae3714 100755 --- a/bin/migrate +++ b/bin/migrate @@ -3,7 +3,6 @@ set -e SCRIPT_DIR=$(dirname "$(readlink -f "$0")") -export POSTHOG_RUNNING_MIGRATION=true # NOTE when running in docker, rust might not exist so we need to check for it if [ -d "$SCRIPT_DIR/../rust" ]; then bash $SCRIPT_DIR/../rust/bin/migrate-cyclotron @@ -27,5 +26,3 @@ python manage.py run_async_migrations --complete-noop-migrations python manage.py run_async_migrations --check wait $(jobs -p) # Make sure CH migrations are done before we exit - -unset POSTHOG_RUNNING_MIGRATION diff --git a/posthog/apps.py b/posthog/apps.py index 1d0ff094bce54..bc22bc98d4a98 100644 --- a/posthog/apps.py +++ b/posthog/apps.py @@ -48,19 +48,18 @@ def ready(self): {"git_rev": get_git_commit_short(), "git_branch": get_git_branch()}, ) - if not os.getenv("POSTHOG_RUNNING_MIGRATION"): - try: - user = User.objects.filter(last_login__isnull=False).order_by("-last_login").first() - except: - user = None - + try: + user = User.objects.filter(last_login__isnull=False).order_by("-last_login").first() local_api_key = get_self_capture_api_token(user) - - if SELF_CAPTURE and local_api_key: - posthoganalytics.api_key = local_api_key - posthoganalytics.host = settings.SITE_URL - else: - posthoganalytics.disabled = True + except: + user = None + local_api_key = None + + if SELF_CAPTURE and local_api_key: + posthoganalytics.api_key = local_api_key + posthoganalytics.host = settings.SITE_URL + else: + posthoganalytics.disabled = True # load feature flag definitions if not already loaded if not posthoganalytics.disabled and posthoganalytics.feature_flag_definitions() is None: From 60ed602f3536c9edec794d6752d7af39a6d17354 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Mon, 20 Jan 2025 16:11:33 -0800 Subject: [PATCH 3/3] Revert "Move `get_self_capture_api_token` inside of try, catch" This reverts commit 6364cf1031744f2d085d001e4001959fdddfbf01. --- bin/migrate | 3 +++ posthog/apps.py | 23 ++++++++++++----------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/bin/migrate b/bin/migrate index 76645d6ae3714..82fafb2634d01 100755 --- a/bin/migrate +++ b/bin/migrate @@ -3,6 +3,7 @@ set -e SCRIPT_DIR=$(dirname "$(readlink -f "$0")") +export POSTHOG_RUNNING_MIGRATION=true # NOTE when running in docker, rust might not exist so we need to check for it if [ -d "$SCRIPT_DIR/../rust" ]; then bash $SCRIPT_DIR/../rust/bin/migrate-cyclotron @@ -26,3 +27,5 @@ python manage.py run_async_migrations --complete-noop-migrations python manage.py run_async_migrations --check wait $(jobs -p) # Make sure CH migrations are done before we exit + +unset POSTHOG_RUNNING_MIGRATION diff --git a/posthog/apps.py b/posthog/apps.py index bc22bc98d4a98..1d0ff094bce54 100644 --- a/posthog/apps.py +++ b/posthog/apps.py @@ -48,18 +48,19 @@ def ready(self): {"git_rev": get_git_commit_short(), "git_branch": get_git_branch()}, ) - try: - user = User.objects.filter(last_login__isnull=False).order_by("-last_login").first() + if not os.getenv("POSTHOG_RUNNING_MIGRATION"): + try: + user = User.objects.filter(last_login__isnull=False).order_by("-last_login").first() + except: + user = None + local_api_key = get_self_capture_api_token(user) - except: - user = None - local_api_key = None - - if SELF_CAPTURE and local_api_key: - posthoganalytics.api_key = local_api_key - posthoganalytics.host = settings.SITE_URL - else: - posthoganalytics.disabled = True + + if SELF_CAPTURE and local_api_key: + posthoganalytics.api_key = local_api_key + posthoganalytics.host = settings.SITE_URL + else: + posthoganalytics.disabled = True # load feature flag definitions if not already loaded if not posthoganalytics.disabled and posthoganalytics.feature_flag_definitions() is None: