mirror of
https://github.com/wavetermdev/waveterm.git
synced 2025-04-05 18:37:13 +02:00
updates to tables, lineid, sharemode, owneruserid
This commit is contained in:
parent
8186e54cd2
commit
b26b9924f3
@ -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())
|
||||
|
@ -1,3 +1,4 @@
|
||||
DROP TABLE client;
|
||||
DROP TABLE session;
|
||||
DROP TABLE window;
|
||||
DROP TABLE screen;
|
||||
|
@ -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,
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -47,7 +47,6 @@ const (
|
||||
ShareModePrivate = "private"
|
||||
ShareModeView = "view"
|
||||
ShareModeShared = "shared"
|
||||
ShareModeSharedView = "shared-view"
|
||||
)
|
||||
|
||||
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,11 +110,25 @@ 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"`
|
||||
OwnerUserId string `json:"owneruserid"`
|
||||
ShareMode string `json:"sharemode"`
|
||||
ShareOpts WindowShareOptsType `json:"shareopts"`
|
||||
Lines []*LineType `json:"lines"`
|
||||
Cmds []*CmdType `json:"cmds"`
|
||||
Remotes []*RemoteInstance `json:"remotes"`
|
||||
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user