sql

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 28 Imported by: 0

Documentation

Overview

Package sql is the composite SQL store: the platform's store and every module's SQL store over one dialect store, so a transaction begun through any of them spans them all. It holds Open, which migrates every module's stream and runs their hooks, the module list that the migrator, the hooks and Reseal follow, and the registration of the artifacts module's layout watch with the platform's config store.

Role

app, adapter layer (SQL): the composition root's metadata store on SQLite and PostgreSQL. Store embeds the *dialect.Store that holds the pool, the dialect and the keyring and runs the unit of work, and keeps the platform's and each module's SQL store over it as fields, with one accessor for each of their stores. The sqlite and postgres packages below it open Store on their driver; app opens it through them, and the tests of the modules reach it only through app/storetest, as a module may import nothing else from app. It imports the dialect layer, the keyring, the platform's SQL store and every module's adapters/sql, and never app.

Inputs

  • Open: a pool, its dialect.Dialect and Options: whether to skip the migrations, and the keyring that seals secret columns, or none.
  • New: a pool whose streams are migrated already, its dialect and the keyring.
  • Reseal: a store opened with the keyring whose active version every sealed value is to be under.

Outputs

  • Store: the platform's Settings, Leases, WorkQueue, Configs, Instance, Outbox, Uploads, Replays and RequestBudgets; the artifacts module's Repos, RemoteCache, Items, Trash, Packages, Blobs, Replicas, GitLFSLocks, BowerRegistrations, UploadDigests, Builds, ArchivedItems, ColdBlobs and ColdReplicas; the access module's Users, Groups, Permissions, Tokens, Keys, Projects and APIRestrictions; Webhooks; Cleanup and Archive; Replications, Peers, Federation and Platform; Bundles and Distribution; Workers; and the dialect store's Tx, AfterCommit and Close.
  • Open: the store, pinged, with every stream migrated and every module's AfterMigrate run unless Options.SkipMigrate; or the first error, with the pool closed.
  • Modules: every module's dialect.Module, in the canonical order.
  • Reseal: how many sealed values it sealed again.

Invariants

  • Migrations, AfterMigrate hooks and Reseal follow the canonical module order of Modules: the platform, then artifacts, access, notifications, retention, federation, releases and workers (decision 0044, "Persistence"). The names key the modules' rows in schema_migrations, so a renamed module would find no history and apply its baseline again.
  • Every sealed column is registered by its module's dialect.Module value, with the purpose and the row id its store seals under, which are authenticated with the value, so Reseal and key rotation reach every sealed value (decision 0017). TestAccSealedColumnsComplete fails on a purpose that the source seals with and no module registers.
  • New registers the artifacts module's layout watch with the platform's config store, so a layout document that is created, changed or deleted fences remote publication for the repositories that use it in the same transaction (decision 0007).
  • The platform's and the modules' stores are fields rather than embedded, so none of their methods is promoted onto Store, where it could shadow an accessor without a compile error; the embedded dialect store gives Store only Tx, AfterCommit and Close.
  • The tables follow the schema conventions of platform/sql/dialect, and the SQL text, the table names, the purpose strings, the row ids and the error texts are the modules' stores' own.

