sqlstore

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package sqlstore is the SQL ddm.Store: one implementation over database/sql for the SQLite, PostgreSQL, and MySQL dialects.

Why

The engine needs durable declarations, versions, sets, assignments, per-enrollment snapshots, status, and the change queue, with the same semantics on every backend the MDM storage already supports. This package reuses storage/sqlcommon's dialects and migration runner but owns its own migration set, recorded in ddm_schema_migrations, so it can share a database with the MDM schema or live in a separate one; nothing references the enrollments table. Every byte column holds the exact bytes given (no JSON column types, so tokens hash what was stored), every timestamp is written and read in UTC, every list is keyset paginated, and change rows are written inside the mutating transaction. The contract suite in ddm/ddmtest runs against all three dialects.

References

  • Decision record 0020: docs/research/decisions/0020-ddm-engine-membership-and-storage.md
  • Decision record 0021: docs/research/decisions/0021-status-reports-and-subscriptions.md
  • Decision record 0022: docs/research/decisions/0022-change-notifier.md
  • Decision record 0012: docs/research/decisions/0012-sql-storage-backends.md (dialects and migrations)
  • Plan of record: docs/research/implementation_plan.md (phase 5)
  • Migrations: ddm/sqlstore/migrations/{sqlite,postgres,mysql}/0001_init.sql

Index

Constants

View Source
const MigrationsTable = "ddm_schema_migrations"

MigrationsTable records the applied versions of the DDM schema, separate from the storage schema's table so the two version sequences never mix.

Variables

View Source
var ErrUnsupportedDialect = errors.New("sqlstore: unsupported dialect")

ErrUnsupportedDialect is returned for a dialect without embedded migrations (anything but sqlite, postgres, and mysql).

Functions

func Migrate

func Migrate(ctx context.Context, db *sql.DB, d sqlcommon.Dialect) ([]int, error)

Migrate applies every pending DDM migration and returns the versions applied.

func MigrationSet

func MigrationSet(d sqlcommon.Dialect) (sqlcommon.MigrationSet, error)

MigrationSet returns the DDM migrations for the dialect.

func Rollback

func Rollback(ctx context.Context, db *sql.DB, d sqlcommon.Dialect, target int) ([]int, error)

Rollback reverts DDM migrations newer than target (0 reverts all).

func Version

func Version(ctx context.Context, db *sql.DB, d sqlcommon.Dialect) (int, error)

Version returns the highest applied DDM migration (0 when none).

Types

type Options

type Options struct {
	// SkipMigrate leaves the schema alone; the caller has run Migrate.
	SkipMigrate bool
}

Options tune Open.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store implements ddm.Store over a *sql.DB it does not own: closing the pool is the caller's job.

func Open

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

Open wraps an opened pool for the dialect and, unless o.SkipMigrate, applies pending migrations.

func (*Store) AddSetDeclaration

func (s *Store) AddSetDeclaration(ctx context.Context, set, identifier string, at time.Time) (changed bool, err error)

AddSetDeclaration implements ddm.SetStore.

func (*Store) AffectedEnrollments

func (s *Store) AffectedEnrollments(ctx context.Context, identifiers, sets []string) ([]mdm.EnrollmentID, error)

AffectedEnrollments implements ddm.AssignmentStore.

func (*Store) AssignDeclaration

func (s *Store) AssignDeclaration(ctx context.Context, id mdm.EnrollmentID, identifier string, at time.Time) (changed bool, err error)

AssignDeclaration implements ddm.AssignmentStore.

func (*Store) AssignSet

func (s *Store) AssignSet(ctx context.Context, id mdm.EnrollmentID, set string, at time.Time) (changed bool, err error)

AssignSet implements ddm.AssignmentStore.

func (*Store) ChangeStats

func (s *Store) ChangeStats(ctx context.Context, now time.Time) (pending, failed int64, err error)

ChangeStats implements ddm.ChangeStore.

func (*Store) ClearEnrollment

func (s *Store) ClearEnrollment(ctx context.Context, id mdm.EnrollmentID) error

ClearEnrollment implements ddm.Tx.

func (*Store) CompleteChanges

func (s *Store) CompleteChanges(ctx context.Context, seqs []int64) error

CompleteChanges implements ddm.ChangeStore.

func (*Store) DB

func (s *Store) DB() *sql.DB

DB exposes the pool for health checks and tests.

func (*Store) DeclarationSets

func (s *Store) DeclarationSets(ctx context.Context, identifier string) ([]string, error)

DeclarationSets implements ddm.SetStore.

func (*Store) DeclarationStatus

func (s *Store) DeclarationStatus(ctx context.Context, id mdm.EnrollmentID) ([]ddm.DeclarationStatus, error)

DeclarationStatus implements ddm.StatusStore.

func (*Store) DeclarationStatusByIdentifier

func (s *Store) DeclarationStatusByIdentifier(ctx context.Context, identifier string, p storage.Page) (storage.Result[ddm.EnrollmentDeclarationStatus], error)

DeclarationStatusByIdentifier implements ddm.StatusStore.

func (*Store) DeleteDeclaration

func (s *Store) DeleteDeclaration(ctx context.Context, identifier string) error

DeleteDeclaration implements ddm.DeclarationStore.

func (*Store) DeleteSet

