@@ -475,6 +475,7 @@ def upgrade(db: SqliteExtDatabase, version: int):
475
475
for template in templates .values ():
476
476
if template .get ("annotation" , "" ) == "" :
477
477
continue
478
+
478
479
note_id = str (uuid4 ())
479
480
asset_id_to_note_id [asset_id ] = note_id
480
481
db .execute_sql (
@@ -488,15 +489,17 @@ def upgrade(db: SqliteExtDatabase, version: int):
488
489
),
489
490
)
490
491
492
+ del template ["annotation" ]
493
+
491
494
# Grant default view access if the annotation was public
492
495
if template .get ("annotation_visible" , False ):
493
496
db .execute_sql (
494
497
'INSERT INTO "note_access" ("note_id", "can_edit", "can_view") VALUES (?, ?, ?)' ,
495
498
(note_id , 0 , 1 ),
496
499
)
497
500
498
- del template [ "annotation" ]
499
- del template ["annotation_visible" ]
501
+ if "annotation_visible" in template :
502
+ del template ["annotation_visible" ]
500
503
501
504
options = json .loads (template .get ("options" , "[]" ))
502
505
options .append (["templateNoteIds" , [note_id ]])
@@ -508,7 +511,7 @@ def upgrade(db: SqliteExtDatabase, version: int):
508
511
509
512
# Move all annotation to notes
510
513
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 != ''"
512
515
)
513
516
shape_to_note_ids = {}
514
517
for (
@@ -517,13 +520,14 @@ def upgrade(db: SqliteExtDatabase, version: int):
517
520
annotation ,
518
521
annotation_visible ,
519
522
asset_id ,
523
+ character_id ,
520
524
location_id ,
521
525
room_id ,
522
526
creator_id ,
523
527
) in data .fetchall ():
524
528
if asset_id is not None and asset_id in asset_id_to_note_id :
525
529
note_id = asset_id_to_note_id [asset_id ]
526
- else :
530
+ elif location_id is not None :
527
531
note_id = str (uuid4 ())
528
532
db .execute_sql (
529
533
'INSERT INTO "note" ("uuid", "creator_id", "title", "text", "room_id", "location_id") VALUES (?, ?, ?, ?, ?, ?)' ,
@@ -542,6 +546,42 @@ def upgrade(db: SqliteExtDatabase, version: int):
542
546
'INSERT INTO "note_access" ("note_id", "can_edit", "can_view") VALUES (?, ?, ?)' ,
543
547
(note_id , 0 , 1 ),
544
548
)
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
545
585
shape_to_note_ids [shape_uuid ] = note_id
546
586
# Attach shape to note
547
587
db .execute_sql (
@@ -553,7 +593,10 @@ def upgrade(db: SqliteExtDatabase, version: int):
553
593
"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"
554
594
)
555
595
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
557
600
db .execute_sql (
558
601
'INSERT INTO "note_access" ("note_id", "user_id", "can_edit", "can_view") VALUES (?, ?, ?, ?)' ,
559
602
(note_id , user_id , 1 , 1 ),
0 commit comments