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: 23 Imported by: 0

Documentation

Overview

Package sql is the SQL implementation of the artifacts module's stores: repositories with their sealed secrets, the remote cache, items, the trash can, the package index, builds and their search, Git LFS locks, Bower registrations, upload digests and archived items, the blob and replica indexes and the cold tier's indexes, with the module's registration with the SQL layer.

Role

artifacts module, adapter layer (SQL). Store implements the module's port.Store over a shared dialect.Store, on SQLite and PostgreSQL alike; its remote cache and layout fence are the storekit code it shares with the in-memory adapter. Module registers the artifacts stream (adapters/sql/migrations), its two sealed columns and the repository header backfill, second in the canonical module order. The composite SQL store in app/adapters/sql builds one Store over its own dialect store, delegates every artifacts accessor to it, registers its LayoutWatch with the platform's config store and names Module in its module list. The package imports the standard library, the kernel, the platform, the module's core and api, storekit and its stream only.

Inputs

  • New: a *dialect.Store over a database that holds the artifacts stream's tables, with the keyring that seals repository passwords and sensitive header values, or none.
  • The port's calls, with the context of the caller's transaction, if there is one.
  • The watch's Changed: the name of the layout document that the config store's write created, changed or deleted.

Outputs

  • Store, from New, with Repos, RemoteCache, Items, Trash, Packages, Blobs, Replicas, GitLFSLocks, BowerRegistrations, UploadDigests, Builds, ArchivedItems, ColdBlobs and ColdReplicas, LayoutWatch, and the dialect store's Tx, AfterCommit and Close. Items' DockerPages reads a registry repository's image layout a page at a time from the partial indexes items_docker_tags and items_docker_images (migration 0002, #1402). Repos' Page counts and reads a window of the repository list in SQL, its filters and the caller's visibility in the WHERE clause and LIMIT one past the window (#1408), or the window after a key for a caller that walks the whole list (#1409). Items' DescendantsByFullPath reads a folder's descendants in full path byte order from the expression index items_full_path, and Builds' DependencyPage a checksum's dependencies in run, module and file order from build_dependencies_sha1_order, which replaces build_dependencies_sha1, and Trash's BelowByPath the trash can's tree in path order from the expression index trash_path (migration 0004, #1409); each is cut a page at a time, so a streamed list holds one page.
  • Module: the name artifacts, the stream migrations.FS, the two sealed columns and the BeforeReseal and AfterMigrate hooks.

Invariants

  • Store is built over the shared *dialect.Store, so every call joins the unit of work that the caller's context carries, whichever module's store began it (decision 0044, "Persistence"), and a caller's writes across stores commit or roll back together.
  • The repository store writes a configuration's sealed password and header values, and a remote repository's source record, in the transaction that writes the repository row, and Delete invalidates the repository's cache entries in its transaction too, so a write that fails leaves none of them changed (decisions 0003 and 0007).
  • Every sealed column and both hooks are in Module: repository_secrets.password with the purpose repositories.password under the repository key, and repository_header_secrets.value with the purpose repositories.http_header under the row id of the repository, header position and name, so key rotation re-seals them (decision 0017). Neither the purpose nor the row id ever changes: both are authenticated with every value.
  • The header backfill moves sensitive header values still held in a repository document into sealed rows. It runs as AfterMigrate at every open, with the configured keyring, because migrations never see key material, and as BeforeReseal ahead of every re-seal; the metadata store's open runs it before the access module's identity-search backfill.
  • ColdBlobs and ColdReplicas are the hot tier's blob and replica index code over the cold_blobs and cold_blob_replicas tables, so both tiers keep the same rules (decision 0024).
  • The replica index compares states with the SQL literals 'present' and 'deleting', which must keep matching mirror.ReplicaPresent and mirror.ReplicaDeleting.
  • The SQL text, the table names, the purpose strings, the row ids and the error texts are those the metadata store's SQL store used before the module owned them.