func (s *Store) DeleteSet(ctx context.Context, name string) error

DeleteSet implements ddm.SetStore.

func (*Store) EnrollmentDeclarations

func (s *Store) EnrollmentDeclarations(ctx context.Context, id mdm.EnrollmentID) ([]string, error)

EnrollmentDeclarations implements ddm.AssignmentStore.

func (*Store) EnrollmentSets

func (s *Store) EnrollmentSets(ctx context.Context, id mdm.EnrollmentID) ([]string, error)

EnrollmentSets implements ddm.AssignmentStore.

func (*Store) FailChanges

func (s *Store) FailChanges(ctx context.Context, seqs []int64, msg string, nextAttempt time.Time) error

FailChanges implements ddm.ChangeStore.

func (*Store) GetDeclaration

func (s *Store) GetDeclaration(ctx context.Context, identifier string) (*ddm.Declaration, error)

GetDeclaration implements ddm.DeclarationStore.

func (*Store) GetDeclarationVersion

func (s *Store) GetDeclarationVersion(ctx context.Context, identifier, serverToken string) (*ddm.DeclarationVersion, error)

GetDeclarationVersion implements ddm.DeclarationStore.

func (*Store) GetSet

func (s *Store) GetSet(ctx context.Context, name string) (*ddm.Set, error)

GetSet implements ddm.SetStore.

func (*Store) ListDeclarations

func (s *Store) ListDeclarations(ctx context.Context, q ddm.DeclarationQuery, p storage.Page) (storage.Result[ddm.Declaration], error)

ListDeclarations implements ddm.DeclarationStore.

func (*Store) ListSets

func (s *Store) ListSets(ctx context.Context, p storage.Page) (storage.Result[ddm.Set], error)

ListSets implements ddm.SetStore.

func (*Store) PendingChanges

func (s *Store) PendingChanges(ctx context.Context, now time.Time, limit int) ([]ddm.Change, error)

PendingChanges implements ddm.ChangeStore.

func (*Store) PruneVersions

func (s *Store) PruneVersions(ctx context.Context) (int64, error)

PruneVersions implements ddm.DeclarationStore.

func (*Store) PutDeclaration

func (s *Store) PutDeclaration(ctx context.Context, d *ddm.Declaration) (changed bool, err error)

PutDeclaration implements ddm.DeclarationStore.

func (*Store) PutSet

func (s *Store) PutSet(ctx context.Context, name string, at time.Time) (created bool, err error)

PutSet implements ddm.SetStore.

func (*Store) PutSnapshot

func (s *Store) PutSnapshot(ctx context.Context, snap *ddm.Snapshot) error

PutSnapshot implements ddm.SnapshotStore.

func (*Store) PutStatus

func (s *Store) PutStatus(ctx context.Context, id mdm.EnrollmentID, u ddm.StatusUpdate) (out ddm.StatusOutcome, err error)

PutStatus implements ddm.StatusStore.

func (*Store) RecordChanges

func (s *Store) RecordChanges(ctx context.Context, ids []mdm.EnrollmentID, reason string, at time.Time) error

RecordChanges implements ddm.ChangeStore.

func (*Store) RemoveSetDeclaration

func (s *Store) RemoveSetDeclaration(ctx context.Context, set, identifier string) (changed bool, err error)

RemoveSetDeclaration implements ddm.SetStore.

func (*Store) SetDeclarations

func (s *Store) SetDeclarations(ctx context.Context, set string) ([]string, error)

SetDeclarations implements ddm.SetStore.

func (*Store) SetEnrollments

func (s *Store) SetEnrollments(ctx context.Context, set string, p storage.Page) (storage.Result[mdm.EnrollmentID], error)

SetEnrollments implements ddm.AssignmentStore.

func (*Store) Snapshot

func (s *Store) Snapshot(ctx context.Context, id mdm.EnrollmentID) (*ddm.Snapshot, error)

Snapshot implements ddm.SnapshotStore.

func (*Store) StaticDeclarations

func (s *Store) StaticDeclarations(ctx context.Context, id mdm.EnrollmentID) ([]ddm.Declaration, error)

StaticDeclarations implements ddm.AssignmentStore.

func (*Store) StatusErrors

func (s *Store) StatusErrors(ctx context.Context, id mdm.EnrollmentID, p storage.Page) (storage.Result[ddm.StatusError], error)

StatusErrors implements ddm.StatusStore.

func (*Store) StatusReports

StatusReports implements ddm.StatusStore.

func (*Store) StatusValues

StatusValues implements ddm.StatusStore.

func (*Store) UnassignDeclaration

func (s *Store) UnassignDeclaration(ctx context.Context, id mdm.EnrollmentID, identifier string) (bool, error)

UnassignDeclaration implements ddm.AssignmentStore.

func (*Store) UnassignSet

func (s *Store) UnassignSet(ctx context.Context, id mdm.EnrollmentID, set string) (bool, error)

UnassignSet implements ddm.AssignmentStore.

func (*Store) Update

func (s *Store) Update(ctx context.Context, fn func(ddm.Tx) error) error

Update implements ddm.Store. fn runs in one transaction that commits when fn returns nil and rolls back otherwise. fn must use the Tx it is given; the Store's own methods would run outside the transaction.

Jump to

Keyboard shortcuts

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