Live video on the AT Protocol
1package statedb
2
3import (
4 "context"
5 "os"
6 "time"
7
8 "github.com/lmittmann/tint"
9 slogGorm "github.com/orandin/slog-gorm"
10 "github.com/streamplace/oatproxy/pkg/oatproxy"
11 "gorm.io/driver/sqlite"
12 "gorm.io/gorm"
13 "stream.place/streamplace/pkg/config"
14 "stream.place/streamplace/pkg/log"
15 "stream.place/streamplace/pkg/model"
16)
17
18func Migrate(cli *config.CLI) error {
19 gormLogger := slogGorm.New(
20 slogGorm.WithHandler(tint.NewHandler(os.Stderr, &tint.Options{
21 TimeFormat: time.RFC3339,
22 })),
23 // slogGorm.WithTraceAll(),
24 )
25
26 newDB, err := MakeDB(context.Background(), cli, nil, nil)
27 if err != nil {
28 return err
29 }
30
31 oldDB, err := gorm.Open(sqlite.Open(cli.DataFilePath([]string{"db.sqlite"})), &gorm.Config{
32 Logger: gormLogger,
33 })
34 if err != nil {
35 return err
36 }
37
38 var sessions []oatproxy.OAuthSession
39 if err := oldDB.Find(&sessions).Error; err != nil {
40 return err
41 }
42
43 for _, session := range sessions {
44 log.Log(context.Background(), "migrating session", "session", session.DownstreamDPoPJKT)
45 err := newDB.DB.Save(&session).Error
46 if err != nil {
47 return err
48 }
49 }
50
51 var notifications []Notification
52 if err := oldDB.Find(¬ifications).Error; err != nil {
53 time.Sleep(1 * time.Second)
54 return err
55 }
56
57 for _, notification := range notifications {
58 log.Log(context.Background(), "migrating notification", "notification", notification)
59 err := newDB.DB.Save(¬ification).Error
60 if err != nil {
61 return err
62 }
63 }
64
65 var repos []model.Repo
66 if err := oldDB.Find(&repos).Error; err != nil {
67 time.Sleep(1 * time.Second)
68 return err
69 }
70
71 for _, repo := range repos {
72 newRepo := Repo{
73 DID: repo.DID,
74 IndexedAt: time.Now().UTC(),
75 }
76 log.Log(context.Background(), "migrating repo", "repo", repo)
77 err := newDB.DB.Save(&newRepo).Error
78 if err != nil {
79 return err
80 }
81 }
82
83 return nil
84}