New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[couch to sql] UserRole & Permissions: Part I - sql models + populate command #29688
Changes from all commits
1ffd617
633e5bd
73a8b10
5e88858
6e06b68
514b828
d61617d
70d9009
83d4349
adfbe98
36142fc
c08cd3d
f8c8ed4
b02a6d3
1944519
8bea73a
8db649c
210de3d
a0ac09d
5b35766
5751a46
2705645
2228470
04caac3
d15c5a1
4a96306
5e14369
a4d4279
c7cd3ce
0ac117c
6ce7268
4227259
c3859f1
e62d686
be4eb1e
6b9597a
d0cbb6a
c56f409
150a5c5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from corehq.apps.cleanup.management.commands.populate_sql_model_from_couch_model import PopulateSQLCommand | ||
from corehq.apps.users.models import UserRole, Permissions | ||
from corehq.apps.users.models_sql import ( | ||
migrate_role_assignable_by_to_sql, | ||
migrate_role_permissions_to_sql, | ||
SQLUserRole, | ||
) | ||
|
||
|
||
class Command(PopulateSQLCommand): | ||
@classmethod | ||
def couch_db_slug(cls): | ||
return "users" | ||
|
||
@classmethod | ||
def couch_doc_type(self): | ||
return 'UserRole' | ||
|
||
@classmethod | ||
def sql_class(self): | ||
from corehq.apps.users.models import SQLUserRole | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. F811 redefinition of unused 'SQLUserRole' from line 3 |
||
return SQLUserRole | ||
|
||
@classmethod | ||
def commit_adding_migration(cls): | ||
return "TODO: add once the PR adding this file is merged" | ||
|
||
@classmethod | ||
def diff_couch_and_sql(cls, couch, sql): | ||
diffs = [] | ||
for field in UserRole._migration_get_fields(): | ||
diffs.append(cls.diff_attr(field, couch, sql)) | ||
|
||
couch_permissions = { | ||
info.name: info | ||
for info in Permissions.wrap(couch["permissions"]).to_list() | ||
} | ||
sql_permissions = { | ||
info.name: info | ||
for info in sql.permissions.to_list() | ||
} | ||
|
||
for name in sorted(set(couch_permissions) | set(sql_permissions)): | ||
couch_permission = couch_permissions.get(name) | ||
diffs.append(cls.diff_attr( | ||
"allow", | ||
couch_permission._asdict() if couch_permission else {}, | ||
sql_permissions.get(name), | ||
name_prefix=f"permissions.{name}" | ||
)) | ||
|
||
couch_assignable_by = couch["assignable_by"] | ||
sql_assignable_by = list(sql.roleassignableby_set.values_list('assignable_by_role__couch_id', flat=True)) | ||
diffs.extend(cls.diff_lists( | ||
"assignable_by", | ||
sorted(couch_assignable_by) if couch_assignable_by else [], | ||
sorted(sql_assignable_by), | ||
)) | ||
return diffs | ||
|
||
def update_or_create_sql_object(self, doc): | ||
model, created = self.sql_class().objects.update_or_create( | ||
couch_id=doc['_id'], | ||
defaults={ | ||
"domain": doc.get("domain"), | ||
"name": doc.get("name"), | ||
"default_landing_page": doc.get("default_landing_page"), | ||
"is_non_admin_editable": doc.get("is_non_admin_editable"), | ||
"is_archived": doc.get("is_archived"), | ||
"upstream_id": doc.get("upstream_id"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So for now There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potentially. Both this and There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI I've updated this so that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And then reverted to Couch IDs in 0ac117c |
||
}) | ||
couch_role = UserRole.wrap(doc) | ||
migrate_role_permissions_to_sql(couch_role, model) | ||
migrate_role_assignable_by_to_sql(couch_role, model) | ||
return model, created |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
F401 'corehq.apps.users.models_sql.SQLUserRole' imported but unused