Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(notebooks): preserve markdown migration history author (#67137)

Marius Andrab2d9d589dd9d
diff --git a/products/notebooks/backend/markdown_migration.py b/products/notebooks/backend/markdown_migration.py
index 670b3fca68de..7a6151016c8e 100644
--- a/products/notebooks/backend/markdown_migration.py
+++ b/products/notebooks/backend/markdown_migration.py
@@ -1,4 +1,5 @@
from collections.abc import Callable
+from datetime import timedelta
from typing import Any, cast
from django.db import transaction
@@ -139,7 +140,7 @@ def _convert_notebook(notebook: Notebook, *, user: User, content: dict[str, Any]
if is_markdown_notebook_content(locked_notebook.content):
return False
- before_update = Notebook.objects.get(pk=locked_notebook.pk)
+ before_update = Notebook.objects.select_related("created_by", "last_modified_by").get(pk=locked_notebook.pk)
annotated_content = annotate_python_nodes(content)
locked_notebook.content = annotated_content
locked_notebook.text_content = text_content
@@ -170,10 +171,10 @@ def _convert_notebook(notebook: Notebook, *, user: User, content: dict[str, Any]
notebook=locked_notebook,
organization_id=cast(UUIDT, locked_notebook.team.organization_id),
team_id=locked_notebook.team_id,
- user=user,
+ user=before_update.last_modified_by or before_update.created_by or user,
was_impersonated=False,
changes=changes,
- created_at=before_update.last_modified_at,
+ created_at=before_update.last_modified_at + timedelta(seconds=1),
)
return True
diff --git a/products/notebooks/backend/test/test_markdown_migration.py b/products/notebooks/backend/test/test_markdown_migration.py
index fe00267d8a64..a10a4f10a80f 100644
--- a/products/notebooks/backend/test/test_markdown_migration.py
+++ b/products/notebooks/backend/test/test_markdown_migration.py
@@ -328,7 +328,9 @@ class TestNotebookMarkdownMigration(BaseTest):
assert changes_by_field["version"]["after"] == 8
assert "last_modified_at" not in changes_by_field
assert "last_modified_by" not in changes_by_field
- assert log.created_at == original_modified_at
+ assert log.user_id == original_modifier.id
+ assert log.user_id != self.user.id
+ assert log.created_at == original_modified_at + timedelta(seconds=1)
mock_publish.assert_called_once_with(self.team.id, notebook.short_id, 8, diff=None)
def test_apply_scopes_mention_label_lookup_to_notebook_organization(self) -> None: