From b26b9924f30533441c395ea256c15e7a02776f2e Mon Sep 17 00:00:00 2001 From: sawka Date: Tue, 16 Aug 2022 12:08:26 -0700 Subject: [PATCH] updates to tables, lineid, sharemode, owneruserid --- cmd/main-server.go | 4 +-- db/migrations/000001_init.down.sql | 1 + db/migrations/000001_init.up.sql | 12 +++++-- pkg/sstore/dbops.go | 16 ++++----- pkg/sstore/migrate.go | 2 +- pkg/sstore/sstore.go | 58 +++++++++++++++++++++--------- pkg/sstore/updatebus.go | 6 ++-- 7 files changed, 64 insertions(+), 35 deletions(-) diff --git a/cmd/main-server.go b/cmd/main-server.go index 6c5d359f..320893be 100644 --- a/cmd/main-server.go +++ b/cmd/main-server.go @@ -437,13 +437,13 @@ func main() { if len(os.Args) >= 2 && strings.HasPrefix(os.Args[1], "--migrate") { err := sstore.MigrateCommandOpts(os.Args[1:]) if err != nil { - fmt.Printf("[error] %v\n", err) + fmt.Printf("[error] migrate cmd: %v\n", err) } return } err = sstore.TryMigrateUp() if err != nil { - fmt.Printf("[error] %v\n", err) + fmt.Printf("[error] migrate up: %v\n", err) return } err = sstore.EnsureLocalRemote(context.Background()) diff --git a/db/migrations/000001_init.down.sql b/db/migrations/000001_init.down.sql index fcb93770..8896331f 100644 --- a/db/migrations/000001_init.down.sql +++ b/db/migrations/000001_init.down.sql @@ -1,3 +1,4 @@ +DROP TABLE client; DROP TABLE session; DROP TABLE window; DROP TABLE screen; diff --git a/db/migrations/000001_init.up.sql b/db/migrations/000001_init.up.sql index 18db0089..24c6de5c 100644 --- a/db/migrations/000001_init.up.sql +++ b/db/migrations/000001_init.up.sql @@ -9,7 +9,10 @@ CREATE TABLE session ( name varchar(50) NOT NULL, sessionidx int NOT NULL, activescreenid varchar(36) NOT NULL, - notifynum int NOT NULL + notifynum int NOT NULL, + owneruserid varchar(36) NOT NULL, + sharemode varchar(12) NOT NULL, + accesskey varchar(36) NOT NULL ); CREATE TABLE window ( @@ -17,6 +20,9 @@ CREATE TABLE window ( windowid varchar(36) NOT NULL, curremote varchar(50) NOT NULL, winopts json NOT NULL, + owneruserid varchar(36) NOT NULL, + sharemode varchar(12) NOT NULL, + shareopts json NOT NULL, PRIMARY KEY (sessionid, windowid) ); @@ -27,6 +33,8 @@ CREATE TABLE screen ( activewindowid varchar(36) NOT NULL, screenidx int NOT NULL, screenopts json NOT NULL, + owneruserid varchar(36) NOT NULL, + sharemode varchar(12) NOT NULL, PRIMARY KEY (sessionid, screenid) ); @@ -52,7 +60,7 @@ CREATE TABLE remote_instance ( CREATE TABLE line ( sessionid varchar(36) NOT NULL, windowid varchar(36) NOT NULL, - lineid int NOT NULL, + lineid varchar(36) NOT NULL, userid varchar(36) NOT NULL, ts bigint NOT NULL, linetype varchar(10) NOT NULL, diff --git a/pkg/sstore/dbops.go b/pkg/sstore/dbops.go index 7218cc12..e09a6285 100644 --- a/pkg/sstore/dbops.go +++ b/pkg/sstore/dbops.go @@ -250,7 +250,7 @@ func InsertSessionWithName(ctx context.Context, sessionName string, activate boo names := tx.SelectStrings(`SELECT name FROM session`) sessionName = fmtUniqueName(sessionName, "session-%d", len(names)+1, names) maxSessionIdx := tx.GetInt(`SELECT COALESCE(max(sessionidx), 0) FROM session`) - query := `INSERT INTO session (sessionid, name, activescreenid, sessionidx, notifynum) VALUES (?, ?, '', ?, ?)` + query := `INSERT INTO session (sessionid, name, activescreenid, sessionidx, notifynum, owneruserid, sharemode, accesskey) VALUES (?, ?, '', ?, ?, '', 'local', '')` tx.ExecWrap(query, newSessionId, sessionName, maxSessionIdx+1, 0) _, err := InsertScreen(tx.Context(), newSessionId, "", true) if err != nil { @@ -319,7 +319,7 @@ func InsertScreen(ctx context.Context, sessionId string, screenName string, acti screenNames := tx.SelectStrings(`SELECT name FROM screen WHERE sessionid = ?`, sessionId) screenName = fmtUniqueName(screenName, "s%d", maxScreenIdx+1, screenNames) newScreenId = uuid.New().String() - query = `INSERT INTO screen (sessionid, screenid, name, activewindowid, screenidx, screenopts) VALUES (?, ?, ?, ?, ?, ?)` + query = `INSERT INTO screen (sessionid, screenid, name, activewindowid, screenidx, screenopts, owneruserid, sharemode) VALUES (?, ?, ?, ?, ?, ?, '', 'local')` tx.ExecWrap(query, sessionId, newScreenId, screenName, newWindowId, maxScreenIdx+1, ScreenOptsType{}) layout := LayoutType{Type: LayoutFull} query = `INSERT INTO screen_window (sessionid, screenid, windowid, name, layout) VALUES (?, ?, ?, ?, ?)` @@ -365,8 +365,8 @@ func GetScreenById(ctx context.Context, sessionId string, screenId string) (*Scr func txCreateWindow(tx *TxWrap, sessionId string) string { windowId := uuid.New().String() - query := `INSERT INTO window (sessionid, windowid, curremote, winopts) VALUES (?, ?, ?, ?)` - tx.ExecWrap(query, sessionId, windowId, LocalRemoteName, WindowOptsType{}) + query := `INSERT INTO window (sessionid, windowid, curremote, winopts, shareopts, owneruserid, sharemode) VALUES (?, ?, ?, ?, ?, '', 'local')` + tx.ExecWrap(query, sessionId, windowId, LocalRemoteName, WindowOptsType{}, WindowShareOptsType{}) return windowId } @@ -374,8 +374,8 @@ func InsertLine(ctx context.Context, line *LineType, cmd *CmdType) error { if line == nil { return fmt.Errorf("line cannot be nil") } - if line.LineId != 0 { - return fmt.Errorf("new line cannot have LineId set") + if line.LineId == "" { + return fmt.Errorf("line must have lineid set") } return WithTx(ctx, func(tx *TxWrap) error { var windowId string @@ -384,10 +384,6 @@ func InsertLine(ctx context.Context, line *LineType, cmd *CmdType) error { if !hasWindow { return fmt.Errorf("window not found, cannot insert line[%s/%s]", line.SessionId, line.WindowId) } - var maxLineId int64 - query = `SELECT COALESCE(max(lineid), 0) FROM line WHERE sessionid = ? AND windowid = ?` - tx.GetWrap(&maxLineId, query, line.SessionId, line.WindowId) - line.LineId = maxLineId + 1 query = `INSERT INTO line ( sessionid, windowid, lineid, ts, userid, linetype, text, cmdid) VALUES (:sessionid,:windowid,:lineid,:ts,:userid,:linetype,:text,:cmdid)` tx.NamedExecWrap(query, line) diff --git a/pkg/sstore/migrate.go b/pkg/sstore/migrate.go index 043222cb..7e815128 100644 --- a/pkg/sstore/migrate.go +++ b/pkg/sstore/migrate.go @@ -22,7 +22,7 @@ func MakeMigrate() (*migrate.Migrate, error) { dbUrl := fmt.Sprintf("sqlite3://%s", GetSessionDBName()) m, err := migrate.New(migrationPathUrl, dbUrl) if err != nil { - return nil, err + return nil, fmt.Errorf("making migration [%s] db[%s]: %w", migrationPathUrl, GetSessionDBName(), err) } return m, nil } diff --git a/pkg/sstore/sstore.go b/pkg/sstore/sstore.go index 18e90383..f2f10d03 100644 --- a/pkg/sstore/sstore.go +++ b/pkg/sstore/sstore.go @@ -43,11 +43,10 @@ const ( ) const ( - ShareModeLocal = "local" - ShareModePrivate = "private" - ShareModeView = "view" - ShareModeShared = "shared" - ShareModeSharedView = "shared-view" + ShareModeLocal = "local" + ShareModePrivate = "private" + ShareModeView = "view" + ShareModeShared = "shared" ) var globalDBLock = &sync.Mutex{} @@ -66,7 +65,11 @@ func GetDB(ctx context.Context) (*sqlx.DB, error) { globalDBLock.Lock() defer globalDBLock.Unlock() if globalDB == nil && globalDBErr == nil { - globalDB, globalDBErr = sqlx.Open("sqlite3", fmt.Sprintf("file:%s?cache=shared&mode=rwc&_journal_mode=WAL&_busy_timeout=5000", GetSessionDBName())) + dbName := GetSessionDBName() + globalDB, globalDBErr = sqlx.Open("sqlite3", fmt.Sprintf("file:%s?cache=shared&mode=rwc&_journal_mode=WAL&_busy_timeout=5000", dbName)) + if globalDBErr != nil { + globalDBErr = fmt.Errorf("opening db[%s]: %w", dbName, globalDBErr) + } } return globalDB, globalDBErr } @@ -84,6 +87,9 @@ type SessionType struct { Name string `json:"name"` SessionIdx int64 `json:"sessionidx"` ActiveScreenId string `json:"activescreenid"` + OwnerUserId string `json:"owneruserid"` + ShareMode string `json:"sharemode"` + AccessKey string `json:"-"` NotifyNum int64 `json:"notifynum"` Screens []*ScreenType `json:"screens"` Remotes []*RemoteInstance `json:"remotes"` @@ -104,14 +110,28 @@ func (opts WindowOptsType) Value() (driver.Value, error) { return quickValueJson(opts) } +type WindowShareOptsType struct { +} + +func (opts *WindowShareOptsType) Scan(val interface{}) error { + return quickScanJson(opts, val) +} + +func (opts WindowShareOptsType) Value() (driver.Value, error) { + return quickValueJson(opts) +} + type WindowType struct { - SessionId string `json:"sessionid"` - WindowId string `json:"windowid"` - CurRemote string `json:"curremote"` - WinOpts WindowOptsType `json:"winopts"` - Lines []*LineType `json:"lines"` - Cmds []*CmdType `json:"cmds"` - Remotes []*RemoteInstance `json:"remotes"` + SessionId string `json:"sessionid"` + WindowId string `json:"windowid"` + CurRemote string `json:"curremote"` + WinOpts WindowOptsType `json:"winopts"` + OwnerUserId string `json:"owneruserid"` + ShareMode string `json:"sharemode"` + ShareOpts WindowShareOptsType `json:"shareopts"` + Lines []*LineType `json:"lines"` + Cmds []*CmdType `json:"cmds"` + Remotes []*RemoteInstance `json:"remotes"` // only for updates Remove bool `json:"remove,omitempty"` @@ -136,6 +156,8 @@ type ScreenType struct { Name string `json:"name"` ActiveWindowId string `json:"activewindowid"` ScreenOpts ScreenOptsType `json:"screenopts"` + OwnerUserId string `json:"owneruserid"` + ShareMode string `json:"sharemode"` Windows []*ScreenWindowType `json:"windows"` // only for updates @@ -186,7 +208,7 @@ type HistoryItemType struct { SessionId string `json:"sessionid"` ScreenId string `json:"screenid"` WindowId string `json:"windowid"` - LineId int64 `json:"lineid"` + LineId string `json:"lineid"` HadError bool `json:"haderror"` CmdId string `json:"cmdid"` CmdStr string `json:"cmdstr"` @@ -238,7 +260,7 @@ type RemoteInstance struct { type LineType struct { SessionId string `json:"sessionid"` WindowId string `json:"windowid"` - LineId int64 `json:"lineid"` + LineId string `json:"lineid"` Ts int64 `json:"ts"` UserId string `json:"userid"` LineType string `json:"linetype"` @@ -359,6 +381,7 @@ func makeNewLineCmd(sessionId string, windowId string, userId string, cmdId stri rtn := &LineType{} rtn.SessionId = sessionId rtn.WindowId = windowId + rtn.LineId = uuid.New().String() rtn.Ts = time.Now().UnixMilli() rtn.UserId = userId rtn.LineType = LineTypeCmd @@ -370,6 +393,7 @@ func makeNewLineText(sessionId string, windowId string, userId string, text stri rtn := &LineType{} rtn.SessionId = sessionId rtn.WindowId = windowId + rtn.LineId = uuid.New().String() rtn.Ts = time.Now().UnixMilli() rtn.UserId = userId rtn.LineType = LineTypeText @@ -398,11 +422,11 @@ func AddCmdLine(ctx context.Context, sessionId string, windowId string, userId s func EnsureLocalRemote(ctx context.Context) error { remoteId, err := base.GetRemoteId() if err != nil { - return err + return fmt.Errorf("getting local remoteid: %w", err) } remote, err := GetRemoteById(ctx, remoteId) if err != nil { - return err + return fmt.Errorf("getting remote[%s] from db: %w", remoteId, err) } if remote != nil { return nil diff --git a/pkg/sstore/updatebus.go b/pkg/sstore/updatebus.go index c633f909..6915fa2e 100644 --- a/pkg/sstore/updatebus.go +++ b/pkg/sstore/updatebus.go @@ -74,13 +74,13 @@ func (LineUpdate) UpdateType() string { return LineCmdUpdateStr } -func ReadLineCmdIdFromUpdate(update UpdatePacket) (int64, string) { +func ReadLineCmdIdFromUpdate(update UpdatePacket) (string, string) { lineUpdate, ok := update.(LineUpdate) if !ok { - return 0, "" + return "", "" } if lineUpdate.Line == nil { - return 0, "" + return "", "" } return lineUpdate.Line.LineId, lineUpdate.Line.CmdId }