-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(migrations): fix django migration order dependency (#6302)
(cherry picked from commit 71c58ce) # Conflicts: # api/src/backend/api/migrations/0004_rbac.py # api/src/backend/api/migrations/0005_rbac_missing_admin_roles.py
- Loading branch information
Showing
3 changed files
with
293 additions
and
1 deletion.
There are no files selected for viewing
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
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,248 @@ | ||
# Generated by Django 5.1.1 on 2024-12-05 12:29 | ||
|
||
import uuid | ||
|
||
import django.db.models.deletion | ||
from django.conf import settings | ||
from django.db import migrations, models | ||
|
||
import api.rls | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("api", "0003_update_provider_unique_constraint_with_is_deleted"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="Role", | ||
fields=[ | ||
( | ||
"id", | ||
models.UUIDField( | ||
default=uuid.uuid4, | ||
editable=False, | ||
primary_key=True, | ||
serialize=False, | ||
), | ||
), | ||
("name", models.CharField(max_length=255)), | ||
("manage_users", models.BooleanField(default=False)), | ||
("manage_account", models.BooleanField(default=False)), | ||
("manage_billing", models.BooleanField(default=False)), | ||
("manage_providers", models.BooleanField(default=False)), | ||
("manage_integrations", models.BooleanField(default=False)), | ||
("manage_scans", models.BooleanField(default=False)), | ||
("unlimited_visibility", models.BooleanField(default=False)), | ||
("inserted_at", models.DateTimeField(auto_now_add=True)), | ||
("updated_at", models.DateTimeField(auto_now=True)), | ||
( | ||
"tenant", | ||
models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, to="api.tenant" | ||
), | ||
), | ||
], | ||
options={ | ||
"db_table": "roles", | ||
}, | ||
), | ||
migrations.CreateModel( | ||
name="RoleProviderGroupRelationship", | ||
fields=[ | ||
( | ||
"id", | ||
models.UUIDField( | ||
default=uuid.uuid4, | ||
editable=False, | ||
primary_key=True, | ||
serialize=False, | ||
), | ||
), | ||
("inserted_at", models.DateTimeField(auto_now_add=True)), | ||
( | ||
"tenant", | ||
models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, to="api.tenant" | ||
), | ||
), | ||
], | ||
options={ | ||
"db_table": "role_provider_group_relationship", | ||
}, | ||
), | ||
migrations.CreateModel( | ||
name="UserRoleRelationship", | ||
fields=[ | ||
( | ||
"id", | ||
models.UUIDField( | ||
default=uuid.uuid4, | ||
editable=False, | ||
primary_key=True, | ||
serialize=False, | ||
), | ||
), | ||
("inserted_at", models.DateTimeField(auto_now_add=True)), | ||
( | ||
"tenant", | ||
models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, to="api.tenant" | ||
), | ||
), | ||
], | ||
options={ | ||
"db_table": "role_user_relationship", | ||
}, | ||
), | ||
migrations.AddField( | ||
model_name="roleprovidergrouprelationship", | ||
name="provider_group", | ||
field=models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, to="api.providergroup" | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="roleprovidergrouprelationship", | ||
name="role", | ||
field=models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, to="api.role" | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="role", | ||
name="provider_groups", | ||
field=models.ManyToManyField( | ||
related_name="roles", | ||
through="api.RoleProviderGroupRelationship", | ||
to="api.providergroup", | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="userrolerelationship", | ||
name="role", | ||
field=models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, to="api.role" | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="userrolerelationship", | ||
name="user", | ||
field=models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="role", | ||
name="users", | ||
field=models.ManyToManyField( | ||
related_name="roles", | ||
through="api.UserRoleRelationship", | ||
to=settings.AUTH_USER_MODEL, | ||
), | ||
), | ||
migrations.AddConstraint( | ||
model_name="roleprovidergrouprelationship", | ||
constraint=models.UniqueConstraint( | ||
fields=("role_id", "provider_group_id"), | ||
name="unique_role_provider_group_relationship", | ||
), | ||
), | ||
migrations.AddConstraint( | ||
model_name="roleprovidergrouprelationship", | ||
constraint=api.rls.RowLevelSecurityConstraint( | ||
"tenant_id", | ||
name="rls_on_roleprovidergrouprelationship", | ||
statements=["SELECT", "INSERT", "UPDATE", "DELETE"], | ||
), | ||
), | ||
migrations.AddConstraint( | ||
model_name="userrolerelationship", | ||
constraint=models.UniqueConstraint( | ||
fields=("role_id", "user_id"), name="unique_role_user_relationship" | ||
), | ||
), | ||
migrations.AddConstraint( | ||
model_name="userrolerelationship", | ||
constraint=api.rls.RowLevelSecurityConstraint( | ||
"tenant_id", | ||
name="rls_on_userrolerelationship", | ||
statements=["SELECT", "INSERT", "UPDATE", "DELETE"], | ||
), | ||
), | ||
migrations.AddConstraint( | ||
model_name="role", | ||
constraint=models.UniqueConstraint( | ||
fields=("tenant_id", "name"), name="unique_role_per_tenant" | ||
), | ||
), | ||
migrations.AddConstraint( | ||
model_name="role", | ||
constraint=api.rls.RowLevelSecurityConstraint( | ||
"tenant_id", | ||
name="rls_on_role", | ||
statements=["SELECT", "INSERT", "UPDATE", "DELETE"], | ||
), | ||
), | ||
migrations.CreateModel( | ||
name="InvitationRoleRelationship", | ||
fields=[ | ||
( | ||
"id", | ||
models.UUIDField( | ||
default=uuid.uuid4, | ||
editable=False, | ||
primary_key=True, | ||
serialize=False, | ||
), | ||
), | ||
("inserted_at", models.DateTimeField(auto_now_add=True)), | ||
( | ||
"invitation", | ||
models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, to="api.invitation" | ||
), | ||
), | ||
( | ||
"role", | ||
models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, to="api.role" | ||
), | ||
), | ||
( | ||
"tenant", | ||
models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, to="api.tenant" | ||
), | ||
), | ||
], | ||
options={ | ||
"db_table": "role_invitation_relationship", | ||
}, | ||
), | ||
migrations.AddConstraint( | ||
model_name="invitationrolerelationship", | ||
constraint=models.UniqueConstraint( | ||
fields=("role_id", "invitation_id"), | ||
name="unique_role_invitation_relationship", | ||
), | ||
), | ||
migrations.AddConstraint( | ||
model_name="invitationrolerelationship", | ||
constraint=api.rls.RowLevelSecurityConstraint( | ||
"tenant_id", | ||
name="rls_on_invitationrolerelationship", | ||
statements=["SELECT", "INSERT", "UPDATE", "DELETE"], | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="role", | ||
name="invitations", | ||
field=models.ManyToManyField( | ||
related_name="roles", | ||
through="api.InvitationRoleRelationship", | ||
to="api.invitation", | ||
), | ||
), | ||
] |
44 changes: 44 additions & 0 deletions
44
api/src/backend/api/migrations/0005_rbac_missing_admin_roles.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,44 @@ | ||
from django.db import migrations | ||
|
||
from api.db_router import MainRouter | ||
|
||
|
||
def create_admin_role(apps, schema_editor): | ||
Tenant = apps.get_model("api", "Tenant") | ||
Role = apps.get_model("api", "Role") | ||
User = apps.get_model("api", "User") | ||
UserRoleRelationship = apps.get_model("api", "UserRoleRelationship") | ||
|
||
for tenant in Tenant.objects.using(MainRouter.admin_db).all(): | ||
admin_role, _ = Role.objects.using(MainRouter.admin_db).get_or_create( | ||
name="admin", | ||
tenant=tenant, | ||
defaults={ | ||
"manage_users": True, | ||
"manage_account": True, | ||
"manage_billing": True, | ||
"manage_providers": True, | ||
"manage_integrations": True, | ||
"manage_scans": True, | ||
"unlimited_visibility": True, | ||
}, | ||
) | ||
users = User.objects.using(MainRouter.admin_db).filter( | ||
membership__tenant=tenant | ||
) | ||
for user in users: | ||
UserRoleRelationship.objects.using(MainRouter.admin_db).get_or_create( | ||
user=user, | ||
role=admin_role, | ||
tenant=tenant, | ||
) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("api", "0004_rbac"), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(create_admin_role), | ||
] |