-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Make migration for project membership list
- Loading branch information
David Code Howard
committed
Sep 15, 2023
1 parent
211a576
commit 2a0f7ca
Showing
3 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
...d/apps/project_management/migrations/0015_remove_project_group_project_membership_list.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
), | ||
), | ||
] |
57 changes: 57 additions & 0 deletions
57
terraso_backend/apps/project_management/migrations/0016_auto_20230915_1527.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)] |
26 changes: 26 additions & 0 deletions
26
terraso_backend/apps/project_management/migrations/0017_auto_20230915_1527.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
), | ||
), | ||
] |