-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathblossomMigration.go
More file actions
58 lines (45 loc) · 1.65 KB
/
Copy pathblossomMigration.go
File metadata and controls
58 lines (45 loc) · 1.65 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"context"
"log/slog"
"github.com/fiatjaf/khatru/blossom"
)
func migrateBlossomMetadata(ctx context.Context, bl *blossom.BlossomServer) {
// Create a temporary Blossom dbWrapper for the migration
outboxDBWrapper := blossom.EventStoreBlobIndexWrapper{Store: outboxDB, ServiceURL: getHTTPScheme(config.RelayURL) + config.RelayURL}
// List all BlobDescriptor for the relay owner pubkey
ownerPubkey := nPubToPubkey("OWNER_NPUB", config.OwnerNpub)
blobsChan, err := outboxDBWrapper.List(ctx, ownerPubkey)
if err != nil {
slog.Error("🚫 Failed to list blobs", "error", err)
return
}
var blobs []blossom.BlobDescriptor
for blob := range blobsChan {
blobs = append(blobs, blob)
}
if len(blobs) == 0 {
slog.Debug("No blobs found to migrate", "ownerPubkey", ownerPubkey)
return
}
// Create a map to track migrated blobs
migrated := make(map[string]blossom.BlobDescriptor, len(blobs))
slog.Info("BlobDescriptors will be migrated from Outbox to Blossom's DB", "count", len(blobs))
for _, blob := range blobs {
slog.Debug("Moving BlobDescriptor", "sha256", blob.SHA256, "type", blob.Type, "size", blob.Size)
if blob.Type == "" {
blob.Type = "application/octet-stream"
}
err := bl.Store.Keep(ctx, blob, ownerPubkey)
if err != nil {
slog.Error("🚫 Failed to store blob in Blossom DB", "sha256", blob.SHA256, "error", err)
continue
}
err = outboxDBWrapper.Delete(ctx, blob.SHA256, ownerPubkey)
if err != nil {
slog.Error("🚫 Failed to delete blob from outbox DB", "sha256", blob.SHA256, "error", err)
}
migrated[blob.SHA256] = blob
}
slog.Info("✅ Blob migration completed", "migrated", len(migrated))
}