From 2a0f7ca71ee35942ad9e326bceb97853722fadea Mon Sep 17 00:00:00 2001 From: David Code Howard Date: Fri, 15 Sep 2023 13:56:17 -0400 Subject: [PATCH] chore: Make migration for project membership list --- ...e_project_group_project_membership_list.py | 31 ++++++++++ .../migrations/0016_auto_20230915_1527.py | 57 +++++++++++++++++++ .../migrations/0017_auto_20230915_1527.py | 26 +++++++++ 3 files changed, 114 insertions(+) create mode 100644 terraso_backend/apps/project_management/migrations/0015_remove_project_group_project_membership_list.py create mode 100644 terraso_backend/apps/project_management/migrations/0016_auto_20230915_1527.py create mode 100644 terraso_backend/apps/project_management/migrations/0017_auto_20230915_1527.py diff --git a/terraso_backend/apps/project_management/migrations/0015_remove_project_group_project_membership_list.py b/terraso_backend/apps/project_management/migrations/0015_remove_project_group_project_membership_list.py new file mode 100644 index 000000000..b3277acae --- /dev/null +++ b/terraso_backend/apps/project_management/migrations/0015_remove_project_group_project_membership_list.py @@ -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" + ), + ), + ] diff --git a/terraso_backend/apps/project_management/migrations/0016_auto_20230915_1527.py b/terraso_backend/apps/project_management/migrations/0016_auto_20230915_1527.py new file mode 100644 index 000000000..8866910ee --- /dev/null +++ b/terraso_backend/apps/project_management/migrations/0016_auto_20230915_1527.py @@ -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)] diff --git a/terraso_backend/apps/project_management/migrations/0017_auto_20230915_1527.py b/terraso_backend/apps/project_management/migrations/0017_auto_20230915_1527.py new file mode 100644 index 000000000..d00adffd1 --- /dev/null +++ b/terraso_backend/apps/project_management/migrations/0017_auto_20230915_1527.py @@ -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, + ), + ), + ]