-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase.go
204 lines (180 loc) · 5.27 KB
/
database.go
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package interfaces
import (
"encoding/binary"
"fmt"
"strconv"
"time"
"github.com/google/uuid"
"github.com/rs/zerolog/log"
gorm_zerolog "github.com/wei840222/gorm-zerolog"
"github.com/wwwillw/pixelland-chat/graph/model"
"github.com/xissy/lexorank"
"gopkg.in/loremipsum.v1"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
type DatabaseConfig struct {
Host string
Port int
User string
DbName string
Password string
SslMode string
}
type Database struct {
*gorm.DB
}
var db_instance Database
// Opening the database and create singleton client instance
func InitDatabase(config DatabaseConfig, retries int) (*Database, error) {
dsn := fmt.Sprintf(
"host=%s port=%d user=%s dbname=%s password=%s sslmode=%s",
config.Host, config.Port, config.User, config.DbName, config.Password, config.SslMode)
log.Info().Msgf("Connecting to database: %s", dsn)
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
for err != nil {
log.Warn().Err(err).Msg("Failed to connect to database, retrying...")
if retries > 1 {
retries--
time.Sleep(5 * time.Second)
db, err = gorm.Open(postgres.Open(dsn), &gorm.Config{
Logger: gorm_zerolog.New(),
})
continue
}
panic(err)
}
log.Info().Msg("Successfully connected to database")
sqlDB, err := db.DB()
if err != nil {
return nil, err
}
sqlDB.SetMaxIdleConns(10)
sqlDB.SetMaxOpenConns(30)
sqlDB.SetConnMaxLifetime(time.Hour)
db_instance = Database{db}
return &db_instance, nil
}
func (db *Database) RunMigrations() error {
log.Info().Msg("running database migrations")
if err := db.SetupJoinTable(&model.User{}, "Badges", &model.UserBadge{}); err != nil {
return err
}
return db.AutoMigrate(
&model.User{},
&model.Instance{},
&model.Channel{},
&model.Message{},
&model.InstanceUser{},
&model.Invite{},
&model.Notification{},
&model.UserBadge{},
&model.Badge{},
)
}
func (db *Database) DropAll() error {
log.Info().Msg("dropping database tables")
return db.Migrator().DropTable(
&model.Instance{},
&model.Channel{},
&model.Message{},
&model.InstanceUser{},
&model.Invite{},
)
}
func (db *Database) Seed() error {
log.Info().Msg("Seeding database")
loremIpsumGenerator := loremipsum.New()
adminUser := model.User{}
adminUser.ID = uuid.MustParse("3f6f51e4-aa2c-452c-a1b2-140bd7198ad1")
adminUser.UID = "JenRxFv73kScEjTx4t0iH6l0ZdB3"
if err := db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "id"}},
UpdateAll: true,
}).Create(&adminUser).Error; err != nil {
return err
}
overworldInstanceUserId := uuid.MustParse("3f6f51e4-aa2c-452c-a1b2-140bd7198ad5")
overworldInstance := model.Instance{
Name: "Overworld",
ReadAccess: model.AccessPublic.String(),
ShowAuthor: true,
ShowChat: true,
ShowLikes: true,
ShowComments: true,
Icon: "https://storage.googleapis.com/pixelland_dev_tiles/db9238ed-8377-4600-9b17-c0ecd06c3f23/0.png",
Description: loremIpsumGenerator.Sentence(),
}
overworldInstance.ID = uuid.MustParse("db9238ed-8377-4600-9b17-c0ecd06c3f23")
if err := db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "id"}},
UpdateAll: true,
}).Create(&overworldInstance).Error; err != nil {
return err
}
overworldInstanceUser := model.InstanceUser{
InstanceID: overworldInstance.ID,
UserID: adminUser.ID,
Roles: []string{model.RoleMember.String(), model.RoleModerator.String(), model.RoleAdmin.String()},
Rank: "u",
Avatar: "https://avatars.dicebear.com/api/human/abcdef.svg",
Name: "Will",
Bio: "I am a human",
}
overworldInstanceUser.ID = overworldInstanceUserId
if err := db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "id"}},
UpdateAll: true,
}).Create(&overworldInstanceUser).Error; err != nil {
return err
}
overworldCommentschannel := model.Channel{
InstanceID: overworldInstance.ID,
Name: "Comments",
Readers: []string{model.RoleAllUsers.String()},
Publishers: []string{model.RoleAllUsers.String()},
Rank: "a",
AuthorID: overworldInstanceUser.ID,
IsComments: true,
}
overworldCommentschannel.ID = uuid.MustParse("3f6f51e4-aa2c-452c-abdf-140bd7198a23")
if err := db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "id"}},
UpdateAll: true,
}).Create(&overworldCommentschannel).Error; err != nil {
return err
}
overworldInstance.AuthorID = overworldInstanceUser.ID
overworldInstance.Author = &overworldInstanceUser
if err := db.Save(&overworldInstance).Error; err != nil {
return err
}
var i uint16
for i = 0; i < 30; i++ {
b := make([]byte, 16)
binary.LittleEndian.PutUint16(b, i)
id, _ := uuid.FromBytes(b)
rank, _ := lexorank.Rank(strconv.Itoa(int(i)), strconv.Itoa(int(i+1)))
channel := model.Channel{
InstanceID: overworldInstance.ID,
Name: strconv.Itoa(int(i)),
Readers: []string{model.RoleAllUsers.String()},
Publishers: []string{model.RoleAllUsers.String()},
Rank: rank,
AuthorID: overworldInstanceUser.ID,
}
channel.ID = id
if err := db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "id"}},
UpdateAll: true,
}).Create(&channel).Error; err != nil {
return err
}
}
return nil
}
// GetSqlClient returns the singleton database instance
func GetDatabase() *Database {
return &db_instance
}