V0.38.2 library packages (#235)
Co-authored-by: gobha <jasafpro@gmail.com> Co-committed-by: gobha <jasafpro@gmail.com>
This commit is contained in:
@@ -66,6 +66,7 @@ type Stores struct {
|
||||
RateLimits RateLimitStore // v0.32.0: Distributed rate limiting
|
||||
Export ExportStore // v0.34.0: Data portability batch reads + GDPR ops
|
||||
Connections ConnectionStore // v0.38.1: Extension connection credentials
|
||||
Dependencies DependencyStore // v0.38.2: Library package dependency graph
|
||||
}
|
||||
|
||||
// =========================================
|
||||
@@ -1396,6 +1397,35 @@ type ConnectionStore interface {
|
||||
DeleteByIDAndScope(ctx context.Context, id, scope, ownerID string) (int64, error)
|
||||
}
|
||||
|
||||
// =========================================
|
||||
// DEPENDENCY STORE (v0.38.2)
|
||||
// =========================================
|
||||
|
||||
type DependencyStore interface {
|
||||
// Create records that consumerID depends on libraryID.
|
||||
Create(ctx context.Context, dep *models.ExtDependency) error
|
||||
|
||||
// Delete removes a single dependency record.
|
||||
Delete(ctx context.Context, consumerID, libraryID string) error
|
||||
|
||||
// DeleteAllForConsumer removes all dependency records for a consumer.
|
||||
// Called when uninstalling a consumer package.
|
||||
DeleteAllForConsumer(ctx context.Context, consumerID string) error
|
||||
|
||||
// ListByConsumer returns all libraries that consumerID depends on.
|
||||
ListByConsumer(ctx context.Context, consumerID string) ([]models.ExtDependency, error)
|
||||
|
||||
// ListByLibrary returns all consumers that depend on libraryID.
|
||||
ListByLibrary(ctx context.Context, libraryID string) ([]models.ExtDependency, error)
|
||||
|
||||
// ListAll returns the full dependency graph.
|
||||
ListAll(ctx context.Context) ([]models.ExtDependency, error)
|
||||
|
||||
// HasConsumers returns true if any package depends on libraryID.
|
||||
// Used for uninstall protection.
|
||||
HasConsumers(ctx context.Context, libraryID string) (bool, error)
|
||||
}
|
||||
|
||||
// =========================================
|
||||
|
||||
// ListOptions provides standard pagination/sort for list queries.
|
||||
|
||||
95
server/store/postgres/ext_dependency.go
Normal file
95
server/store/postgres/ext_dependency.go
Normal file
@@ -0,0 +1,95 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
|
||||
"chat-switchboard/models"
|
||||
)
|
||||
|
||||
// ── DependencyStore (v0.38.2) ────────────────────────
|
||||
|
||||
type DependencyStore struct{}
|
||||
|
||||
func NewDependencyStore() *DependencyStore { return &DependencyStore{} }
|
||||
|
||||
const depCols = `consumer_id, library_id, version_spec, resolved_ver`
|
||||
|
||||
func (s *DependencyStore) Create(ctx context.Context, dep *models.ExtDependency) error {
|
||||
_, err := DB.ExecContext(ctx, `
|
||||
INSERT INTO ext_dependencies (consumer_id, library_id, version_spec, resolved_ver)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
ON CONFLICT (consumer_id, library_id) DO UPDATE
|
||||
SET version_spec = EXCLUDED.version_spec, resolved_ver = EXCLUDED.resolved_ver`,
|
||||
dep.ConsumerID, dep.LibraryID, dep.VersionSpec, dep.ResolvedVer,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *DependencyStore) Delete(ctx context.Context, consumerID, libraryID string) error {
|
||||
_, err := DB.ExecContext(ctx,
|
||||
`DELETE FROM ext_dependencies WHERE consumer_id = $1 AND library_id = $2`,
|
||||
consumerID, libraryID)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *DependencyStore) DeleteAllForConsumer(ctx context.Context, consumerID string) error {
|
||||
_, err := DB.ExecContext(ctx,
|
||||
`DELETE FROM ext_dependencies WHERE consumer_id = $1`, consumerID)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *DependencyStore) ListByConsumer(ctx context.Context, consumerID string) ([]models.ExtDependency, error) {
|
||||
rows, err := DB.QueryContext(ctx,
|
||||
`SELECT `+depCols+` FROM ext_dependencies WHERE consumer_id = $1 ORDER BY library_id`,
|
||||
consumerID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
return scanDependencies(rows)
|
||||
}
|
||||
|
||||
func (s *DependencyStore) ListByLibrary(ctx context.Context, libraryID string) ([]models.ExtDependency, error) {
|
||||
rows, err := DB.QueryContext(ctx,
|
||||
`SELECT `+depCols+` FROM ext_dependencies WHERE library_id = $1 ORDER BY consumer_id`,
|
||||
libraryID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
return scanDependencies(rows)
|
||||
}
|
||||
|
||||
func (s *DependencyStore) ListAll(ctx context.Context) ([]models.ExtDependency, error) {
|
||||
rows, err := DB.QueryContext(ctx,
|
||||
`SELECT `+depCols+` FROM ext_dependencies ORDER BY consumer_id, library_id`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
return scanDependencies(rows)
|
||||
}
|
||||
|
||||
func (s *DependencyStore) HasConsumers(ctx context.Context, libraryID string) (bool, error) {
|
||||
var exists bool
|
||||
err := DB.QueryRowContext(ctx,
|
||||
`SELECT EXISTS(SELECT 1 FROM ext_dependencies WHERE library_id = $1)`,
|
||||
libraryID).Scan(&exists)
|
||||
return exists, err
|
||||
}
|
||||
|
||||
// ── Internal ───────────────────────────────────────
|
||||
|
||||
func scanDependencies(rows *sql.Rows) ([]models.ExtDependency, error) {
|
||||
var result []models.ExtDependency
|
||||
for rows.Next() {
|
||||
var d models.ExtDependency
|
||||
err := rows.Scan(&d.ConsumerID, &d.LibraryID, &d.VersionSpec, &d.ResolvedVer)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result = append(result, d)
|
||||
}
|
||||
return result, rows.Err()
|
||||
}
|
||||
@@ -52,5 +52,6 @@ func NewStores(db *sql.DB) store.Stores {
|
||||
RateLimits: NewRateLimitStore(),
|
||||
Export: NewExportStore(),
|
||||
Connections: NewConnectionStore(),
|
||||
Dependencies: NewDependencyStore(),
|
||||
}
|
||||
}
|
||||
|
||||
93
server/store/sqlite/ext_dependency.go
Normal file
93
server/store/sqlite/ext_dependency.go
Normal file
@@ -0,0 +1,93 @@
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
|
||||
"chat-switchboard/models"
|
||||
)
|
||||
|
||||
// ── DependencyStore (v0.38.2) ────────────────────────
|
||||
|
||||
type DependencyStore struct{}
|
||||
|
||||
func NewDependencyStore() *DependencyStore { return &DependencyStore{} }
|
||||
|
||||
const depCols = `consumer_id, library_id, version_spec, resolved_ver`
|
||||
|
||||
func (s *DependencyStore) Create(ctx context.Context, dep *models.ExtDependency) error {
|
||||
_, err := DB.ExecContext(ctx, `
|
||||
INSERT OR REPLACE INTO ext_dependencies (consumer_id, library_id, version_spec, resolved_ver)
|
||||
VALUES (?, ?, ?, ?)`,
|
||||
dep.ConsumerID, dep.LibraryID, dep.VersionSpec, dep.ResolvedVer,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *DependencyStore) Delete(ctx context.Context, consumerID, libraryID string) error {
|
||||
_, err := DB.ExecContext(ctx,
|
||||
`DELETE FROM ext_dependencies WHERE consumer_id = ? AND library_id = ?`,
|
||||
consumerID, libraryID)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *DependencyStore) DeleteAllForConsumer(ctx context.Context, consumerID string) error {
|
||||
_, err := DB.ExecContext(ctx,
|
||||
`DELETE FROM ext_dependencies WHERE consumer_id = ?`, consumerID)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *DependencyStore) ListByConsumer(ctx context.Context, consumerID string) ([]models.ExtDependency, error) {
|
||||
rows, err := DB.QueryContext(ctx,
|
||||
`SELECT `+depCols+` FROM ext_dependencies WHERE consumer_id = ? ORDER BY library_id`,
|
||||
consumerID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
return scanDependencies(rows)
|
||||
}
|
||||
|
||||
func (s *DependencyStore) ListByLibrary(ctx context.Context, libraryID string) ([]models.ExtDependency, error) {
|
||||
rows, err := DB.QueryContext(ctx,
|
||||
`SELECT `+depCols+` FROM ext_dependencies WHERE library_id = ? ORDER BY consumer_id`,
|
||||
libraryID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
return scanDependencies(rows)
|
||||
}
|
||||
|
||||
func (s *DependencyStore) ListAll(ctx context.Context) ([]models.ExtDependency, error) {
|
||||
rows, err := DB.QueryContext(ctx,
|
||||
`SELECT `+depCols+` FROM ext_dependencies ORDER BY consumer_id, library_id`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
return scanDependencies(rows)
|
||||
}
|
||||
|
||||
func (s *DependencyStore) HasConsumers(ctx context.Context, libraryID string) (bool, error) {
|
||||
var count int
|
||||
err := DB.QueryRowContext(ctx,
|
||||
`SELECT COUNT(*) FROM ext_dependencies WHERE library_id = ?`,
|
||||
libraryID).Scan(&count)
|
||||
return count > 0, err
|
||||
}
|
||||
|
||||
// ── Internal ───────────────────────────────────────
|
||||
|
||||
func scanDependencies(rows *sql.Rows) ([]models.ExtDependency, error) {
|
||||
var result []models.ExtDependency
|
||||
for rows.Next() {
|
||||
var d models.ExtDependency
|
||||
err := rows.Scan(&d.ConsumerID, &d.LibraryID, &d.VersionSpec, &d.ResolvedVer)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result = append(result, d)
|
||||
}
|
||||
return result, rows.Err()
|
||||
}
|
||||
@@ -52,5 +52,6 @@ func NewStores(db *sql.DB) store.Stores {
|
||||
RateLimits: NewRateLimitStore(),
|
||||
Export: NewExportStore(),
|
||||
Connections: NewConnectionStore(),
|
||||
Dependencies: NewDependencyStore(),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user