Skip to content

Commit 0a8bd77

Browse files
Add persistence
Co-authored-by: clmnin <[email protected]>
1 parent 727b11e commit 0a8bd77

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package upgrades
2+
3+
import (
4+
"database/sql"
5+
)
6+
7+
func init() {
8+
upgrades[32] = upgrade{"Store space in user table", func(tx *sql.Tx, ctx context) error {
9+
_, err := tx.Exec(`ALTER TABLE "user" ADD COLUMN space_room TEXT NOT NULL DEFAULT ''`)
10+
return err
11+
}}
12+
}

database/upgrades/upgrades.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type upgrade struct {
3939
fn upgradeFunc
4040
}
4141

42-
const NumberOfUpgrades = 33
42+
const NumberOfUpgrades = 34
4343

4444
var upgrades [NumberOfUpgrades]upgrade
4545

database/user.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (uq *UserQuery) New() *User {
3838
}
3939

4040
func (uq *UserQuery) GetAll() (users []*User) {
41-
rows, err := uq.db.Query(`SELECT mxid, username, agent, device, management_room FROM "user"`)
41+
rows, err := uq.db.Query(`SELECT mxid, username, agent, device, management_room, space_room FROM "user"`)
4242
if err != nil || rows == nil {
4343
return nil
4444
}
@@ -50,15 +50,15 @@ func (uq *UserQuery) GetAll() (users []*User) {
5050
}
5151

5252
func (uq *UserQuery) GetByMXID(userID id.UserID) *User {
53-
row := uq.db.QueryRow(`SELECT mxid, username, agent, device, management_room FROM "user" WHERE mxid=$1`, userID)
53+
row := uq.db.QueryRow(`SELECT mxid, username, agent, device, management_room, space_room FROM "user" WHERE mxid=$1`, userID)
5454
if row == nil {
5555
return nil
5656
}
5757
return uq.New().Scan(row)
5858
}
5959

6060
func (uq *UserQuery) GetByUsername(username string) *User {
61-
row := uq.db.QueryRow(`SELECT mxid, username, agent, device, management_room FROM "user" WHERE username=$1`, username)
61+
row := uq.db.QueryRow(`SELECT mxid, username, agent, device, management_room, space_room FROM "user" WHERE username=$1`, username)
6262
if row == nil {
6363
return nil
6464
}
@@ -78,7 +78,7 @@ type User struct {
7878
func (user *User) Scan(row Scannable) *User {
7979
var username sql.NullString
8080
var device, agent sql.NullByte
81-
err := row.Scan(&user.MXID, &username, &agent, &device, &user.ManagementRoom)
81+
err := row.Scan(&user.MXID, &username, &agent, &device, &user.ManagementRoom, &user.SpaceRoom)
8282
if err != nil {
8383
if err != sql.ErrNoRows {
8484
user.log.Errorln("Database scan failed:", err)
@@ -113,16 +113,16 @@ func (user *User) devicePtr() *uint8 {
113113
}
114114

115115
func (user *User) Insert() {
116-
_, err := user.db.Exec(`INSERT INTO "user" (mxid, username, agent, device, management_room) VALUES ($1, $2, $3, $4, $5)`,
117-
user.MXID, user.usernamePtr(), user.agentPtr(), user.devicePtr(), user.ManagementRoom)
116+
_, err := user.db.Exec(`INSERT INTO "user" (mxid, username, agent, device, management_room, space_room) VALUES ($1, $2, $3, $4, $5, $6)`,
117+
user.MXID, user.usernamePtr(), user.agentPtr(), user.devicePtr(), user.ManagementRoom, user.SpaceRoom)
118118
if err != nil {
119119
user.log.Warnfln("Failed to insert %s: %v", user.MXID, err)
120120
}
121121
}
122122

123123
func (user *User) Update() {
124-
_, err := user.db.Exec(`UPDATE "user" SET username=$1, agent=$2, device=$3, management_room=$4 WHERE mxid=$5`,
125-
user.usernamePtr(), user.agentPtr(), user.devicePtr(), user.ManagementRoom, user.MXID)
124+
_, err := user.db.Exec(`UPDATE "user" SET username=$1, agent=$2, device=$3, management_room=$4, space_room=$5 WHERE mxid=$6`,
125+
user.usernamePtr(), user.agentPtr(), user.devicePtr(), user.ManagementRoom, user.SpaceRoom, user.MXID)
126126
if err != nil {
127127
user.log.Warnfln("Failed to update %s: %v", user.MXID, err)
128128
}

0 commit comments

Comments
 (0)