References

  • Decision 0003: docs/research/decisions/0003-repository-model.md.
  • Decision 0004: docs/research/decisions/0004-storage-sql-metadata-and-blobs.md.
  • Decision 0007: docs/research/decisions/0007-remote-repositories.md (the remote cache and its fences).
  • Decision 0010: docs/research/decisions/0010-retention-trash-and-gc.md (the trash can and blob garbage collection).
  • Decision 0012: docs/research/decisions/0012-search-and-aql.md.
  • Decision 0014: docs/research/decisions/0014-build-info.md.
  • Decision 0017: docs/research/decisions/0017-sealed-columns-and-master-key.md.
  • Decision 0018: docs/research/decisions/0018-package-index.md.
  • Decision 0024: docs/research/decisions/0024-cold-storage-tier.md.
  • Decision 0028: docs/research/decisions/0028-blob-mirroring.md.
  • 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, backfills as hooks, layout fencing as a config watch; "Names the migration introduces", which lists Store, New, LayoutWatch and Module).
  • Target layout: docs/research/architecture-options/5-modular-monolith.md, lines 601 to 609 (the artifacts port, SQL adapter and stream), lines 586 and 587 (the header and remote cache suites, which the module's tests and the metadata store's split by owner), lines 631 to 633 (RunModule) and line 1055 (the metadata store split per owner).
  • Plan: docs/implementation/plan.md#p2-stores, #p5-full-aql-searches-build-info, #p9-remote-repositories and #p9d-modular-monolith-migration, epic STO (https://github.com/weaveplatform/artifactweave/issues/939).
  • https://github.com/weaveplatform/artifactweave/issues/1061
  • https://github.com/weaveplatform/artifactweave/issues/1062

Status

