0005_alter_team_members_alter_team_projects.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. CREATE OR REPLACE TRIGGER check_teamproject_organization_trigger
  20. BEFORE INSERT OR UPDATE ON teams_team_projects
  21. FOR EACH ROW EXECUTE FUNCTION check_teamproject_organization();
  22. """
  23. class Migration(migrations.Migration):
  24. dependencies = [
  25. ("organizations_ext", "0004_organizationsubscription_alter_organization_slug"),
  26. ("projects", "0015_rename_label_projectkey_name_projectkey_is_active"),
  27. ("teams", "0001_squashed_0004_alter_team_id_alter_team_members"),
  28. ]
  29. operations = [
  30. migrations.AlterField(
  31. model_name="team",
  32. name="members",
  33. field=models.ManyToManyField(
  34. blank=True,
  35. related_name="teams",
  36. to="organizations_ext.organizationuser",
  37. ),
  38. ),
  39. migrations.AlterField(
  40. model_name="team",
  41. name="projects",
  42. field=models.ManyToManyField(related_name="teams", to="projects.project"),
  43. ),
  44. migrations.RunSQL(TEAM_PROJECT_TRIGGER, ""),
  45. ]