Skip to content

Commit

Permalink
chore: Make migration for project membership list
Browse files Browse the repository at this point in the history
  • Loading branch information
David Code Howard committed Sep 15, 2023
1 parent 211a576 commit 2a0f7ca
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Generated by Django 4.2.5 on 2023-09-15 15:24

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


class Migration(migrations.Migration):
dependencies = [
("collaboration", "0005_change_collaborator_to_editor"),
("project_management", "0014_site_privacy_alter_project_description"),
]

operations = [
migrations.AddField(
model_name="project",
name="membership_list",
field=models.OneToOneField(
null=True,
on_delete=django.db.models.deletion.CASCADE,
to="collaboration.membershiplist",
),
preserve_default=False,
),
migrations.AlterField(
model_name="project",
name="group",
field=models.OneToOneField(
null=True, on_delete=django.db.models.deletion.CASCADE, to="core.group"
),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Generated by Django 4.2.5 on 2023-09-15 15:27

from django.db import migrations

from apps.project_management import collaboration_roles


def migrate_forward(apps, schema_editor):
Project = apps.get_model("project_management", "Project")
MembershipList = apps.get_model("collaboration", "MembershipList")
Membership = apps.get_model("collaboration", "Membership")
# The example docs https://docs.djangoproject.com/en/4.2/ref/migration-operations/#runpython
# use the db_alias.
# probably not technically necessary because we don't use multiple DBs
db_alias = schema_editor.connection.alias
for project in Project.objects.using(db_alias).all():
group = project.group
project.membership_list = MembershipList.objects.using(db_alias).create(
membership_type="open",
enroll_method="join",
)
# add group memberships to membership list
for membership in group.memberships.all():
user_role = (
collaboration_roles.ROLE_MANAGER
if membership.user_role == "manager"
else collaboration_roles.ROLE_VIEWER
)

if (
Membership.objects.using(db_alias)
.filter(user=membership.user, membership_list=project.membership_list)
.exists()
):
# it seems like group constraints are not as strict as MembershipLists
# if there are several Memberships with the same user, just ignore
# (this was happening with my local DB instance)
continue
Membership.objects.using(db_alias).create(
user=membership.user,
membership_list=project.membership_list,
user_role=user_role,
membership_status="approved",
)
# clean up
membership.delete()
project.group.delete()
project.group = None
project.save()


class Migration(migrations.Migration):
dependencies = [
("project_management", "0015_remove_project_group_project_membership_list"),
]

operations = [migrations.RunPython(migrate_forward, atomic=True)]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 4.2.5 on 2023-09-15 15:27

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


class Migration(migrations.Migration):
dependencies = [
("project_management", "0016_auto_20230915_1527"),
]

operations = [
migrations.RemoveField(
model_name="project",
name="group",
),
migrations.AlterField(
model_name="project",
name="membership_list",
field=models.OneToOneField(
on_delete=django.db.models.deletion.CASCADE,
to="collaboration.membershiplist",
null=False,
),
),
]

0 comments on commit 2a0f7ca

Please sign in to comment.