Implemented P2: the repository, item, trash, package and blob stores and the archived items and cold indexes of the archive store, as the metadata store's (pull request 3), and the replica index with the blob mirror (pull request 30). Later phases added the build store (pull request 193) and build search (pull request 206), the remote cache (pull request 646), sealed header values (pull request 656), layout fencing (pull request 680), Git LFS locks (pull request 849) and Bower registrations (pull request 912). Moved here in P9d, which added Store, New, LayoutWatch and Module. The Docker page reader and its indexes page the registry's tags lists and catalogs (#1402), and Repos' Page Get Repositories (#1408). UploadDigests keeps a Hugging Face LFS upload's SHA-1 and MD5 in upload_digests, migration 0005 (#1581).

Testing

  • sql_test.go: TestAccContract runs the artifacts contract (adapters/storetest.Run) on SQLite in memory and in a file through sqltest.RunModule. The metadata store's contract runs the same cases on PostgreSQL, with RemoteLayoutFences beside them.
  • repository_headers_suite_test.go: TestAccRepositoryHeaderSecrets (sensitive header values are sealed in rows of their own and survive the configuration merge) and TestAccRepositoryHeaderMigration (AfterMigrate moves a legacy document's header values into sealed rows, keeps its other fields, changes nothing on a later open and leaves a re-seal nothing to do), on SQLite in memory and in a file and on PostgreSQL.
  • remotecache_suite_test.go: TestAccRemoteCachePersistence (each repository keeps a source lifetime of its own, and the lifetime and the completed and in-flight retrievals survive a reopen), on the same three backends.
  • repository_headers_test.go: five TestAccRepositoryHeader tests (a failed header write rolls back, corrupt header rows fail closed, a failed backfill leaves no partial secrets and succeeds on retry, a re-seal seals plain values and refuses a wrong key, and the backfill refuses a keyring that cannot open the existing secrets).
  • remotecache_test.go: TestAccRemoteCacheDatabaseFaults (a failed or corrupt statement anywhere in a cache call fails the call) and TestAccRemoteCacheClosedAndBadPage.
  • builds_test.go: TestAccBuildsFaults and TestAccBuildsUnmigrated (every build call reports an injected fault, a corrupt document or a missing schema as the driver's error, never as a store sentinel).
  • content_test.go: TestAccContentWriteFailures, TestAccContentPropertyAndStatsFailures, TestAccContentVersionFailures and TestAccContentUnreadableRows (failed repository, item, property and package version writes, undecodable documents and passwords read without the key that sealed them).
  • restart_test.go: TestAccP9bPackageIndexesSurviveRestart, TestAccGitLFSLocksSurviveRestart and TestAccBowerRegistrationsSurviveRestart (rows written to a SQLite file are read back after it is closed and opened again, and deletes stay deleted).
  • replicas_test.go: TestAccReplicaDriverFailures (a row that cannot be scanned and a dropped table surface as driver errors, never as store sentinels).
  • archived_test.go: TestAccArchivedItemsClosedDatabase and TestAccArchivedItemsCorruptRows (every archived-item call fails on a closed pool and on rows corrupted behind the store's back).
  • docker_page_test.go: TestAccDockerPagesContract (the Docker page reader's contract, adapters/storetest.RunDockerPages, on SQLite in memory and in a file and on PostgreSQL), TestAccDockerPagesAtScale (on 5000 tags of one image and 5000 images, a late page of each is read through its index, with no sort, on SQLite and PostgreSQL) and TestAccDockerPagesClosedDatabase.
  • streamed_lists_test.go: TestAccStreamedListsAtScale (over 15,000 items and trash entries and 5,000 dependency rows sharing one checksum, a late page of a deep list, of a dependency search and of the trash can's tree is one seek on items_full_path, build_dependencies_sha1_order or trash_path, with no sort, on SQLite and PostgreSQL) and TestAccStreamedListsClosedDatabase. Their contracts run in adapters/storetest (ItemsByFullPath, TrashByPath, the build store's DependencyPage and the repository window's After).
  • repo_page_test.go: TestAccRepoPagesContract (the repository window's contract, adapters/storetest.RunRepoPages, on SQLite in memory and in a file and on PostgreSQL) and TestAccRepoPagesAtScale (the first and a late window of 5000 repositories, as an administrator and as a partial reader sees them, with their timings and plans logged).
  • puppet_page_test.go: TestAccVersionNamesContract (the package store's VersionNames contract, adapters/storetest.RunVersionNames) and TestAccVersionNamesAtScale (a module of 5,000 versions beside another: its names come from package_versions_order in its own order, with no sort, and a late page row is one seek on package_versions_version, whose query plans it logs), each on SQLite in memory and in a file and on PostgreSQL (#1404), and TestAccPackagePagesContract (adapters/storetest.RunPackagePages, the list paging contract).
  • package_page_test.go: TestAccPackagePagesAtScale (5,000 packages and their NuGet versions, 5,000 releases across 1,000 modules and a module of 5,000 versions: each count, tally and late page walks packages_name or package_versions_order in its own order, sorting at most one package's versions or a short prefix's names, with timings and plans logged, on SQLite in memory and in a file and on PostgreSQL; #1425, #1426).

They open SQLite over the artifacts registration alone (store_test.go), which migrates the stream and runs the header backfill as the metadata store's open does.

Index

Constants

This section is empty.

Variables

View Source
var Module = dialect.Module{
	Name:       "artifacts",
	Migrations: migrations.FS,

	BeforeReseal: func(ctx context.Context, s *dialect.Store) (int, error) {
		return New(s).backfillRepositoryHeaders(ctx)
	},

	AfterMigrate: func(ctx context.Context, s *dialect.Store) error {
		_, err := New(s).backfillRepositoryHeaders(ctx)
		return err
	},
	Sealed: []dialect.SealedColumn{
		{
			Table: "repository_secrets", Column: "password", Purpose: purposeRepoPassword,
			Keys: []string{"repo_key"}, RowID: dialect.FirstKey,
		},
		{
			Table: "repository_header_secrets", Column: "value", Purpose: purposeRepoHeader,
			Keys: []string{"repo_key", "position", "name"},
			RowID: func(k []string) string {
				index, _ := strconv.Atoi(k[1])
				return repositoryHeaderID(k[0], index, k[2])
			},
		},
	},
}

Module registers the artifacts module with the SQL layer: its migration stream, under the name that keys the stream's rows in schema_migrations; the repository header backfill, before every reseal and after every migration; and the sealed columns its repository store writes. Each column keeps the purpose and the row id the store seals with, because both are authenticated with every value (decision 0017): a change to either makes the stored secrets unreadable.

Functions

This section is empty.

Types

type Store

type Store struct{ *dialect.Store }

Store is the SQL implementation of the artifacts module's port.Store: the repository, remote cache, item, trash, package, Git LFS lock, Bower, build and archived-item stores, the blob and replica indexes and the cold tier's indexes. It is built over a shared dialect.Store, so every call joins the transaction that the caller's context carries, whichever module's store began it.

func New

func New(db *dialect.Store) *Store

New returns the artifacts module's stores over db, which must hold the module stream's tables.

func (*Store) ArchivedItems

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

ArchivedItems returns the archived item store: the records archived items leave behind (decision 0024).

func (*Store) Blobs

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

Blobs returns the blob index over the blobs table: content-addressed, reference-counted rows (decisions 0004 and 0010).

func (*Store) BowerRegistrations

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

BowerRegistrations returns the Bower registration store: the immutable name-to-Git-URL registrations of Bower repositories.

func (*Store) Builds

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

Builds returns the build store: build-info runs with their modules, properties, artifacts, dependencies and promotions, and the AQL build search (decision 0014).

func (*Store) ColdBlobs

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

ColdBlobs returns the cold tier's blob index: the blob index code over the cold_blobs table (decision 0024).

func (*Store) ColdReplicas

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

ColdReplicas returns the cold tier's replica index: the replica index code over the cold_blob_replicas table (decision 0024).

func (*Store) GitLFSLocks

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

GitLFSLocks returns the Git LFS lock store: the file locks of Git LFS repositories, kept apart from the LFS objects.

func (*Store) Items

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

Items returns the item store: items in the canonical form of decision 0004, with their properties and download statistics, and the AQL item search (decision 0012).

func (*Store) LayoutWatch

func (s *Store) LayoutWatch() configdoc.Watch

LayoutWatch returns the config watch that fences remote publication when a layout document is created, changed or deleted: every remote repository that uses the layout as its own or its upstream layout gets a new source generation (storekit.FenceLayoutSources). The composition registers it with the platform's config store, which calls it inside the config write's transaction, so the edit and the fence commit or roll back together (decision 0007).

func (*Store) Packages

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

Packages returns the package store: the package index of decision 0018.

func (*Store) RemoteCache

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

RemoteCache returns the remote cache store: storekit's compare-and-swap logic over the remote_sources and remote_cache rows (decision 0007).

func (*Store) Replicas

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

Replicas returns the replica index over the blob_replicas table: which mirror members hold which blobs (decision 0028).

func (*Store) Repos

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

Repos returns the repository store: repository configurations, with remote passwords and sensitive header values sealed in rows of their own (decisions 0003 and 0017), and each remote repository's source record, which Create, Update and Delete change in the same transaction (decision 0007).

func (*Store) Trash

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

Trash returns the trash store: the trash can that deletes fill and restores and expiry empty (decision 0010).

func (*Store) UploadDigests

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

UploadDigests returns the upload digest store: the SHA-1 and MD5 an upload computed, by the content's SHA-256 (migration 0005).

Directories

Path Synopsis
Package migrations holds the artifacts module's migration stream: the SQLite and PostgreSQL schema of the tables the module's SQL adapter owns.
Package migrations holds the artifacts module's migration stream: the SQLite and PostgreSQL schema of the tables the module's SQL adapter owns.

Jump to

Keyboard shortcuts

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