dsmigrate

package module
v0.7.44 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: Apache-2.0 Imports: 9 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package dsmigrate upgrades Datastore entities to the current schema while the app keeps serving, instead of in one offline rewrite.

Each kind gets a Schema: its current version and one Upgrader per older version. An entity carries its version in the schema_version property, and an entity without one is version 0.

Picture a user who last logged in under an old build. Their Identity entity is version 0. The new build reads it through Get, which runs the upgraders from 0 up to the current version and loads the result into the caller's struct. Get then writes the upgraded entity back, but only if the stored entity is still exactly what it read, so a concurrent write from another instance, or from an old build still serving, is never overwritten. Put stamps the current version on every write, so new entities never look old.

Reads alone never reach an entity that nobody reads, and a query that filters on a field an upgrader adds can't find an entity that hasn't been upgraded yet. Backfill walks a whole kind and upgrades what it finds, in batches, while the app serves. It resumes from a cursor, and rerunning it changes nothing.

The module depends only on cloud.google.com/go/datastore, so libraries that keep entities in Datastore (oneauth's stores, goapplib's content services) can use it without pulling in goapplib. It shares goapplib's version and is tagged dsmigrate/vX.Y.Z on the same commit as vX.Y.Z.

Index

Constants

View Source
const VersionProperty = "schema_version"

VersionProperty is the property that holds an entity's schema version. An entity without it is version 0, which is how entities written before a kind had a Schema look.

Variables

This section is empty.

Functions

func Get

func Get(ctx context.Context, client *datastore.Client, key *datastore.Key, dst any, reg *Registry) error

Get loads the entity at key into dst, a pointer to a struct, upgrading it to the current schema on the way. When it upgraded, it writes the result back if the stored entity hasn't changed since the read (see WriteBack). A failed write-back doesn't fail the read; it goes to reg.OnWriteBackError.

It returns datastore.ErrNoSuchEntity for a missing entity, like client.Get.

func GetAll

func GetAll(ctx context.Context, client *datastore.Client, q *datastore.Query, dst any, reg *Registry) ([]*datastore.Key, error)

GetAll runs q and loads the results into dst, a pointer to a slice of structs or of struct pointers, upgrading each one as Get does. q must not be keys-only. Each upgraded entity is written back on its own, under the same check as Get.

An entity that hasn't been upgraded yet can't match a filter on a property its upgrader adds. Run Backfill before relying on such a query.

func Put

func Put(ctx context.Context, client *datastore.Client, key *datastore.Key, src any, reg *Registry) (*datastore.Key, error)

Put saves src, a pointer to a struct or a datastore.PropertyLoadSaver, at key with the kind's current version stamped on, so the entity is never treated as old. Inside a transaction, use reg.Save and tx.Put instead.

func Version

func Version(props datastore.PropertyList) int

Version returns the entity's schema version: VersionProperty as an integer, or 0 when it's missing or not an integer.

func WriteBack

func WriteBack(ctx context.Context, client *datastore.Client, key *datastore.Key, original, upgraded datastore.PropertyList) (written bool, err error)

WriteBack stores upgraded at key in a transaction, but only if the stored entity still equals original, the properties the upgrade started from. It reports written=false when the entity changed or vanished meanwhile: another instance upgraded it first, or something wrote a newer copy. That copy wins, and the next read upgrades it if it still needs it.

Types

type BackfillOptions

type BackfillOptions struct {
	// Namespace to scan. Empty is the default namespace.
	Namespace string
	// BatchSize is how many entities each query page reads. Zero means 100.
	BatchSize int
	// Limit stops the run after scanning this many entities, with a Cursor to resume from. Zero
	// scans the whole kind.
	Limit int
	// Cursor resumes a run from BackfillReport.Cursor. Empty starts at the beginning.
	Cursor string
	// DryRun counts the stale entities without writing anything.
	DryRun bool
}

BackfillOptions tunes a Backfill run.

type BackfillReport

type BackfillReport struct {
	Scanned  int // entities read
	Stale    int // entities below the kind's current version
	Upgraded int // stale entities written back (0 on a dry run)
	Skipped  int // stale entities that changed during the run and were left for the next pass
	// Cursor resumes the scan where this run stopped. It's empty when the run reached the end.
	Cursor string
}

