Skip to content

Commit 42c668e

Browse files
authored
bug(Notes): Fix migration for layer=None shapes
1 parent 788a765 commit 42c668e

File tree

1 file changed

+48
-5
lines changed

1 file changed

+48
-5
lines changed

server/src/save.py

+48-5
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ def upgrade(db: SqliteExtDatabase, version: int):
475475
for template in templates.values():
476476
if template.get("annotation", "") == "":
477477
continue
478+
478479
note_id = str(uuid4())
479480
asset_id_to_note_id[asset_id] = note_id
480481
db.execute_sql(
@@ -488,15 +489,17 @@ def upgrade(db: SqliteExtDatabase, version: int):
488489
),
489490
)
490491

492+
del template["annotation"]
493+
491494
# Grant default view access if the annotation was public
492495
if template.get("annotation_visible", False):
493496
db.execute_sql(
494497
'INSERT INTO "note_access" ("note_id", "can_edit", "can_view") VALUES (?, ?, ?)',
495498
(note_id, 0, 1),
496499
)
497500

498-
del template["annotation"]
499-
del template["annotation_visible"]
501+
if "annotation_visible" in template:
502+
del template["annotation_visible"]
500503

501504
options = json.loads(template.get("options", "[]"))
502505
options.append(["templateNoteIds", [note_id]])
@@ -508,7 +511,7 @@ def upgrade(db: SqliteExtDatabase, version: int):
508511

509512
# Move all annotation to notes
510513
data = db.execute_sql(
511-
"SELECT s.uuid, s.name, s.annotation, s.annotation_visible, s.asset_id, l2.id, r.id, u.id FROM shape s INNER JOIN layer l ON s.layer_id = l.id INNER JOIN floor f ON f.id = l.floor_id INNER JOIN location l2 ON l2.id = f.location_id INNER JOIN room r ON r.id = l2.room_id INNER JOIN user u ON u.id = r.creator_id WHERE s.annotation != ''"
514+
"SELECT s.uuid, s.name, s.annotation, s.annotation_visible, s.asset_id, s.character_id, l2.id, r.id, u.id FROM shape s LEFT OUTER JOIN layer l ON s.layer_id = l.id LEFT OUTER JOIN floor f ON f.id = l.floor_id LEFT OUTER JOIN location l2 ON l2.id = f.location_id LEFT OUTER JOIN room r ON r.id = l2.room_id LEFT OUTER JOIN user u ON u.id = r.creator_id WHERE s.annotation != ''"
512515
)
513516
shape_to_note_ids = {}
514517
for (
@@ -517,13 +520,14 @@ def upgrade(db: SqliteExtDatabase, version: int):
517520
annotation,
518521
annotation_visible,
519522
asset_id,
523+
character_id,
520524
location_id,
521525
room_id,
522526
creator_id,
523527
) in data.fetchall():
524528
if asset_id is not None and asset_id in asset_id_to_note_id:
525529
note_id = asset_id_to_note_id[asset_id]
526-
else:
530+
elif location_id is not None:
527531
note_id = str(uuid4())
528532
db.execute_sql(
529533
'INSERT INTO "note" ("uuid", "creator_id", "title", "text", "room_id", "location_id") VALUES (?, ?, ?, ?, ?, ?)',
@@ -542,6 +546,42 @@ def upgrade(db: SqliteExtDatabase, version: int):
542546
'INSERT INTO "note_access" ("note_id", "can_edit", "can_view") VALUES (?, ?, ?)',
543547
(note_id, 0, 1),
544548
)
549+
elif character_id is not None:
550+
# The shape no longer has a layer associated, we do have a character to figure out who the owner is!
551+
data = db.execute_sql(
552+
"SELECT owner_id, asset_id FROM character WHERE id=?",
553+
(character_id,),
554+
)
555+
results = data.fetchone()
556+
if results is None:
557+
continue
558+
559+
owner_id, asset_id = results[0]
560+
if asset_id is not None and asset_id in asset_id_to_note_id:
561+
note_id = asset_id_to_note_id[asset_id]
562+
else:
563+
note_id = str(uuid4())
564+
565+
db.execute_sql(
566+
'INSERT INTO "note" ("uuid", "creator_id", "title", "text") VALUES (?, ?, ?, ?)',
567+
(
568+
note_id,
569+
owner_id,
570+
name or "?",
571+
annotation,
572+
),
573+
)
574+
# Grant default view access if the annotation was public
575+
if annotation_visible:
576+
db.execute_sql(
577+
'INSERT INTO "note_access" ("note_id", "can_edit", "can_view") VALUES (?, ?, ?)',
578+
(note_id, 0, 1),
579+
)
580+
else:
581+
# The shape has no layer info and is not a character, we have no clue on who the note belongs to
582+
# These shapes are probably no longer valid anyway
583+
# In theory shape.owners should have info, but in the cases checked, this was empty
584+
continue
545585
shape_to_note_ids[shape_uuid] = note_id
546586
# Attach shape to note
547587
db.execute_sql(
@@ -553,7 +593,10 @@ def upgrade(db: SqliteExtDatabase, version: int):
553593
"SELECT so.user_id, so.shape_id FROM shape_owner so INNER JOIN shape s ON s.uuid = so.shape_id WHERE s.annotation != '' AND so.edit_access = 1"
554594
)
555595
for user_id, shape_id in data.fetchall():
556-
note_id = shape_to_note_ids[shape_id]
596+
try:
597+
note_id = shape_to_note_ids[shape_id]
598+
except KeyError:
599+
continue
557600
db.execute_sql(
558601
'INSERT INTO "note_access" ("note_id", "user_id", "can_edit", "can_view") VALUES (?, ?, ?, ?)',
559602
(note_id, user_id, 1, 1),

0 commit comments

Comments
 (0)