Skip to content

Commit

Permalink
add field to file model, add smigration script
Browse files Browse the repository at this point in the history
  • Loading branch information
githubering182 committed Nov 28, 2024
1 parent 7afdd8a commit 77456f6
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions backend-app/file/file_tests/serializers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def test_file_serializer(self):
set(data.keys()),
data_keys.union({
"rebound",
"rebound_project",
"related_duplicates",
'upload_date',
"update_date",
Expand Down
3 changes: 3 additions & 0 deletions backend-app/file/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Model,
CharField,
DateTimeField,
IntegerField,
BooleanField,
ForeignKey,
DO_NOTHING,
Expand Down Expand Up @@ -31,6 +32,8 @@ class File(Model):
project: ForeignKey = ForeignKey("project.Project", on_delete=DO_NOTHING)
author: ForeignKey = ForeignKey("user.CustomUser", on_delete=DO_NOTHING)
rebound: ForeignKey = ForeignKey("self", on_delete=DO_NOTHING, null=True)
# todo: temp to remap storge bucket on file migration from project to projcet
rebound_project = IntegerField(null=True)
validator: ForeignKey = ForeignKey(
"user.CustomUser",
null=True,
Expand Down
4 changes: 2 additions & 2 deletions frontend-app/src/components/ui/FileMedia/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ function FileMedia({ files, slide, pathID }, ref) {
const setFile = (token) => {
resetZoom();
if (!files[slide]) return;
var { id, file_type, rebound } = files[slide];
var { id, file_type, rebound, rebound_project } = files[slide];
setTypeVideo(file_type === 'video');

var baseUrl = `${getOriginDomain()}:9000/api/storage`;
var queryUrl = `project_${pathID}/${rebound || id}/`;
var queryUrl = `project_${rebound_project || pathID}/${rebound || id}/`;

setFileUrl(`${baseUrl}/${queryUrl}?access=${token || tempFileToken}`);
setMark(rebound ? "DUPLICATE" : "");
Expand Down
33 changes: 33 additions & 0 deletions scripts/file_project_rebound.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from django.db import connection, transaction
from file.models import File


def migrate_files(
target_project: int,
dest_project: int,
id_mapping: list[tuple[int, int]]
):
with transaction.atomic():
files = File.objects.filter(project_id=target_project)

assert files.update(project_id=dest_project, rebound_project=target_project), "No File to update"

update_attributes_query = """
update attribute_group_attribute
set attribute_id = %s
where attributegroup_id in %s and attribute_id = %s;
"""

ag_id_list = tuple([
str(uid) for uid
in files.values_list("attributegroup__uid", flat=True).distinct()
])

query_values = [
(dest_id, ag_id_list, target_id)
for target_id, dest_id
in id_mapping
]

with connection.cursor() as cursor:
cursor.executemany(update_attributes_query, query_values)

0 comments on commit 77456f6

Please sign in to comment.