diff --git a/db/migrations/000001_init.up.sql b/db/migrations/000001_init.up.sql index 7bd73b0d..c58399aa 100644 --- a/db/migrations/000001_init.up.sql +++ b/db/migrations/000001_init.up.sql @@ -87,7 +87,8 @@ CREATE TABLE remote ( sshopts json NOT NULL, remoteopts json NOT NULL, lastconnectts bigint NOT NULL, - archived boolean NOT NULL + archived boolean NOT NULL, + remoteidx int NOT NULL ); CREATE TABLE cmd ( diff --git a/db/schema.sql b/db/schema.sql index d3bf932b..9e71be1c 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -82,7 +82,8 @@ CREATE TABLE remote ( sshopts json NOT NULL, remoteopts json NOT NULL, lastconnectts bigint NOT NULL, - archived boolean NOT NULL + archived boolean NOT NULL, + remoteidx int NOT NULL ); CREATE TABLE cmd ( sessionid varchar(36) NOT NULL, diff --git a/pkg/remote/remote.go b/pkg/remote/remote.go index ed5b5c11..d32a44c9 100644 --- a/pkg/remote/remote.go +++ b/pkg/remote/remote.go @@ -81,6 +81,7 @@ type RemoteRuntimeState struct { DefaultState *sstore.RemoteState `json:"defaultstate"` ConnectMode string `json:"connectmode"` Archived bool `json:"archived"` + RemoteIdx int64 `json:"remoteidx"` } func logf(rem *sstore.RemoteType, fmtStr string, args ...interface{}) { @@ -324,6 +325,7 @@ func (msh *MShellProc) GetRemoteRuntimeState() RemoteRuntimeState { Status: msh.Status, ConnectMode: msh.Remote.ConnectMode, Archived: msh.Remote.Archived, + RemoteIdx: msh.Remote.RemoteIdx, } if msh.Err != nil { state.ErrorStr = msh.Err.Error() diff --git a/pkg/sstore/dbops.go b/pkg/sstore/dbops.go index c7e8f189..a999de56 100644 --- a/pkg/sstore/dbops.go +++ b/pkg/sstore/dbops.go @@ -30,7 +30,7 @@ func NumSessions(ctx context.Context) (int, error) { func GetAllRemotes(ctx context.Context) ([]*RemoteType, error) { var rtn []*RemoteType err := WithTx(ctx, func(tx *TxWrap) error { - query := `SELECT * FROM remote` + query := `SELECT * FROM remote ORDER BY remoteidx` marr := tx.SelectMaps(query) for _, m := range marr { rtn = append(rtn, RemoteFromMap(m)) @@ -121,6 +121,9 @@ func UpsertRemote(ctx context.Context, r *RemoteType) error { if tx.Exists(query, r.RemoteCanonicalName) { return fmt.Errorf("remote has duplicate canonicalname '%s', cannot create", r.RemoteCanonicalName) } + query = `SELECT max(remoteidx) FROM remote` + maxRemoteIdx := tx.GetInt(query) + r.RemoteIdx = int64(maxRemoteIdx + 1) query = `INSERT INTO remote ( remoteid, physicalid, remotetype, remotealias, remotecanonicalname, remotesudo, remoteuser, remotehost, connectmode, initpk, sshopts, remoteopts, lastconnectts, archived) VALUES (:remoteid,:physicalid,:remotetype,:remotealias,:remotecanonicalname,:remotesudo,:remoteuser,:remotehost,:connectmode,:initpk,:sshopts,:remoteopts,:lastconnectts,:archived)` diff --git a/pkg/sstore/sstore.go b/pkg/sstore/sstore.go index 326934a3..7241df81 100644 --- a/pkg/sstore/sstore.go +++ b/pkg/sstore/sstore.go @@ -431,6 +431,7 @@ type RemoteType struct { RemoteOpts *RemoteOptsType `json:"remoteopts"` LastConnectTs int64 `json:"lastconnectts"` Archived bool `json:"archived"` + RemoteIdx int64 `json:"remoteidx"` } func (r *RemoteType) GetName() string { @@ -471,6 +472,7 @@ func (r *RemoteType) ToMap() map[string]interface{} { rtn["remoteopts"] = quickJson(r.RemoteOpts) rtn["lastconnectts"] = r.LastConnectTs rtn["archived"] = r.Archived + rtn["remoteidx"] = r.RemoteIdx return rtn } @@ -493,6 +495,7 @@ func RemoteFromMap(m map[string]interface{}) *RemoteType { quickSetJson(&r.RemoteOpts, m, "remoteopts") quickSetInt64(&r.LastConnectTs, m, "lastconnectts") quickSetBool(&r.Archived, m, "archived") + quickSetInt64(&r.RemoteIdx, m, "remoteidx") return &r }