Skip to content

Commit

Permalink
chore(environments): Add migration to backfill projects (#20887)
Browse files Browse the repository at this point in the history
* chore(environments): Add migration to backfill projects

* Fix `noop`

* Add `project_id` to plugin server test setup

* Fix `project_id`

* Also add `posthog_project` to plugin server tests

* Update `createTeam`

* Fix func tests
  • Loading branch information
Twixes authored Mar 14, 2024
1 parent 74c2296 commit f02d045
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 9 deletions.
2 changes: 1 addition & 1 deletion latest_migrations.manifest
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ contenttypes: 0002_remove_content_type_name
ee: 0015_add_verified_properties
otp_static: 0002_throttling
otp_totp: 0002_auto_20190420_0723
posthog: 0396_projects_and_environments
posthog: 0397_projects_backfill
sessions: 0001_initial
social_django: 0010_uid_db_index
two_factor: 0007_auto_20201201_1019
14 changes: 12 additions & 2 deletions plugin-server/functional_tests/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,18 @@ export const createTeam = async (
token?: string,
sessionRecordingOptIn = true
) => {
const team = await insertRow(postgres, 'posthog_team', {
const id = Math.round(Math.random() * 1000000000)
await insertRow(postgres, 'posthog_project', {
// Every team (aka environment) must be a child of a project
id,
organization_id: organizationId,
name: 'TEST PROJECT',
created_at: new Date().toISOString(),
})
await insertRow(postgres, 'posthog_team', {
id,
organization_id: organizationId,
project_id: id,
app_urls: [],
name: 'TEST PROJECT',
event_names: [],
Expand All @@ -392,7 +402,7 @@ export const createTeam = async (
access_control: false,
slack_incoming_webhook,
})
return team.id
return id
}

export const createAction = async (action: Omit<RawAction, 'id'>, steps: Omit<ActionStep, 'id' | 'action_id'>[]) => {
Expand Down
24 changes: 20 additions & 4 deletions plugin-server/tests/helpers/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,15 @@ export async function createUserTeamAndOrganization(
joined_at: new Date().toISOString(),
updated_at: new Date().toISOString(),
})
await insertRow(db, 'posthog_project', {
id: teamId,
organization_id: organizationId,
name: 'TEST PROJECT',
created_at: new Date().toISOString(),
})
await insertRow(db, 'posthog_team', {
id: teamId,
project_id: teamId,
organization_id: organizationId,
app_urls: [],
name: 'TEST PROJECT',
Expand Down Expand Up @@ -315,10 +322,19 @@ export const createOrganization = async (pg: PostgresRouter) => {
}

export const createTeam = async (pg: PostgresRouter, organizationId: string, token?: string) => {
const team = await insertRow(pg, 'posthog_team', {
// KLUDGE: auto increment IDs can be racy in tests so we ensure IDs don't clash
id: Math.round(Math.random() * 1000000000),
// KLUDGE: auto increment IDs can be racy in tests so we ensure IDs don't clash
const id = Math.round(Math.random() * 1000000000)
await insertRow(pg, 'posthog_project', {
// Every team (aka environment) must be a child of a project
id,
organization_id: organizationId,
name: 'TEST PROJECT',
created_at: new Date().toISOString(),
})
await insertRow(pg, 'posthog_team', {
id,
organization_id: organizationId,
project_id: id,
app_urls: [],
name: 'TEST PROJECT',
event_names: [],
Expand All @@ -343,7 +359,7 @@ export const createTeam = async (pg: PostgresRouter, organizationId: string, tok
person_display_name_properties: [],
access_control: false,
})
return team.id
return id
}

export const createUser = async (pg: PostgresRouter, distinctId: string) => {
Expand Down
41 changes: 41 additions & 0 deletions posthog/migrations/0397_projects_backfill.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Generated by Django 4.1.13 on 2024-03-12 23:14

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("posthog", "0396_projects_and_environments"),
]

operations = [
migrations.SeparateDatabaseAndState(
database_operations=[
migrations.RunSQL(
sql="""
-- For each team without a parent project, create such a project
INSERT INTO posthog_project (id, name, created_at, organization_id)
SELECT id, name, created_at, organization_id
FROM posthog_team
WHERE project_id IS NULL;
-- At this point, all teams have a parent project, so we can safely set project_id on every team
UPDATE posthog_team
SET project_id = id;""",
reverse_sql=migrations.RunSQL.noop,
)
],
state_operations=[
migrations.AlterField(
model_name="team",
name="project",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="teams",
related_query_name="team",
to="posthog.project",
),
),
],
)
]
2 changes: 0 additions & 2 deletions posthog/models/team/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,6 @@ class Team(UUIDClassicModel):
on_delete=models.CASCADE,
related_name="teams",
related_query_name="team",
null=True,
blank=False,
)
api_token: models.CharField = models.CharField(
max_length=200,
Expand Down

0 comments on commit f02d045

Please sign in to comment.