updates to tables, lineid, sharemode, owneruserid

This commit is contained in:
sawka 2022-08-16 12:08:26 -07:00
parent 8186e54cd2
commit b26b9924f3
7 changed files with 64 additions and 35 deletions

View File

@ -437,13 +437,13 @@ func main() {
if len(os.Args) >= 2 && strings.HasPrefix(os.Args[1], "--migrate") { if len(os.Args) >= 2 && strings.HasPrefix(os.Args[1], "--migrate") {
err := sstore.MigrateCommandOpts(os.Args[1:]) err := sstore.MigrateCommandOpts(os.Args[1:])
if err != nil { if err != nil {
fmt.Printf("[error] %v\n", err) fmt.Printf("[error] migrate cmd: %v\n", err)
} }
return return
} }
err = sstore.TryMigrateUp() err = sstore.TryMigrateUp()
if err != nil { if err != nil {
fmt.Printf("[error] %v\n", err) fmt.Printf("[error] migrate up: %v\n", err)
return return
} }
err = sstore.EnsureLocalRemote(context.Background()) err = sstore.EnsureLocalRemote(context.Background())

View File

@ -1,3 +1,4 @@
DROP TABLE client;
DROP TABLE session; DROP TABLE session;
DROP TABLE window; DROP TABLE window;
DROP TABLE screen; DROP TABLE screen;

View File

@ -9,7 +9,10 @@ CREATE TABLE session (
name varchar(50) NOT NULL, name varchar(50) NOT NULL,
sessionidx int NOT NULL, sessionidx int NOT NULL,
activescreenid varchar(36) 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 ( CREATE TABLE window (
@ -17,6 +20,9 @@ CREATE TABLE window (
windowid varchar(36) NOT NULL, windowid varchar(36) NOT NULL,
curremote varchar(50) NOT NULL, curremote varchar(50) NOT NULL,
winopts json NOT NULL, winopts json NOT NULL,
owneruserid varchar(36) NOT NULL,
sharemode varchar(12) NOT NULL,
shareopts json NOT NULL,
PRIMARY KEY (sessionid, windowid) PRIMARY KEY (sessionid, windowid)
); );
@ -27,6 +33,8 @@ CREATE TABLE screen (
activewindowid varchar(36) NOT NULL, activewindowid varchar(36) NOT NULL,
screenidx int NOT NULL, screenidx int NOT NULL,
screenopts json NOT NULL, screenopts json NOT NULL,
owneruserid varchar(36) NOT NULL,
sharemode varchar(12) NOT NULL,
PRIMARY KEY (sessionid, screenid) PRIMARY KEY (sessionid, screenid)
); );
@ -52,7 +60,7 @@ CREATE TABLE remote_instance (
CREATE TABLE line ( CREATE TABLE line (
sessionid varchar(36) NOT NULL, sessionid varchar(36) NOT NULL,
windowid varchar(36) NOT NULL, windowid varchar(36) NOT NULL,
lineid int NOT NULL, lineid varchar(36) NOT NULL,
userid varchar(36) NOT NULL, userid varchar(36) NOT NULL,
ts bigint NOT NULL, ts bigint NOT NULL,
linetype varchar(10) NOT NULL, linetype varchar(10) NOT NULL,

View File

@ -250,7 +250,7 @@ func InsertSessionWithName(ctx context.Context, sessionName string, activate boo
names := tx.SelectStrings(`SELECT name FROM session`) names := tx.SelectStrings(`SELECT name FROM session`)
sessionName = fmtUniqueName(sessionName, "session-%d", len(names)+1, names) sessionName = fmtUniqueName(sessionName, "session-%d", len(names)+1, names)
maxSessionIdx := tx.GetInt(`SELECT COALESCE(max(sessionidx), 0) FROM session`) 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) tx.ExecWrap(query, newSessionId, sessionName, maxSessionIdx+1, 0)
_, err := InsertScreen(tx.Context(), newSessionId, "", true) _, err := InsertScreen(tx.Context(), newSessionId, "", true)
if err != nil { 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) screenNames := tx.SelectStrings(`SELECT name FROM screen WHERE sessionid = ?`, sessionId)
screenName = fmtUniqueName(screenName, "s%d", maxScreenIdx+1, screenNames) screenName = fmtUniqueName(screenName, "s%d", maxScreenIdx+1, screenNames)
newScreenId = uuid.New().String() 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{}) tx.ExecWrap(query, sessionId, newScreenId, screenName, newWindowId, maxScreenIdx+1, ScreenOptsType{})
layout := LayoutType{Type: LayoutFull} layout := LayoutType{Type: LayoutFull}
query = `INSERT INTO screen_window (sessionid, screenid, windowid, name, layout) VALUES (?, ?, ?, ?, ?)` 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 { func txCreateWindow(tx *TxWrap, sessionId string) string {
windowId := uuid.New().String() windowId := uuid.New().String()
query := `INSERT INTO window (sessionid, windowid, curremote, winopts) VALUES (?, ?, ?, ?)` query := `INSERT INTO window (sessionid, windowid, curremote, winopts, shareopts, owneruserid, sharemode) VALUES (?, ?, ?, ?, ?, '', 'local')`
tx.ExecWrap(query, sessionId, windowId, LocalRemoteName, WindowOptsType{}) tx.ExecWrap(query, sessionId, windowId, LocalRemoteName, WindowOptsType{}, WindowShareOptsType{})
return windowId return windowId
} }
@ -374,8 +374,8 @@ func InsertLine(ctx context.Context, line *LineType, cmd *CmdType) error {
if line == nil { if line == nil {
return fmt.Errorf("line cannot be nil") return fmt.Errorf("line cannot be nil")
} }
if line.LineId != 0 { if line.LineId == "" {
return fmt.Errorf("new line cannot have LineId set") return fmt.Errorf("line must have lineid set")
} }
return WithTx(ctx, func(tx *TxWrap) error { return WithTx(ctx, func(tx *TxWrap) error {
var windowId string var windowId string
@ -384,10 +384,6 @@ func InsertLine(ctx context.Context, line *LineType, cmd *CmdType) error {
if !hasWindow { if !hasWindow {
return fmt.Errorf("window not found, cannot insert line[%s/%s]", line.SessionId, line.WindowId) 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) query = `INSERT INTO line ( sessionid, windowid, lineid, ts, userid, linetype, text, cmdid)
VALUES (:sessionid,:windowid,:lineid,:ts,:userid,:linetype,:text,:cmdid)` VALUES (:sessionid,:windowid,:lineid,:ts,:userid,:linetype,:text,:cmdid)`
tx.NamedExecWrap(query, line) tx.NamedExecWrap(query, line)

View File

@ -22,7 +22,7 @@ func MakeMigrate() (*migrate.Migrate, error) {
dbUrl := fmt.Sprintf("sqlite3://%s", GetSessionDBName()) dbUrl := fmt.Sprintf("sqlite3://%s", GetSessionDBName())
m, err := migrate.New(migrationPathUrl, dbUrl) m, err := migrate.New(migrationPathUrl, dbUrl)
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("making migration [%s] db[%s]: %w", migrationPathUrl, GetSessionDBName(), err)
} }
return m, nil return m, nil
} }

View File

@ -47,7 +47,6 @@ const (
ShareModePrivate = "private" ShareModePrivate = "private"
ShareModeView = "view" ShareModeView = "view"
ShareModeShared = "shared" ShareModeShared = "shared"
ShareModeSharedView = "shared-view"
) )
var globalDBLock = &sync.Mutex{} var globalDBLock = &sync.Mutex{}
@ -66,7 +65,11 @@ func GetDB(ctx context.Context) (*sqlx.DB, error) {
globalDBLock.Lock() globalDBLock.Lock()
defer globalDBLock.Unlock() defer globalDBLock.Unlock()
if globalDB == nil && globalDBErr == nil { 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 return globalDB, globalDBErr
} }
@ -84,6 +87,9 @@ type SessionType struct {
Name string `json:"name"` Name string `json:"name"`
SessionIdx int64 `json:"sessionidx"` SessionIdx int64 `json:"sessionidx"`
ActiveScreenId string `json:"activescreenid"` ActiveScreenId string `json:"activescreenid"`
OwnerUserId string `json:"owneruserid"`
ShareMode string `json:"sharemode"`
AccessKey string `json:"-"`
NotifyNum int64 `json:"notifynum"` NotifyNum int64 `json:"notifynum"`
Screens []*ScreenType `json:"screens"` Screens []*ScreenType `json:"screens"`
Remotes []*RemoteInstance `json:"remotes"` Remotes []*RemoteInstance `json:"remotes"`
@ -104,11 +110,25 @@ func (opts WindowOptsType) Value() (driver.Value, error) {
return quickValueJson(opts) 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 { type WindowType struct {
SessionId string `json:"sessionid"` SessionId string `json:"sessionid"`
WindowId string `json:"windowid"` WindowId string `json:"windowid"`
CurRemote string `json:"curremote"` CurRemote string `json:"curremote"`
WinOpts WindowOptsType `json:"winopts"` WinOpts WindowOptsType `json:"winopts"`
OwnerUserId string `json:"owneruserid"`
ShareMode string `json:"sharemode"`
ShareOpts WindowShareOptsType `json:"shareopts"`
Lines []*LineType `json:"lines"` Lines []*LineType `json:"lines"`
Cmds []*CmdType `json:"cmds"` Cmds []*CmdType `json:"cmds"`
Remotes []*RemoteInstance `json:"remotes"` Remotes []*RemoteInstance `json:"remotes"`
@ -136,6 +156,8 @@ type ScreenType struct {
Name string `json:"name"` Name string `json:"name"`
ActiveWindowId string `json:"activewindowid"` ActiveWindowId string `json:"activewindowid"`
ScreenOpts ScreenOptsType `json:"screenopts"` ScreenOpts ScreenOptsType `json:"screenopts"`
OwnerUserId string `json:"owneruserid"`
ShareMode string `json:"sharemode"`
Windows []*ScreenWindowType `json:"windows"` Windows []*ScreenWindowType `json:"windows"`
// only for updates // only for updates
@ -186,7 +208,7 @@ type HistoryItemType struct {
SessionId string `json:"sessionid"` SessionId string `json:"sessionid"`
ScreenId string `json:"screenid"` ScreenId string `json:"screenid"`
WindowId string `json:"windowid"` WindowId string `json:"windowid"`
LineId int64 `json:"lineid"` LineId string `json:"lineid"`
HadError bool `json:"haderror"` HadError bool `json:"haderror"`
CmdId string `json:"cmdid"` CmdId string `json:"cmdid"`
CmdStr string `json:"cmdstr"` CmdStr string `json:"cmdstr"`
@ -238,7 +260,7 @@ type RemoteInstance struct {
type LineType struct { type LineType struct {
SessionId string `json:"sessionid"` SessionId string `json:"sessionid"`
WindowId string `json:"windowid"` WindowId string `json:"windowid"`
LineId int64 `json:"lineid"` LineId string `json:"lineid"`
Ts int64 `json:"ts"` Ts int64 `json:"ts"`
UserId string `json:"userid"` UserId string `json:"userid"`
LineType string `json:"linetype"` LineType string `json:"linetype"`
@ -359,6 +381,7 @@ func makeNewLineCmd(sessionId string, windowId string, userId string, cmdId stri
rtn := &LineType{} rtn := &LineType{}
rtn.SessionId = sessionId rtn.SessionId = sessionId
rtn.WindowId = windowId rtn.WindowId = windowId
rtn.LineId = uuid.New().String()
rtn.Ts = time.Now().UnixMilli() rtn.Ts = time.Now().UnixMilli()
rtn.UserId = userId rtn.UserId = userId
rtn.LineType = LineTypeCmd rtn.LineType = LineTypeCmd
@ -370,6 +393,7 @@ func makeNewLineText(sessionId string, windowId string, userId string, text stri
rtn := &LineType{} rtn := &LineType{}
rtn.SessionId = sessionId rtn.SessionId = sessionId
rtn.WindowId = windowId rtn.WindowId = windowId
rtn.LineId = uuid.New().String()
rtn.Ts = time.Now().UnixMilli() rtn.Ts = time.Now().UnixMilli()
rtn.UserId = userId rtn.UserId = userId
rtn.LineType = LineTypeText rtn.LineType = LineTypeText
@ -398,11 +422,11 @@ func AddCmdLine(ctx context.Context, sessionId string, windowId string, userId s
func EnsureLocalRemote(ctx context.Context) error { func EnsureLocalRemote(ctx context.Context) error {
remoteId, err := base.GetRemoteId() remoteId, err := base.GetRemoteId()
if err != nil { if err != nil {
return err return fmt.Errorf("getting local remoteid: %w", err)
} }
remote, err := GetRemoteById(ctx, remoteId) remote, err := GetRemoteById(ctx, remoteId)
if err != nil { if err != nil {
return err return fmt.Errorf("getting remote[%s] from db: %w", remoteId, err)
} }
if remote != nil { if remote != nil {
return nil return nil

View File

@ -74,13 +74,13 @@ func (LineUpdate) UpdateType() string {
return LineCmdUpdateStr return LineCmdUpdateStr
} }
func ReadLineCmdIdFromUpdate(update UpdatePacket) (int64, string) { func ReadLineCmdIdFromUpdate(update UpdatePacket) (string, string) {
lineUpdate, ok := update.(LineUpdate) lineUpdate, ok := update.(LineUpdate)
if !ok { if !ok {
return 0, "" return "", ""
} }
if lineUpdate.Line == nil { if lineUpdate.Line == nil {
return 0, "" return "", ""
} }
return lineUpdate.Line.LineId, lineUpdate.Line.CmdId return lineUpdate.Line.LineId, lineUpdate.Line.CmdId
} }