diff --git a/bin/tests b/bin/tests index 02811c44230..e40e26511b2 100755 --- a/bin/tests +++ b/bin/tests @@ -28,5 +28,15 @@ PG_HOST="${PGHOST:=localhost}" PG_USER="${PGUSER:=posthog}" PG_PASSWORD="${PGPASSWORD:=posthog}" PG_PORT="${PGPORT:=5432}" -psql posthog -d "postgres://${PG_USER}:${PG_PASSWORD}@${PG_HOST}:${PG_PORT}" -c "drop database if exists test_posthog" -nodemon -w ./posthog -w ./ee --ext py --exec "OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES pytest --reuse-db -s $* --snapshot-update; mypy posthog ee" +PGOPTIONS='--client-min-messages=warning' psql posthog -d "postgres://${PG_USER}:${PG_PASSWORD}@${PG_HOST}:${PG_PORT}" -c "drop database if exists test_posthog" 1> /dev/null + +if [[ "$*" == *"migration"* ]] +then + echo "Looks like you're testing a migration. Running all migrations first." + MIGRATIONS="" +else + MIGRATIONS="--no-migrations" + export SKIP_TRIGRAM_INDEX_FOR_TESTS=1 +fi + +nodemon -w ./posthog -w ./ee --ext py --exec "OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES pytest --reuse-db --durations-min=2.0 ${MIGRATIONS} -s $* --snapshot-update; mypy posthog ee" diff --git a/posthog/conftest.py b/posthog/conftest.py index 6d8277794fa..b37885744ad 100644 --- a/posthog/conftest.py +++ b/posthog/conftest.py @@ -1,3 +1,5 @@ +import threading + import pytest from django.conf import settings from infi.clickhouse_orm import Database @@ -60,8 +62,18 @@ def create_clickhouse_tables(num_tables: int): if num_tables == len(TABLES_TO_CREATE_DROP): return + jobs = [] for item in TABLES_TO_CREATE_DROP: - sync_execute(item) + thread = threading.Thread(target=sync_execute, args=(item,)) + jobs.append(thread) + + # Start the threads (i.e. calculate the random number lists) + for j in jobs: + j.start() + + # Ensure all of the threads have finished + for j in jobs: + j.join() def reset_clickhouse_tables(): @@ -95,8 +107,18 @@ def reset_clickhouse_tables(): TRUNCATE_GROUPS_TABLE_SQL, ] + jobs = [] for item in TABLES_TO_CREATE_DROP: - sync_execute(item) + thread = threading.Thread(target=sync_execute, args=(item,)) + jobs.append(thread) + + # Start the threads (i.e. calculate the random number lists) + for j in jobs: + j.start() + + # Ensure all of the threads have finished + for j in jobs: + j.join() @pytest.fixture(scope="package") diff --git a/posthog/models/event_definition.py b/posthog/models/event_definition.py index 5330108f8c0..54516de3698 100644 --- a/posthog/models/event_definition.py +++ b/posthog/models/event_definition.py @@ -1,3 +1,5 @@ +import os + from django.contrib.postgres.indexes import GinIndex from django.db import models from django.utils import timezone @@ -22,9 +24,15 @@ class EventDefinition(UUIDModel): class Meta: unique_together = ("team", "name") - indexes = [ - GinIndex(name="index_event_definition_name", fields=["name"], opclasses=["gin_trgm_ops"]), - ] # To speed up DB-based fuzzy searching + indexes = ( + [ + GinIndex( + name="index_event_definition_name", fields=["name"], opclasses=["gin_trgm_ops"] + ), # To speed up DB-based fuzzy searching + ] + if not os.environ.get("SKIP_TRIGRAM_INDEX_FOR_TESTS") + else [] + ) # This index breaks the --no-migrations option when running tests def __str__(self) -> str: return f"{self.name} / {self.team.name}" diff --git a/posthog/models/property_definition.py b/posthog/models/property_definition.py index 682d4ab9f30..f8682590a27 100644 --- a/posthog/models/property_definition.py +++ b/posthog/models/property_definition.py @@ -1,3 +1,5 @@ +import os + from django.contrib.postgres.indexes import GinIndex from django.db import models @@ -50,9 +52,15 @@ class PropertyDefinition(UUIDModel): class Meta: unique_together = ("team", "name") - indexes = [ - GinIndex(name="index_property_definition_name", fields=["name"], opclasses=["gin_trgm_ops"]), - ] # To speed up DB-based fuzzy searching + indexes = ( + [ + GinIndex( + name="index_property_definition_name", fields=["name"], opclasses=["gin_trgm_ops"] + ), # To speed up DB-based fuzzy searching + ] + if not os.environ.get("SKIP_TRIGRAM_INDEX_FOR_TESTS") + else [] + ) # This index breaks the --no-migrations option when running tests constraints = [ models.CheckConstraint(name="property_type_is_valid", check=models.Q(property_type__in=PropertyType.values)) ]