0005_alter_team_members_alter_team_projects.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # Generated by Django 5.0.7 on 2024-07-24 15:06
  2. from django.db import migrations, models
  3. # Disallow teams to be assigned to projects with a differing organization
  4. TEAM_PROJECT_TRIGGER = """
  5. CREATE OR REPLACE FUNCTION check_teamproject_organization() RETURNS TRIGGER AS $$
  6. BEGIN
  7. IF NEW.team_id IS NOT NULL AND NEW.project_id IS NOT NULL THEN
  8. PERFORM 1
  9. FROM projects_project p
  10. JOIN teams_team t ON t.organization_id = p.organization_id
  11. WHERE p.id = NEW.project_id AND t.id = NEW.team_id;
  12. IF NOT FOUND THEN
  13. RAISE EXCEPTION 'Team and Project must belong to the same organization';
  14. END IF;
  15. END IF;
  16. RETURN NEW;
  17. END;
  18. $$ LANGUAGE plpgsql;
  19. DROP TRIGGER IF EXISTS check_teamproject_organization_trigger ON teams_team_projects;
  20. CREATE TRIGGER check_teamproject_organization_trigger
  21. BEFORE INSERT OR UPDATE ON teams_team_projects
  22. FOR EACH ROW EXECUTE FUNCTION check_teamproject_organization();
  23. """
  24. class Migration(migrations.Migration):
  25. dependencies = [
  26. ("organizations_ext", "0004_organizationsubscription_alter_organization_slug"),
  27. ("projects", "0015_rename_label_projectkey_name_projectkey_is_active"),
  28. ("teams", "0001_squashed_0004_alter_team_id_alter_team_members"),
  29. ]
  30. operations = [
  31. migrations.AlterField(
  32. model_name="team",
  33. name="members",
  34. field=models.ManyToManyField(
  35. blank=True,
  36. related_name="teams",
  37. to="organizations_ext.organizationuser",
  38. ),
  39. ),
  40. migrations.AlterField(
  41. model_name="team",
  42. name="projects",
  43. field=models.ManyToManyField(related_name="teams", to="projects.project"),
  44. ),
  45. migrations.RunSQL(TEAM_PROJECT_TRIGGER, ""),
  46. ]