From 0a8bd7794bb91034af0d516190a206ce0c97a577 Mon Sep 17 00:00:00 2001 From: Helder Ferreira Date: Tue, 28 Dec 2021 21:22:59 +0000 Subject: [PATCH] Add persistence Co-authored-by: clmnin --- database/upgrades/2021-12-28-management-space.go | 12 ++++++++++++ database/upgrades/upgrades.go | 2 +- database/user.go | 16 ++++++++-------- 3 files changed, 21 insertions(+), 9 deletions(-) create mode 100644 database/upgrades/2021-12-28-management-space.go diff --git a/database/upgrades/2021-12-28-management-space.go b/database/upgrades/2021-12-28-management-space.go new file mode 100644 index 0000000..1f3d772 --- /dev/null +++ b/database/upgrades/2021-12-28-management-space.go @@ -0,0 +1,12 @@ +package upgrades + +import ( + "database/sql" +) + +func init() { + upgrades[32] = upgrade{"Store space in user table", func(tx *sql.Tx, ctx context) error { + _, err := tx.Exec(`ALTER TABLE "user" ADD COLUMN space_room TEXT NOT NULL DEFAULT ''`) + return err + }} +} diff --git a/database/upgrades/upgrades.go b/database/upgrades/upgrades.go index 293126c..d971f66 100644 --- a/database/upgrades/upgrades.go +++ b/database/upgrades/upgrades.go @@ -39,7 +39,7 @@ type upgrade struct { fn upgradeFunc } -const NumberOfUpgrades = 33 +const NumberOfUpgrades = 34 var upgrades [NumberOfUpgrades]upgrade diff --git a/database/user.go b/database/user.go index 96a2aef..3f10636 100644 --- a/database/user.go +++ b/database/user.go @@ -38,7 +38,7 @@ func (uq *UserQuery) New() *User { } func (uq *UserQuery) GetAll() (users []*User) { - rows, err := uq.db.Query(`SELECT mxid, username, agent, device, management_room FROM "user"`) + rows, err := uq.db.Query(`SELECT mxid, username, agent, device, management_room, space_room FROM "user"`) if err != nil || rows == nil { return nil } @@ -50,7 +50,7 @@ func (uq *UserQuery) GetAll() (users []*User) { } func (uq *UserQuery) GetByMXID(userID id.UserID) *User { - row := uq.db.QueryRow(`SELECT mxid, username, agent, device, management_room FROM "user" WHERE mxid=$1`, userID) + row := uq.db.QueryRow(`SELECT mxid, username, agent, device, management_room, space_room FROM "user" WHERE mxid=$1`, userID) if row == nil { return nil } @@ -58,7 +58,7 @@ func (uq *UserQuery) GetByMXID(userID id.UserID) *User { } func (uq *UserQuery) GetByUsername(username string) *User { - row := uq.db.QueryRow(`SELECT mxid, username, agent, device, management_room FROM "user" WHERE username=$1`, username) + row := uq.db.QueryRow(`SELECT mxid, username, agent, device, management_room, space_room FROM "user" WHERE username=$1`, username) if row == nil { return nil } @@ -78,7 +78,7 @@ type User struct { func (user *User) Scan(row Scannable) *User { var username sql.NullString var device, agent sql.NullByte - err := row.Scan(&user.MXID, &username, &agent, &device, &user.ManagementRoom) + err := row.Scan(&user.MXID, &username, &agent, &device, &user.ManagementRoom, &user.SpaceRoom) if err != nil { if err != sql.ErrNoRows { user.log.Errorln("Database scan failed:", err) @@ -113,16 +113,16 @@ func (user *User) devicePtr() *uint8 { } func (user *User) Insert() { - _, err := user.db.Exec(`INSERT INTO "user" (mxid, username, agent, device, management_room) VALUES ($1, $2, $3, $4, $5)`, - user.MXID, user.usernamePtr(), user.agentPtr(), user.devicePtr(), user.ManagementRoom) + _, err := user.db.Exec(`INSERT INTO "user" (mxid, username, agent, device, management_room, space_room) VALUES ($1, $2, $3, $4, $5, $6)`, + user.MXID, user.usernamePtr(), user.agentPtr(), user.devicePtr(), user.ManagementRoom, user.SpaceRoom) if err != nil { user.log.Warnfln("Failed to insert %s: %v", user.MXID, err) } } func (user *User) Update() { - _, err := user.db.Exec(`UPDATE "user" SET username=$1, agent=$2, device=$3, management_room=$4 WHERE mxid=$5`, - user.usernamePtr(), user.agentPtr(), user.devicePtr(), user.ManagementRoom, user.MXID) + _, err := user.db.Exec(`UPDATE "user" SET username=$1, agent=$2, device=$3, management_room=$4, space_room=$5 WHERE mxid=$6`, + user.usernamePtr(), user.agentPtr(), user.devicePtr(), user.ManagementRoom, user.SpaceRoom, user.MXID) if err != nil { user.log.Warnfln("Failed to update %s: %v", user.MXID, err) }