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
- Variables
- func Migrate(ctx context.Context, db *sql.DB, d sqlcommon.Dialect) ([]int, error)
- func MigrationSet(d sqlcommon.Dialect) (sqlcommon.MigrationSet, error)
- func Rollback(ctx context.Context, db *sql.DB, d sqlcommon.Dialect, target int) ([]int, error)
- func Version(ctx context.Context, db *sql.DB, d sqlcommon.Dialect) (int, error)
- type Options
- type Store
- func (s *Store) AddSetDeclaration(ctx context.Context, set, identifier string, at time.Time) (changed bool, err error)
- func (s *Store) AffectedEnrollments(ctx context.Context, identifiers, sets []string) ([]mdm.EnrollmentID, error)
- func (s *Store) AssignDeclaration(ctx context.Context, id mdm.EnrollmentID, identifier string, at time.Time) (changed bool, err error)
- func (s *Store) AssignSet(ctx context.Context, id mdm.EnrollmentID, set string, at time.Time) (changed bool, err error)
- func (s *Store) ChangeStats(ctx context.Context, now time.Time) (pending, failed int64, err error)
- func (s *Store) ClearEnrollment(ctx context.Context, id mdm.EnrollmentID) error
- func (s *Store) CompleteChanges(ctx context.Context, seqs []int64) error
- func (s *Store) DB() *sql.DB
- func (s *Store) DeclarationSets(ctx context.Context, identifier string) ([]string, error)
- func (s *Store) DeclarationStatus(ctx context.Context, id mdm.EnrollmentID) ([]ddm.DeclarationStatus, error)
- func (s *Store) DeclarationStatusByIdentifier(ctx context.Context, identifier string, p storage.Page) (storage.Result[ddm.EnrollmentDeclarationStatus], error)
- func (s *Store) DeleteDeclaration(ctx context.Context, identifier string) error
- func (s *Store) DeleteSet(ctx context.Context, name string) error
- func (s *Store) EnrollmentDeclarations(ctx context.Context, id mdm.EnrollmentID) ([]string, error)
- func (s *Store) EnrollmentSets(ctx context.Context, id mdm.EnrollmentID) ([]string, error)
- func (s *Store) FailChanges(ctx context.Context, seqs []int64, msg string, nextAttempt time.Time) error
- func (s *Store) GetDeclaration(ctx context.Context, identifier string) (*ddm.Declaration, error)
- func (s *Store) GetDeclarationVersion(ctx context.Context, identifier, serverToken string) (*ddm.DeclarationVersion, error)
- func (s *Store) GetSet(ctx context.Context, name string) (*ddm.Set, error)
- func (s *Store) ListDeclarations(ctx context.Context, q ddm.DeclarationQuery, p storage.Page) (storage.Result[ddm.Declaration], error)
- func (s *Store) ListSets(ctx context.Context, p storage.Page) (storage.Result[ddm.Set], error)
- func (s *Store) PendingChanges(ctx context.Context, now time.Time, limit int) ([]ddm.Change, error)
- func (s *Store) PruneVersions(ctx context.Context) (int64, error)
- func (s *Store) PutDeclaration(ctx context.Context, d *ddm.Declaration) (changed bool, err error)
- func (s *Store) PutSet(ctx context.Context, name string, at time.Time) (created bool, err error)
- func (s *Store) PutSnapshot(ctx context.Context, snap *ddm.Snapshot) error
- func (s *Store) PutStatus(ctx context.Context, id mdm.EnrollmentID, u ddm.StatusUpdate) (out ddm.StatusOutcome, err error)
- func (s *Store) RecordChanges(ctx context.Context, ids []mdm.EnrollmentID, reason string, at time.Time) error
- func (s *Store) RemoveSetDeclaration(ctx context.Context, set, identifier string) (changed bool, err error)
- func (s *Store) SetDeclarations(ctx context.Context, set string) ([]string, error)
- func (s *Store) SetEnrollments(ctx context.Context, set string, p storage.Page) (storage.Result[mdm.EnrollmentID], error)
- func (s *Store) Snapshot(ctx context.Context, id mdm.EnrollmentID) (*ddm.Snapshot, error)
- func (s *Store) StaticDeclarations(ctx context.Context, id mdm.EnrollmentID) ([]ddm.Declaration, error)
- func (s *Store) StatusErrors(ctx context.Context, id mdm.EnrollmentID, p storage.Page) (storage.Result[ddm.StatusError], error)
- func (s *Store) StatusReports(ctx context.Context, id mdm.EnrollmentID, p storage.Page) (storage.Result[ddm.StatusReportRecord], error)
- func (s *Store) StatusValues(ctx context.Context, id mdm.EnrollmentID, q ddm.StatusValueQuery, ...) (storage.Result[ddm.StatusValue], error)
- func (s *Store) UnassignDeclaration(ctx context.Context, id mdm.EnrollmentID, identifier string) (bool, error)
- func (s *Store) UnassignSet(ctx context.Context, id mdm.EnrollmentID, set string) (bool, error)
- func (s *Store) Update(ctx context.Context, fn func(ddm.Tx) error) error
Constants ¶
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 ¶
var ErrUnsupportedDialect = errors.New("sqlstore: unsupported dialect")
ErrUnsupportedDialect is returned for a dialect without embedded migrations (anything but sqlite, postgres, and mysql).
Functions ¶
func MigrationSet ¶
func MigrationSet(d sqlcommon.Dialect) (sqlcommon.MigrationSet, error)
MigrationSet returns the DDM migrations for the dialect.
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 ¶
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 ¶
ChangeStats implements ddm.ChangeStore.
func (*Store) ClearEnrollment ¶
ClearEnrollment implements ddm.Tx.
func (*Store) CompleteChanges ¶
CompleteChanges implements ddm.ChangeStore.
func (*Store) DeclarationSets ¶
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 ¶
DeleteDeclaration implements ddm.DeclarationStore.
func (*Store) EnrollmentDeclarations ¶
EnrollmentDeclarations implements ddm.AssignmentStore.
func (*Store) EnrollmentSets ¶
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 ¶
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) 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) PendingChanges ¶
PendingChanges implements ddm.ChangeStore.
func (*Store) PruneVersions ¶
PruneVersions implements ddm.DeclarationStore.
func (*Store) PutDeclaration ¶
PutDeclaration implements ddm.DeclarationStore.
func (*Store) PutSnapshot ¶
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 ¶
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) 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 ¶
func (s *Store) StatusReports(ctx context.Context, id mdm.EnrollmentID, p storage.Page) (storage.Result[ddm.StatusReportRecord], error)
StatusReports implements ddm.StatusStore.
func (*Store) StatusValues ¶
func (s *Store) StatusValues(ctx context.Context, id mdm.EnrollmentID, q ddm.StatusValueQuery, p storage.Page) (storage.Result[ddm.StatusValue], error)
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 ¶
UnassignSet implements ddm.AssignmentStore.