migration

package
v0.0.0-...-ec844c3 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ExtensionMigrations embed.FS
View Source
var FossMigrations embed.FS

Functions

This section is empty.

Types

type Manifest

type Manifest struct {
	VersionTable []string
	Migrations   map[string][]Migration
}

Manifest is a collection of available migrations. VersionTable is used to order and store all version of migrations contained in the manifest. Migrations is the actual underlying map of [version:[]migration]

func NewManifest

func NewManifest() Manifest

NewManifest creates a new Manifest and initializes the migrations map

func (*Manifest) AddMigration

func (s *Manifest) AddMigration(migration Migration)

AddMigration will add migrations to the correct location in the migrations map, with care to make sure the map is initialized, as well as the version location within the map.

type Migration

type Migration struct {
	Filename string
	Source   fs.FS
	Version  version.Version
}

Migration contains information about a specific migration such as the file location, it's Source, and Version.

type Migrator

type Migrator struct {
	Sources        []Source
	ExtensionsData []Source
	DB             *gorm.DB
}

Migrator is the main SQL migration tool for BloodHound.

func NewMigrator

func NewMigrator(db *gorm.DB) *Migrator

NewMigrator returns a new Migrator with the FossMigrations Source predefined.

func (*Migrator) CreateMigrationSchema

func (s *Migrator) CreateMigrationSchema() error

CreateMigrationSchema creates all the necessary SQL schema for tracking migration status.

func (*Migrator) ExecuteExtensionDataPopulation

func (s *Migrator) ExecuteExtensionDataPopulation() error

func (*Migrator) ExecuteMigrations

func (s *Migrator) ExecuteMigrations(manifest Manifest) error

ExecuteMigrations takes a Manifest and runs all migrations contained in it, by version. It starts by ranging over the Manifest.VersionTable, which is an ordered list of all versions covered in the Manifest. Each version entry in the Manifest map may have multiple migrations from different sources. To ensure consistency, all migrations for each version are run in a transaction, as well as logging the successful migration entry in the `migrations` table.

func (*Migrator) ExecuteStepwiseMigrations

func (s *Migrator) ExecuteStepwiseMigrations() error

ExecuteStepwiseMigrations will run all necessary migrations for a deployment. It begins by checking if migration schema exists. If it does not, we assume the deployment is a new installation, otherwise we assume it may have migration updates.

If the deployment is new, we install migration schema to track migrations and then build a manifest of all existing migrations

If the deployment already existed, we query for the last successful migration run and then build a manifest starting after the last successful version

Once schema is verified and a manifest is created, we run ExecuteMigrations.

func (*Migrator) GenerateManifest

func (s *Migrator) GenerateManifest() (Manifest, error)

GenerateManifest is a wrapper around GenerateManifestAfterVersion, using -1.-1.-1 as the version. This ensures that a full manifest of all available migrations is generated. This is most useful for new installations.

func (*Migrator) GenerateManifestAfterVersion

func (s *Migrator) GenerateManifestAfterVersion(lastVersion version.Version) (Manifest, error)

GenerateManifestAfterVersion takes a version.Version argument and uses that to generate a manifest from the available migration Source's. It will loop through sources and build Migration's from the available migration files. Files labeled `schema` are considered the initial migration and get versioned as v0.0.0. All valid migrations that are versioned after the version given will be added to the manifest for migration. The final step is the build and sort the VersionTable that will be used for applying the migration in order by ExecuteMigrations.

func (*Migrator) HasMigrationTable

func (s *Migrator) HasMigrationTable() (bool, error)

HasMigrationTable is a utility for checking if migration schema is initialized. We assume that if the `migrations` table exists, the schema must be initialized, and vice versa.

func (*Migrator) LatestMigration

func (s *Migrator) LatestMigration() (model.Migration, error)

LatestMigration retrieves the last entry in the migration table to determine what the last successful migration version was.

func (*Migrator) RequiresMigration

func (s *Migrator) RequiresMigration() (bool, error)

type Source

type Source struct {
	FileSystem fs.FS
	Directory  string
}

Source is meant to be a file system source that contains SQL migration files.

Jump to

Keyboard shortcuts

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