aboutsummaryrefslogtreecommitdiff
path: root/internal/admin/room_content_migrate.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/admin/room_content_migrate.go')
-rw-r--r--internal/admin/room_content_migrate.go38
1 files changed, 38 insertions, 0 deletions
diff --git a/internal/admin/room_content_migrate.go b/internal/admin/room_content_migrate.go
new file mode 100644
index 0000000..47353fd
--- /dev/null
+++ b/internal/admin/room_content_migrate.go
@@ -0,0 +1,38 @@
+package admin
+
+import (
+ "fmt"
+ "path/filepath"
+
+ "thehouseoficarus/internal/behavior"
+ "thehouseoficarus/internal/persistence"
+)
+
+// rewriteContentRoomRefs rewrites structural room-ID references in every
+// non-room content file under dataDir per idMap, stages the changed files into
+// txn, and returns the before/after images so the caller can record them for
+// undo. This keeps a room rename's content updates in the SAME journaled
+// commit as the room files.
+func rewriteContentRoomRefs(dataDir string, idMap map[int]int, txn *persistence.Transaction) ([]ExtraFile, error) {
+ if len(idMap) == 0 {
+ return nil, nil
+ }
+ var extras []ExtraFile
+ for _, sub := range behavior.RoomRefContentDirs {
+ changes, err := behavior.RewriteRoomRefsInDir(filepath.Join(dataDir, sub), idMap)
+ if err != nil {
+ return extras, fmt.Errorf("scan %s: %w", sub, err)
+ }
+ for _, ch := range changes {
+ if err := txn.Replace(ch.Path, ch.Content, 0644); err != nil {
+ return extras, fmt.Errorf("stage %s: %w", ch.Path, err)
+ }
+ extras = append(extras, ExtraFile{
+ FilePath: ch.Path,
+ OldContent: ch.Old,
+ NewContent: ch.Content,
+ })
+ }
+ }
+ return extras, nil
+}