aboutsummaryrefslogtreecommitdiff
path: root/internal/admin/room_content_migrate.go
blob: 47353fd4f6b7e1e9d31e64ebfa247939cae6e951 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
}