BackfillReport says what a Backfill run did.

func Backfill

func Backfill(ctx context.Context, client *datastore.Client, kind string, reg *Registry, opts BackfillOptions) (BackfillReport, error)

Backfill upgrades every stale entity of kind, page by page, while the app keeps serving. Each write goes through WriteBack, so it never overwrites a concurrent write, and a rerun finds nothing left to do. A run cut short by Limit, an error or a cancelled context reports a Cursor to resume from.

type Registry

type Registry struct {
	// IgnoreFieldMismatch drops a *datastore.ErrFieldMismatch from loads, the way a client built
	// with datastore.WithIgnoreFieldMismatch does. Set it when the stores using this registry run
	// on such a client, or legacy properties that no struct field takes will fail their reads.
	IgnoreFieldMismatch bool

	// OnWriteBackError, if set, hears about an upgrade a read couldn't write back. The read still
	// succeeds with the upgraded entity, and the next read or a Backfill tries the write again.
	OnWriteBackError func(key *datastore.Key, err error)
	// contains filtered or unexported fields
}

Registry holds the Schema for each kind. A kind without one passes through unchanged: Get, GetAll and Put then behave like the plain client calls, apart from field mismatch handling.

func NewRegistry

func NewRegistry(schemas ...Schema) (*Registry, error)

NewRegistry validates and registers the schemas. It fails on an invalid Schema or a kind registered twice.

func (*Registry) Load

func (r *Registry) Load(key *datastore.Key, props datastore.PropertyList, dst any) (upgraded datastore.PropertyList, changed bool, err error)

Load upgrades props, the properties stored at key, and loads them into dst, a pointer to a struct or a datastore.PropertyLoadSaver. The kind's Schema comes from key.Kind. It reports whether an upgrade ran, so the caller can write the result back. It doesn't write anything itself, which makes it the call to use inside a transaction.

Load fills dst's key the way client.Get does. A struct field tagged datastore:"__key__" gets key, and a dst that implements datastore.KeyLoader gets LoadKey(key) instead. key must not be nil.

func (*Registry) Register

func (r *Registry) Register(s Schema) error

Register validates and adds a Schema. It fails on an invalid Schema or a kind registered twice.

func (*Registry) Save

func (r *Registry) Save(kind string, src any) (datastore.PropertyList, error)

Save turns src, a pointer to a struct or a datastore.PropertyLoadSaver, into properties with the kind's current version stamped on. A kind without a Schema gets no version.

func (*Registry) Schema

func (r *Registry) Schema(kind string) (Schema, bool)

Schema returns the kind's Schema, if one is registered. A nil Registry has none.

func (*Registry) Upgrade

func (r *Registry) Upgrade(kind string, props datastore.PropertyList) (datastore.PropertyList, bool, error)

Upgrade runs the kind's Schema over props. A kind without one, or a nil Registry, returns props unchanged.

type Schema

type Schema struct {
	Kind     string
	Current  int
	Upgrades map[int]Upgrader
}

Schema describes one kind's versions. Upgrades[n] turns version n into n+1, so a Schema at Current 2 needs Upgrades[0] and Upgrades[1].

func (Schema) Upgrade

func (s Schema) Upgrade(props datastore.PropertyList) (out datastore.PropertyList, changed bool, err error)

Upgrade runs the upgraders from the entity's version up to Current and reports whether any ran. An entity at Current is returned as is. So is one above Current, which a newer build wrote: an older build can't know that schema, so it leaves the entity alone rather than rewrite it.

func (Schema) Validate

func (s Schema) Validate() error

Validate reports a Schema that can't upgrade every version below Current.

type Upgrader

type Upgrader func(props datastore.PropertyList) (datastore.PropertyList, error)

Upgrader turns the properties of an entity at one version into the next version's. It may add, rename, drop or rewrite properties, and must not change the key. It doesn't set VersionProperty; Schema.Upgrade does that after each step.

Jump to

Keyboard shortcuts

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