源码快照 @ 2026-09-01 (source-only, 剔除非源码素材)
Publish Claude Code Binaries / Stage and publish binaries (push) Canceled after 0s
Publish Tutti App Runtime / Build darwin-arm64 (push) Canceled after 0s
Publish Tutti App Runtime / Build darwin-amd64 (push) Canceled after 0s
Publish Tutti App Runtime / Build linux-amd64 (push) Canceled after 0s
Publish Tutti App Runtime / Build windows-amd64 (push) Canceled after 0s
Sync Tutti Agent Skills / sync (push) Canceled after 0s
Windows Agent Adapters / Test Windows agent adapters (push) Canceled after 0s
Windows Daemon Adapters / Test Windows daemon adapters (push) Canceled after 0s
Publish Tutti App Runtime / Publish runtime catalog (push) Canceled after 0s
Publish Claude Code Binaries / Stage and publish binaries (push) Canceled after 0s
Publish Tutti App Runtime / Build darwin-arm64 (push) Canceled after 0s
Publish Tutti App Runtime / Build darwin-amd64 (push) Canceled after 0s
Publish Tutti App Runtime / Build linux-amd64 (push) Canceled after 0s
Publish Tutti App Runtime / Build windows-amd64 (push) Canceled after 0s
Sync Tutti Agent Skills / sync (push) Canceled after 0s
Windows Agent Adapters / Test Windows agent adapters (push) Canceled after 0s
Windows Daemon Adapters / Test Windows daemon adapters (push) Canceled after 0s
Publish Tutti App Runtime / Publish runtime catalog (push) Canceled after 0s
This commit is contained in:
commit
2ed3415fb7
8018 files changed
+1936476
No files matched your search
@@ -0,0 +1,28 @@
|
||||
# Connector SQLite Store
|
||||
|
||||
`packages/connector/daemon/application/adapters/sqlite` is the canonical local SQLite implementation
|
||||
of the Connector Host repository and changed-event outbox contracts. Hosts own
|
||||
the selected database path and process lifecycle; the module owns schema,
|
||||
migration, transactions, revisions, leases, and operation persistence.
|
||||
|
||||
Device installation remains on the Connector row. Account authorization is
|
||||
stored independently in `connector_market_authorization_projections`, keyed by
|
||||
`account_id + connector_key`, so account switching cannot overwrite installed
|
||||
truth. Local uninstall deletes installed-release evidence but never deletes the
|
||||
account authorization Projection; disconnect is a separate authorization
|
||||
operation.
|
||||
|
||||
Authorization Session receipts remain private inside completed Start operation
|
||||
records. Snapshot application uses one SQLite transaction to advance the
|
||||
account Projection and surface every matching unresolved receipt when the
|
||||
server reports the Connector connected. Older Snapshot revisions never replace
|
||||
newer Projection state; the daemon resolves a receipt only after its scoped
|
||||
Runtime Reconcile completes.
|
||||
|
||||
Active operations and pending outbox events are durable and never age-pruned.
|
||||
The lifecycle cleanup contract removes bounded batches of expired terminal
|
||||
operations and already-published events. Installed release evidence is stored
|
||||
separately from operation history so runtime recovery does not depend on an
|
||||
expired operation row. A probe-detected missing implementation retains that
|
||||
evidence while installation is failed, allowing repair or uninstall to keep
|
||||
targeting the accepted release.
|
||||
@@ -0,0 +1,313 @@
|
||||
package storesqlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
market "github.com/tutti-os/tutti/packages/connector/daemon/core"
|
||||
)
|
||||
|
||||
func (store *Store) AuthorizationProjection(
|
||||
ctx context.Context,
|
||||
accountID, connectorKey string,
|
||||
) (market.AuthorizationProjection, error) {
|
||||
var payload string
|
||||
if err := store.db.QueryRowContext(ctx, `
|
||||
SELECT projection_json FROM connector_market_authorization_projections
|
||||
WHERE account_id = ? AND connector_key = ?`, accountID, connectorKey).Scan(&payload); err != nil {
|
||||
return market.AuthorizationProjection{}, mapNotFound(err)
|
||||
}
|
||||
var projection market.AuthorizationProjection
|
||||
if err := json.Unmarshal([]byte(payload), &projection); err != nil {
|
||||
return market.AuthorizationProjection{}, fmt.Errorf("decode connector authorization projection: %w", err)
|
||||
}
|
||||
return projection, nil
|
||||
}
|
||||
|
||||
func overlayAuthorizationProjectionsOn(
|
||||
ctx context.Context,
|
||||
tx *sql.Tx,
|
||||
accountID string,
|
||||
connectors []market.Connector,
|
||||
) ([]market.Connector, error) {
|
||||
rows, err := tx.QueryContext(ctx, `SELECT connector_key, projection_json FROM connector_market_authorization_projections WHERE account_id = ?`, accountID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
projections := make(map[string]market.AuthorizationProjection)
|
||||
for rows.Next() {
|
||||
var connectorKey, payload string
|
||||
if err := rows.Scan(&connectorKey, &payload); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var projection market.AuthorizationProjection
|
||||
if err := json.Unmarshal([]byte(payload), &projection); err != nil {
|
||||
return nil, fmt.Errorf("decode connector authorization projection: %w", err)
|
||||
}
|
||||
projections[connectorKey] = projection
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for index := range connectors {
|
||||
projection, ok := projections[connectors[index].Key]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
connectors[index].Authorization = market.Authorization{
|
||||
State: projection.State, FailureCode: projection.FailureCode,
|
||||
}
|
||||
}
|
||||
return connectors, nil
|
||||
}
|
||||
|
||||
func (store *Store) SaveAuthorizationProjection(
|
||||
ctx context.Context,
|
||||
projection market.AuthorizationProjection,
|
||||
) error {
|
||||
payload, err := json.Marshal(projection)
|
||||
if err != nil {
|
||||
return fmt.Errorf("encode connector authorization projection: %w", err)
|
||||
}
|
||||
tx, err := store.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
var currentPayload string
|
||||
publicChanged := true
|
||||
currentErr := tx.QueryRowContext(ctx, `SELECT projection_json FROM connector_market_authorization_projections
|
||||
WHERE account_id = ? AND connector_key = ?`, projection.AccountID, projection.ConnectorKey).Scan(¤tPayload)
|
||||
if currentErr == nil {
|
||||
var current market.AuthorizationProjection
|
||||
if err := json.Unmarshal([]byte(currentPayload), ¤t); err != nil {
|
||||
return fmt.Errorf("decode connector authorization projection: %w", err)
|
||||
}
|
||||
if current.ServerSynchronized && !projection.ServerSynchronized ||
|
||||
current.ServerSynchronized && projection.ServerSynchronized && current.ServerRevision > projection.ServerRevision {
|
||||
return tx.Commit()
|
||||
}
|
||||
publicChanged = !samePublicAuthorizationProjection(current, projection)
|
||||
} else if !errors.Is(currentErr, sql.ErrNoRows) {
|
||||
return currentErr
|
||||
}
|
||||
if _, err = tx.ExecContext(ctx, `
|
||||
INSERT INTO connector_market_authorization_projections (account_id, connector_key, projection_json)
|
||||
VALUES (?, ?, ?)
|
||||
ON CONFLICT(account_id, connector_key) DO UPDATE SET projection_json = excluded.projection_json`,
|
||||
projection.AccountID, projection.ConnectorKey, string(payload)); err != nil {
|
||||
return err
|
||||
}
|
||||
if publicChanged {
|
||||
if err := bumpAuthorizationProjectionRevisionOn(ctx, tx, projection.ConnectorKey); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func (store *Store) ApplyAuthorizationSnapshot(
|
||||
ctx context.Context,
|
||||
accountID string,
|
||||
snapshot market.AuthorizationSnapshot,
|
||||
) (market.AuthorizationSnapshotApplyResult, error) {
|
||||
accountID = strings.TrimSpace(accountID)
|
||||
if accountID == "" {
|
||||
return market.AuthorizationSnapshotApplyResult{}, errors.New("authorization snapshot account is required")
|
||||
}
|
||||
tx, err := store.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return market.AuthorizationSnapshotApplyResult{}, err
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
var currentRevision uint64
|
||||
err = tx.QueryRowContext(ctx, `SELECT revision FROM connector_market_authorization_snapshot_revisions WHERE account_id = ?`, accountID).Scan(¤tRevision)
|
||||
hasRevision := err == nil
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return market.AuthorizationSnapshotApplyResult{}, err
|
||||
}
|
||||
rows, err := tx.QueryContext(ctx, `SELECT connector_key, projection_json FROM connector_market_authorization_projections WHERE account_id = ?`, accountID)
|
||||
if err != nil {
|
||||
return market.AuthorizationSnapshotApplyResult{}, err
|
||||
}
|
||||
existing := make(map[string]market.AuthorizationProjection)
|
||||
for rows.Next() {
|
||||
var connectorKey, payload string
|
||||
if err := rows.Scan(&connectorKey, &payload); err != nil {
|
||||
_ = rows.Close()
|
||||
return market.AuthorizationSnapshotApplyResult{}, err
|
||||
}
|
||||
var projection market.AuthorizationProjection
|
||||
if err := json.Unmarshal([]byte(payload), &projection); err != nil {
|
||||
_ = rows.Close()
|
||||
return market.AuthorizationSnapshotApplyResult{}, fmt.Errorf("decode connector authorization projection: %w", err)
|
||||
}
|
||||
existing[connectorKey] = projection
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return market.AuthorizationSnapshotApplyResult{}, err
|
||||
}
|
||||
now := time.Now().UTC()
|
||||
result := market.AuthorizationSnapshotApplyResult{}
|
||||
effective := existing
|
||||
if !hasRevision || snapshot.Revision > currentRevision {
|
||||
incoming := make(map[string]market.AuthorizationProjection, len(snapshot.Connectors))
|
||||
for _, projection := range snapshot.Connectors {
|
||||
projection.AccountID = accountID
|
||||
projection.ConnectorKey = strings.TrimSpace(projection.ConnectorKey)
|
||||
if projection.ConnectorKey == "" {
|
||||
return market.AuthorizationSnapshotApplyResult{}, errors.New("authorization snapshot contains an empty connector key")
|
||||
}
|
||||
projection.ServerRevision = snapshot.Revision
|
||||
projection.ServerSynchronized = true
|
||||
projection.UpdatedAt = now
|
||||
incoming[projection.ConnectorKey] = projection
|
||||
}
|
||||
for connectorKey, previous := range existing {
|
||||
if _, ok := incoming[connectorKey]; ok {
|
||||
continue
|
||||
}
|
||||
if !previous.ServerSynchronized {
|
||||
continue
|
||||
}
|
||||
incoming[connectorKey] = market.AuthorizationProjection{
|
||||
AccountID: accountID, ConnectorKey: connectorKey, ServerRevision: snapshot.Revision,
|
||||
ServerSynchronized: true, State: market.AuthorizationStateDisconnected, UpdatedAt: now,
|
||||
}
|
||||
}
|
||||
result.ChangedConnectorKeys = make([]string, 0, len(incoming))
|
||||
for connectorKey, projection := range incoming {
|
||||
previous, exists := existing[connectorKey]
|
||||
if !exists || previous.State != projection.State || previous.ConnectionID != projection.ConnectionID ||
|
||||
previous.ConnectionVersion != projection.ConnectionVersion || previous.ConnectorVersion != projection.ConnectorVersion {
|
||||
result.ChangedConnectorKeys = append(result.ChangedConnectorKeys, connectorKey)
|
||||
}
|
||||
payload, err := json.Marshal(projection)
|
||||
if err != nil {
|
||||
return market.AuthorizationSnapshotApplyResult{}, fmt.Errorf("encode connector authorization projection: %w", err)
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, `INSERT INTO connector_market_authorization_projections (account_id, connector_key, projection_json)
|
||||
VALUES (?, ?, ?) ON CONFLICT(account_id,connector_key) DO UPDATE SET projection_json = excluded.projection_json`, accountID, connectorKey, string(payload)); err != nil {
|
||||
return market.AuthorizationSnapshotApplyResult{}, err
|
||||
}
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, `INSERT INTO connector_market_authorization_snapshot_revisions (account_id, revision) VALUES (?, ?)
|
||||
ON CONFLICT(account_id) DO UPDATE SET revision = excluded.revision`, accountID, snapshot.Revision); err != nil {
|
||||
return market.AuthorizationSnapshotApplyResult{}, err
|
||||
}
|
||||
sort.Strings(result.ChangedConnectorKeys)
|
||||
for _, connectorKey := range result.ChangedConnectorKeys {
|
||||
if err := bumpAuthorizationProjectionRevisionOn(ctx, tx, connectorKey); err != nil {
|
||||
return market.AuthorizationSnapshotApplyResult{}, err
|
||||
}
|
||||
}
|
||||
effective = incoming
|
||||
}
|
||||
pending, err := connectedAuthorizationReceiptConnectorKeys(ctx, tx, accountID, effective)
|
||||
if err != nil {
|
||||
return market.AuthorizationSnapshotApplyResult{}, err
|
||||
}
|
||||
result.PendingReceiptConnectorKeys = pending
|
||||
sort.Strings(result.ChangedConnectorKeys)
|
||||
sort.Strings(result.PendingReceiptConnectorKeys)
|
||||
if err := tx.Commit(); err != nil {
|
||||
return market.AuthorizationSnapshotApplyResult{}, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func samePublicAuthorizationProjection(left, right market.AuthorizationProjection) bool {
|
||||
return left.State == right.State && left.ConnectionID == right.ConnectionID &&
|
||||
left.FailureCode == right.FailureCode && left.ConnectionVersion == right.ConnectionVersion &&
|
||||
left.ConnectorVersion == right.ConnectorVersion && left.ServerSynchronized == right.ServerSynchronized
|
||||
}
|
||||
|
||||
func bumpAuthorizationProjectionRevisionOn(ctx context.Context, tx *sql.Tx, connectorKey string) error {
|
||||
var connectorPayload string
|
||||
if err := tx.QueryRowContext(ctx, `SELECT connector_json FROM connector_market_connectors WHERE connector_key = ?`, connectorKey).Scan(&connectorPayload); err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
connector, err := decodeConnector(connectorPayload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var revision uint64
|
||||
if err := tx.QueryRowContext(ctx, `UPDATE connector_market_metadata SET revision = revision + 1 WHERE id = ? RETURNING revision`, metadataID).Scan(&revision); err != nil {
|
||||
return err
|
||||
}
|
||||
connector.Revision = revision
|
||||
payload, err := json.Marshal(connector)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, `UPDATE connector_market_connectors SET connector_json = ? WHERE connector_key = ?`, string(payload), connectorKey); err != nil {
|
||||
return err
|
||||
}
|
||||
eventPayload, err := json.Marshal(market.ChangedEvent{ConnectorKey: connectorKey, Revision: revision})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = tx.ExecContext(ctx, `INSERT INTO connector_market_outbox (revision, event_json, published_at_unix_ms) VALUES (?, ?, NULL)`, revision, string(eventPayload))
|
||||
return err
|
||||
}
|
||||
|
||||
func connectedAuthorizationReceiptConnectorKeys(
|
||||
ctx context.Context,
|
||||
tx *sql.Tx,
|
||||
accountID string,
|
||||
projections map[string]market.AuthorizationProjection,
|
||||
) ([]string, error) {
|
||||
connected := make(map[string]struct{})
|
||||
for connectorKey, projection := range projections {
|
||||
if projection.ServerSynchronized && projection.State == market.AuthorizationStateConnected {
|
||||
connected[connectorKey] = struct{}{}
|
||||
}
|
||||
}
|
||||
if len(connected) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
rows, err := tx.QueryContext(ctx, `SELECT operation_json FROM connector_market_operations
|
||||
WHERE kind = 'start_authorization' AND state = 'completed' ORDER BY operation_id`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pending := make(map[string]struct{})
|
||||
for rows.Next() {
|
||||
var payload string
|
||||
if err := rows.Scan(&payload); err != nil {
|
||||
_ = rows.Close()
|
||||
return nil, err
|
||||
}
|
||||
operation, err := decodeOperation(payload)
|
||||
if err != nil {
|
||||
_ = rows.Close()
|
||||
return nil, err
|
||||
}
|
||||
if operation.Scope.AccountID != accountID || operation.Execution.AuthorizationSession == nil ||
|
||||
operation.Execution.AuthorizationSession.IsResolved() {
|
||||
continue
|
||||
}
|
||||
if _, ok := connected[operation.ConnectorKey]; !ok {
|
||||
continue
|
||||
}
|
||||
pending[operation.ConnectorKey] = struct{}{}
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
keys := make([]string, 0, len(pending))
|
||||
for connectorKey := range pending {
|
||||
keys = append(keys, connectorKey)
|
||||
}
|
||||
return keys, nil
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
module github.com/tutti-os/tutti/packages/connector/daemon/adapters/sqlite
|
||||
|
||||
go 1.24.3
|
||||
|
||||
toolchain go1.24.5
|
||||
|
||||
require (
|
||||
github.com/tutti-os/tutti/packages/connector/daemon/core v0.0.0
|
||||
modernc.org/sqlite v1.45.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/ncruces/go-strftime v1.0.0 // indirect
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||
golang.org/x/exp v0.0.0-20251023183803-a4bb9ffd2546 // indirect
|
||||
golang.org/x/sys v0.41.0 // indirect
|
||||
golang.org/x/tools v0.42.0 // indirect
|
||||
modernc.org/libc v1.67.6 // indirect
|
||||
modernc.org/mathutil v1.7.1 // indirect
|
||||
modernc.org/memory v1.11.0 // indirect
|
||||
)
|
||||
|
||||
replace github.com/tutti-os/tutti/packages/connector/daemon/core => ../../core
|
||||
@@ -0,0 +1,53 @@
|
||||
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
||||
github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e h1:ijClszYn+mADRFY17kjQEVQ1XRhq2/JR1M3sGqeJoxs=
|
||||
github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e/go.mod h1:boTsfXsheKC2y+lKOCMpSfarhxDeIzfZG1jqGcPl3cA=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
|
||||
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/ncruces/go-strftime v1.0.0 h1:HMFp8mLCTPp341M/ZnA4qaf7ZlsbTc+miZjCLOFAw7w=
|
||||
github.com/ncruces/go-strftime v1.0.0/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||
golang.org/x/exp v0.0.0-20251023183803-a4bb9ffd2546 h1:mgKeJMpvi0yx/sU5GsxQ7p6s2wtOnGAHZWCHUM4KGzY=
|
||||
golang.org/x/exp v0.0.0-20251023183803-a4bb9ffd2546/go.mod h1:j/pmGrbnkbPtQfxEe5D0VQhZC6qKbfKifgD0oM7sR70=
|
||||
golang.org/x/mod v0.33.0 h1:tHFzIWbBifEmbwtGz65eaWyGiGZatSrT9prnU8DbVL8=
|
||||
golang.org/x/mod v0.33.0/go.mod h1:swjeQEj+6r7fODbD2cqrnje9PnziFuw4bmLbBZFrQ5w=
|
||||
golang.org/x/sync v0.19.0 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4=
|
||||
golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k=
|
||||
golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||
golang.org/x/tools v0.42.0 h1:uNgphsn75Tdz5Ji2q36v/nsFSfR/9BRFvqhGBaJGd5k=
|
||||
golang.org/x/tools v0.42.0/go.mod h1:Ma6lCIwGZvHK6XtgbswSoWroEkhugApmsXyrUmBhfr0=
|
||||
modernc.org/cc/v4 v4.27.1 h1:9W30zRlYrefrDV2JE2O8VDtJ1yPGownxciz5rrbQZis=
|
||||
modernc.org/cc/v4 v4.27.1/go.mod h1:uVtb5OGqUKpoLWhqwNQo/8LwvoiEBLvZXIQ/SmO6mL0=
|
||||
modernc.org/ccgo/v4 v4.30.1 h1:4r4U1J6Fhj98NKfSjnPUN7Ze2c6MnAdL0hWw6+LrJpc=
|
||||
modernc.org/ccgo/v4 v4.30.1/go.mod h1:bIOeI1JL54Utlxn+LwrFyjCx2n2RDiYEaJVSrgdrRfM=
|
||||
modernc.org/fileutil v1.3.40 h1:ZGMswMNc9JOCrcrakF1HrvmergNLAmxOPjizirpfqBA=
|
||||
modernc.org/fileutil v1.3.40/go.mod h1:HxmghZSZVAz/LXcMNwZPA/DRrQZEVP9VX0V4LQGQFOc=
|
||||
modernc.org/gc/v2 v2.6.5 h1:nyqdV8q46KvTpZlsw66kWqwXRHdjIlJOhG6kxiV/9xI=
|
||||
modernc.org/gc/v2 v2.6.5/go.mod h1:YgIahr1ypgfe7chRuJi2gD7DBQiKSLMPgBQe9oIiito=
|
||||
modernc.org/gc/v3 v3.1.1 h1:k8T3gkXWY9sEiytKhcgyiZ2L0DTyCQ/nvX+LoCljoRE=
|
||||
modernc.org/gc/v3 v3.1.1/go.mod h1:HFK/6AGESC7Ex+EZJhJ2Gni6cTaYpSMmU/cT9RmlfYY=
|
||||
modernc.org/goabi0 v0.2.0 h1:HvEowk7LxcPd0eq6mVOAEMai46V+i7Jrj13t4AzuNks=
|
||||
modernc.org/goabi0 v0.2.0/go.mod h1:CEFRnnJhKvWT1c1JTI3Avm+tgOWbkOu5oPA8eH8LnMI=
|
||||
modernc.org/libc v1.67.6 h1:eVOQvpModVLKOdT+LvBPjdQqfrZq+pC39BygcT+E7OI=
|
||||
modernc.org/libc v1.67.6/go.mod h1:JAhxUVlolfYDErnwiqaLvUqc8nfb2r6S6slAgZOnaiE=
|
||||
modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU=
|
||||
modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg=
|
||||
modernc.org/memory v1.11.0 h1:o4QC8aMQzmcwCK3t3Ux/ZHmwFPzE6hf2Y5LbkRs+hbI=
|
||||
modernc.org/memory v1.11.0/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw=
|
||||
modernc.org/opt v0.1.4 h1:2kNGMRiUjrp4LcaPuLY2PzUfqM/w9N23quVwhKt5Qm8=
|
||||
modernc.org/opt v0.1.4/go.mod h1:03fq9lsNfvkYSfxrfUhZCWPk1lm4cq4N+Bh//bEtgns=
|
||||
modernc.org/sortutil v1.2.1 h1:+xyoGf15mM3NMlPDnFqrteY07klSFxLElE2PVuWIJ7w=
|
||||
modernc.org/sortutil v1.2.1/go.mod h1:7ZI3a3REbai7gzCLcotuw9AC4VZVpYMjDzETGsSMqJE=
|
||||
modernc.org/sqlite v1.45.0 h1:r51cSGzKpbptxnby+EIIz5fop4VuE4qFoVEjNvWoObs=
|
||||
modernc.org/sqlite v1.45.0/go.mod h1:CzbrU2lSB1DKUusvwGz7rqEKIq+NUd8GWuBBZDs9/nA=
|
||||
modernc.org/strutil v1.2.1 h1:UneZBkQA+DX2Rp35KcM69cSsNES9ly8mQWD71HKlOA0=
|
||||
modernc.org/strutil v1.2.1/go.mod h1:EHkiggD70koQxjVdSBM3JKM7k6L0FbGE5eymy9i3B9A=
|
||||
modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=
|
||||
modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=
|
||||
@@ -0,0 +1,376 @@
|
||||
package storesqlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
market "github.com/tutti-os/tutti/packages/connector/daemon/core"
|
||||
)
|
||||
|
||||
const maxLifecycleCleanupBatchSize = 500
|
||||
|
||||
func (store *Store) migrateLifecycle(ctx context.Context) error {
|
||||
if _, err := store.db.ExecContext(ctx, `ALTER TABLE connector_market_operations ADD COLUMN updated_at_unix_ms INTEGER NOT NULL DEFAULT 0`); err != nil &&
|
||||
!isDuplicateColumnError(err) {
|
||||
return fmt.Errorf("migrate connector market operation update timestamp: %w", err)
|
||||
}
|
||||
if _, err := store.db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS connector_market_release_installations (
|
||||
connector_key TEXT NOT NULL,
|
||||
release_digest TEXT NOT NULL,
|
||||
release_json TEXT NOT NULL,
|
||||
PRIMARY KEY (connector_key, release_digest)
|
||||
)`); err != nil {
|
||||
return fmt.Errorf("migrate connector release installations: %w", err)
|
||||
}
|
||||
if err := store.backfillLifecycleColumns(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
statements := []string{
|
||||
`CREATE INDEX IF NOT EXISTS connector_market_operations_terminal_cleanup
|
||||
ON connector_market_operations(updated_at_unix_ms, operation_id)
|
||||
WHERE state IN ('completed', 'failed')`,
|
||||
`CREATE INDEX IF NOT EXISTS connector_market_outbox_published_cleanup
|
||||
ON connector_market_outbox(published_at_unix_ms, sequence)
|
||||
WHERE published_at_unix_ms IS NOT NULL`,
|
||||
}
|
||||
for _, statement := range statements {
|
||||
if _, err := store.db.ExecContext(ctx, statement); err != nil {
|
||||
return fmt.Errorf("migrate connector market lifecycle index: %w", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func isDuplicateColumnError(err error) bool {
|
||||
return err != nil && strings.Contains(strings.ToLower(err.Error()), "duplicate column")
|
||||
}
|
||||
|
||||
func (store *Store) backfillLifecycleColumns(ctx context.Context) error {
|
||||
tx, err := store.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
rows, err := tx.QueryContext(ctx, `
|
||||
SELECT operation_id, operation_json FROM connector_market_operations
|
||||
WHERE updated_at_unix_ms = 0`)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
type timestampBackfill struct {
|
||||
operationID string
|
||||
updatedAtMS int64
|
||||
}
|
||||
var updates []timestampBackfill
|
||||
for rows.Next() {
|
||||
var operationID, payload string
|
||||
if err := rows.Scan(&operationID, &payload); err != nil {
|
||||
_ = rows.Close()
|
||||
return err
|
||||
}
|
||||
operation, err := decodeOperation(payload)
|
||||
if err != nil {
|
||||
_ = rows.Close()
|
||||
return err
|
||||
}
|
||||
updates = append(updates, timestampBackfill{operationID: operationID, updatedAtMS: operation.UpdatedAt.UTC().UnixMilli()})
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, update := range updates {
|
||||
if _, err := tx.ExecContext(ctx, `UPDATE connector_market_operations SET updated_at_unix_ms = ? WHERE operation_id = ? AND updated_at_unix_ms = 0`,
|
||||
update.updatedAtMS, update.operationID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := backfillConnectorInstallationTimes(ctx, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := backfillInstalledReleaseEvidence(ctx, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func backfillConnectorInstallationTimes(ctx context.Context, tx *sql.Tx) error {
|
||||
rows, err := tx.QueryContext(ctx, `SELECT connector_key, connector_json FROM connector_market_connectors`)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
type connectorBackfill struct {
|
||||
connectorKey string
|
||||
connector market.Connector
|
||||
}
|
||||
var connectors []connectorBackfill
|
||||
for rows.Next() {
|
||||
var connectorKey, payload string
|
||||
if err := rows.Scan(&connectorKey, &payload); err != nil {
|
||||
_ = rows.Close()
|
||||
return err
|
||||
}
|
||||
connector, err := decodeConnector(payload)
|
||||
if err != nil {
|
||||
_ = rows.Close()
|
||||
return err
|
||||
}
|
||||
connectors = append(connectors, connectorBackfill{connectorKey: connectorKey, connector: connector})
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, entry := range connectors {
|
||||
if entry.connector.Installation.State != market.InstallationStateInstalled ||
|
||||
entry.connector.Installation.InstalledAtUnixMS > 0 {
|
||||
continue
|
||||
}
|
||||
var operationPayload string
|
||||
err := tx.QueryRowContext(ctx, `SELECT operation_json FROM connector_market_operations
|
||||
WHERE connector_key = ? AND kind = ? AND state = ?
|
||||
ORDER BY updated_at_unix_ms DESC LIMIT 1`, entry.connectorKey, market.OperationKindInstall, market.OperationStateCompleted).Scan(&operationPayload)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
operation, err := decodeOperation(operationPayload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
installedAtUnixMS := operation.UpdatedAt.UTC().UnixMilli()
|
||||
if installedAtUnixMS <= 0 {
|
||||
continue
|
||||
}
|
||||
entry.connector.Installation.InstalledAtUnixMS = installedAtUnixMS
|
||||
payload, err := json.Marshal(entry.connector)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, `UPDATE connector_market_connectors SET connector_json = ? WHERE connector_key = ?`, string(payload), entry.connectorKey); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func backfillInstalledReleaseEvidence(ctx context.Context, tx *sql.Tx) error {
|
||||
rows, err := tx.QueryContext(ctx, `SELECT connector_json FROM connector_market_connectors`)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var connectors []market.Connector
|
||||
for rows.Next() {
|
||||
var payload string
|
||||
if err := rows.Scan(&payload); err != nil {
|
||||
_ = rows.Close()
|
||||
return err
|
||||
}
|
||||
connector, err := decodeConnector(payload)
|
||||
if err != nil {
|
||||
_ = rows.Close()
|
||||
return err
|
||||
}
|
||||
connectors = append(connectors, connector)
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, connector := range connectors {
|
||||
digest := connector.Installation.InstalledReleaseDigest
|
||||
if digest == "" {
|
||||
continue
|
||||
}
|
||||
var historicalDigest string
|
||||
err := tx.QueryRowContext(ctx, `SELECT release_digest FROM connector_market_release_installations
|
||||
WHERE connector_key = ? AND release_digest = ?`, connector.Key, digest).Scan(&historicalDigest)
|
||||
if err == nil {
|
||||
continue
|
||||
}
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return err
|
||||
}
|
||||
release, found, err := legacyInstalledReleaseEvidence(ctx, tx, connector, digest)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !found {
|
||||
// Older stores could promote a prepared candidate in the current-release
|
||||
// index before runtime convergence completed. If that update then failed,
|
||||
// the previous release metadata was no longer recoverable from SQLite.
|
||||
// Keep the store available and let runtime recovery fail this connector
|
||||
// closed until a subsequent install repairs its durable evidence.
|
||||
continue
|
||||
}
|
||||
if err := saveInstalledReleaseOn(ctx, tx, release); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func legacyInstalledReleaseEvidence(ctx context.Context, tx *sql.Tx, connector market.Connector, digest string) (market.Release, bool, error) {
|
||||
var currentPayload string
|
||||
err := tx.QueryRowContext(ctx, `SELECT release_json FROM connector_market_installed_releases
|
||||
WHERE connector_key = ? AND release_digest = ?`, connector.Key, digest).Scan(¤tPayload)
|
||||
if err == nil {
|
||||
var release market.Release
|
||||
if err := json.Unmarshal([]byte(currentPayload), &release); err != nil {
|
||||
return market.Release{}, false, fmt.Errorf("decode current installed connector release: %w", err)
|
||||
}
|
||||
return release, true, nil
|
||||
}
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return market.Release{}, false, err
|
||||
}
|
||||
if connector.Release.ReleaseDigest == digest {
|
||||
return connector.Release, true, nil
|
||||
}
|
||||
rows, err := tx.QueryContext(ctx, `SELECT operation_json FROM connector_market_operations
|
||||
WHERE connector_key = ? AND kind = ? AND state = ? ORDER BY updated_at_unix_ms DESC`,
|
||||
connector.Key, market.OperationKindInstall, market.OperationStateCompleted)
|
||||
if err != nil {
|
||||
return market.Release{}, false, err
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var payload string
|
||||
if err := rows.Scan(&payload); err != nil {
|
||||
return market.Release{}, false, err
|
||||
}
|
||||
operation, err := decodeOperation(payload)
|
||||
if err != nil {
|
||||
return market.Release{}, false, err
|
||||
}
|
||||
if operation.Target != nil && operation.Target.Release != nil && operation.Target.ReleaseDigest == digest {
|
||||
return *operation.Target.Release, true, nil
|
||||
}
|
||||
}
|
||||
return market.Release{}, false, rows.Err()
|
||||
}
|
||||
|
||||
func saveInstalledReleaseEvidenceOn(ctx context.Context, tx *sql.Tx, operation market.Operation) error {
|
||||
switch operation.Kind {
|
||||
case market.OperationKindInstall:
|
||||
if operation.Execution.ReleaseInstallation == nil && operation.State != market.OperationStateCompleted {
|
||||
return nil
|
||||
}
|
||||
if operation.Target == nil || operation.Target.Release == nil || operation.Target.ReleaseDigest == "" {
|
||||
return errors.New("prepared connector install is missing release evidence")
|
||||
}
|
||||
if operation.State == market.OperationStateCompleted {
|
||||
return saveInstalledReleaseOn(ctx, tx, *operation.Target.Release)
|
||||
}
|
||||
return saveReleaseInstallationOn(ctx, tx, *operation.Target.Release)
|
||||
case market.OperationKindUninstall:
|
||||
if operation.State != market.OperationStateCompleted {
|
||||
return nil
|
||||
}
|
||||
_, err := tx.ExecContext(ctx, `DELETE FROM connector_market_installed_releases WHERE connector_key = ?`, operation.ConnectorKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = tx.ExecContext(ctx, `DELETE FROM connector_market_release_installations WHERE connector_key = ?`, operation.ConnectorKey)
|
||||
return err
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func saveInstalledReleaseOn(ctx context.Context, tx *sql.Tx, release market.Release) error {
|
||||
if err := saveReleaseInstallationOn(ctx, tx, release); err != nil {
|
||||
return err
|
||||
}
|
||||
payload, err := json.Marshal(release)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = tx.ExecContext(ctx, `
|
||||
INSERT INTO connector_market_installed_releases (connector_key, release_digest, release_json)
|
||||
VALUES (?, ?, ?)
|
||||
ON CONFLICT(connector_key) DO UPDATE SET
|
||||
release_digest = excluded.release_digest,
|
||||
release_json = excluded.release_json`, release.ConnectorKey, release.ReleaseDigest, string(payload))
|
||||
return err
|
||||
}
|
||||
|
||||
func saveReleaseInstallationOn(ctx context.Context, tx *sql.Tx, release market.Release) error {
|
||||
payload, err := json.Marshal(release)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = tx.ExecContext(ctx, `
|
||||
INSERT INTO connector_market_release_installations (connector_key, release_digest, release_json)
|
||||
VALUES (?, ?, ?)
|
||||
ON CONFLICT(connector_key, release_digest) DO UPDATE SET
|
||||
release_json = excluded.release_json`, release.ConnectorKey, release.ReleaseDigest, string(payload))
|
||||
return err
|
||||
}
|
||||
|
||||
func (store *Store) CleanupLifecycle(ctx context.Context, request market.LifecycleCleanupRequest) (market.LifecycleCleanupResult, error) {
|
||||
if request.BatchSize <= 0 || request.BatchSize > maxLifecycleCleanupBatchSize {
|
||||
return market.LifecycleCleanupResult{}, fmt.Errorf("connector market lifecycle cleanup batch size must be between 1 and %d", maxLifecycleCleanupBatchSize)
|
||||
}
|
||||
if request.TerminalOperationsUpdatedThrough.IsZero() || request.PublishedEventsPublishedThrough.IsZero() {
|
||||
return market.LifecycleCleanupResult{}, errors.New("connector market lifecycle cleanup cutoffs are required")
|
||||
}
|
||||
tx, err := store.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return market.LifecycleCleanupResult{}, err
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
operationResult, err := tx.ExecContext(ctx, `
|
||||
DELETE FROM connector_market_operations
|
||||
WHERE operation_id IN (
|
||||
SELECT operation_id FROM connector_market_operations
|
||||
WHERE state IN ('completed', 'failed') AND updated_at_unix_ms <= ?
|
||||
ORDER BY updated_at_unix_ms, operation_id LIMIT ?
|
||||
)
|
||||
AND state IN ('completed', 'failed') AND updated_at_unix_ms <= ?`,
|
||||
request.TerminalOperationsUpdatedThrough.UTC().UnixMilli(), request.BatchSize,
|
||||
request.TerminalOperationsUpdatedThrough.UTC().UnixMilli())
|
||||
if err != nil {
|
||||
return market.LifecycleCleanupResult{}, err
|
||||
}
|
||||
outboxResult, err := tx.ExecContext(ctx, `
|
||||
DELETE FROM connector_market_outbox
|
||||
WHERE sequence IN (
|
||||
SELECT sequence FROM connector_market_outbox
|
||||
WHERE published_at_unix_ms IS NOT NULL AND published_at_unix_ms <= ?
|
||||
ORDER BY published_at_unix_ms, sequence LIMIT ?
|
||||
)
|
||||
AND published_at_unix_ms IS NOT NULL AND published_at_unix_ms <= ?`,
|
||||
request.PublishedEventsPublishedThrough.UTC().UnixMilli(), request.BatchSize,
|
||||
request.PublishedEventsPublishedThrough.UTC().UnixMilli())
|
||||
if err != nil {
|
||||
return market.LifecycleCleanupResult{}, err
|
||||
}
|
||||
operationsDeleted, err := operationResult.RowsAffected()
|
||||
if err != nil {
|
||||
return market.LifecycleCleanupResult{}, err
|
||||
}
|
||||
eventsDeleted, err := outboxResult.RowsAffected()
|
||||
if err != nil {
|
||||
return market.LifecycleCleanupResult{}, err
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return market.LifecycleCleanupResult{}, err
|
||||
}
|
||||
return market.LifecycleCleanupResult{
|
||||
TerminalOperationsDeleted: operationsDeleted,
|
||||
PublishedEventsDeleted: eventsDeleted,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,618 @@
|
||||
package storesqlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
market "github.com/tutti-os/tutti/packages/connector/daemon/core"
|
||||
)
|
||||
|
||||
func TestStoreCleanupLifecycleHonorsCutoffsBatchesAndProtectedRows(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store, err := Open(ctx, filepath.Join(t.TempDir(), "tuttid.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
|
||||
cutoff := time.Date(2026, 8, 6, 12, 0, 0, 0, time.UTC)
|
||||
operations := []market.Operation{
|
||||
lifecycleTestOperation("completed-before", "connector-completed", market.OperationStateCompleted, cutoff.Add(-time.Millisecond)),
|
||||
lifecycleTestOperation("failed-at", "connector-failed", market.OperationStateFailed, cutoff),
|
||||
lifecycleTestOperation("completed-after", "connector-newer", market.OperationStateCompleted, cutoff.Add(time.Millisecond)),
|
||||
lifecycleTestOperation("accepted-old", "connector-accepted", market.OperationStateAccepted, cutoff.Add(-time.Hour)),
|
||||
lifecycleTestOperation("running-old", "connector-running", market.OperationStateRunning, cutoff.Add(-time.Hour)),
|
||||
}
|
||||
if err := store.Transaction(ctx, func(tx market.Transaction) error {
|
||||
for index, operation := range operations {
|
||||
if err := tx.SaveOperation(operation); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.EnqueueConnectorMarketChanged(market.ChangedEvent{OperationID: operation.OperationID, Revision: uint64(index + 1)}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for sequence, publishedAt := range map[int64]time.Time{
|
||||
1: cutoff.Add(-time.Millisecond),
|
||||
2: cutoff,
|
||||
3: cutoff.Add(time.Millisecond),
|
||||
5: cutoff.Add(2 * time.Millisecond),
|
||||
} {
|
||||
if err := store.MarkChangedEventPublished(ctx, sequence, publishedAt); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
request := market.LifecycleCleanupRequest{
|
||||
TerminalOperationsUpdatedThrough: cutoff,
|
||||
PublishedEventsPublishedThrough: cutoff,
|
||||
BatchSize: 1,
|
||||
}
|
||||
first, err := store.CleanupLifecycle(ctx, request)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
second, err := store.CleanupLifecycle(ctx, request)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
third, err := store.CleanupLifecycle(ctx, request)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if first.TerminalOperationsDeleted != 1 || first.PublishedEventsDeleted != 1 ||
|
||||
second.TerminalOperationsDeleted != 1 || second.PublishedEventsDeleted != 1 ||
|
||||
third.TerminalOperationsDeleted != 0 || third.PublishedEventsDeleted != 0 {
|
||||
t.Fatalf("cleanup results = %#v, %#v, %#v", first, second, third)
|
||||
}
|
||||
for _, operationID := range []string{"completed-before", "failed-at"} {
|
||||
if _, err := store.Operation(ctx, operationID); !errors.Is(err, market.ErrNotFound) {
|
||||
t.Fatalf("operation %q error = %v, want not found", operationID, err)
|
||||
}
|
||||
}
|
||||
for _, operationID := range []string{"completed-after", "accepted-old", "running-old"} {
|
||||
if _, err := store.Operation(ctx, operationID); err != nil {
|
||||
t.Fatalf("protected operation %q error = %v", operationID, err)
|
||||
}
|
||||
}
|
||||
reusedRequest := lifecycleTestOperation("accepted-reused-request", "connector-reused-request", market.OperationStateAccepted, cutoff.Add(time.Minute))
|
||||
reusedRequest.ClientRequestID = operations[0].ClientRequestID
|
||||
if err := store.Transaction(ctx, func(tx market.Transaction) error {
|
||||
return tx.SaveOperation(reusedRequest)
|
||||
}); err != nil {
|
||||
t.Fatalf("reuse expired client request ID: %v", err)
|
||||
}
|
||||
pending, err := store.PendingChangedEvents(ctx, 10)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(pending) != 1 || pending[0].Sequence != 4 {
|
||||
t.Fatalf("pending events = %#v, want only sequence 4", pending)
|
||||
}
|
||||
var publishedRows int
|
||||
if err := store.db.QueryRowContext(ctx, `SELECT COUNT(*) FROM connector_market_outbox WHERE published_at_unix_ms IS NOT NULL`).Scan(&publishedRows); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if publishedRows != 2 {
|
||||
t.Fatalf("published outbox rows = %d, want 2 protected newer rows", publishedRows)
|
||||
}
|
||||
if _, err := store.CleanupLifecycle(ctx, market.LifecycleCleanupRequest{
|
||||
TerminalOperationsUpdatedThrough: cutoff,
|
||||
PublishedEventsPublishedThrough: cutoff,
|
||||
BatchSize: maxLifecycleCleanupBatchSize + 1,
|
||||
}); err == nil {
|
||||
t.Fatal("oversized lifecycle cleanup batch was accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoreCleanupLifecycleIsSafeAcrossConnections(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
databasePath := filepath.Join(t.TempDir(), "tuttid.db")
|
||||
first, err := Open(ctx, databasePath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer first.Close()
|
||||
second, err := Open(ctx, databasePath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer second.Close()
|
||||
cutoff := time.Date(2026, 8, 6, 12, 0, 0, 0, time.UTC)
|
||||
if err := first.Transaction(ctx, func(tx market.Transaction) error {
|
||||
for index := range 20 {
|
||||
operation := lifecycleTestOperation(
|
||||
fmt.Sprintf("terminal-%02d", index),
|
||||
fmt.Sprintf("connector-%02d", index),
|
||||
market.OperationStateCompleted,
|
||||
cutoff.Add(-time.Hour),
|
||||
)
|
||||
if err := tx.SaveOperation(operation); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
request := market.LifecycleCleanupRequest{
|
||||
TerminalOperationsUpdatedThrough: cutoff,
|
||||
PublishedEventsPublishedThrough: cutoff,
|
||||
BatchSize: 7,
|
||||
}
|
||||
start := make(chan struct{})
|
||||
results := make(chan market.LifecycleCleanupResult, 2)
|
||||
errorsFound := make(chan error, 2)
|
||||
var wait sync.WaitGroup
|
||||
for _, candidate := range []*Store{first, second} {
|
||||
wait.Add(1)
|
||||
go func(store *Store) {
|
||||
defer wait.Done()
|
||||
<-start
|
||||
result, cleanupErr := store.CleanupLifecycle(ctx, request)
|
||||
if cleanupErr != nil {
|
||||
errorsFound <- cleanupErr
|
||||
return
|
||||
}
|
||||
results <- result
|
||||
}(candidate)
|
||||
}
|
||||
close(start)
|
||||
wait.Wait()
|
||||
close(results)
|
||||
close(errorsFound)
|
||||
for cleanupErr := range errorsFound {
|
||||
t.Fatal(cleanupErr)
|
||||
}
|
||||
var deleted int64
|
||||
for result := range results {
|
||||
if result.TerminalOperationsDeleted > int64(request.BatchSize) {
|
||||
t.Fatalf("one cleanup deleted %d operations, batch size %d", result.TerminalOperationsDeleted, request.BatchSize)
|
||||
}
|
||||
deleted += result.TerminalOperationsDeleted
|
||||
}
|
||||
if deleted != 14 {
|
||||
t.Fatalf("concurrent cleanup deleted %d operations, want 14", deleted)
|
||||
}
|
||||
var remaining int
|
||||
if err := first.db.QueryRowContext(ctx, `SELECT COUNT(*) FROM connector_market_operations`).Scan(&remaining); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if remaining != 6 {
|
||||
t.Fatalf("remaining operations = %d, want 6", remaining)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoreCleanupPreservesRecoverableWorkAcrossReopen(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
databasePath := filepath.Join(t.TempDir(), "tuttid.db")
|
||||
store, err := Open(ctx, databasePath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
operation := lifecycleTestOperation(
|
||||
"accepted-restart",
|
||||
"connector-restart",
|
||||
market.OperationStateAccepted,
|
||||
time.Date(2026, 8, 5, 12, 0, 0, 0, time.UTC),
|
||||
)
|
||||
if err := store.Transaction(ctx, func(tx market.Transaction) error {
|
||||
if err := tx.SaveOperation(operation); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.EnqueueConnectorMarketChanged(market.ChangedEvent{
|
||||
ConnectorKey: operation.ConnectorKey,
|
||||
OperationID: operation.OperationID,
|
||||
Revision: 1,
|
||||
})
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cutoff := time.Date(2026, 8, 6, 12, 0, 0, 0, time.UTC)
|
||||
if _, err := store.CleanupLifecycle(ctx, market.LifecycleCleanupRequest{
|
||||
TerminalOperationsUpdatedThrough: cutoff,
|
||||
PublishedEventsPublishedThrough: cutoff,
|
||||
BatchSize: 10,
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := store.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
reopened, err := Open(ctx, databasePath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer reopened.Close()
|
||||
recoverable, err := reopened.RecoverableOperations(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(recoverable) != 1 || recoverable[0].OperationID != operation.OperationID {
|
||||
t.Fatalf("recoverable operations = %#v", recoverable)
|
||||
}
|
||||
pending, err := reopened.PendingChangedEvents(ctx, 10)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(pending) != 1 || pending[0].Event.OperationID != "" || pending[0].Event.ConnectorKey != operation.ConnectorKey {
|
||||
t.Fatalf("pending events = %#v", pending)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoreInstalledReleaseEvidenceSurvivesTerminalCleanupAndReopen(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
databasePath := filepath.Join(t.TempDir(), "tuttid.db")
|
||||
store, err := Open(ctx, databasePath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
release := testConnector().Release
|
||||
connector := testConnector()
|
||||
connector.Installation = market.Installation{
|
||||
State: market.InstallationStateInstalled,
|
||||
InstalledVersion: release.Version,
|
||||
InstalledReleaseID: release.ReleaseID,
|
||||
InstalledReleaseDigest: release.ReleaseDigest,
|
||||
}
|
||||
completedAt := time.Date(2026, 8, 5, 12, 0, 0, 0, time.UTC)
|
||||
operation := lifecycleTestOperation("install-evidence", connector.Key, market.OperationStateCompleted, completedAt)
|
||||
operation.Kind = market.OperationKindInstall
|
||||
operation.Target = &market.OperationTarget{
|
||||
ConnectorKey: connector.Key, Version: release.Version, ReleaseID: release.ReleaseID,
|
||||
ReleaseDigest: release.ReleaseDigest, ArtifactSHA256: release.Artifact.SHA256, Release: &release,
|
||||
}
|
||||
if err := store.Transaction(ctx, func(tx market.Transaction) error {
|
||||
connector.Revision = tx.AdvanceRevision()
|
||||
if err := tx.SaveConnector(connector); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.SaveOperation(operation)
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
connector.Release.Version = "2.0.0"
|
||||
connector.Release.ReleaseID = "43"
|
||||
connector.Release.ReleaseDigest = "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
|
||||
connector.Release.ManifestDigest = "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"
|
||||
if err := store.Transaction(ctx, func(tx market.Transaction) error {
|
||||
connector.Revision = tx.AdvanceRevision()
|
||||
return tx.SaveConnector(connector)
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result, err := store.CleanupLifecycle(ctx, market.LifecycleCleanupRequest{
|
||||
TerminalOperationsUpdatedThrough: completedAt,
|
||||
PublishedEventsPublishedThrough: completedAt,
|
||||
BatchSize: 10,
|
||||
})
|
||||
if err != nil || result.TerminalOperationsDeleted != 1 {
|
||||
t.Fatalf("cleanup result = %#v, error = %v", result, err)
|
||||
}
|
||||
if _, err := store.Operation(ctx, operation.OperationID); !errors.Is(err, market.ErrNotFound) {
|
||||
t.Fatalf("cleaned operation error = %v", err)
|
||||
}
|
||||
if err := store.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
reopened, err := Open(ctx, databasePath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer reopened.Close()
|
||||
got, err := reopened.InstalledRelease(ctx, connector.Key, release.ReleaseDigest)
|
||||
if err != nil || got.ReleaseDigest != release.ReleaseDigest || got.ManifestDigest != release.ManifestDigest {
|
||||
t.Fatalf("installed release = %#v, error = %v", got, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoreCompletedLocalUninstallDeletesReleaseEvidenceButKeepsAuthorizationProjection(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store, err := Open(ctx, filepath.Join(t.TempDir(), "tuttid.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
release := testConnector().Release
|
||||
connector := testConnector()
|
||||
connector.Installation = market.Installation{State: market.InstallationStateInstalled,
|
||||
InstalledVersion: release.Version, InstalledReleaseID: release.ReleaseID, InstalledReleaseDigest: release.ReleaseDigest}
|
||||
installedAt := time.Date(2026, 8, 5, 12, 0, 0, 0, time.UTC)
|
||||
install := lifecycleTestOperation("install-before-local-uninstall", connector.Key, market.OperationStateCompleted, installedAt)
|
||||
install.Kind = market.OperationKindInstall
|
||||
install.Target = &market.OperationTarget{ConnectorKey: connector.Key, Version: release.Version, ReleaseID: release.ReleaseID,
|
||||
ReleaseDigest: release.ReleaseDigest, ArtifactSHA256: release.Artifact.SHA256, Release: &release}
|
||||
if err := store.Transaction(ctx, func(tx market.Transaction) error {
|
||||
connector.Revision = tx.AdvanceRevision()
|
||||
if err := tx.SaveConnector(connector); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.SaveOperation(install)
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
projection := market.AuthorizationProjection{AccountID: "account-1", ConnectorKey: connector.Key,
|
||||
ConnectionID: "connection-1", State: market.AuthorizationStateConnected, ServerRevision: 8,
|
||||
ServerSynchronized: true, UpdatedAt: installedAt}
|
||||
if err := store.SaveAuthorizationProjection(ctx, projection); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
uninstall := lifecycleTestOperation("local-uninstall", connector.Key, market.OperationStateCompleted, installedAt.Add(time.Minute))
|
||||
uninstall.Kind = market.OperationKindUninstall
|
||||
uninstall.Target = &market.OperationTarget{ConnectorKey: connector.Key, Version: release.Version,
|
||||
ReleaseID: release.ReleaseID, ReleaseDigest: release.ReleaseDigest}
|
||||
if err := store.Transaction(ctx, func(tx market.Transaction) error {
|
||||
connector.Installation = market.Installation{State: market.InstallationStateNotInstalled}
|
||||
connector.Revision = tx.AdvanceRevision()
|
||||
if err := tx.SaveConnector(connector); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.SaveOperation(uninstall)
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := store.InstalledRelease(ctx, connector.Key, release.ReleaseDigest); !errors.Is(err, market.ErrNotFound) {
|
||||
t.Fatalf("installed release error = %v, want not found", err)
|
||||
}
|
||||
storedProjection, err := store.AuthorizationProjection(ctx, projection.AccountID, projection.ConnectorKey)
|
||||
if err != nil || storedProjection != projection {
|
||||
t.Fatalf("authorization projection = %#v, error = %v", storedProjection, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoreRetainsCurrentAndPreparedCandidateReleaseEvidence(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store, err := Open(ctx, filepath.Join(t.TempDir(), "tuttid.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
|
||||
connector := testConnector()
|
||||
current := connector.Release
|
||||
connector.Installation = market.Installation{
|
||||
State: market.InstallationStateUpdating, InstalledVersion: current.Version,
|
||||
InstalledReleaseID: current.ReleaseID, InstalledReleaseDigest: current.ReleaseDigest,
|
||||
}
|
||||
candidate := current
|
||||
candidate.Version = "2.0.0"
|
||||
candidate.ReleaseID = connector.Key + "@2.0.0"
|
||||
candidate.ReleaseDigest = "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
|
||||
candidate.ManifestDigest = "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"
|
||||
installedAt := time.Date(2026, 8, 5, 12, 0, 0, 0, time.UTC)
|
||||
currentOperation := lifecycleTestOperation("current-release", connector.Key, market.OperationStateCompleted, installedAt)
|
||||
currentOperation.Kind = market.OperationKindInstall
|
||||
currentOperation.Target = &market.OperationTarget{
|
||||
ConnectorKey: connector.Key, Version: current.Version, ReleaseID: current.ReleaseID,
|
||||
ReleaseDigest: current.ReleaseDigest, ArtifactSHA256: current.Artifact.SHA256, Release: ¤t,
|
||||
}
|
||||
candidateOperation := lifecycleTestOperation("candidate-release", connector.Key, market.OperationStateRunning, installedAt.Add(time.Minute))
|
||||
candidateOperation.Kind = market.OperationKindInstall
|
||||
candidateOperation.Stage = market.OperationStageRuntimePending
|
||||
candidateOperation.Target = &market.OperationTarget{
|
||||
ConnectorKey: connector.Key, Version: candidate.Version, ReleaseID: candidate.ReleaseID,
|
||||
ReleaseDigest: candidate.ReleaseDigest, ArtifactSHA256: candidate.Artifact.SHA256, Release: &candidate,
|
||||
}
|
||||
candidateOperation.Execution.ReleaseInstallation = &market.ReleaseInstallationReceipt{
|
||||
OperationID: candidateOperation.OperationID, ConnectorKey: connector.Key,
|
||||
Version: candidate.Version, ReleaseID: candidate.ReleaseID, ReleaseDigest: candidate.ReleaseDigest,
|
||||
}
|
||||
if err := store.Transaction(ctx, func(tx market.Transaction) error {
|
||||
connector.Revision = tx.AdvanceRevision()
|
||||
if err := tx.SaveConnector(connector); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.SaveOperation(currentOperation); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.SaveOperation(candidateOperation)
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var currentDigest string
|
||||
if err := store.db.QueryRowContext(ctx, `SELECT release_digest FROM connector_market_installed_releases
|
||||
WHERE connector_key = ?`, connector.Key).Scan(¤tDigest); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if currentDigest != current.ReleaseDigest {
|
||||
t.Fatalf("current release pointer = %q, want %q", currentDigest, current.ReleaseDigest)
|
||||
}
|
||||
|
||||
for name, expected := range map[string]market.Release{"current": current, "candidate": candidate} {
|
||||
got, err := store.InstalledRelease(ctx, connector.Key, expected.ReleaseDigest)
|
||||
if err != nil || got.ReleaseDigest != expected.ReleaseDigest || got.ManifestDigest != expected.ManifestDigest {
|
||||
t.Fatalf("%s release = %#v, error = %v", name, got, err)
|
||||
}
|
||||
}
|
||||
|
||||
candidateOperation.State = market.OperationStateCompleted
|
||||
candidateOperation.Stage = market.OperationStageCompleted
|
||||
if err := store.Transaction(ctx, func(tx market.Transaction) error {
|
||||
return tx.SaveOperation(candidateOperation)
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := store.db.QueryRowContext(ctx, `SELECT release_digest FROM connector_market_installed_releases
|
||||
WHERE connector_key = ?`, connector.Key).Scan(¤tDigest); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if currentDigest != candidate.ReleaseDigest {
|
||||
t.Fatalf("promoted release pointer = %q, want %q", currentDigest, candidate.ReleaseDigest)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoreMigrationBackfillsLifecycleTimestampAndInstalledReleaseEvidence(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
databasePath := filepath.Join(t.TempDir(), "tuttid.db")
|
||||
legacy, err := sql.Open("sqlite", databasePath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, statement := range []string{
|
||||
`CREATE TABLE connector_market_connectors (connector_key TEXT PRIMARY KEY, connector_json TEXT NOT NULL)`,
|
||||
`CREATE TABLE connector_market_installed_releases (
|
||||
connector_key TEXT PRIMARY KEY,
|
||||
release_digest TEXT NOT NULL,
|
||||
release_json TEXT NOT NULL
|
||||
)`,
|
||||
`CREATE TABLE connector_market_operations (
|
||||
operation_id TEXT PRIMARY KEY,
|
||||
client_request_id TEXT NOT NULL UNIQUE,
|
||||
connector_key TEXT NOT NULL,
|
||||
kind TEXT NOT NULL,
|
||||
state TEXT NOT NULL,
|
||||
lease_owner TEXT NOT NULL,
|
||||
lease_token INTEGER NOT NULL DEFAULT 0,
|
||||
lease_expires_at_unix_ms INTEGER,
|
||||
operation_json TEXT NOT NULL
|
||||
)`,
|
||||
} {
|
||||
if _, err := legacy.ExecContext(ctx, statement); err != nil {
|
||||
_ = legacy.Close()
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
installedRelease := testConnector().Release
|
||||
connector := testConnector()
|
||||
connector.Installation = market.Installation{
|
||||
State: market.InstallationStateInstalled,
|
||||
InstalledVersion: installedRelease.Version,
|
||||
InstalledReleaseID: installedRelease.ReleaseID,
|
||||
InstalledReleaseDigest: installedRelease.ReleaseDigest,
|
||||
}
|
||||
connector.Release.Version = "2.0.0"
|
||||
connector.Release.ReleaseDigest = "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
|
||||
operation := lifecycleTestOperation(
|
||||
"legacy-install",
|
||||
connector.Key,
|
||||
market.OperationStateCompleted,
|
||||
time.Date(2026, 8, 5, 12, 0, 0, 0, time.UTC),
|
||||
)
|
||||
operation.Kind = market.OperationKindInstall
|
||||
operation.Target = &market.OperationTarget{
|
||||
ConnectorKey: connector.Key, Version: installedRelease.Version, ReleaseID: installedRelease.ReleaseID,
|
||||
ReleaseDigest: installedRelease.ReleaseDigest, ArtifactSHA256: installedRelease.Artifact.SHA256, Release: &installedRelease,
|
||||
}
|
||||
connectorPayload, err := json.Marshal(connector)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
operationPayload, err := json.Marshal(operation)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := legacy.ExecContext(ctx, `INSERT INTO connector_market_connectors (connector_key, connector_json) VALUES (?, ?)`,
|
||||
connector.Key, string(connectorPayload)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
installedReleasePayload, err := json.Marshal(installedRelease)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := legacy.ExecContext(ctx, `INSERT INTO connector_market_installed_releases
|
||||
(connector_key, release_digest, release_json) VALUES (?, ?, ?)`, connector.Key, installedRelease.ReleaseDigest,
|
||||
string(installedReleasePayload)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := legacy.ExecContext(ctx, `INSERT INTO connector_market_operations (
|
||||
operation_id, client_request_id, connector_key, kind, state, lease_owner, lease_token, lease_expires_at_unix_ms, operation_json
|
||||
) VALUES (?, ?, ?, ?, ?, '', 0, NULL, ?)`, operation.OperationID, operation.ClientRequestID, operation.ConnectorKey,
|
||||
operation.Kind, operation.State, string(operationPayload)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := legacy.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
store, err := Open(ctx, databasePath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
var updatedAtMS int64
|
||||
if err := store.db.QueryRowContext(ctx, `SELECT updated_at_unix_ms FROM connector_market_operations WHERE operation_id = ?`, operation.OperationID).Scan(&updatedAtMS); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if updatedAtMS != operation.UpdatedAt.UnixMilli() {
|
||||
t.Fatalf("migrated updated timestamp = %d, want %d", updatedAtMS, operation.UpdatedAt.UnixMilli())
|
||||
}
|
||||
migratedConnector, err := store.Connector(ctx, connector.Key)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if migratedConnector.Installation.InstalledAtUnixMS != operation.UpdatedAt.UnixMilli() {
|
||||
t.Fatalf("migrated install timestamp = %d, want %d", migratedConnector.Installation.InstalledAtUnixMS, operation.UpdatedAt.UnixMilli())
|
||||
}
|
||||
release, err := store.InstalledRelease(ctx, connector.Key, installedRelease.ReleaseDigest)
|
||||
if err != nil || release.ReleaseDigest != installedRelease.ReleaseDigest {
|
||||
t.Fatalf("migrated installed release = %#v, error = %v", release, err)
|
||||
}
|
||||
var historicalCount int
|
||||
if err := store.db.QueryRowContext(ctx, `SELECT COUNT(*) FROM connector_market_release_installations
|
||||
WHERE connector_key = ? AND release_digest = ?`, connector.Key, installedRelease.ReleaseDigest).Scan(&historicalCount); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if historicalCount != 1 {
|
||||
t.Fatalf("migrated release installation evidence count = %d, want 1", historicalCount)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoreReopenToleratesMissingLegacyInstalledReleaseEvidence(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
databasePath := filepath.Join(t.TempDir(), "tuttid.db")
|
||||
store, err := Open(ctx, databasePath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
connector := testConnector()
|
||||
installedRelease := connector.Release
|
||||
connector.Installation = market.Installation{
|
||||
State: market.InstallationStateInstalled, InstalledVersion: installedRelease.Version,
|
||||
InstalledReleaseID: installedRelease.ReleaseID, InstalledReleaseDigest: installedRelease.ReleaseDigest,
|
||||
}
|
||||
connector.Release.Version = "2.0.0"
|
||||
connector.Release.ReleaseID = connector.Key + "@2.0.0"
|
||||
connector.Release.ReleaseDigest = "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
|
||||
connector.Release.ManifestDigest = "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"
|
||||
if err := store.Transaction(ctx, func(tx market.Transaction) error {
|
||||
connector.Revision = tx.AdvanceRevision()
|
||||
return tx.SaveConnector(connector)
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := store.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
reopened, err := Open(ctx, databasePath)
|
||||
if err != nil {
|
||||
t.Fatalf("reopen with missing legacy evidence: %v", err)
|
||||
}
|
||||
defer reopened.Close()
|
||||
if _, err := reopened.Connector(ctx, connector.Key); err != nil {
|
||||
t.Fatalf("load connector after compatible reopen: %v", err)
|
||||
}
|
||||
if _, err := reopened.InstalledRelease(ctx, connector.Key, installedRelease.ReleaseDigest); !errors.Is(err, market.ErrNotFound) {
|
||||
t.Fatalf("missing release evidence error = %v, want not found", err)
|
||||
}
|
||||
}
|
||||
|
||||
func lifecycleTestOperation(operationID, connectorKey string, state market.OperationState, updatedAt time.Time) market.Operation {
|
||||
return market.Operation{
|
||||
OperationID: operationID, ClientRequestID: "request-" + operationID, ConnectorKey: connectorKey,
|
||||
Kind: market.OperationKindRefreshCatalog, State: state, Stage: market.OperationStageCompleted,
|
||||
CreatedAt: updatedAt.Add(-time.Minute), UpdatedAt: updatedAt,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
package storesqlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
market "github.com/tutti-os/tutti/packages/connector/daemon/core"
|
||||
)
|
||||
|
||||
func (store *Store) OperationForScope(
|
||||
ctx context.Context,
|
||||
scope market.OperationScope,
|
||||
operationID string,
|
||||
) (market.Operation, error) {
|
||||
var payload string
|
||||
if err := store.db.QueryRowContext(ctx, `
|
||||
SELECT operation_json FROM connector_market_operations
|
||||
WHERE operation_id = ? AND owner_account_id = ? AND visibility = ?`,
|
||||
strings.TrimSpace(operationID), strings.TrimSpace(scope.AccountID), market.OperationVisibilityAccount,
|
||||
).Scan(&payload); err != nil {
|
||||
return market.Operation{}, mapNotFound(err)
|
||||
}
|
||||
operation, err := decodeOperation(payload)
|
||||
if err != nil {
|
||||
return market.Operation{}, err
|
||||
}
|
||||
return publicOperation(operation), nil
|
||||
}
|
||||
|
||||
func (store *Store) migrateOperationOwnership(ctx context.Context) error {
|
||||
columns, err := store.operationColumns(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !columns["owner_account_id"] || !columns["visibility"] {
|
||||
if err := store.rebuildOperationsWithOwnership(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for _, statement := range []string{
|
||||
`CREATE UNIQUE INDEX IF NOT EXISTS connector_market_operation_owner_request
|
||||
ON connector_market_operations(owner_account_id, client_request_id)`,
|
||||
`CREATE UNIQUE INDEX IF NOT EXISTS connector_market_one_active_operation
|
||||
ON connector_market_operations(connector_key)
|
||||
WHERE state IN ('accepted', 'running')`,
|
||||
`CREATE INDEX IF NOT EXISTS connector_market_operations_terminal_cleanup
|
||||
ON connector_market_operations(updated_at_unix_ms, operation_id)
|
||||
WHERE state IN ('completed', 'failed')`,
|
||||
} {
|
||||
if _, err := store.db.ExecContext(ctx, statement); err != nil {
|
||||
return fmt.Errorf("migrate connector market operation ownership index: %w", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (store *Store) operationColumns(ctx context.Context) (map[string]bool, error) {
|
||||
rows, err := store.db.QueryContext(ctx, `PRAGMA table_info(connector_market_operations)`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
columns := make(map[string]bool)
|
||||
for rows.Next() {
|
||||
var cid, notNull, primaryKey int
|
||||
var name, columnType string
|
||||
var defaultValue sql.NullString
|
||||
if err := rows.Scan(&cid, &name, &columnType, ¬Null, &defaultValue, &primaryKey); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
columns[strings.ToLower(name)] = true
|
||||
}
|
||||
return columns, rows.Err()
|
||||
}
|
||||
|
||||
func (store *Store) rebuildOperationsWithOwnership(ctx context.Context) error {
|
||||
tx, err := store.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
rows, err := tx.QueryContext(ctx, `SELECT operation_json FROM connector_market_operations ORDER BY operation_id`)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var operations []market.Operation
|
||||
for rows.Next() {
|
||||
var payload string
|
||||
if err := rows.Scan(&payload); err != nil {
|
||||
_ = rows.Close()
|
||||
return err
|
||||
}
|
||||
operation, err := decodeOperation(payload)
|
||||
if err != nil {
|
||||
_ = rows.Close()
|
||||
return err
|
||||
}
|
||||
operations = append(operations, market.NormalizeOperationOwnership(operation))
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, `CREATE TABLE connector_market_operations_ownership_v2 (
|
||||
operation_id TEXT PRIMARY KEY,
|
||||
client_request_id TEXT NOT NULL,
|
||||
owner_account_id TEXT NOT NULL,
|
||||
visibility TEXT NOT NULL CHECK (visibility IN ('account', 'system_private')),
|
||||
connector_key TEXT NOT NULL,
|
||||
kind TEXT NOT NULL,
|
||||
state TEXT NOT NULL,
|
||||
lease_owner TEXT NOT NULL,
|
||||
lease_token INTEGER NOT NULL DEFAULT 0,
|
||||
lease_expires_at_unix_ms INTEGER,
|
||||
updated_at_unix_ms INTEGER NOT NULL DEFAULT 0,
|
||||
operation_json TEXT NOT NULL
|
||||
)`); err != nil {
|
||||
return fmt.Errorf("create connector market operation ownership table: %w", err)
|
||||
}
|
||||
for _, operation := range operations {
|
||||
payload, err := json.Marshal(operation)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var leaseExpiresAt any
|
||||
if operation.LeaseExpiresAt != nil {
|
||||
leaseExpiresAt = operation.LeaseExpiresAt.UTC().UnixMilli()
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, `INSERT INTO connector_market_operations_ownership_v2 (
|
||||
operation_id, client_request_id, owner_account_id, visibility, connector_key, kind, state,
|
||||
lease_owner, lease_token, lease_expires_at_unix_ms, updated_at_unix_ms, operation_json
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
operation.OperationID, operation.ClientRequestID, operation.OwnerAccountID, operation.Visibility,
|
||||
operation.ConnectorKey, operation.Kind, operation.State, operation.LeaseOwner, operation.LeaseToken,
|
||||
leaseExpiresAt, operation.UpdatedAt.UTC().UnixMilli(), string(payload)); err != nil {
|
||||
return fmt.Errorf("backfill connector market operation ownership: %w", err)
|
||||
}
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, `DROP TABLE connector_market_operations`); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, `ALTER TABLE connector_market_operations_ownership_v2 RENAME TO connector_market_operations`); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
@@ -0,0 +1,332 @@
|
||||
package storesqlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
market "github.com/tutti-os/tutti/packages/connector/daemon/core"
|
||||
)
|
||||
|
||||
const maxDueRuntimeConvergences = 100
|
||||
|
||||
func (store *Store) migrateRuntimeConvergence(ctx context.Context) error {
|
||||
statements := []string{
|
||||
`CREATE TABLE IF NOT EXISTS connector_market_runtime_convergence (
|
||||
account_id TEXT NOT NULL,
|
||||
connector_key TEXT NOT NULL,
|
||||
desired_generation INTEGER NOT NULL CHECK (desired_generation > 0),
|
||||
observed_generation INTEGER NOT NULL CHECK (observed_generation >= 0),
|
||||
observed_boot_epoch TEXT NOT NULL,
|
||||
next_attempt_at_unix_ms INTEGER NOT NULL,
|
||||
lease_owner TEXT NOT NULL,
|
||||
lease_token INTEGER NOT NULL DEFAULT 0,
|
||||
lease_expires_at_unix_ms INTEGER,
|
||||
updated_at_unix_ms INTEGER NOT NULL,
|
||||
convergence_json TEXT NOT NULL,
|
||||
PRIMARY KEY (account_id, connector_key)
|
||||
)`,
|
||||
`CREATE INDEX IF NOT EXISTS connector_market_runtime_convergence_due
|
||||
ON connector_market_runtime_convergence(account_id, next_attempt_at_unix_ms, connector_key)`,
|
||||
}
|
||||
for _, statement := range statements {
|
||||
if _, err := store.db.ExecContext(ctx, statement); err != nil {
|
||||
return fmt.Errorf("migrate connector runtime convergence: %w", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (store *Store) RuntimeConvergence(
|
||||
ctx context.Context,
|
||||
scope market.OperationScope,
|
||||
connectorKey string,
|
||||
) (market.RuntimeConvergence, error) {
|
||||
return runtimeConvergenceOn(ctx, store.db, scope, connectorKey)
|
||||
}
|
||||
|
||||
func (store *Store) DueRuntimeConvergences(
|
||||
ctx context.Context,
|
||||
scope market.OperationScope,
|
||||
bootEpoch string,
|
||||
now time.Time,
|
||||
limit int,
|
||||
) ([]market.RuntimeConvergence, error) {
|
||||
if limit <= 0 || limit > maxDueRuntimeConvergences {
|
||||
limit = maxDueRuntimeConvergences
|
||||
}
|
||||
rows, err := store.db.QueryContext(ctx, `
|
||||
SELECT convergence_json
|
||||
FROM connector_market_runtime_convergence
|
||||
WHERE account_id = ?
|
||||
AND (desired_generation != observed_generation OR observed_boot_epoch != ?)
|
||||
AND next_attempt_at_unix_ms <= ?
|
||||
AND (lease_owner = '' OR lease_expires_at_unix_ms IS NULL OR lease_expires_at_unix_ms <= ?)
|
||||
ORDER BY next_attempt_at_unix_ms, connector_key
|
||||
LIMIT ?`, strings.TrimSpace(scope.AccountID), strings.TrimSpace(bootEpoch), now.UTC().UnixMilli(), now.UTC().UnixMilli(), limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
result := make([]market.RuntimeConvergence, 0)
|
||||
for rows.Next() {
|
||||
var payload string
|
||||
if err := rows.Scan(&payload); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
convergence, err := decodeRuntimeConvergence(payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result = append(result, convergence)
|
||||
}
|
||||
return result, rows.Err()
|
||||
}
|
||||
|
||||
func (store *Store) ClaimRuntimeConvergence(
|
||||
ctx context.Context,
|
||||
scope market.OperationScope,
|
||||
connectorKey, bootEpoch, owner string,
|
||||
now, leaseExpiresAt time.Time,
|
||||
) (market.RuntimeConvergence, bool, error) {
|
||||
tx, err := store.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return market.RuntimeConvergence{}, false, err
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
accountID, connectorKey := runtimeConvergenceKey(scope, connectorKey)
|
||||
result, err := tx.ExecContext(ctx, `
|
||||
UPDATE connector_market_runtime_convergence
|
||||
SET lease_owner = ?, lease_token = lease_token + 1, lease_expires_at_unix_ms = ?
|
||||
WHERE account_id = ? AND connector_key = ?
|
||||
AND (desired_generation != observed_generation OR observed_boot_epoch != ?)
|
||||
AND next_attempt_at_unix_ms <= ?
|
||||
AND (lease_owner = '' OR lease_expires_at_unix_ms IS NULL OR lease_expires_at_unix_ms <= ?)`,
|
||||
strings.TrimSpace(owner), leaseExpiresAt.UTC().UnixMilli(), accountID, connectorKey,
|
||||
strings.TrimSpace(bootEpoch), now.UTC().UnixMilli(), now.UTC().UnixMilli())
|
||||
if err != nil {
|
||||
return market.RuntimeConvergence{}, false, err
|
||||
}
|
||||
changed, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return market.RuntimeConvergence{}, false, err
|
||||
}
|
||||
convergence, err := runtimeConvergenceOn(ctx, tx, scope, connectorKey)
|
||||
if err != nil {
|
||||
return market.RuntimeConvergence{}, false, err
|
||||
}
|
||||
if changed == 0 {
|
||||
return convergence, false, tx.Commit()
|
||||
}
|
||||
var token uint64
|
||||
if err := tx.QueryRowContext(ctx, `
|
||||
SELECT lease_token FROM connector_market_runtime_convergence
|
||||
WHERE account_id = ? AND connector_key = ?`, accountID, connectorKey).Scan(&token); err != nil {
|
||||
return market.RuntimeConvergence{}, false, err
|
||||
}
|
||||
expiresAt := leaseExpiresAt.UTC()
|
||||
convergence.LeaseOwner = strings.TrimSpace(owner)
|
||||
convergence.LeaseToken = token
|
||||
convergence.LeaseExpiresAt = &expiresAt
|
||||
if err := saveRuntimeConvergenceOn(ctx, tx, convergence); err != nil {
|
||||
return market.RuntimeConvergence{}, false, err
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return market.RuntimeConvergence{}, false, err
|
||||
}
|
||||
return convergence, true, nil
|
||||
}
|
||||
|
||||
func (store *Store) RenewRuntimeConvergenceLease(
|
||||
ctx context.Context,
|
||||
scope market.OperationScope,
|
||||
connectorKey, owner string,
|
||||
token uint64,
|
||||
now, leaseExpiresAt time.Time,
|
||||
) error {
|
||||
tx, err := store.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
convergence, err := runtimeConvergenceOn(ctx, tx, scope, connectorKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if convergence.LeaseOwner != strings.TrimSpace(owner) || convergence.LeaseToken != token ||
|
||||
convergence.LeaseExpiresAt == nil || !convergence.LeaseExpiresAt.After(now.UTC()) {
|
||||
return market.ErrOperationLeaseLost
|
||||
}
|
||||
expiresAt := leaseExpiresAt.UTC()
|
||||
convergence.LeaseExpiresAt = &expiresAt
|
||||
if err := saveRuntimeConvergenceOn(ctx, tx, convergence); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func (store *Store) ReleaseRuntimeConvergenceLease(
|
||||
ctx context.Context,
|
||||
scope market.OperationScope,
|
||||
connectorKey, owner string,
|
||||
token uint64,
|
||||
) error {
|
||||
tx, err := store.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
convergence, err := runtimeConvergenceOn(ctx, tx, scope, connectorKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if convergence.LeaseOwner != strings.TrimSpace(owner) || convergence.LeaseToken != token {
|
||||
return tx.Commit()
|
||||
}
|
||||
convergence.LeaseOwner = ""
|
||||
convergence.LeaseExpiresAt = nil
|
||||
if err := saveRuntimeConvergenceOn(ctx, tx, convergence); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func (store *Store) CompleteRuntimeConvergence(
|
||||
ctx context.Context,
|
||||
scope market.OperationScope,
|
||||
connectorKey, owner string,
|
||||
token, desiredGeneration uint64,
|
||||
observed market.RuntimeObserved,
|
||||
now time.Time,
|
||||
) error {
|
||||
return store.finishRuntimeConvergence(ctx, scope, connectorKey, owner, token, desiredGeneration, now,
|
||||
func(convergence *market.RuntimeConvergence) {
|
||||
convergence.Observed = observed
|
||||
convergence.Attempt = 0
|
||||
convergence.NextAttemptAt = time.Time{}
|
||||
convergence.LastErrorCode = ""
|
||||
convergence.LastError = ""
|
||||
})
|
||||
}
|
||||
|
||||
func (store *Store) RetryRuntimeConvergence(
|
||||
ctx context.Context,
|
||||
scope market.OperationScope,
|
||||
connectorKey, owner string,
|
||||
token, desiredGeneration uint64,
|
||||
nextAttemptAt time.Time,
|
||||
errorCode, errorMessage string,
|
||||
now time.Time,
|
||||
) error {
|
||||
return store.finishRuntimeConvergence(ctx, scope, connectorKey, owner, token, desiredGeneration, now,
|
||||
func(convergence *market.RuntimeConvergence) {
|
||||
convergence.Attempt++
|
||||
convergence.NextAttemptAt = nextAttemptAt.UTC()
|
||||
convergence.LastErrorCode = strings.TrimSpace(errorCode)
|
||||
convergence.LastError = strings.TrimSpace(errorMessage)
|
||||
})
|
||||
}
|
||||
|
||||
func (store *Store) finishRuntimeConvergence(
|
||||
ctx context.Context,
|
||||
scope market.OperationScope,
|
||||
connectorKey, owner string,
|
||||
token, desiredGeneration uint64,
|
||||
now time.Time,
|
||||
update func(*market.RuntimeConvergence),
|
||||
) error {
|
||||
tx, err := store.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
convergence, err := runtimeConvergenceOn(ctx, tx, scope, connectorKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if convergence.Desired.Generation != desiredGeneration {
|
||||
return market.ErrOperationLeaseLost
|
||||
}
|
||||
if convergence.LeaseOwner != strings.TrimSpace(owner) || convergence.LeaseToken != token ||
|
||||
convergence.LeaseExpiresAt == nil || !convergence.LeaseExpiresAt.After(now.UTC()) {
|
||||
return market.ErrOperationLeaseLost
|
||||
}
|
||||
update(&convergence)
|
||||
convergence.LeaseOwner = ""
|
||||
convergence.LeaseExpiresAt = nil
|
||||
convergence.UpdatedAt = now.UTC()
|
||||
if err := saveRuntimeConvergenceOn(ctx, tx, convergence); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func runtimeConvergenceOn(
|
||||
ctx context.Context,
|
||||
database interface {
|
||||
QueryRowContext(context.Context, string, ...any) *sql.Row
|
||||
},
|
||||
scope market.OperationScope,
|
||||
connectorKey string,
|
||||
) (market.RuntimeConvergence, error) {
|
||||
accountID, connectorKey := runtimeConvergenceKey(scope, connectorKey)
|
||||
var payload string
|
||||
if err := database.QueryRowContext(ctx, `
|
||||
SELECT convergence_json FROM connector_market_runtime_convergence
|
||||
WHERE account_id = ? AND connector_key = ?`, accountID, connectorKey).Scan(&payload); err != nil {
|
||||
return market.RuntimeConvergence{}, mapNotFound(err)
|
||||
}
|
||||
return decodeRuntimeConvergence(payload)
|
||||
}
|
||||
|
||||
func saveRuntimeConvergenceOn(ctx context.Context, tx *sql.Tx, convergence market.RuntimeConvergence) error {
|
||||
accountID, connectorKey := runtimeConvergenceKey(convergence.Desired.Scope, convergence.Desired.ConnectorKey)
|
||||
if connectorKey == "" || convergence.Desired.Generation == 0 {
|
||||
return errors.New("connector runtime convergence requires a connector key and desired generation")
|
||||
}
|
||||
payload, err := json.Marshal(convergence)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var leaseExpiresAt any
|
||||
if convergence.LeaseExpiresAt != nil {
|
||||
leaseExpiresAt = convergence.LeaseExpiresAt.UTC().UnixMilli()
|
||||
}
|
||||
_, err = tx.ExecContext(ctx, `
|
||||
INSERT INTO connector_market_runtime_convergence (
|
||||
account_id, connector_key, desired_generation, observed_generation,
|
||||
observed_boot_epoch, next_attempt_at_unix_ms, lease_owner, lease_token,
|
||||
lease_expires_at_unix_ms, updated_at_unix_ms, convergence_json
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(account_id, connector_key) DO UPDATE SET
|
||||
desired_generation = excluded.desired_generation,
|
||||
observed_generation = excluded.observed_generation,
|
||||
observed_boot_epoch = excluded.observed_boot_epoch,
|
||||
next_attempt_at_unix_ms = excluded.next_attempt_at_unix_ms,
|
||||
lease_owner = excluded.lease_owner,
|
||||
lease_token = excluded.lease_token,
|
||||
lease_expires_at_unix_ms = excluded.lease_expires_at_unix_ms,
|
||||
updated_at_unix_ms = excluded.updated_at_unix_ms,
|
||||
convergence_json = excluded.convergence_json`,
|
||||
accountID, connectorKey, convergence.Desired.Generation, convergence.Observed.DesiredGeneration,
|
||||
strings.TrimSpace(convergence.Observed.BootEpoch), convergence.NextAttemptAt.UTC().UnixMilli(),
|
||||
strings.TrimSpace(convergence.LeaseOwner), convergence.LeaseToken, leaseExpiresAt,
|
||||
convergence.UpdatedAt.UTC().UnixMilli(), string(payload))
|
||||
return err
|
||||
}
|
||||
|
||||
func decodeRuntimeConvergence(payload string) (market.RuntimeConvergence, error) {
|
||||
var convergence market.RuntimeConvergence
|
||||
if err := json.Unmarshal([]byte(payload), &convergence); err != nil {
|
||||
return market.RuntimeConvergence{}, fmt.Errorf("decode connector runtime convergence: %w", err)
|
||||
}
|
||||
return convergence, nil
|
||||
}
|
||||
|
||||
func runtimeConvergenceKey(scope market.OperationScope, connectorKey string) (string, string) {
|
||||
return strings.TrimSpace(scope.AccountID), strings.TrimSpace(connectorKey)
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
package storesqlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
market "github.com/tutti-os/tutti/packages/connector/daemon/core"
|
||||
)
|
||||
|
||||
func TestRuntimeConvergencePersistsAndReconcilesPerBootEpoch(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
databasePath := filepath.Join(t.TempDir(), "tuttid.db")
|
||||
store, err := Open(ctx, databasePath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
now := time.Date(2026, 8, 15, 2, 0, 0, 0, time.UTC)
|
||||
scope := market.OperationScope{AccountID: "account-1"}
|
||||
desired := runtimeConvergenceFixture(scope, "github", 1, now)
|
||||
if err := store.Transaction(ctx, func(tx market.Transaction) error {
|
||||
return tx.SaveRuntimeConvergence(desired)
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := store.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
store, err = Open(ctx, databasePath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
due, err := store.DueRuntimeConvergences(ctx, scope, "boot-1", now, 10)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(due) != 1 || due[0].Desired.Generation != 1 {
|
||||
t.Fatalf("due convergence = %#v", due)
|
||||
}
|
||||
claimed, ok, err := store.ClaimRuntimeConvergence(
|
||||
ctx, scope, "github", "boot-1", "worker-1", now, now.Add(time.Minute),
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !ok || claimed.LeaseToken == 0 {
|
||||
t.Fatalf("claimed convergence = %#v, claimed = %v", claimed, ok)
|
||||
}
|
||||
observed := market.RuntimeObserved{
|
||||
DesiredGeneration: 1,
|
||||
BootEpoch: "boot-1",
|
||||
Enabled: true,
|
||||
ConnectionID: desired.Desired.ConnectionID,
|
||||
ReleaseDigest: desired.Desired.ReleaseDigest,
|
||||
Readiness: market.RuntimeReadiness{State: market.RuntimeReadinessReady},
|
||||
ObservedAt: now.Add(time.Second),
|
||||
}
|
||||
if err := store.CompleteRuntimeConvergence(
|
||||
ctx, scope, "github", "worker-1", claimed.LeaseToken, 1, observed, now.Add(time.Second),
|
||||
); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
due, err = store.DueRuntimeConvergences(ctx, scope, "boot-1", now.Add(2*time.Second), 10)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(due) != 0 {
|
||||
t.Fatalf("same-boot convergence remained due: %#v", due)
|
||||
}
|
||||
due, err = store.DueRuntimeConvergences(ctx, scope, "boot-2", now.Add(2*time.Second), 10)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(due) != 1 {
|
||||
t.Fatalf("new boot due convergence = %#v", due)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRuntimeConvergenceRejectsStaleGenerationCompletion(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store, err := Open(ctx, filepath.Join(t.TempDir(), "tuttid.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
now := time.Date(2026, 8, 15, 3, 0, 0, 0, time.UTC)
|
||||
scope := market.OperationScope{AccountID: "account-1"}
|
||||
first := runtimeConvergenceFixture(scope, "github", 1, now)
|
||||
if err := store.Transaction(ctx, func(tx market.Transaction) error {
|
||||
return tx.SaveRuntimeConvergence(first)
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
claimed, ok, err := store.ClaimRuntimeConvergence(
|
||||
ctx, scope, "github", "boot-1", "worker-1", now, now.Add(time.Minute),
|
||||
)
|
||||
if err != nil || !ok {
|
||||
t.Fatalf("claim = %#v, %v, %v", claimed, ok, err)
|
||||
}
|
||||
second := first
|
||||
second.Desired.Generation = 2
|
||||
second.Desired.Enabled = false
|
||||
second.Desired.UpdatedAt = now.Add(time.Second)
|
||||
second.NextAttemptAt = now.Add(time.Second)
|
||||
second.LeaseOwner = ""
|
||||
second.LeaseToken = claimed.LeaseToken + 1
|
||||
second.LeaseExpiresAt = nil
|
||||
second.UpdatedAt = now.Add(time.Second)
|
||||
if err := store.Transaction(ctx, func(tx market.Transaction) error {
|
||||
return tx.SaveRuntimeConvergence(second)
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = store.CompleteRuntimeConvergence(ctx, scope, "github", "worker-1", claimed.LeaseToken, 1,
|
||||
market.RuntimeObserved{DesiredGeneration: 1, BootEpoch: "boot-1"}, now.Add(2*time.Second))
|
||||
if !errors.Is(err, market.ErrOperationLeaseLost) {
|
||||
t.Fatalf("stale completion error = %v", err)
|
||||
}
|
||||
stored, err := store.RuntimeConvergence(ctx, scope, "github")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if stored.Desired.Generation != 2 || stored.Observed.DesiredGeneration != 0 {
|
||||
t.Fatalf("stored convergence = %#v", stored)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRuntimeConvergenceLeaseCannotBeReenteredBySameWorker(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store, err := Open(ctx, filepath.Join(t.TempDir(), "tuttid.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
now := time.Date(2026, 8, 15, 4, 0, 0, 0, time.UTC)
|
||||
scope := market.OperationScope{AccountID: "account-1"}
|
||||
if err := store.Transaction(ctx, func(tx market.Transaction) error {
|
||||
return tx.SaveRuntimeConvergence(runtimeConvergenceFixture(scope, "github", 1, now))
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
first, claimed, err := store.ClaimRuntimeConvergence(
|
||||
ctx, scope, "github", "boot-1", "worker-1", now, now.Add(time.Minute),
|
||||
)
|
||||
if err != nil || !claimed {
|
||||
t.Fatalf("first claim = %#v, %v, %v", first, claimed, err)
|
||||
}
|
||||
second, claimed, err := store.ClaimRuntimeConvergence(
|
||||
ctx, scope, "github", "boot-1", "worker-1", now.Add(time.Second), now.Add(time.Minute),
|
||||
)
|
||||
if err != nil || claimed {
|
||||
t.Fatalf("reentrant claim = %#v, %v, %v", second, claimed, err)
|
||||
}
|
||||
if second.LeaseToken != first.LeaseToken || second.LeaseOwner != first.LeaseOwner {
|
||||
t.Fatalf("lease changed after rejected reentry: first=%#v second=%#v", first, second)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnapshotHidesLegacyRuntimeReconcileOperations(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store, err := Open(ctx, filepath.Join(t.TempDir(), "tuttid.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
now := time.Date(2026, 8, 15, 5, 0, 0, 0, time.UTC)
|
||||
operation := market.Operation{
|
||||
OperationID: "runtime-legacy", ClientRequestID: "runtime-legacy", ConnectorKey: "github",
|
||||
Kind: market.OperationKindReconcileRuntime, State: market.OperationStateCompleted,
|
||||
Stage: market.OperationStageCompleted, CreatedAt: now, UpdatedAt: now,
|
||||
}
|
||||
if err := store.Transaction(ctx, func(tx market.Transaction) error { return tx.SaveOperation(operation) }); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
snapshot, err := store.Snapshot(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(snapshot.Operations) != 0 {
|
||||
t.Fatalf("public snapshot leaked private operations: %#v", snapshot.Operations)
|
||||
}
|
||||
if _, err := store.Operation(ctx, operation.OperationID); err != nil {
|
||||
t.Fatalf("legacy recovery record was deleted: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func runtimeConvergenceFixture(
|
||||
scope market.OperationScope,
|
||||
connectorKey string,
|
||||
generation uint64,
|
||||
now time.Time,
|
||||
) market.RuntimeConvergence {
|
||||
return market.RuntimeConvergence{
|
||||
Desired: market.RuntimeDesired{
|
||||
Scope: scope, ConnectorKey: connectorKey, Generation: generation, Enabled: true,
|
||||
ConnectionID: "account-connection", ReleaseDigest: "sha256:release", AuthorizationState: market.AuthorizationStateConnected,
|
||||
UpdatedAt: now,
|
||||
},
|
||||
NextAttemptAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,841 @@
|
||||
package storesqlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
market "github.com/tutti-os/tutti/packages/connector/daemon/core"
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
const metadataID = 1
|
||||
|
||||
type Store struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
var _ market.Repository = (*Store)(nil)
|
||||
var _ market.ChangedEventOutbox = (*Store)(nil)
|
||||
var _ market.LifecycleCleanupStore = (*Store)(nil)
|
||||
var _ market.AuthorizationProjectionStore = (*Store)(nil)
|
||||
var _ market.AuthorizationSnapshotStore = (*Store)(nil)
|
||||
|
||||
func Open(ctx context.Context, dbPath string) (*Store, error) {
|
||||
dbPath = strings.TrimSpace(dbPath)
|
||||
if dbPath == "" {
|
||||
return nil, errors.New("connector market database path is required")
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Dir(dbPath), 0o700); err != nil {
|
||||
return nil, fmt.Errorf("create connector market database directory: %w", err)
|
||||
}
|
||||
query := url.Values{}
|
||||
query.Add("_pragma", "busy_timeout(5000)")
|
||||
query.Add("_pragma", "foreign_keys(1)")
|
||||
dsn := connectorMarketSQLiteDSN(dbPath, query)
|
||||
db, err := sql.Open("sqlite", dsn)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("open connector market database: %w", err)
|
||||
}
|
||||
db.SetMaxOpenConns(1)
|
||||
db.SetMaxIdleConns(1)
|
||||
store := &Store{db: db}
|
||||
if err := store.migrate(ctx); err != nil {
|
||||
_ = db.Close()
|
||||
return nil, err
|
||||
}
|
||||
return store, nil
|
||||
}
|
||||
|
||||
func connectorMarketSQLiteDSN(dbPath string, query url.Values) string {
|
||||
databaseURL := &url.URL{Scheme: "file", Path: dbPath, RawQuery: query.Encode()}
|
||||
if runtime.GOOS == "windows" && filepath.IsAbs(dbPath) {
|
||||
slashPath := filepath.ToSlash(dbPath)
|
||||
if uncPath := strings.TrimPrefix(slashPath, "//"); uncPath != slashPath {
|
||||
host, path, found := strings.Cut(uncPath, "/")
|
||||
if found {
|
||||
databaseURL.Host = host
|
||||
databaseURL.Path = "/" + path
|
||||
}
|
||||
} else {
|
||||
databaseURL.Path = "/" + slashPath
|
||||
}
|
||||
}
|
||||
return databaseURL.String()
|
||||
}
|
||||
|
||||
func (store *Store) Close() error {
|
||||
if store == nil || store.db == nil {
|
||||
return nil
|
||||
}
|
||||
return store.db.Close()
|
||||
}
|
||||
|
||||
func (store *Store) migrate(ctx context.Context) error {
|
||||
statements := []string{
|
||||
`DROP TABLE IF EXISTS connector_market_catalog_trust`,
|
||||
`DROP TABLE IF EXISTS connector_market_security_revocations`,
|
||||
`CREATE TABLE IF NOT EXISTS connector_market_metadata (
|
||||
id INTEGER PRIMARY KEY CHECK (id = 1),
|
||||
revision INTEGER NOT NULL,
|
||||
catalog_state TEXT NOT NULL,
|
||||
source_revision TEXT NOT NULL
|
||||
)`,
|
||||
`INSERT INTO connector_market_metadata (id, revision, catalog_state, source_revision)
|
||||
VALUES (1, 0, 'stale', '') ON CONFLICT(id) DO NOTHING`,
|
||||
`CREATE TABLE IF NOT EXISTS connector_market_connectors (
|
||||
connector_key TEXT PRIMARY KEY,
|
||||
connector_json TEXT NOT NULL
|
||||
)`,
|
||||
`CREATE TABLE IF NOT EXISTS connector_market_installed_releases (
|
||||
connector_key TEXT PRIMARY KEY,
|
||||
release_digest TEXT NOT NULL,
|
||||
release_json TEXT NOT NULL
|
||||
)`,
|
||||
`CREATE TABLE IF NOT EXISTS connector_market_authorization_projections (
|
||||
account_id TEXT NOT NULL,
|
||||
connector_key TEXT NOT NULL,
|
||||
projection_json TEXT NOT NULL,
|
||||
PRIMARY KEY (account_id, connector_key)
|
||||
)`,
|
||||
`CREATE TABLE IF NOT EXISTS connector_market_authorization_snapshot_revisions (
|
||||
account_id TEXT PRIMARY KEY,
|
||||
revision INTEGER NOT NULL CHECK (revision >= 0)
|
||||
)`,
|
||||
`CREATE TABLE IF NOT EXISTS connector_market_operations (
|
||||
operation_id TEXT PRIMARY KEY,
|
||||
client_request_id TEXT NOT NULL,
|
||||
owner_account_id TEXT NOT NULL,
|
||||
visibility TEXT NOT NULL CHECK (visibility IN ('account', 'system_private')),
|
||||
connector_key TEXT NOT NULL,
|
||||
kind TEXT NOT NULL,
|
||||
state TEXT NOT NULL,
|
||||
lease_owner TEXT NOT NULL,
|
||||
lease_token INTEGER NOT NULL DEFAULT 0,
|
||||
lease_expires_at_unix_ms INTEGER,
|
||||
updated_at_unix_ms INTEGER NOT NULL DEFAULT 0,
|
||||
operation_json TEXT NOT NULL
|
||||
)`,
|
||||
`CREATE UNIQUE INDEX IF NOT EXISTS connector_market_one_active_operation
|
||||
ON connector_market_operations(connector_key)
|
||||
WHERE state IN ('accepted', 'running')`,
|
||||
`CREATE TABLE IF NOT EXISTS connector_market_outbox (
|
||||
sequence INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
revision INTEGER NOT NULL,
|
||||
event_json TEXT NOT NULL,
|
||||
published_at_unix_ms INTEGER
|
||||
)`,
|
||||
`CREATE INDEX IF NOT EXISTS connector_market_outbox_pending
|
||||
ON connector_market_outbox(published_at_unix_ms, sequence)`,
|
||||
}
|
||||
for _, statement := range statements {
|
||||
if _, err := store.db.ExecContext(ctx, statement); err != nil {
|
||||
return fmt.Errorf("migrate connector market store: %w", err)
|
||||
}
|
||||
}
|
||||
if _, err := store.db.ExecContext(ctx, `ALTER TABLE connector_market_operations ADD COLUMN lease_token INTEGER NOT NULL DEFAULT 0`); err != nil &&
|
||||
!strings.Contains(strings.ToLower(err.Error()), "duplicate column") {
|
||||
return fmt.Errorf("migrate connector market operation lease token: %w", err)
|
||||
}
|
||||
if err := store.migrateLifecycle(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := store.migrateOperationOwnership(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
return store.migrateRuntimeConvergence(ctx)
|
||||
}
|
||||
|
||||
func (store *Store) Snapshot(ctx context.Context) (market.Snapshot, error) {
|
||||
return store.snapshot(ctx, "")
|
||||
}
|
||||
|
||||
// SnapshotForScope reads market state and the account authorization overlay
|
||||
// from one SQLite snapshot, so its revision/event cursor describe exactly the
|
||||
// projection returned to the renderer.
|
||||
func (store *Store) SnapshotForScope(ctx context.Context, scope market.OperationScope) (market.Snapshot, error) {
|
||||
return store.snapshot(ctx, strings.TrimSpace(scope.AccountID))
|
||||
}
|
||||
|
||||
func (store *Store) snapshot(ctx context.Context, accountID string) (market.Snapshot, error) {
|
||||
tx, err := store.db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
||||
if err != nil {
|
||||
return market.Snapshot{}, err
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
var result market.Snapshot
|
||||
if err := tx.QueryRowContext(ctx, `
|
||||
SELECT revision, catalog_state, source_revision
|
||||
FROM connector_market_metadata WHERE id = ?`, metadataID).
|
||||
Scan(&result.Revision, &result.CatalogState, &result.SourceRevision); err != nil {
|
||||
return market.Snapshot{}, fmt.Errorf("read connector market metadata: %w", err)
|
||||
}
|
||||
connectors, err := listConnectorsOn(ctx, tx)
|
||||
if err != nil {
|
||||
return market.Snapshot{}, err
|
||||
}
|
||||
if accountID != "" {
|
||||
connectors, err = overlayAuthorizationProjectionsOn(ctx, tx, accountID, connectors)
|
||||
if err != nil {
|
||||
return market.Snapshot{}, err
|
||||
}
|
||||
}
|
||||
operations, err := listOperationsOn(ctx, tx, accountID)
|
||||
if err != nil {
|
||||
return market.Snapshot{}, err
|
||||
}
|
||||
result.Connectors = connectors
|
||||
result.Operations = operations
|
||||
if err := tx.QueryRowContext(ctx, `SELECT COALESCE(MAX(sequence), 0) FROM connector_market_outbox`).Scan(&result.EventCursor); err != nil {
|
||||
return market.Snapshot{}, fmt.Errorf("read connector market event cursor: %w", err)
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return market.Snapshot{}, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (store *Store) Connector(ctx context.Context, connectorKey string) (market.Connector, error) {
|
||||
var payload string
|
||||
if err := store.db.QueryRowContext(ctx, `
|
||||
SELECT connector_json FROM connector_market_connectors WHERE connector_key = ?`, connectorKey).Scan(&payload); err != nil {
|
||||
return market.Connector{}, mapNotFound(err)
|
||||
}
|
||||
connector, err := decodeConnector(payload)
|
||||
if err != nil {
|
||||
return market.Connector{}, err
|
||||
}
|
||||
return connector, nil
|
||||
}
|
||||
|
||||
func (store *Store) Operation(ctx context.Context, operationID string) (market.Operation, error) {
|
||||
var payload string
|
||||
if err := store.db.QueryRowContext(ctx, `
|
||||
SELECT operation_json FROM connector_market_operations WHERE operation_id = ?`, operationID).Scan(&payload); err != nil {
|
||||
return market.Operation{}, mapNotFound(err)
|
||||
}
|
||||
return decodeOperation(payload)
|
||||
}
|
||||
|
||||
func (store *Store) ClaimOperation(
|
||||
ctx context.Context,
|
||||
operationID string,
|
||||
owner string,
|
||||
now time.Time,
|
||||
leaseExpiresAt time.Time,
|
||||
) (market.Operation, bool, error) {
|
||||
tx, err := store.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return market.Operation{}, false, err
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
result, err := tx.ExecContext(ctx, `
|
||||
UPDATE connector_market_operations
|
||||
SET lease_owner = ?, lease_token = lease_token + 1, lease_expires_at_unix_ms = ?
|
||||
WHERE operation_id = ?
|
||||
AND state IN ('accepted', 'running')
|
||||
AND (
|
||||
lease_owner = '' OR lease_expires_at_unix_ms IS NULL OR
|
||||
lease_expires_at_unix_ms <= ?
|
||||
)`, owner, leaseExpiresAt.UTC().UnixMilli(), operationID, now.UTC().UnixMilli())
|
||||
if err != nil {
|
||||
return market.Operation{}, false, err
|
||||
}
|
||||
changed, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return market.Operation{}, false, err
|
||||
}
|
||||
operation, err := operationOn(ctx, tx, operationID)
|
||||
if err != nil {
|
||||
return market.Operation{}, false, err
|
||||
}
|
||||
if changed == 0 {
|
||||
return operation, false, tx.Commit()
|
||||
}
|
||||
if err := tx.QueryRowContext(ctx, `SELECT lease_token FROM connector_market_operations WHERE operation_id = ?`, operationID).Scan(&operation.LeaseToken); err != nil {
|
||||
return market.Operation{}, false, err
|
||||
}
|
||||
expiresAt := leaseExpiresAt.UTC()
|
||||
operation.LeaseOwner = owner
|
||||
operation.LeaseExpiresAt = &expiresAt
|
||||
if err := saveOperationOn(ctx, tx, operation); err != nil {
|
||||
return market.Operation{}, false, err
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return market.Operation{}, false, err
|
||||
}
|
||||
return operation, true, nil
|
||||
}
|
||||
|
||||
func (store *Store) RenewOperationLease(ctx context.Context, operationID, owner string, token uint64, now, leaseExpiresAt time.Time) error {
|
||||
tx, err := store.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
operation, err := operationOn(ctx, tx, operationID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var currentOwner string
|
||||
var currentToken uint64
|
||||
var currentExpiry sql.NullInt64
|
||||
if err := tx.QueryRowContext(ctx, `SELECT lease_owner, lease_token, lease_expires_at_unix_ms FROM connector_market_operations WHERE operation_id = ?`, operationID).
|
||||
Scan(¤tOwner, ¤tToken, ¤tExpiry); err != nil {
|
||||
return err
|
||||
}
|
||||
if currentOwner != owner || currentToken != token || !currentExpiry.Valid || currentExpiry.Int64 <= now.UTC().UnixMilli() {
|
||||
return market.ErrOperationLeaseLost
|
||||
}
|
||||
expiresAt := leaseExpiresAt.UTC()
|
||||
operation.LeaseOwner, operation.LeaseToken, operation.LeaseExpiresAt = owner, token, &expiresAt
|
||||
payload, err := json.Marshal(operation)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
result, err := tx.ExecContext(ctx, `UPDATE connector_market_operations SET lease_expires_at_unix_ms = ?, operation_json = ? WHERE operation_id = ? AND lease_owner = ? AND lease_token = ? AND lease_expires_at_unix_ms > ?`,
|
||||
expiresAt.UnixMilli(), string(payload), operationID, owner, token, now.UTC().UnixMilli())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
changed, err := result.RowsAffected()
|
||||
if err != nil || changed != 1 {
|
||||
return market.ErrOperationLeaseLost
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func (store *Store) ReleaseOperationLease(ctx context.Context, operationID, owner string, token uint64) error {
|
||||
tx, err := store.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
result, err := tx.ExecContext(ctx, `
|
||||
UPDATE connector_market_operations
|
||||
SET lease_owner = '', lease_expires_at_unix_ms = NULL
|
||||
WHERE operation_id = ? AND lease_owner = ? AND lease_token = ?`, operationID, owner, token)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
changed, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if changed == 0 {
|
||||
return tx.Commit()
|
||||
}
|
||||
operation, err := operationOn(ctx, tx, operationID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
operation.LeaseOwner = ""
|
||||
operation.LeaseExpiresAt = nil
|
||||
if err := saveOperationOn(ctx, tx, operation); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func (store *Store) InstalledRelease(ctx context.Context, connectorKey, releaseDigest string) (market.Release, error) {
|
||||
var payload string
|
||||
err := store.db.QueryRowContext(ctx, `
|
||||
SELECT release_json FROM connector_market_release_installations
|
||||
WHERE connector_key = ? AND release_digest = ?`, connectorKey, releaseDigest).Scan(&payload)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
err = store.db.QueryRowContext(ctx, `
|
||||
SELECT release_json FROM connector_market_installed_releases
|
||||
WHERE connector_key = ? AND release_digest = ?`, connectorKey, releaseDigest).Scan(&payload)
|
||||
}
|
||||
if err != nil {
|
||||
return market.Release{}, mapNotFound(err)
|
||||
}
|
||||
var release market.Release
|
||||
if err := json.Unmarshal([]byte(payload), &release); err != nil {
|
||||
return market.Release{}, fmt.Errorf("decode installed connector release: %w", err)
|
||||
}
|
||||
return release, nil
|
||||
}
|
||||
|
||||
func (store *Store) RecoverableOperations(ctx context.Context) ([]market.Operation, error) {
|
||||
rows, err := store.db.QueryContext(ctx, `
|
||||
SELECT operation_json FROM connector_market_operations
|
||||
WHERE state IN ('accepted', 'running')
|
||||
ORDER BY operation_id`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
operations := make([]market.Operation, 0)
|
||||
for rows.Next() {
|
||||
var payload string
|
||||
if err := rows.Scan(&payload); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
operation, err := decodeOperation(payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
operations = append(operations, operation)
|
||||
}
|
||||
return operations, rows.Err()
|
||||
}
|
||||
|
||||
func (store *Store) UnresolvedAuthorizationSessionOperations(
|
||||
ctx context.Context,
|
||||
scope market.OperationScope,
|
||||
) ([]market.Operation, error) {
|
||||
rows, err := store.db.QueryContext(ctx, `
|
||||
SELECT operation_json FROM connector_market_operations
|
||||
WHERE kind = 'start_authorization' AND state = 'completed'
|
||||
ORDER BY operation_id`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
operations := make([]market.Operation, 0)
|
||||
for rows.Next() {
|
||||
var payload string
|
||||
if err := rows.Scan(&payload); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
operation, err := decodeOperation(payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if operation.Scope.AccountID != scope.AccountID || operation.Execution.AuthorizationSession == nil ||
|
||||
operation.Execution.AuthorizationSession.IsResolved() {
|
||||
continue
|
||||
}
|
||||
operations = append(operations, operation)
|
||||
}
|
||||
return operations, rows.Err()
|
||||
}
|
||||
|
||||
func (store *Store) ResolveAuthorizationSession(
|
||||
ctx context.Context,
|
||||
operationID string,
|
||||
resolution market.AuthorizationSessionResolution,
|
||||
) error {
|
||||
if !validAuthorizationSessionResolutionTransition(resolution) {
|
||||
return errors.New("valid authorization session resolution is required")
|
||||
}
|
||||
tx, err := store.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
operation, err := operationOn(ctx, tx, operationID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if operation.Execution.AuthorizationSession == nil || operation.Execution.AuthorizationSession.IsResolved() {
|
||||
return tx.Commit()
|
||||
}
|
||||
operation.Execution.AuthorizationSession.Resolution = resolution
|
||||
operation.UpdatedAt = time.Now().UTC()
|
||||
if err := saveOperationOn(ctx, tx, operation); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func validAuthorizationSessionResolutionTransition(resolution market.AuthorizationSessionResolution) bool {
|
||||
switch resolution {
|
||||
case market.AuthorizationSessionResolutionCanceling,
|
||||
market.AuthorizationSessionResolutionProviderConnected,
|
||||
market.AuthorizationSessionResolutionProviderFailed,
|
||||
market.AuthorizationSessionResolutionAccountStateConverged,
|
||||
market.AuthorizationSessionResolutionSuperseded:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (store *Store) Transaction(ctx context.Context, fn func(market.Transaction) error) error {
|
||||
tx, err := store.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
var revision uint64
|
||||
if err := tx.QueryRowContext(ctx, `SELECT revision FROM connector_market_metadata WHERE id = ?`, metadataID).Scan(&revision); err != nil {
|
||||
return err
|
||||
}
|
||||
transaction := &transaction{ctx: ctx, tx: tx, revision: revision}
|
||||
if err := fn(transaction); err != nil {
|
||||
return err
|
||||
}
|
||||
if transaction.revision != revision {
|
||||
result, err := tx.ExecContext(ctx, `
|
||||
UPDATE connector_market_metadata SET revision = ? WHERE id = ? AND revision = ?`,
|
||||
transaction.revision, metadataID, revision)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
changed, err := result.RowsAffected()
|
||||
if err != nil || changed != 1 {
|
||||
return errors.New("connector market revision changed during transaction")
|
||||
}
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func (store *Store) PendingChangedEvents(ctx context.Context, limit int) ([]market.ChangedEventRecord, error) {
|
||||
if limit <= 0 || limit > 500 {
|
||||
limit = 100
|
||||
}
|
||||
rows, err := store.db.QueryContext(ctx, `
|
||||
SELECT sequence, event_json FROM connector_market_outbox
|
||||
WHERE published_at_unix_ms IS NULL ORDER BY sequence LIMIT ?`, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
entries := make([]market.ChangedEventRecord, 0)
|
||||
for rows.Next() {
|
||||
var entry market.ChangedEventRecord
|
||||
var payload string
|
||||
if err := rows.Scan(&entry.Sequence, &payload); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := json.Unmarshal([]byte(payload), &entry.Event); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
entries = append(entries, entry)
|
||||
}
|
||||
return entries, rows.Err()
|
||||
}
|
||||
|
||||
func (store *Store) MarkChangedEventPublished(ctx context.Context, sequence int64, publishedAt time.Time) error {
|
||||
_, err := store.db.ExecContext(ctx, `
|
||||
UPDATE connector_market_outbox
|
||||
SET published_at_unix_ms = COALESCE(published_at_unix_ms, ?)
|
||||
WHERE sequence = ?`, publishedAt.UTC().UnixMilli(), sequence)
|
||||
return err
|
||||
}
|
||||
|
||||
type transaction struct {
|
||||
ctx context.Context
|
||||
tx *sql.Tx
|
||||
revision uint64
|
||||
}
|
||||
|
||||
func (transaction *transaction) Revision() uint64 { return transaction.revision }
|
||||
|
||||
func (transaction *transaction) AdvanceRevision() uint64 {
|
||||
transaction.revision++
|
||||
return transaction.revision
|
||||
}
|
||||
|
||||
func (transaction *transaction) Connectors() ([]market.Connector, error) {
|
||||
rows, err := transaction.tx.QueryContext(transaction.ctx, `
|
||||
SELECT connector_json FROM connector_market_connectors ORDER BY connector_key`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
connectors := make([]market.Connector, 0)
|
||||
for rows.Next() {
|
||||
var payload string
|
||||
if err := rows.Scan(&payload); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
connector, err := decodeConnector(payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
connectors = append(connectors, connector)
|
||||
}
|
||||
return connectors, rows.Err()
|
||||
}
|
||||
|
||||
func (transaction *transaction) Connector(connectorKey string) (market.Connector, error) {
|
||||
var payload string
|
||||
if err := transaction.tx.QueryRowContext(transaction.ctx, `
|
||||
SELECT connector_json FROM connector_market_connectors WHERE connector_key = ?`, connectorKey).Scan(&payload); err != nil {
|
||||
return market.Connector{}, mapNotFound(err)
|
||||
}
|
||||
return decodeConnector(payload)
|
||||
}
|
||||
|
||||
func (transaction *transaction) Operation(operationID string) (market.Operation, error) {
|
||||
return operationOn(transaction.ctx, transaction.tx, operationID)
|
||||
}
|
||||
|
||||
func (transaction *transaction) OperationByClientRequestID(ownerAccountID, clientRequestID string) (*market.Operation, error) {
|
||||
var payload string
|
||||
if err := transaction.tx.QueryRowContext(transaction.ctx, `
|
||||
SELECT operation_json FROM connector_market_operations
|
||||
WHERE owner_account_id = ? AND client_request_id = ?`,
|
||||
strings.TrimSpace(ownerAccountID), clientRequestID).Scan(&payload); err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
operation, err := decodeOperation(payload)
|
||||
return &operation, err
|
||||
}
|
||||
|
||||
func (transaction *transaction) ActiveOperationInLane(connectorKey string) (*market.Operation, error) {
|
||||
var payload string
|
||||
if err := transaction.tx.QueryRowContext(transaction.ctx, `
|
||||
SELECT operation_json FROM connector_market_operations
|
||||
WHERE connector_key = ? AND state IN ('accepted', 'running') LIMIT 1`,
|
||||
strings.TrimSpace(connectorKey)).Scan(&payload); err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
operation, err := decodeOperation(payload)
|
||||
return &operation, err
|
||||
}
|
||||
|
||||
func (transaction *transaction) SaveCatalogRevision(sourceRevision string) error {
|
||||
_, err := transaction.tx.ExecContext(transaction.ctx, `
|
||||
UPDATE connector_market_metadata SET source_revision = ? WHERE id = ?`, sourceRevision, metadataID)
|
||||
return err
|
||||
}
|
||||
|
||||
func (transaction *transaction) SetCatalogState(state market.CatalogState) error {
|
||||
_, err := transaction.tx.ExecContext(transaction.ctx, `
|
||||
UPDATE connector_market_metadata SET catalog_state = ? WHERE id = ?`, state, metadataID)
|
||||
return err
|
||||
}
|
||||
|
||||
func (transaction *transaction) SaveConnector(connector market.Connector) error {
|
||||
payload, err := json.Marshal(connector)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = transaction.tx.ExecContext(transaction.ctx, `
|
||||
INSERT INTO connector_market_connectors (connector_key, connector_json)
|
||||
VALUES (?, ?)
|
||||
ON CONFLICT(connector_key) DO UPDATE SET connector_json = excluded.connector_json`,
|
||||
connector.Key, string(payload))
|
||||
return err
|
||||
}
|
||||
|
||||
func (transaction *transaction) DeleteConnector(connectorKey string) error {
|
||||
_, err := transaction.tx.ExecContext(transaction.ctx, `
|
||||
DELETE FROM connector_market_connectors WHERE connector_key = ?`, connectorKey)
|
||||
return err
|
||||
}
|
||||
|
||||
func (transaction *transaction) SaveOperation(operation market.Operation) error {
|
||||
return saveOperationOn(transaction.ctx, transaction.tx, operation)
|
||||
}
|
||||
|
||||
func (transaction *transaction) RuntimeConvergence(
|
||||
scope market.OperationScope,
|
||||
connectorKey string,
|
||||
) (market.RuntimeConvergence, error) {
|
||||
return runtimeConvergenceOn(transaction.ctx, transaction.tx, scope, connectorKey)
|
||||
}
|
||||
|
||||
func (transaction *transaction) SaveRuntimeConvergence(convergence market.RuntimeConvergence) error {
|
||||
return saveRuntimeConvergenceOn(transaction.ctx, transaction.tx, convergence)
|
||||
}
|
||||
|
||||
func (transaction *transaction) DeleteRuntimeConvergence(scope market.OperationScope, connectorKey string) error {
|
||||
_, err := transaction.tx.ExecContext(transaction.ctx, `
|
||||
DELETE FROM connector_market_runtime_convergence
|
||||
WHERE account_id = ? AND connector_key = ?`, strings.TrimSpace(scope.AccountID), strings.TrimSpace(connectorKey))
|
||||
return err
|
||||
}
|
||||
|
||||
func (transaction *transaction) EnqueueConnectorMarketChanged(event market.ChangedEvent) error {
|
||||
if strings.TrimSpace(event.OperationID) != "" {
|
||||
operation, err := transaction.Operation(event.OperationID)
|
||||
if err != nil && !errors.Is(err, market.ErrNotFound) {
|
||||
return err
|
||||
}
|
||||
if err == nil {
|
||||
operation = market.NormalizeOperationOwnership(operation)
|
||||
if operation.Visibility == market.OperationVisibilityAccount {
|
||||
accountEvent := event
|
||||
accountEvent.OwnerAccountID = operation.OwnerAccountID
|
||||
accountEvent.Visibility = market.OperationVisibilityAccount
|
||||
if err := transaction.appendChangedEvent(accountEvent); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
event.OperationID = ""
|
||||
event.OwnerAccountID = ""
|
||||
event.Visibility = market.OperationVisibilitySystemPrivate
|
||||
return transaction.appendChangedEvent(event)
|
||||
}
|
||||
|
||||
func (transaction *transaction) appendChangedEvent(event market.ChangedEvent) error {
|
||||
payload, err := json.Marshal(event)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = transaction.tx.ExecContext(transaction.ctx, `
|
||||
INSERT INTO connector_market_outbox (revision, event_json, published_at_unix_ms)
|
||||
VALUES (?, ?, NULL)`, event.Revision, string(payload))
|
||||
return err
|
||||
}
|
||||
|
||||
type queryer interface {
|
||||
QueryContext(context.Context, string, ...any) (*sql.Rows, error)
|
||||
QueryRowContext(context.Context, string, ...any) *sql.Row
|
||||
}
|
||||
|
||||
func listConnectorsOn(ctx context.Context, database queryer) ([]market.Connector, error) {
|
||||
rows, err := database.QueryContext(ctx, `
|
||||
SELECT connector_json FROM connector_market_connectors ORDER BY connector_key`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
connectors := make([]market.Connector, 0)
|
||||
for rows.Next() {
|
||||
var payload string
|
||||
if err := rows.Scan(&payload); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
connector, err := decodeConnector(payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
connectors = append(connectors, connector)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
_ = rows.Close()
|
||||
return nil, err
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return connectors, nil
|
||||
}
|
||||
|
||||
func listOperationsOn(ctx context.Context, database queryer, accountID string) ([]market.Operation, error) {
|
||||
accountID = strings.TrimSpace(accountID)
|
||||
if accountID == "" {
|
||||
return []market.Operation{}, nil
|
||||
}
|
||||
rows, err := database.QueryContext(ctx, `
|
||||
SELECT operation_json FROM connector_market_operations
|
||||
WHERE owner_account_id = ? AND visibility = ? ORDER BY operation_id`,
|
||||
accountID, market.OperationVisibilityAccount)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
operations := make([]market.Operation, 0)
|
||||
for rows.Next() {
|
||||
var payload string
|
||||
if err := rows.Scan(&payload); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
operation, err := decodeOperation(payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
operations = append(operations, publicOperation(operation))
|
||||
}
|
||||
return operations, rows.Err()
|
||||
}
|
||||
|
||||
func operationOn(ctx context.Context, tx *sql.Tx, operationID string) (market.Operation, error) {
|
||||
var payload string
|
||||
if err := tx.QueryRowContext(ctx, `
|
||||
SELECT operation_json FROM connector_market_operations WHERE operation_id = ?`, operationID).Scan(&payload); err != nil {
|
||||
return market.Operation{}, mapNotFound(err)
|
||||
}
|
||||
return decodeOperation(payload)
|
||||
}
|
||||
|
||||
func saveOperationOn(ctx context.Context, tx *sql.Tx, operation market.Operation) error {
|
||||
operation = market.NormalizeOperationOwnership(operation)
|
||||
payload, err := json.Marshal(operation)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var leaseExpiresAt any
|
||||
if operation.LeaseExpiresAt != nil {
|
||||
leaseExpiresAt = operation.LeaseExpiresAt.UTC().UnixMilli()
|
||||
}
|
||||
result, err := tx.ExecContext(ctx, `
|
||||
INSERT INTO connector_market_operations (
|
||||
operation_id, client_request_id, owner_account_id, visibility, connector_key, kind, state,
|
||||
lease_owner, lease_token, lease_expires_at_unix_ms, updated_at_unix_ms, operation_json
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(operation_id) DO UPDATE SET
|
||||
owner_account_id = excluded.owner_account_id,
|
||||
visibility = excluded.visibility,
|
||||
state = excluded.state,
|
||||
lease_owner = excluded.lease_owner,
|
||||
lease_token = excluded.lease_token,
|
||||
lease_expires_at_unix_ms = excluded.lease_expires_at_unix_ms,
|
||||
updated_at_unix_ms = excluded.updated_at_unix_ms,
|
||||
operation_json = excluded.operation_json
|
||||
WHERE excluded.lease_token = 0 OR (
|
||||
connector_market_operations.lease_owner = excluded.lease_owner AND
|
||||
connector_market_operations.lease_token = excluded.lease_token
|
||||
)`,
|
||||
operation.OperationID, operation.ClientRequestID, operation.OwnerAccountID, operation.Visibility, operation.ConnectorKey,
|
||||
operation.Kind, operation.State, operation.LeaseOwner, operation.LeaseToken, leaseExpiresAt,
|
||||
operation.UpdatedAt.UTC().UnixMilli(), string(payload))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
changed, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if operation.LeaseToken > 0 && changed != 1 {
|
||||
return market.ErrOperationLeaseLost
|
||||
}
|
||||
return saveInstalledReleaseEvidenceOn(ctx, tx, operation)
|
||||
}
|
||||
|
||||
func decodeConnector(payload string) (market.Connector, error) {
|
||||
var connector market.Connector
|
||||
if err := json.Unmarshal([]byte(payload), &connector); err != nil {
|
||||
return market.Connector{}, fmt.Errorf("decode connector market connector: %w", err)
|
||||
}
|
||||
return connector, nil
|
||||
}
|
||||
|
||||
func decodeOperation(payload string) (market.Operation, error) {
|
||||
var operation market.Operation
|
||||
if err := json.Unmarshal([]byte(payload), &operation); err != nil {
|
||||
return market.Operation{}, fmt.Errorf("decode connector market operation: %w", err)
|
||||
}
|
||||
return operation, nil
|
||||
}
|
||||
|
||||
func publicOperation(operation market.Operation) market.Operation {
|
||||
operation.Execution = market.OperationExecution{}
|
||||
operation.LeaseOwner = ""
|
||||
operation.LeaseToken = 0
|
||||
operation.LeaseExpiresAt = nil
|
||||
if operation.Target != nil {
|
||||
target := *operation.Target
|
||||
target.Release = nil
|
||||
operation.Target = &target
|
||||
}
|
||||
return operation
|
||||
}
|
||||
|
||||
func mapNotFound(err error) error {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return market.ErrNotFound
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,783 @@
|
||||
package storesqlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
market "github.com/tutti-os/tutti/packages/connector/daemon/core"
|
||||
)
|
||||
|
||||
func TestConnectorMarketSQLiteDSNUsesWindowsFileURI(t *testing.T) {
|
||||
if runtime.GOOS != "windows" {
|
||||
t.Skip("Windows-specific SQLite file URI")
|
||||
}
|
||||
for _, test := range []struct {
|
||||
name, databasePath, host, uriPath string
|
||||
}{
|
||||
{name: "drive path", databasePath: `Z:\Users\Example User\.tutti-dev\tuttid.db`, uriPath: "/Z:/Users/Example User/.tutti-dev/tuttid.db"},
|
||||
{name: "UNC path", databasePath: `\\storage-host\tutti state\tuttid.db`, host: "storage-host", uriPath: "/tutti state/tuttid.db"},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
dsn := connectorMarketSQLiteDSN(test.databasePath, url.Values{
|
||||
"_pragma": {"busy_timeout(5000)", "foreign_keys(1)"},
|
||||
})
|
||||
parsed, err := url.Parse(dsn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if parsed.Scheme != "file" || parsed.Host != test.host || parsed.Path != test.uriPath || len(parsed.Query()["_pragma"]) != 2 {
|
||||
t.Fatalf("connector market SQLite DSN = %q", dsn)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoreMigrationDropsLegacyTrustTables(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
databasePath := filepath.Join(t.TempDir(), "tuttid.db")
|
||||
legacy, err := sql.Open("sqlite", databasePath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, statement := range []string{
|
||||
`CREATE TABLE connector_market_catalog_trust (id INTEGER PRIMARY KEY, trust_json TEXT NOT NULL)`,
|
||||
`CREATE TABLE connector_market_security_revocations (connector_key TEXT NOT NULL, release_digest TEXT NOT NULL)`,
|
||||
} {
|
||||
if _, err := legacy.ExecContext(ctx, statement); err != nil {
|
||||
_ = legacy.Close()
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if err := legacy.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
store, err := Open(ctx, databasePath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
for _, table := range []string{"connector_market_catalog_trust", "connector_market_security_revocations"} {
|
||||
var count int
|
||||
if err := store.db.QueryRowContext(ctx, `SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name = ?`, table).Scan(&count); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if count != 0 {
|
||||
t.Fatalf("legacy table %q still exists", table)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoreMigrationBackfillsOwnedOperationsAndKeepsUnknownOwnerPrivate(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
databasePath := filepath.Join(t.TempDir(), "tuttid.db")
|
||||
legacy, err := sql.Open("sqlite", databasePath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := legacy.ExecContext(ctx, `CREATE TABLE connector_market_operations (
|
||||
operation_id TEXT PRIMARY KEY,
|
||||
client_request_id TEXT NOT NULL UNIQUE,
|
||||
connector_key TEXT NOT NULL,
|
||||
kind TEXT NOT NULL,
|
||||
state TEXT NOT NULL,
|
||||
lease_owner TEXT NOT NULL,
|
||||
lease_token INTEGER NOT NULL DEFAULT 0,
|
||||
lease_expires_at_unix_ms INTEGER,
|
||||
updated_at_unix_ms INTEGER NOT NULL DEFAULT 0,
|
||||
operation_json TEXT NOT NULL
|
||||
)`); err != nil {
|
||||
_ = legacy.Close()
|
||||
t.Fatal(err)
|
||||
}
|
||||
now := time.Unix(1, 0).UTC()
|
||||
operations := []market.Operation{
|
||||
{OperationID: "owned", ClientRequestID: "request-owned", ConnectorKey: "github", Kind: market.OperationKindInstall,
|
||||
Scope: market.OperationScope{AccountID: "account-a"}, State: market.OperationStateFailed, CreatedAt: now, UpdatedAt: now},
|
||||
{OperationID: "private", ClientRequestID: "request-private", ConnectorKey: "slack", Kind: market.OperationKindReconcileRuntime,
|
||||
Scope: market.OperationScope{AccountID: "account-a"}, State: market.OperationStateFailed, CreatedAt: now, UpdatedAt: now},
|
||||
{OperationID: "unknown-owner", ClientRequestID: "request-unknown", ConnectorKey: "notion", Kind: market.OperationKindInstall,
|
||||
State: market.OperationStateAccepted, CreatedAt: now, UpdatedAt: now},
|
||||
}
|
||||
for _, operation := range operations {
|
||||
payload, err := json.Marshal(operation)
|
||||
if err != nil {
|
||||
_ = legacy.Close()
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := legacy.ExecContext(ctx, `INSERT INTO connector_market_operations (
|
||||
operation_id, client_request_id, connector_key, kind, state, lease_owner, lease_token,
|
||||
lease_expires_at_unix_ms, updated_at_unix_ms, operation_json
|
||||
) VALUES (?, ?, ?, ?, ?, '', 0, NULL, ?, ?)`, operation.OperationID, operation.ClientRequestID,
|
||||
operation.ConnectorKey, operation.Kind, operation.State, operation.UpdatedAt.UnixMilli(), string(payload)); err != nil {
|
||||
_ = legacy.Close()
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if err := legacy.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
store, err := Open(ctx, databasePath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
snapshot, err := store.SnapshotForScope(ctx, market.OperationScope{AccountID: "account-a"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(snapshot.Operations) != 1 || snapshot.Operations[0].OperationID != "owned" {
|
||||
t.Fatalf("migrated public operations = %#v", snapshot.Operations)
|
||||
}
|
||||
if _, err := store.OperationForScope(ctx, market.OperationScope{AccountID: "account-a"}, "private"); !errors.Is(err, market.ErrNotFound) {
|
||||
t.Fatalf("private operation error = %v", err)
|
||||
}
|
||||
if _, err := store.OperationForScope(ctx, market.OperationScope{AccountID: "account-a"}, "unknown-owner"); !errors.Is(err, market.ErrNotFound) {
|
||||
t.Fatalf("unknown-owner operation error = %v", err)
|
||||
}
|
||||
recoverable, err := store.RecoverableOperations(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(recoverable) != 1 || recoverable[0].OperationID != "unknown-owner" {
|
||||
t.Fatalf("recoverable migrated operations = %#v", recoverable)
|
||||
}
|
||||
for _, accountID := range []string{"account-a", "account-b"} {
|
||||
operation := market.Operation{
|
||||
OperationID: "reused-" + accountID, ClientRequestID: "reused-after-migration", ConnectorKey: "linear",
|
||||
Kind: market.OperationKindInstall, Scope: market.OperationScope{AccountID: accountID},
|
||||
State: market.OperationStateFailed, CreatedAt: now, UpdatedAt: now,
|
||||
}
|
||||
if err := store.Transaction(ctx, func(tx market.Transaction) error { return tx.SaveOperation(operation) }); err != nil {
|
||||
t.Fatalf("reuse client request after migration for %s: %v", accountID, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStorePersistsRevisionOperationBindingAndOutboxAtomically(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store, err := Open(ctx, filepath.Join(t.TempDir(), "tuttid.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
connector := testConnector()
|
||||
operation := market.Operation{
|
||||
OperationID: "operation-1", ClientRequestID: "request-1", ConnectorKey: connector.Key,
|
||||
Kind: market.OperationKindInstall, State: market.OperationStateAccepted,
|
||||
Scope: market.OperationScope{AccountID: "account-1"},
|
||||
Stage: market.OperationStageAccepted, CreatedAt: time.Unix(1, 0).UTC(), UpdatedAt: time.Unix(1, 0).UTC(),
|
||||
}
|
||||
if err := store.Transaction(ctx, func(tx market.Transaction) error {
|
||||
revision := tx.AdvanceRevision()
|
||||
connector.Revision = revision
|
||||
if err := tx.SaveConnector(connector); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.SaveOperation(operation); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.EnqueueConnectorMarketChanged(market.ChangedEvent{
|
||||
ConnectorKey: connector.Key, OperationID: operation.OperationID, Revision: revision,
|
||||
})
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
snapshot, err := store.SnapshotForScope(ctx, operation.Scope)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if snapshot.Revision != 1 || snapshot.EventCursor != 2 || len(snapshot.Connectors) != 1 || len(snapshot.Operations) != 1 {
|
||||
t.Fatalf("snapshot = %#v", snapshot)
|
||||
}
|
||||
entries, err := store.PendingChangedEvents(ctx, 10)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(entries) != 2 || entries[0].Event.OperationID != operation.OperationID ||
|
||||
entries[0].Event.OwnerAccountID != operation.Scope.AccountID || entries[1].Event.OperationID != "" {
|
||||
t.Fatalf("outbox = %#v", entries)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoreScopedSnapshotOnlyReturnsOperationsOwnedByAccount(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store, err := Open(ctx, filepath.Join(t.TempDir(), "tuttid.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
now := time.Unix(1, 0).UTC()
|
||||
connector := testConnector()
|
||||
connector.Installation = market.Installation{
|
||||
State: market.InstallationStateInstalled, InstalledVersion: connector.Release.Version,
|
||||
InstalledReleaseID: connector.Release.ReleaseID, InstalledReleaseDigest: connector.Release.ReleaseDigest,
|
||||
}
|
||||
if err := store.Transaction(ctx, func(tx market.Transaction) error { return tx.SaveConnector(connector) }); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, operation := range []market.Operation{
|
||||
{OperationID: "operation-a", ClientRequestID: "request-a", ConnectorKey: "github", Kind: market.OperationKindInstall,
|
||||
Scope: market.OperationScope{AccountID: "account-a"}, State: market.OperationStateAccepted, CreatedAt: now, UpdatedAt: now},
|
||||
{OperationID: "operation-b", ClientRequestID: "request-b", ConnectorKey: "github", Kind: market.OperationKindUninstall,
|
||||
Scope: market.OperationScope{AccountID: "account-b"}, State: market.OperationStateCompleted, CreatedAt: now, UpdatedAt: now},
|
||||
{OperationID: "operation-private", ClientRequestID: "request-private", ConnectorKey: "github", Kind: market.OperationKindReconcileRuntime,
|
||||
Scope: market.OperationScope{AccountID: "account-b"}, State: market.OperationStateCompleted, CreatedAt: now, UpdatedAt: now},
|
||||
{OperationID: "operation-legacy", ClientRequestID: "request-legacy", ConnectorKey: "github", Kind: market.OperationKindInstall,
|
||||
State: market.OperationStateFailed, CreatedAt: now, UpdatedAt: now},
|
||||
} {
|
||||
operation := operation
|
||||
if err := store.Transaction(ctx, func(tx market.Transaction) error { return tx.SaveOperation(operation) }); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
snapshot, err := store.SnapshotForScope(ctx, market.OperationScope{AccountID: "account-b"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(snapshot.Operations) != 1 || snapshot.Operations[0].OperationID != "operation-b" {
|
||||
t.Fatalf("account-b operations = %#v, want only operation-b", snapshot.Operations)
|
||||
}
|
||||
if len(snapshot.Connectors) != 1 || snapshot.Connectors[0].Installation.State != market.InstallationStateInstalled {
|
||||
t.Fatalf("account-b machine connector state = %#v", snapshot.Connectors)
|
||||
}
|
||||
if operation, err := store.OperationForScope(ctx, market.OperationScope{AccountID: "account-a"}, "operation-a"); err != nil || operation.OperationID != "operation-a" {
|
||||
t.Fatalf("account-a operation = %#v, error = %v", operation, err)
|
||||
}
|
||||
if _, err := store.OperationForScope(ctx, market.OperationScope{AccountID: "account-b"}, "operation-a"); !errors.Is(err, market.ErrNotFound) {
|
||||
t.Fatalf("account-b operation-a error = %v, want ErrNotFound", err)
|
||||
}
|
||||
recoverable, err := store.RecoverableOperations(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(recoverable) != 1 || recoverable[0].OperationID != "operation-a" {
|
||||
t.Fatalf("hidden account-a operation was not recoverable: %#v", recoverable)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoreAllowsClientRequestIDReuseAcrossAccounts(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store, err := Open(ctx, filepath.Join(t.TempDir(), "tuttid.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
now := time.Unix(1, 0).UTC()
|
||||
for _, accountID := range []string{"account-a", "account-b"} {
|
||||
operation := market.Operation{
|
||||
OperationID: "operation-" + accountID, ClientRequestID: "shared-request", ConnectorKey: "github",
|
||||
Kind: market.OperationKindInstall, Scope: market.OperationScope{AccountID: accountID},
|
||||
State: market.OperationStateFailed, CreatedAt: now, UpdatedAt: now,
|
||||
}
|
||||
if err := store.Transaction(ctx, func(tx market.Transaction) error { return tx.SaveOperation(operation) }); err != nil {
|
||||
t.Fatalf("save operation for %s: %v", accountID, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoreKeepsActiveConnectorLifecycleUniqueAcrossAccounts(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store, err := Open(ctx, filepath.Join(t.TempDir(), "tuttid.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
now := time.Unix(1, 0).UTC()
|
||||
for _, accountID := range []string{"account-a", "account-b"} {
|
||||
operation := market.Operation{
|
||||
OperationID: "operation-" + accountID, ClientRequestID: "request-" + accountID, ConnectorKey: "github",
|
||||
Kind: market.OperationKindInstall, Scope: market.OperationScope{AccountID: accountID},
|
||||
State: market.OperationStateAccepted, CreatedAt: now, UpdatedAt: now,
|
||||
}
|
||||
err := store.Transaction(ctx, func(tx market.Transaction) error { return tx.SaveOperation(operation) })
|
||||
if accountID == "account-a" && err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if accountID == "account-b" && err == nil {
|
||||
t.Fatal("second account started a concurrent physical lifecycle operation for the same connector")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoreScopesActiveOperationsToExactLane(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store, err := Open(ctx, filepath.Join(t.TempDir(), "tuttid.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
now := time.Unix(1, 0).UTC()
|
||||
operations := []market.Operation{
|
||||
{
|
||||
OperationID: "refresh", ClientRequestID: "refresh", ConnectorKey: "",
|
||||
Kind: market.OperationKindRefreshCatalog, State: market.OperationStateRunning,
|
||||
CreatedAt: now, UpdatedAt: now,
|
||||
},
|
||||
{
|
||||
OperationID: "authorization", ClientRequestID: "authorization", ConnectorKey: "google-sheets",
|
||||
Kind: market.OperationKindStartAuthorization, State: market.OperationStateAccepted,
|
||||
Scope: market.OperationScope{AccountID: "account-a"}, CreatedAt: now, UpdatedAt: now,
|
||||
},
|
||||
}
|
||||
for _, operation := range operations {
|
||||
operation := operation
|
||||
if err := store.Transaction(ctx, func(tx market.Transaction) error { return tx.SaveOperation(operation) }); err != nil {
|
||||
t.Fatalf("save %s operation: %v", operation.OperationID, err)
|
||||
}
|
||||
}
|
||||
if err := store.Transaction(ctx, func(tx market.Transaction) error {
|
||||
catalog, err := tx.ActiveOperationInLane("")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
connector, err := tx.ActiveOperationInLane("google-sheets")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if catalog == nil || catalog.OperationID != "refresh" {
|
||||
return fmt.Errorf("catalog lane operation = %#v", catalog)
|
||||
}
|
||||
if connector == nil || connector.OperationID != "authorization" {
|
||||
return fmt.Errorf("connector lane operation = %#v", connector)
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
secondRefresh := market.Operation{
|
||||
OperationID: "refresh-2", ClientRequestID: "refresh-2", ConnectorKey: "",
|
||||
Kind: market.OperationKindRefreshCatalog, State: market.OperationStateAccepted,
|
||||
CreatedAt: now, UpdatedAt: now,
|
||||
}
|
||||
if err := store.Transaction(ctx, func(tx market.Transaction) error { return tx.SaveOperation(secondRefresh) }); err == nil {
|
||||
t.Fatal("second active catalog refresh unexpectedly entered the catalog lane")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStorePrivateOperationEventDoesNotExposeOperationID(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store, err := Open(ctx, filepath.Join(t.TempDir(), "tuttid.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
operation := market.Operation{
|
||||
OperationID: "reconcile-1", ClientRequestID: "reconcile-request", ConnectorKey: "github",
|
||||
Kind: market.OperationKindReconcileRuntime, Scope: market.OperationScope{AccountID: "account-a"},
|
||||
State: market.OperationStateAccepted, CreatedAt: time.Unix(1, 0).UTC(), UpdatedAt: time.Unix(1, 0).UTC(),
|
||||
}
|
||||
if err := store.Transaction(ctx, func(tx market.Transaction) error {
|
||||
if err := tx.SaveOperation(operation); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.EnqueueConnectorMarketChanged(market.ChangedEvent{
|
||||
ConnectorKey: operation.ConnectorKey, OperationID: operation.OperationID, Revision: tx.AdvanceRevision(),
|
||||
})
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
events, err := store.PendingChangedEvents(ctx, 10)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(events) != 1 || events[0].Event.OperationID != "" {
|
||||
t.Fatalf("private operation event = %#v, want one machine event without operation id", events)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoreKeepsAuthorizationSessionPrivateAndAvailableAfterReopen(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
databasePath := filepath.Join(t.TempDir(), "tuttid.db")
|
||||
store, err := Open(ctx, databasePath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
connector := testConnector()
|
||||
connector.Authorization = market.Authorization{State: market.AuthorizationStatePending}
|
||||
operation := market.Operation{
|
||||
OperationID: "authorization-1", ClientRequestID: "request-1", ConnectorKey: connector.Key,
|
||||
Kind: market.OperationKindStartAuthorization, State: market.OperationStateCompleted,
|
||||
Scope: market.OperationScope{AccountID: "account-1"},
|
||||
Stage: market.OperationStageCompleted, CreatedAt: time.Unix(1, 0).UTC(), UpdatedAt: time.Unix(2, 0).UTC(),
|
||||
Execution: market.OperationExecution{AuthorizationSession: &market.AuthorizationSession{
|
||||
OperationID: "authorization-1", ConnectorKey: connector.Key,
|
||||
SessionID: "session-1", ActionType: "redirect", AuthorizationURL: "https://example.test/authorize",
|
||||
StepRevision: 3, State: market.AuthorizationStatePending,
|
||||
}},
|
||||
}
|
||||
if err := store.Transaction(ctx, func(tx market.Transaction) error {
|
||||
if err := tx.SaveConnector(connector); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.SaveOperation(operation)
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := store.ResolveAuthorizationSession(
|
||||
ctx, operation.OperationID, market.AuthorizationSessionResolutionCanceling,
|
||||
); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := store.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
reopened, err := Open(ctx, databasePath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer reopened.Close()
|
||||
snapshot, err := reopened.SnapshotForScope(ctx, operation.Scope)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(snapshot.Operations) != 1 || snapshot.Operations[0].Execution.AuthorizationSession != nil {
|
||||
t.Fatalf("public snapshot exposed authorization session: %#v", snapshot.Operations)
|
||||
}
|
||||
operations, err := reopened.UnresolvedAuthorizationSessionOperations(ctx, operation.Scope)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(operations) != 1 || operations[0].Execution.AuthorizationSession == nil ||
|
||||
operations[0].Execution.AuthorizationSession.SessionID != "session-1" ||
|
||||
operations[0].Execution.AuthorizationSession.StepRevision != 3 ||
|
||||
operations[0].Execution.AuthorizationSession.AuthorizationURL != "" ||
|
||||
operations[0].Execution.AuthorizationSession.Resolution != market.AuthorizationSessionResolutionCanceling {
|
||||
t.Fatalf("authorization operations = %#v", operations)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStorePersistsAuthorizationProjectionByAccount(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store, err := Open(ctx, filepath.Join(t.TempDir(), "tuttid.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
first := market.AuthorizationProjection{AccountID: "account-1", ConnectorKey: "github",
|
||||
ConnectionID: "connection-1", State: market.AuthorizationStateConnected, UpdatedAt: time.Unix(1, 0).UTC()}
|
||||
second := market.AuthorizationProjection{AccountID: "account-2", ConnectorKey: "github",
|
||||
ConnectionID: "connection-2", State: market.AuthorizationStateExpired, UpdatedAt: time.Unix(2, 0).UTC()}
|
||||
for _, projection := range []market.AuthorizationProjection{first, second} {
|
||||
if err := store.SaveAuthorizationProjection(ctx, projection); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
loaded, err := store.AuthorizationProjection(ctx, first.AccountID, first.ConnectorKey)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if loaded != first {
|
||||
t.Fatalf("projection = %#v, want %#v", loaded, first)
|
||||
}
|
||||
loaded, err = store.AuthorizationProjection(ctx, second.AccountID, second.ConnectorKey)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if loaded != second {
|
||||
t.Fatalf("projection = %#v, want %#v", loaded, second)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoreScopedSnapshotAtomicallyIncludesAuthorizationRevisionAndEventCursor(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store, err := Open(ctx, filepath.Join(t.TempDir(), "tuttid.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
connector := testConnector()
|
||||
if err := store.Transaction(ctx, func(tx market.Transaction) error {
|
||||
connector.Revision = tx.AdvanceRevision()
|
||||
return tx.SaveConnector(connector)
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
projection := market.AuthorizationProjection{
|
||||
AccountID: "account-1", ConnectorKey: connector.Key, ConnectionID: "connection-1",
|
||||
State: market.AuthorizationStateConnected, UpdatedAt: time.Unix(1, 0).UTC(),
|
||||
}
|
||||
if err := store.SaveAuthorizationProjection(ctx, projection); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
snapshot, err := store.SnapshotForScope(ctx, market.OperationScope{AccountID: projection.AccountID})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if snapshot.Revision != 2 || snapshot.EventCursor != 1 || len(snapshot.Connectors) != 1 ||
|
||||
snapshot.Connectors[0].Revision != 2 || snapshot.Connectors[0].Authorization.State != market.AuthorizationStateConnected {
|
||||
t.Fatalf("scoped snapshot = %#v", snapshot)
|
||||
}
|
||||
projection.UpdatedAt = projection.UpdatedAt.Add(time.Minute)
|
||||
if err := store.SaveAuthorizationProjection(ctx, projection); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
unchanged, err := store.SnapshotForScope(ctx, market.OperationScope{AccountID: projection.AccountID})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if unchanged.Revision != snapshot.Revision || unchanged.EventCursor != snapshot.EventCursor {
|
||||
t.Fatalf("non-public projection update advanced snapshot: %#v", unchanged)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoreAuthorizationSnapshotIsMonotonicAndDisconnectsMissingConnectors(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store, err := Open(ctx, filepath.Join(t.TempDir(), "tuttid.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
applied, err := store.ApplyAuthorizationSnapshot(ctx, "account-1", market.AuthorizationSnapshot{Revision: 8, Connectors: []market.AuthorizationProjection{{
|
||||
ConnectorKey: "tencent-docs", ConnectorVersion: "0.2.0", ConnectionID: "connection-1", ConnectionVersion: 3,
|
||||
State: market.AuthorizationStateConnected,
|
||||
}}})
|
||||
if err != nil || len(applied.ChangedConnectorKeys) != 1 || applied.ChangedConnectorKeys[0] != "tencent-docs" {
|
||||
t.Fatalf("initial snapshot applied=%#v error=%v", applied, err)
|
||||
}
|
||||
if err := store.SaveAuthorizationProjection(ctx, market.AuthorizationProjection{
|
||||
AccountID: "account-1", ConnectorKey: "tencent-docs", State: market.AuthorizationStateDisconnected,
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
projection, err := store.AuthorizationProjection(ctx, "account-1", "tencent-docs")
|
||||
if err != nil || projection.State != market.AuthorizationStateConnected || projection.ServerRevision != 8 {
|
||||
t.Fatalf("provisional write replaced server snapshot: %#v, %v", projection, err)
|
||||
}
|
||||
applied, err = store.ApplyAuthorizationSnapshot(ctx, "account-1", market.AuthorizationSnapshot{Revision: 7})
|
||||
if err != nil || len(applied.ChangedConnectorKeys) != 0 {
|
||||
t.Fatalf("stale snapshot applied=%#v error=%v", applied, err)
|
||||
}
|
||||
applied, err = store.ApplyAuthorizationSnapshot(ctx, "account-1", market.AuthorizationSnapshot{Revision: 9})
|
||||
if err != nil || len(applied.ChangedConnectorKeys) != 1 || applied.ChangedConnectorKeys[0] != "tencent-docs" {
|
||||
t.Fatalf("removal snapshot applied=%#v error=%v", applied, err)
|
||||
}
|
||||
projection, err = store.AuthorizationProjection(ctx, "account-1", "tencent-docs")
|
||||
if err != nil || projection.State != market.AuthorizationStateDisconnected || projection.ConnectionID != "" || projection.ServerRevision != 9 {
|
||||
t.Fatalf("removed projection = %#v, %v", projection, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoreAuthorizationSnapshotAtomicallyResolvesOnlyMatchingAccountReceipts(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store, err := Open(ctx, filepath.Join(t.TempDir(), "tuttid.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
connected := market.AuthorizationSnapshot{Revision: 8, Connectors: []market.AuthorizationProjection{{
|
||||
ConnectorKey: "tencent-docs", ConnectorVersion: "0.2.0", ConnectionID: "connection-1",
|
||||
ConnectionVersion: 3, State: market.AuthorizationStateConnected,
|
||||
}}}
|
||||
if _, err := store.ApplyAuthorizationSnapshot(ctx, "account-1", connected); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, accountID := range []string{"account-1", "account-2"} {
|
||||
operation := market.Operation{
|
||||
OperationID: "authorization-" + accountID, ClientRequestID: "request-" + accountID,
|
||||
ConnectorKey: "tencent-docs", Kind: market.OperationKindStartAuthorization,
|
||||
Scope: market.OperationScope{AccountID: accountID}, State: market.OperationStateCompleted,
|
||||
Stage: market.OperationStageCompleted, CreatedAt: time.Unix(1, 0).UTC(), UpdatedAt: time.Unix(2, 0).UTC(),
|
||||
Execution: market.OperationExecution{AuthorizationSession: &market.AuthorizationSession{
|
||||
OperationID: "authorization-" + accountID, ConnectorKey: "tencent-docs", SessionID: "session-" + accountID,
|
||||
ActionType: "redirect", State: market.AuthorizationStatePending,
|
||||
Resolution: market.AuthorizationSessionResolutionUnresolved,
|
||||
}},
|
||||
}
|
||||
if err := store.Transaction(ctx, func(tx market.Transaction) error { return tx.SaveOperation(operation) }); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Reapplying the same server revision must still surface a receipt created
|
||||
// after the projection was first stored, without terminalizing it before
|
||||
// the daemon has awaited Runtime Reconcile.
|
||||
applied, err := store.ApplyAuthorizationSnapshot(ctx, "account-1", connected)
|
||||
if err != nil || len(applied.ChangedConnectorKeys) != 0 ||
|
||||
len(applied.PendingReceiptConnectorKeys) != 1 || applied.PendingReceiptConnectorKeys[0] != "tencent-docs" {
|
||||
t.Fatalf("same-revision apply = %#v, error = %v", applied, err)
|
||||
}
|
||||
accountOne, err := store.Operation(ctx, "authorization-account-1")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if accountOne.Execution.AuthorizationSession == nil ||
|
||||
accountOne.Execution.AuthorizationSession.Resolution != market.AuthorizationSessionResolutionUnresolved {
|
||||
t.Fatalf("account one receipt = %#v", accountOne.Execution.AuthorizationSession)
|
||||
}
|
||||
unresolvedOne, err := store.UnresolvedAuthorizationSessionOperations(ctx, market.OperationScope{AccountID: "account-1"})
|
||||
if err != nil || len(unresolvedOne) != 1 {
|
||||
t.Fatalf("account one unresolved = %#v, error = %v", unresolvedOne, err)
|
||||
}
|
||||
unresolvedTwo, err := store.UnresolvedAuthorizationSessionOperations(ctx, market.OperationScope{AccountID: "account-2"})
|
||||
if err != nil || len(unresolvedTwo) != 1 || unresolvedTwo[0].Execution.AuthorizationSession == nil ||
|
||||
unresolvedTwo[0].Execution.AuthorizationSession.SessionID != "session-account-2" {
|
||||
t.Fatalf("account two unresolved = %#v, error = %v", unresolvedTwo, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoreOperationLeaseFencesOtherWorkersAndExpires(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store, err := Open(ctx, filepath.Join(t.TempDir(), "tuttid.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
operation := market.Operation{
|
||||
OperationID: "operation-1", ClientRequestID: "request-1", ConnectorKey: "github",
|
||||
Kind: market.OperationKindInstall, State: market.OperationStateAccepted,
|
||||
Stage: market.OperationStageAccepted, CreatedAt: time.Unix(1, 0).UTC(), UpdatedAt: time.Unix(1, 0).UTC(),
|
||||
}
|
||||
if err := store.Transaction(ctx, func(tx market.Transaction) error { return tx.SaveOperation(operation) }); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
now := time.Unix(10, 0).UTC()
|
||||
if _, claimed, err := store.ClaimOperation(ctx, operation.OperationID, "worker-a", now, now.Add(time.Minute)); err != nil || !claimed {
|
||||
t.Fatalf("first claim: claimed=%v err=%v", claimed, err)
|
||||
}
|
||||
if _, claimed, err := store.ClaimOperation(ctx, operation.OperationID, "worker-b", now, now.Add(time.Minute)); err != nil || claimed {
|
||||
t.Fatalf("fenced claim: claimed=%v err=%v", claimed, err)
|
||||
}
|
||||
if _, claimed, err := store.ClaimOperation(ctx, operation.OperationID, "worker-b", now.Add(2*time.Minute), now.Add(3*time.Minute)); err != nil || !claimed {
|
||||
t.Fatalf("expired claim: claimed=%v err=%v", claimed, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoreOperationLeaseTokenFencesStaleRenewSaveAndRelease(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store, err := Open(ctx, filepath.Join(t.TempDir(), "tuttid.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
operation := market.Operation{OperationID: "operation-1", ClientRequestID: "request-1", ConnectorKey: "github",
|
||||
Kind: market.OperationKindInstall, State: market.OperationStateAccepted, Stage: market.OperationStageAccepted,
|
||||
CreatedAt: time.Unix(1, 0).UTC(), UpdatedAt: time.Unix(1, 0).UTC()}
|
||||
if err := store.Transaction(ctx, func(tx market.Transaction) error { return tx.SaveOperation(operation) }); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
now := time.Unix(10, 0).UTC()
|
||||
first, claimed, err := store.ClaimOperation(ctx, operation.OperationID, "worker-a", now, now.Add(time.Minute))
|
||||
if err != nil || !claimed {
|
||||
t.Fatalf("first claim = %#v, %v, %v", first, claimed, err)
|
||||
}
|
||||
secondNow := now.Add(2 * time.Minute)
|
||||
second, claimed, err := store.ClaimOperation(ctx, operation.OperationID, "worker-b", secondNow, secondNow.Add(time.Minute))
|
||||
if err != nil || !claimed || second.LeaseToken <= first.LeaseToken {
|
||||
t.Fatalf("second claim = %#v, %v, %v", second, claimed, err)
|
||||
}
|
||||
if err := store.RenewOperationLease(ctx, operation.OperationID, "worker-a", first.LeaseToken, secondNow, secondNow.Add(time.Minute)); !errors.Is(err, market.ErrOperationLeaseLost) {
|
||||
t.Fatalf("stale renew error = %v", err)
|
||||
}
|
||||
first.State = market.OperationStateCompleted
|
||||
if err := store.Transaction(ctx, func(tx market.Transaction) error { return tx.SaveOperation(first) }); !errors.Is(err, market.ErrOperationLeaseLost) {
|
||||
t.Fatalf("stale save error = %v", err)
|
||||
}
|
||||
if err := store.ReleaseOperationLease(ctx, operation.OperationID, "worker-a", first.LeaseToken); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, claimed, err := store.ClaimOperation(ctx, operation.OperationID, "worker-c", secondNow, secondNow.Add(time.Minute)); err != nil || claimed {
|
||||
t.Fatalf("stale release cleared current lease: claimed=%v err=%v", claimed, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoreOperationLeaseHasSingleWinnerAcrossConnections(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
databasePath := filepath.Join(t.TempDir(), "tuttid.db")
|
||||
firstStore, err := Open(ctx, databasePath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer firstStore.Close()
|
||||
secondStore, err := Open(ctx, databasePath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer secondStore.Close()
|
||||
operation := market.Operation{
|
||||
OperationID: "operation-1", ClientRequestID: "request-1", ConnectorKey: "github",
|
||||
Kind: market.OperationKindInstall, State: market.OperationStateAccepted,
|
||||
Stage: market.OperationStageAccepted, CreatedAt: time.Unix(1, 0).UTC(), UpdatedAt: time.Unix(1, 0).UTC(),
|
||||
}
|
||||
if err := firstStore.Transaction(ctx, func(tx market.Transaction) error { return tx.SaveOperation(operation) }); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
type claimResult struct {
|
||||
claimed bool
|
||||
err error
|
||||
}
|
||||
start := make(chan struct{})
|
||||
results := make(chan claimResult, 2)
|
||||
var workers sync.WaitGroup
|
||||
for index, store := range []*Store{firstStore, secondStore} {
|
||||
workers.Add(1)
|
||||
go func(workerIndex int, workerStore *Store) {
|
||||
defer workers.Done()
|
||||
<-start
|
||||
now := time.Unix(10, 0).UTC()
|
||||
_, claimed, claimErr := workerStore.ClaimOperation(
|
||||
ctx, operation.OperationID, fmt.Sprintf("worker-%d", workerIndex), now, now.Add(time.Minute),
|
||||
)
|
||||
results <- claimResult{claimed: claimed, err: claimErr}
|
||||
}(index, store)
|
||||
}
|
||||
close(start)
|
||||
workers.Wait()
|
||||
close(results)
|
||||
|
||||
winners := 0
|
||||
for result := range results {
|
||||
if result.err != nil {
|
||||
t.Fatal(result.err)
|
||||
}
|
||||
if result.claimed {
|
||||
winners++
|
||||
}
|
||||
}
|
||||
if winners != 1 {
|
||||
t.Fatalf("lease winners = %d, want 1", winners)
|
||||
}
|
||||
}
|
||||
|
||||
func testConnector() market.Connector {
|
||||
release := market.Release{
|
||||
SchemaVersion: "1", ReleaseID: "42", ConnectorKey: "github", Version: "1.0.0",
|
||||
ReleaseDigest: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
||||
ManifestDigest: "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
|
||||
Manifest: market.Manifest{
|
||||
IconURL: "https://cdn.example.test/tutti/connector-market/github/1.0.0/github-1.0.0-icon.svg",
|
||||
SchemaVersion: "1",
|
||||
DisplayName: "GitHub",
|
||||
Implementation: market.Implementation{Kind: market.ImplementationKindManagedStdio,
|
||||
ManagedStdio: &market.ManagedStdioImplementation{Runtime: market.RuntimeRequirement{Language: "node", Profile: "connector-node-static", ABI: "node20-darwin-arm64"}, MCP: &market.ManagedMCPInterface{Entrypoint: "bin/github.js"}}},
|
||||
AuthorizationKind: "none",
|
||||
},
|
||||
Artifact: market.Artifact{
|
||||
Key: "connectors/github/1.0.0.zip",
|
||||
SHA256: "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc",
|
||||
SizeBytes: 123, MediaType: "application/vnd.tutti.connector+zip",
|
||||
},
|
||||
PublishedAt: time.Unix(1, 0).UTC(), Status: market.ReleaseStatusAvailable,
|
||||
}
|
||||
return market.Connector{
|
||||
Key: "github", Release: release,
|
||||
Installation: market.Installation{State: market.InstallationStateNotInstalled},
|
||||
Authorization: market.Authorization{State: market.AuthorizationStateNotRequired},
|
||||
Compatibility: market.Compatibility{State: market.CompatibilityStateSupported},
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user