dnmig1

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2024 License: MPL-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package dnmig1 provides a downwards database schema Migrator type for major version 1 and its corresponding Adapter type which can adapt it to the version independent repo.DownMigrator[repo.SchemaSettler] interface.

Each downwards schema migrator package, but the first major version which has no older major version, may contain (and embed) a down.sql file in order to migrate from its own major version to the previous major version, creating the relevant views.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Adapter

type Adapter struct {
	T Type
}

Adapter wraps a Type interface instance and adapts it to the version-independent repo.DownMigrator[repo.SchemaSettler] interface.

func (Adapter) MigrateDown

func (a Adapter) MigrateDown(ctx context.Context) (
	repo.DownMigrator[repo.SchemaSettler], error,
)

MigrateDown adapts the MigrateDown method of the wrapped `a.T` instance, so its return value can be provided as a version-independent repo.DownMigrator[repo.SchemaSettler] interface instead of the version-dependent *Migrator type. Any returned error is produced by the underlying MigrateDown method.

func (Adapter) Settler

func (a Adapter) Settler() repo.SchemaSettler

Settler adapts the Settler method of the wrapped `a.T` instance, so its return value can be provided as a version-independent repo.SchemaSettler interface instead of the version-dependent S type.

type Migrator

type Migrator struct {
	Tx repo.Tx // a transaction of the destination database
}

Migrator provides a database schema downwards migrator which wraps a transaction of the destination database, assumes that it contains a filled schema, namely mig1, which contains the major version 1 database schema views (or tables), and allows downwards major version migrations by implementing the Type generic interface. It also implements the pkg/adapter/db/postgres/migration/schi.DnMigAdapter interface, so it may be adapted to a version-independent interface. This adaptation may not be implemented by a generic adapter struct because each downwards migrator Type may depend on a long sequence of migrator types belonging to the older major versions, while Golang does not support variadic type parameters.

func (*Migrator) Adapt

func (dnmig1 *Migrator) Adapt() repo.DownMigrator[repo.SchemaSettler]

Adapt creates an instance of the Adapter struct and wraps `dnmig1` object in order to adapt it to the version-independent repo.DownMigrator[repo.SchemaSettler] interface.

This method makes the Migrator to implement the pkg/adapter/db/postgres/migration/schi.DnMigAdapter interface. It is not required to explicitly test that Migrator implements the DnMigAdapter interface (in a test file) because downwards migrator types are only used by high-level migrator types (in schXvY packages) which implement the pkg/adapter/db/postgres/migration/schi.Migrator generic interface themselves and so guarantee that downwards migrators actually implement the schi.DnMigAdapter interface. Generally, it is preferred to have an actual code in order to ensure that an interface is implemented instead of a syntactic test code. Ensuring such an implementation in test codes is only justified when even a wrong implementation may be taken at compile-time (since its type is so general which may be passed instead of another struct) and so a test code is required for catching bugs before running the main non-test code. For example, asserting that Migrator implements the Type type is required because its users only work with the version independent interface which is returned by this function. By the way, we do not have to write that test code since the Adapter type is wrapping the Migrator as an instance of the Type type. If we detected a performance issue (by runtime profiling or benchmarks), it is possible to update Adapter in order to wrap a *Migrator directly and move its Type implementation assertion to a test file.

func (*Migrator) MigrateDown

func (dnmig1 *Migrator) MigrateDown(
	_ context.Context,
) (*Migrator, error)

MigrateDown migrates from the major version 1 to the previous major version by creating relevant views in a schema such as mig0 based on the views in a schema such as mig1 considering their latest supported minor versions. However, version 0 changes are not maintained and major version 1 is the first version which its schema changes are tracked by migrations. Therefore, this method always returns an error (and a nil migrator as the first return value).

func (*Migrator) Settler

func (dnmig1 *Migrator) Settler() *stlmig1.Settler

Settler returns a settler object (with S type) without performing any migration action (so, no error condition may arise). Returned settler object may be employed to persist the migration results. See the stlmig1.Settler type for more details.

type S

type S = *stlmig1.Settler

S is the schema settler type.

type Type

type Type = down.Migrator[S, *Migrator]

Type is provided by Migrator type.

Jump to

Keyboard shortcuts

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