-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.go
More file actions
139 lines (123 loc) · 4.3 KB
/
database.go
File metadata and controls
139 lines (123 loc) · 4.3 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"log"
"os"
"time"
"github.com/glebarez/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
type Account struct {
ID int64 `gorm:"primaryKey;autoIncrement"`
Pubkey string `gorm:"size:65"`
PubkeyNpub string `gorm:"size:65"`
Privatekey string `gorm:"size:1024"` // encrypted
Active bool
ChatMessages []ChatMessage `gorm:"foreignKey:AccountID;references:ID"`
}
type Login struct {
PasswordHash string `gorm:"size:256"` //salted and hashed
}
type Metadata struct {
PubkeyHex string `gorm:"primaryKey;size:65"`
PubkeyNpub string `gorm:"size:65"`
Name string `gorm:"size:1024"`
About string `gorm:"size:4096"`
Nip05 string `gorm:"size:512"`
Lud06 string `gorm:"size:2048"`
Lud16 string `gorm:"size:512"`
Website string `gorm:"size:512"`
DisplayName string `gorm:"size:512"`
Picture string `gorm:"size:65535"`
TotalFollows int
UpdatedAt time.Time `gorm:"autoUpdateTime"`
ContactsUpdatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP"`
MetadataUpdatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP"`
Follows []*Metadata `gorm:"many2many:metadata_follows;foreignKey:PubkeyHex;references:PubkeyHex"`
DMRelays []DMRelay `gorm:"foreignKey:PubkeyHex;references:PubkeyHex"`
RawJsonContent string `gorm:"size:512000"`
}
type DMRelay struct {
ID int64 `gorm:"primaryKey;autoIncrement"`
PubkeyHex string `gorm:"size:65"`
Url string `gorm:"size:512"`
UpdatedAt time.Time `gorm:"autoUpdateTime"`
CreatedAt time.Time `gorm:"autoUpdateTime"`
}
type RelayStatus struct {
Url string `gorm:"primaryKey;size:512"`
Status string `gorm:"size:512"`
UpdatedAt time.Time `gorm:"autoUpdateTime"`
// change these defaults to something closer to zero
LastEOSE time.Time `gorm:"default:CURRENT_TIMESTAMP"`
LastDisco time.Time `gorm:"default:CURRENT_TIMESTAMP"`
}
type ChatMessage struct {
ID int64 `gorm:"primaryKey;autoIncrement"`
AccountID int64
EventId string `gorm:"size:65"`
FromPubkey string `gorm:"size:65"`
ToPubkey string `gorm:"size:65"`
Content string `gorm:"size:65535"`
Timestamp time.Time `gorm:"autoUpdateTime"`
ReceivedFromRelay string `gorm:"size:512"`
}
type RelayList struct {
ID int64 `gorm:"primaryKey;autoIncrement"`
PubkeyHex string `gorm:"size:65;index"`
Url string `gorm:"size:512"`
Read bool `gorm:"default:true"`
Write bool `gorm:"default:true"`
UpdatedAt time.Time `gorm:"autoUpdateTime"`
CreatedAt time.Time `gorm:"autoCreateTime"`
}
func GetGormConnection() *gorm.DB {
file, err := os.OpenFile("flightless.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
// Handle error
panic(err)
}
TheLog = log.New(file, "", log.LstdFlags) // io writer
newLogger := logger.New(
TheLog,
logger.Config{
SlowThreshold: time.Second, // Slow SQL threshold
LogLevel: logger.Error, // Log level
IgnoreRecordNotFoundError: true, // Ignore ErrRecordNotFound error for logger
Colorful: false, // Disable color
},
)
dsn, foundDsn := os.LookupEnv("DB")
if !foundDsn {
dsn = "flightless.db?cache=shared&mode=rwc"
}
db, dberr := gorm.Open(sqlite.Open(dsn), &gorm.Config{Logger: newLogger})
if dberr != nil {
panic(dberr)
}
db.Logger.LogMode(logger.Silent)
return db
}
func RunMigrations() {
if err := DB.AutoMigrate(&Login{}); err != nil {
log.Fatalf("Failed to migrate Login table: %v", err)
}
if err := DB.AutoMigrate(&Account{}); err != nil {
log.Fatalf("Failed to migrate Account table: %v", err)
}
if err := DB.AutoMigrate(&DMRelay{}); err != nil {
log.Fatalf("Failed to migrate DMRelay table: %v", err)
}
if err := DB.AutoMigrate(&Metadata{}); err != nil {
log.Fatalf("Failed to migrate Metadata table: %v", err)
}
if err := DB.AutoMigrate(&RelayStatus{}); err != nil {
log.Fatalf("Failed to migrate RelayStatus table: %v", err)
}
if err := DB.AutoMigrate(&ChatMessage{}); err != nil {
log.Fatalf("Failed to migrate ChatMessage table: %v", err)
}
if err := DB.AutoMigrate(&RelayList{}); err != nil {
log.Fatalf("Failed to migrate RelayList table: %v", err)
}
}