migrate

package
v0.0.0-...-e7c82bc Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2024 License: Apache-2.0 Imports: 12 Imported by: 22

Documentation

Overview

Copyright 2023 Northern.tech AS

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Copyright 2023 Northern.tech AS

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Copyright 2023 Northern.tech AS

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Copyright 2023 Northern.tech AS

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Copyright 2023 Northern.tech AS

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Copyright 2023 Northern.tech AS

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Index

Constants

View Source
const (
	DbMigrationsColl = "migration_info"
)

this is a small internal data layer for the migration utils, may be shared by diff migrators

Variables

View Source
var (
	ErrNeedsMigration = "db needs migration"
)

Functions

func GetTenantDbs

func GetTenantDbs(ctx context.Context, client *mongo.Client, matcher store.TenantDbMatchFunc) ([]string, error)

func IsErrNeedsMigration

func IsErrNeedsMigration(e error) bool

func UpdateMigrationInfo

func UpdateMigrationInfo(ctx context.Context, version Version, sess *mongo.Client, db string) error

UpdateMigrationInfo inserts a migration entry in the migration info collection.

func VersionIsLess

func VersionIsLess(left Version, right Version) bool

Types

type DummyMigrator

type DummyMigrator struct {
	Client      *mongo.Client
	Db          string
	Automigrate bool
}

MigratorDummy does not actually apply migrations, just inserts the target version into the db to mark the initial/current state.

func (*DummyMigrator) Apply

func (m *DummyMigrator) Apply(ctx context.Context, target Version, migrations []Migration) error

Apply makes MigratorDummy implement the Migrator interface.

type Migration

type Migration interface {
	Up(from Version) error
	Version() Version
}

Migration defines an incremental mongo migration step, with a concrete version. Current version of DB is passed as `from` parameter. Once migration completes, DB will be in version Version().

type MigrationEntry

type MigrationEntry struct {
	Version   Version   `bson:"version"`
	Timestamp time.Time `bson:"timestamp"`
}

func GetMigrationInfo

func GetMigrationInfo(ctx context.Context, sess *mongo.Client, db string) ([]MigrationEntry, error)

GetMigrationInfo retrieves a list of migrations applied to the db. On success the function returns the MigrationEntries present in the db sorted by the version in decending order.

type Migrator

type Migrator interface {
	Apply(ctx context.Context, target Version, migrations []Migration) error
}

Migrator applies a list of migrations to bring the db up to target version.

type SimpleMigrator

type SimpleMigrator struct {
	Client      *mongo.Client
	Db          string
	Automigrate bool
}

SimpleMigratior applies migrations by comparing `Version` of migrations passed to Apply() and already applied migrations. Only migrations that are of version higher than the last applied migration will be run. For example:

already applied migrations: 1.0.0, 1.0.1, 1.0.2
migrations in Apply(): 1.0.1, 1.0.3, 1.1.0
migrations that will be applied: 1.0.3, 1.1.0

func (*SimpleMigrator) Apply

func (m *SimpleMigrator) Apply(ctx context.Context, target Version, migrations []Migration) error

Apply will apply migrations, provided that Automigrate is on. After each successful migration a new migration record will be added to DB with the version of migration that was just applied. If a migration fails, Apply() returns an error and does not add a migration record (so last migration that is recorded is N-1).

Apply() will log some messages when running. Logger will be extracted from context using go-lib-micro/log.LoggerContextKey as key. If Automigrate is off, the migrator will just check if the DB is up-to-date, and return with ErrNeedsMigration otherwise. Check for it with IsErrNeedsMigration.

type Version

type Version struct {
	Major uint `bson:"major"`
	Minor uint `bson:"minor"`
	Patch uint `bson:"patch"`
}

func MakeVersion

func MakeVersion(maj uint, min uint, patch uint) Version

func NewVersion

func NewVersion(s string) (*Version, error)

func (Version) String

func (v Version) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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