Skip to main content

Migrate

Migrate applies all unapplied SQL migration files from the provided fs.FS. Migration files are expected to be located under "sql/" and follow the naming convention "NNNN_description.sql". Files ending with "_down.sql" are skipped. Migrations are applied in lexicographic order, each within its own transaction. The prefix parameter namespaces versions in the shared schema_migrations table so that multiple services sharing the same database do not collide (e.g. prefix "runs" records version "runs/001_init_schema").

def Migrate(
ctx: context.Context,
db: *sqlx.DB,
prefix: string,
migrations: fs.FS
) - > error

Migrate applies all unapplied SQL migration files from the provided fs.FS. Migration files are expected to be located under "sql/" and follow the naming convention "NNNN_description.sql". Files ending with "_down.sql" are skipped. Migrations are applied in lexicographic order, each within its own transaction. The prefix parameter namespaces versions in the shared schema_migrations table so that multiple services sharing the same database do not collide (e.g. prefix "runs" records version "runs/001_init_schema").

Parameters

NameTypeDescription
ctxcontext.ContextThe context for the operation, allowing for cancellation and timeouts.
db*sqlx.DBThe database connection pool to apply migrations to.
prefixstringA string prefix used to namespace migration versions in the schema_migrations table, preventing collisions between multiple services sharing the same database.
migrationsfs.FSThe file system containing the SQL migration files, expected to be under the "sql/" directory.

Returns

TypeDescription
errorAn error if any step of the migration process fails, otherwise nil.