References

  • Decision 0004: docs/research/decisions/0004-storage-sql-metadata-and-blobs.md (one SQL store over a dialect layer, after hostweave's).
  • Decision 0007: docs/research/decisions/0007-remote-repositories.md (layout fencing).
  • Decision 0017: docs/research/decisions/0017-sealed-columns-and-master-key.md (sealed columns, their purposes and row ids, and re-sealing).
  • Decision 0044: docs/research/decisions/0044-modular-monolith-migration.md ("Persistence": one dialect store shared by every module's stores, one dialect.Module per module in the canonical order, layout fencing as a config watch; "Names the migration introduces").
  • Target layout: docs/research/architecture-options/5-modular-monolith.md, lines 627 and 628 (the migrator runs the platform first, then the modules in the order app registers them) and line 751 (app's platform.go opens and migrates the SQL store).
  • Plan: docs/implementation/plan.md#p2-stores and #p9d-modular-monolith-migration, epic STO (https://github.com/weaveplatform/artifactweave/issues/939).
  • https://github.com/weaveplatform/artifactweave/issues/1098

Status

Implemented P2: the metadata store on SQLite and PostgreSQL, with Reseal (pull request 3); later phases added a store for each entity they introduced. Moved here in P9d: each module's stores split into its own adapter (issues 1056 to 1062), leaving this package the composition over them, which issue 1098 moved under app.

Testing

  • reseal_test.go: TestUnitModulesRegisterInTheCanonicalOrder, TestAccSealedColumnsComplete and TestAccResealSealsPlainValues.
  • sealscan_test.go: TestUnitScanSealsReadsTheFixture, TestUnitScanSealsReportsUnreadablePurposes and TestUnitCheckSealedPurposes, over the packages in testdata/sealscan.
  • store_test.go: TestUnitDialectStoreShadowsNoAccessor.
  • The composite contract and suites run on Store in the sqlite and postgres packages' tests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Modules

func Modules() []dialect.Module

Modules returns every module's registration with the SQL layer, in the canonical module order that the migrator, Open's AfterMigrate hooks and Reseal follow (decision 0044, "Persistence"): each module's migration stream, its sealed columns and its hooks. The names key the modules' rows in schema_migrations, so a renamed module would find no history and apply its baseline again. Each sealed column keeps the purpose and the row id its store seals with, which are authenticated with the value (decision 0017): a change to either makes every stored value of the column unreadable. It is a function rather than a variable, so no caller can change the list, and a hook can reach code that reads it without an initialisation cycle.

Types

type Options

type Options struct {
	// SkipMigrate opens without applying migrations.
	SkipMigrate bool
	// Keyring seals secret columns. Without one they are stored in plain
	// text. Enabling a keyring later requires Reseal to convert existing values.
	Keyring *crypt.Keyring
}

Options configures Open.

type Store

type Store struct {
	*dialect.Store
	// contains filtered or unexported fields
}

Store is the composite SQL store over database/sql for one Dialect. The embedded dialect store holds the pool, the dialect and the keyring and runs the unit of work; Store adds an accessor for each store of the platform and the modules. The platform's and the modules' stores are fields over the same dialect store rather than embedded, so none of their methods is promoted onto Store, where it could shadow an accessor without a compile error.

func New

func New(db *sql.DB, d dialect.Dialect, keys *crypt.Keyring) *Store

New wraps an open, migrated pool. It registers the artifacts module's layout watch with the platform's config store: a layout document that is created, changed or deleted fences the remote repositories that use it, in the same transaction (decision 0007).

func Open

func Open(ctx context.Context, db *sql.DB, d dialect.Dialect, o Options) (*Store, error)

Open pings db and wraps it. Unless o.SkipMigrate, it first applies the pending migrations of every module's stream and then runs each module's AfterMigrate hook, in the canonical module order of Modules. It closes db on failure. Each backend opens its pool and calls it.

func (*Store) APIRestrictions

func (s *Store) APIRestrictions() accport.APIRestrictionStore

APIRestrictions is the access module's API restriction store.

func (*Store) Archive

func (s *Store) Archive() retport.ArchiveStore

Archive is the retention module's archive store.

func (*Store) ArchivedItems

func (s *Store) ArchivedItems() artport.ArchivedItemStore

ArchivedItems is the artifacts module's archived-item store.

func (*Store) Blobs

func (s *Store) Blobs() artport.BlobIndex

Blobs is the artifacts module's blob index.

func (*Store) BowerRegistrations

func (s *Store) BowerRegistrations() artport.BowerRegistrationStore

BowerRegistrations is the artifacts module's Bower registration store.

func (*Store) Builds

func (s *Store) Builds() artport.BuildStore

Builds is the artifacts module's build store.

func (*Store) Bundles

func (s *Store) Bundles() relport.BundleStore

Bundles is the releases module's bundle store.

func (*Store) Cleanup

func (s *Store) Cleanup() retport.CleanupStore

Cleanup is the retention module's cleanup store.

func (*Store) ColdBlobs

func (s *Store) ColdBlobs() artport.BlobIndex

ColdBlobs is the artifacts module's blob index over the archive tier.

func (*Store) ColdReplicas

func (s *Store) ColdReplicas() artport.ReplicaIndex

ColdReplicas is the artifacts module's replica index over the archive tier.

func (*Store) Configs

func (s *Store) Configs() configdoc.ConfigStore

Configs is the platform's config document store, which fences layouts as New registered.

func (*Store) Distribution

func (s *Store) Distribution() relport.DistributionStore

Distribution is the releases module's distribution store.

func (*Store) Federation

func (s *Store) Federation() fedport.FederationStore

Federation is the federation module's federated repository store.

func (*Store) GitLFSLocks

func (s *Store) GitLFSLocks() artport.GitLFSLockStore

GitLFSLocks is the artifacts module's Git LFS lock store.

func (*Store) Groups

func (s *Store) Groups() accport.GroupStore

Groups is the access module's group store.

func (*Store) Instance

func (s *Store) Instance() instance.InstanceStore

Instance is the platform's instance store.

func (*Store) Items

func (s *Store) Items() artport.ItemStore

Items is the artifacts module's item store.

func (*Store) Keys

func (s *Store) Keys() accport.KeyStore

Keys is the access module's key store.

func (*Store) Leases

func (s *Store) Leases() work.LeaseStore

Leases is the platform's lease store.

func (*Store) Outbox

func (s *Store) Outbox() outbox.OutboxStore

Outbox is the platform's federation outbox store.

func (*Store) Packages

func (s *Store) Packages() artport.PackageStore

Packages is the artifacts module's package store.

func (*Store) Peers

func (s *Store) Peers() fedport.PeerStore

Peers is the federation module's peer store.

func (*Store) Permissions

func (s *Store) Permissions() accport.PermissionStore

Permissions is the access module's permission store.

func (*Store) Platform

func (s *Store) Platform() fedport.PlatformStore

Platform is the federation module's platform federation store.

func (*Store) Projects

func (s *Store) Projects() accport.ProjectStore

Projects is the access module's project store.

func (*Store) RemoteCache

func (s *Store) RemoteCache() artport.RemoteCacheStore

RemoteCache is the artifacts module's remote cache store.

func (*Store) Replays

func (s *Store) Replays() idempotency.ReplayStore

Replays is the platform's API replay store.

func (*Store) Replicas

func (s *Store) Replicas() artport.ReplicaIndex

Replicas is the artifacts module's replica index.

func (*Store) Replications

func (s *Store) Replications() fedport.ReplicationStore

Replications is the federation module's replication store.

func (*Store) Repos

func (s *Store) Repos() artport.RepoStore

Repos is the artifacts module's repository store.

func (*Store) RequestBudgets

func (s *Store) RequestBudgets() apilimit.RequestBudgetStore

RequestBudgets is the platform's request budget store.

func (*Store) Reseal

func (s *Store) Reseal(ctx context.Context) (int, error)

Reseal re-seals every sealed value under the keyring's active version and returns how many values changed. Values already under the active version are left alone, and values stored before a keyring was configured are sealed for the first time, so running it again is harmless. Each value is replaced only if it still holds the bytes that were read, so a concurrent write is never overwritten with a stale secret.

func (*Store) Settings

func (s *Store) Settings() settings.SettingsStore

Settings is the platform's settings store.

func (*Store) Tokens

func (s *Store) Tokens() accport.TokenStore

Tokens is the access module's token store.

func (*Store) Trash

func (s *Store) Trash() artport.TrashStore

Trash is the artifacts module's trash store.

func (*Store) UploadDigests

func (s *Store) UploadDigests() artport.UploadDigestStore

UploadDigests is the artifacts module's upload digest store.

func (*Store) Uploads

func (s *Store) Uploads() session.UploadStore

Uploads is the platform's upload session store.

func (*Store) Users

func (s *Store) Users() accport.UserStore

Users is the access module's user store.

func (*Store) Webhooks

func (s *Store) Webhooks() ntfport.WebhookStore

Webhooks is the notifications module's webhook store.

func (*Store) WorkQueue

func (s *Store) WorkQueue() work.WorkQueue

WorkQueue is the platform's work queue.

func (*Store) Workers

func (s *Store) Workers() wrkport.WorkerStore

Workers is the workers module's worker store.

Directories

Path Synopsis
Package postgres opens the composite SQL store on PostgreSQL: the pool of the platform/sql/postgres driver, migrated and wrapped in the composite store of app/adapters/sql.
Package postgres opens the composite SQL store on PostgreSQL: the pool of the platform/sql/postgres driver, migrated and wrapped in the composite store of app/adapters/sql.
Package sqlite opens the composite SQL store on SQLite: the pool of the platform/sql/sqlite driver, migrated and wrapped in the composite store of app/adapters/sql.
Package sqlite opens the composite SQL store on SQLite: the pool of the platform/sql/sqlite driver, migrated and wrapped in the composite store of app/adapters/sql.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL