From bf1161364bf36d649fd3eb4cb1dcb1e3237dadd0 Mon Sep 17 00:00:00 2001 From: Michael Matloka Date: Tue, 5 Nov 2024 13:53:05 +0100 Subject: [PATCH] chore(dev): Introduce `django-linear-migrations` (#25989) --- ee/migrations/max_migration.txt | 1 + latest_migrations.manifest | 11 ------ posthog/management/commands/makemigrations.py | 23 ----------- posthog/migrations/max_migration.txt | 1 + posthog/settings/web.py | 2 + posthog/test/test_latest_migrations.py | 38 ------------------- requirements-dev.in | 1 + requirements-dev.txt | 3 ++ 8 files changed, 8 insertions(+), 72 deletions(-) create mode 100644 ee/migrations/max_migration.txt delete mode 100644 latest_migrations.manifest delete mode 100644 posthog/management/commands/makemigrations.py create mode 100644 posthog/migrations/max_migration.txt delete mode 100644 posthog/test/test_latest_migrations.py diff --git a/ee/migrations/max_migration.txt b/ee/migrations/max_migration.txt new file mode 100644 index 00000000000..21d256f058a --- /dev/null +++ b/ee/migrations/max_migration.txt @@ -0,0 +1 @@ +0016_rolemembership_organization_member diff --git a/latest_migrations.manifest b/latest_migrations.manifest deleted file mode 100644 index 275043afc5b..00000000000 --- a/latest_migrations.manifest +++ /dev/null @@ -1,11 +0,0 @@ -admin: 0003_logentry_add_action_flag_choices -auth: 0012_alter_user_first_name_max_length -axes: 0006_remove_accesslog_trusted -contenttypes: 0002_remove_content_type_name -ee: 0016_rolemembership_organization_member -otp_static: 0002_throttling -otp_totp: 0002_auto_20190420_0723 -posthog: 0510_salesforce_missing_schemas -sessions: 0001_initial -social_django: 0010_uid_db_index -two_factor: 0007_auto_20201201_1019 diff --git a/posthog/management/commands/makemigrations.py b/posthog/management/commands/makemigrations.py deleted file mode 100644 index a9e0ea4f98e..00000000000 --- a/posthog/management/commands/makemigrations.py +++ /dev/null @@ -1,23 +0,0 @@ -"""Cause git to detect a merge conflict when two branches have migrations.""" - -from django.core.management.commands.makemigrations import ( - Command as MakeMigrationsCommand, -) -from django.db.migrations.loader import MigrationLoader - - -class Command(MakeMigrationsCommand): - def handle(self, *app_labels, **options): - # Generate a migrations manifest with latest migration on each app - super().handle(*app_labels, **options) - - loader = MigrationLoader(None, ignore_no_migrations=True) - apps = sorted(loader.migrated_apps) - graph = loader.graph - - with open("latest_migrations.manifest", "w", encoding="utf_8") as f: - for app_name in apps: - leaf_nodes = graph.leaf_nodes(app_name) - if len(leaf_nodes) != 1: - raise Exception("App {} has multiple leaf migrations!".format(app_name)) - f.write("{}: {}\n".format(app_name, leaf_nodes[0][1])) diff --git a/posthog/migrations/max_migration.txt b/posthog/migrations/max_migration.txt new file mode 100644 index 00000000000..7db3cbe8b30 --- /dev/null +++ b/posthog/migrations/max_migration.txt @@ -0,0 +1 @@ +0510_salesforce_missing_schemas diff --git a/posthog/settings/web.py b/posthog/settings/web.py index e9d537d609e..2c38f149371 100644 --- a/posthog/settings/web.py +++ b/posthog/settings/web.py @@ -127,6 +127,8 @@ MIDDLEWARE = [ if DEBUG: # Used on local devenv to reverse-proxy all of /i/* to capture-rs on port 3000 INSTALLED_APPS.append("revproxy") + # rebase_migration command + INSTALLED_APPS.append("django_linear_migrations") # Append Enterprise Edition as an app if available try: diff --git a/posthog/test/test_latest_migrations.py b/posthog/test/test_latest_migrations.py deleted file mode 100644 index 1d60179576f..00000000000 --- a/posthog/test/test_latest_migrations.py +++ /dev/null @@ -1,38 +0,0 @@ -import glob -import os -import pathlib -from unittest import TestCase - - -class TestLatestMigrations(TestCase): - def test_posthog_migration_is_in_sync_with_latest(self) -> None: - """ - regression test - - when manually merging and updating migrations it is possible for - latest_migrations.manifest get out of sync with the migrations files - - this protects against that - """ - latest_manifest_migration = self._get_latest_migration_from_manifest("posthog") - latest_migration_file = self._get_newest_migration_file(f"{pathlib.Path().resolve()}/posthog/migrations/*") - self.assertEqual(latest_manifest_migration, latest_migration_file) - - def test_ee_migrations_is_in_sync_with_latest(self): - latest_manifest_migration = self._get_latest_migration_from_manifest("ee") - latest_migration_file = self._get_newest_migration_file(f"{pathlib.Path().resolve()}/ee/migrations/*") - self.assertEqual(latest_manifest_migration, latest_migration_file) - - @staticmethod - def _get_newest_migration_file(path: str) -> str: - migrations = [file for file in glob.glob(path) if file.endswith(".py") and not file.endswith("__init__.py")] - latest_file = max(sorted(migrations)) - return os.path.basename(latest_file).replace(".py", "") - - @staticmethod - def _get_latest_migration_from_manifest(django_app: str) -> str: - root = pathlib.Path().resolve() - manifest = pathlib.Path(f"{root}/latest_migrations.manifest").read_text() - posthog_latest_migration = next(line for line in manifest.splitlines() if line.startswith(f"{django_app}: ")) - - return posthog_latest_migration.replace(f"{django_app}: ", "") diff --git a/requirements-dev.in b/requirements-dev.in index cade84b0364..81484c1a04b 100644 --- a/requirements-dev.in +++ b/requirements-dev.in @@ -18,6 +18,7 @@ mypy-extensions==1.0.0 datamodel-code-generator==0.26.1 djangorestframework-stubs~=3.14.5 django-stubs==5.0.4 +django-linear-migrations==2.16.* Faker==17.5.0 fakeredis[lua]==2.23.3 freezegun==1.2.2 diff --git a/requirements-dev.txt b/requirements-dev.txt index 5e60b95fa08..029a1ebf01e 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -74,8 +74,11 @@ datamodel-code-generator==0.26.1 django==4.2.15 # via # -c requirements.txt + # django-linear-migrations # django-stubs # django-stubs-ext +django-linear-migrations==2.16.0 + # via -r requirements-dev.in django-stubs==5.0.4 # via # -r requirements-dev.in