// To attempts to migrate the database to the target version, reading from current version from the predefined key
// and applying migrations as need to reach the target version
// it returns the final database version (ideally = target) and any errors encountered
To attempts to migrate the database to the target version, reading from current version from the predefined key
and applying migrations as need to reach the target version
it returns the final database version (ideally = target) and any errors encountered
// To attempts to migrate the database to the target version reading from current version from the predefined key // and applying migrations as need to reach the target version // it returns the final database version ( ideally = target ) and any errors encountered
func To(ctx context.Context, ds datastore.Batching, migrations versioning.VersionedMigrationList, to versioning.VersionKey) (versioning.VersionKey, error) {
	sort.Sort(migrations)
	if !verifyIntegrity(migrations) {
		return versioning.VersionKey(""), fmt.Errorf("migrations list must be contiguous")
	}
	verBytes, err := ds.Get(ctx, versioningKey)
	if err == datastore.ErrNotFound {
		hasData, err := notEmpty(ds)
		if err != nil {
			return versioning.VersionKey(""), fmt.Errorf("determining if store has data: %w", err)
		}
		if hasData {
			if migrations[0].OldVersion() != versioning.VersionKey("") {
				return versioning.VersionKey(""), errors.New("cannot migrate from an unversioned database")
			}
			verBytes = []byte("")
		} else {
			err = ds.Put(ctx, versioningKey, []byte(to))
			if err != nil {
				return versioning.VersionKey(""), fmt.Errorf("writing version: %w", err)
			}
			return to, nil
		}
	} else if err != nil {
		return versioning.VersionKey(""), fmt.Errorf("reading version: %w", err)
	}
	currentVersion := versioning.VersionKey(verBytes)
	final, err := runMigrations(ctx, ds, migrations, currentVersion, to)
	ferr := ds.Put(ctx, versioningKey, []byte(final))
	if err != nil {
		return final, err
	}
	return final, ferr
}
func To ( ctx context . Context , ds datastore . Batching , migrations versioning . VersionedMigrationList , to versioning . VersionKey ) ( versioning . VersionKey , error ) { sort . Sort ( migrations ) if ! verifyIntegrity ( migrations ) { return versioning . VersionKey ( " " ) , fmt . Errorf ( " " ) } verBytes , err := ds . Get ( ctx , versioningKey ) if err == datastore . ErrNotFound { hasData , err := notEmpty ( ds ) if err != nil { return versioning . VersionKey ( " " ) , fmt . Errorf ( " " , err ) } if hasData { if migrations [ 0 ] . OldVersion ( ) != versioning . VersionKey ( " " ) { return versioning . VersionKey ( " " ) , errors . New ( " " ) } verBytes = [ ] byte ( " " ) } else { err = ds . Put ( ctx , versioningKey , [ ] byte ( to ) ) if err != nil { return versioning . VersionKey ( " " ) , fmt . Errorf ( " " , err ) } return to , nil } } else if err != nil { return versioning . VersionKey ( " " ) , fmt . Errorf ( " " , err ) } currentVersion := versioning . VersionKey ( verBytes ) final , err := runMigrations ( ctx , ds , migrations , currentVersion , to ) ferr := ds . Put ( ctx , versioningKey , [ ] byte ( final ) ) if err != nil { return final , err } return final , ferr }
To attempts to migrate the database to the target version, reading from current version from the predefined key
and applying migrations as need to reach the target version
it returns the final database version (ideally = target) and any errors encountered
To attempts to migrate the database to the target version reading from current version from the predefined key and applying migrations as need to reach the target version it returns the final database version ( ideally = target ) and any errors encountered
// empty database -- we'll treat it as ready to go after writing current version