Migrations¶
Native migration system for SQLSpec that leverages the SQL file loader and driver system for database versioning. Supports SQL and Python migration files, squashing, and validation.
Commands¶
- class sqlspec.migrations.SyncMigrationCommands[source]¶
Bases:
BaseMigrationCommands[SyncConfigT,Any]Synchronous migration commands.
- upgrade(revision='head', allow_missing=False, auto_sync=True, dry_run=False, *, use_logger=False, echo=None, summary_only=None)[source]¶
Upgrade to a target revision.
Validates migration order and warns if out-of-order migrations are detected. Out-of-order migrations can occur when branches merge in different orders across environments.
- Parameters:
allow_missing¶ (
bool) – If True, allow out-of-order migrations even in strict mode. Defaults to False.auto_sync¶ (
bool) – If True, automatically reconcile renamed migrations in database. Defaults to True. Can be disabled via –no-auto-sync flag.dry_run¶ (
bool) – If True, show what would be done without making changes.use_logger¶ (
bool) – If True, output to logger instead of Rich console. Defaults to False. Can be set via MigrationConfig for persistent default.echo¶ (
bool|None) – Echo output to the console. Defaults to True when unset.summary_only¶ (
bool|None) – Emit a single summary log entry when logger output is enabled.
- Return type:
- downgrade(revision='-1', *, dry_run=False, use_logger=False, echo=None, summary_only=None)[source]¶
Downgrade to a target revision.
- Parameters:
revision¶ (
str) – Target revision or “-1” for one step back.dry_run¶ (
bool) – If True, show what would be done without making changes.use_logger¶ (
bool) – If True, output to logger instead of Rich console. Defaults to False. Can be set via MigrationConfig for persistent default.echo¶ (
bool|None) – Echo output to the console. Defaults to True when unset.summary_only¶ (
bool|None) – Emit a single summary log entry when logger output is enabled.
- Return type:
- revision(message, file_type=None)[source]¶
Create a new migration file with timestamp-based versioning.
Generates a unique timestamp version (YYYYMMDDHHmmss format) to avoid conflicts when multiple developers create migrations concurrently.
- squash(start_version=None, end_version=None, description=None, *, dry_run=False, update_database=True, yes=False, allow_gaps=False, output_format='sql')[source]¶
Squash a range of migrations into a single file.
Combines multiple sequential migrations into a single “release” migration. UP statements are merged in version order, DOWN statements in reverse order.
- Parameters:
start_version¶ (
str|None) – First version in the range to squash (inclusive). When None, defaults to the first sequential migration found.end_version¶ (
str|None) – Last version in the range to squash (inclusive). When None, defaults to the last sequential migration found.description¶ (
str|None) – Description for the squashed migration file. When None, prompts interactively.update_database¶ (
bool) – Update migration records in database.
- Raises:
SquashValidationError – If validation fails (invalid range, gaps, etc.).
- Return type:
- fix(dry_run=False, update_database=True, yes=False)[source]¶
Convert timestamp migrations to sequential format.
Implements hybrid versioning workflow where development uses timestamps and production uses sequential numbers. Creates backup before changes and provides rollback on errors.
- Parameters:
- Return type:
Examples
>>> commands.fix(dry_run=True) # Preview only >>> commands.fix(yes=True) # Auto-approve >>> commands.fix(update_database=False) # Files only
- class sqlspec.migrations.AsyncMigrationCommands[source]¶
Bases:
BaseMigrationCommands[AsyncConfigT,Any]Asynchronous migration commands.
- async upgrade(revision='head', allow_missing=False, auto_sync=True, dry_run=False, *, use_logger=False, echo=None, summary_only=None)[source]¶
Upgrade to a target revision.
Validates migration order and warns if out-of-order migrations are detected. Out-of-order migrations can occur when branches merge in different orders across environments.
- Parameters:
allow_missing¶ (
bool) – If True, allow out-of-order migrations even in strict mode. Defaults to False.auto_sync¶ (
bool) – If True, automatically reconcile renamed migrations in database. Defaults to True. Can be disabled via –no-auto-sync flag.dry_run¶ (
bool) – If True, show what would be done without making changes.use_logger¶ (
bool) – If True, output to logger instead of Rich console. Defaults to False. Can be set via MigrationConfig for persistent default.echo¶ (
bool|None) – Echo output to the console. Defaults to True when unset.summary_only¶ (
bool|None) – Emit a single summary log entry when logger output is enabled.
- Return type:
- async downgrade(revision='-1', *, dry_run=False, use_logger=False, echo=None, summary_only=None)[source]¶
Downgrade to a target revision.
- Parameters:
revision¶ (
str) – Target revision or “-1” for one step back.dry_run¶ (
bool) – If True, show what would be done without making changes.use_logger¶ (
bool) – If True, output to logger instead of Rich console. Defaults to False. Can be set via MigrationConfig for persistent default.echo¶ (
bool|None) – Echo output to the console. Defaults to True when unset.summary_only¶ (
bool|None) – Emit a single summary log entry when logger output is enabled.
- Return type:
- async stamp(revision)[source]¶
Mark database as being at a specific revision without running migrations.
- async revision(message, file_type=None)[source]¶
Create a new migration file with timestamp-based versioning.
Generates a unique timestamp version (YYYYMMDDHHmmss format) to avoid conflicts when multiple developers create migrations concurrently.
- async squash(start_version=None, end_version=None, description=None, *, dry_run=False, update_database=True, yes=False, allow_gaps=False, output_format='sql')[source]¶
Squash a range of migrations into a single file.
Combines multiple sequential migrations into a single “release” migration. UP statements are merged in version order, DOWN statements in reverse order.
- Parameters:
start_version¶ (
str|None) – First version in the range to squash (inclusive). When None, defaults to the first sequential migration found.end_version¶ (
str|None) – Last version in the range to squash (inclusive). When None, defaults to the last sequential migration found.description¶ (
str|None) – Description for the squashed migration file. When None, prompts interactively.update_database¶ (
bool) – Update migration records in database.
- Raises:
SquashValidationError – If validation fails (invalid range, gaps, etc.).
- Return type:
- async fix(dry_run=False, update_database=True, yes=False)[source]¶
Convert timestamp migrations to sequential format.
Implements hybrid versioning workflow where development uses timestamps and production uses sequential numbers. Creates backup before changes and provides rollback on errors.
- Parameters:
- Return type:
Examples
>>> await commands.fix(dry_run=True) # Preview only >>> await commands.fix(yes=True) # Auto-approve >>> await commands.fix(update_database=False) # Files only
Runners¶
- class sqlspec.migrations.SyncMigrationRunner[source]¶
Bases:
BaseMigrationRunnerSynchronous migration runner with pure sync methods.
- execute_upgrade(driver, migration, *, use_transaction=None, on_success=None)[source]¶
Execute an upgrade migration.
- Parameters:
- Returns:
Tuple of (sql_content, execution_time_ms).
- class sqlspec.migrations.AsyncMigrationRunner[source]¶
Bases:
BaseMigrationRunnerAsynchronous migration runner with pure async methods.
- async load_migration(file_path, version=None)[source]¶
Load a migration file and extract its components.
- async execute_upgrade(driver, migration, *, use_transaction=None, on_success=None)[source]¶
Execute an upgrade migration.
- Parameters:
- Returns:
Tuple of (sql_content, execution_time_ms).
- sqlspec.migrations.runner.create_migration_runner(migrations_path, extension_migrations, context, extension_configs, is_async=False, runtime=None, description_hints=None)[source]¶
Factory function to create the appropriate migration runner.
- Parameters:
extension_migrations¶ (
dict[str,Path]) – Extension migration paths.context¶ (
MigrationContext|None) – Migration context.extension_configs¶ (
dict[str, typing.Any]) – Extension configurations.runtime¶ (
ObservabilityRuntime|None) – Observability runtime shared with loaders and execution steps.description_hints¶ (
TemplateDescriptionHints|None) – Optional description extraction hints from template profiles.
- Return type:
- Returns:
Appropriate migration runner instance.
Trackers¶
- class sqlspec.migrations.SyncMigrationTracker[source]¶
Bases:
BaseMigrationTracker[SyncDriverAdapterBase]Synchronous migration version tracker.
- ensure_tracking_table(driver)[source]¶
Create the migration tracking table if it doesn’t exist.
Also checks for and adds any missing columns to support schema migrations.
- Parameters:
driver¶ (
SyncDriverAdapterBase) – The database driver to use.- Return type:
- get_current_version(driver)[source]¶
Get the latest applied migration version.
- Parameters:
driver¶ (
SyncDriverAdapterBase) – The database driver to use.- Return type:
- Returns:
The current version number or None if no migrations applied.
- get_applied_migrations(driver)[source]¶
Get all applied migrations in order.
- Parameters:
driver¶ (
SyncDriverAdapterBase) – The database driver to use.- Return type:
- Returns:
List of migration records.
- record_migration(driver, version, description, execution_time_ms, checksum)[source]¶
Record a successfully applied migration.
Parses version to determine type (sequential or timestamp) and auto-increments execution_sequence for application order tracking.
- Parameters:
- Return type:
- remove_migration(driver, version)[source]¶
Remove a migration record (used during downgrade).
- Parameters:
driver¶ (
SyncDriverAdapterBase) – The database driver to use.
- Return type:
- update_version_record(driver, old_version, new_version)[source]¶
Update migration version record from timestamp to sequential.
Updates version_num and version_type while preserving execution_sequence, applied_at, and other tracking metadata. Used during fix command.
Idempotent: If the version is already updated, logs and continues without error. This allows fix command to be safely re-run after pulling changes.
- Parameters:
- Raises:
ValueError – If neither old_version nor new_version found in database.
- Return type:
- replace_with_squash(driver, squashed_version, replaced_versions, description, checksum)[source]¶
Replace multiple migration records with a single squashed record.
Deletes all replaced version records and inserts a new record for the squashed migration with metadata about which versions it replaces.
- is_squash_already_applied(driver, squashed_version, replaced_versions)[source]¶
Check if a squash operation has already been applied.
Determines if any of the replaced versions exist in the database, indicating that the original migrations were applied before the squash.
- Parameters:
- Return type:
- Returns:
True if any replaced version exists (squash already applied), False otherwise.
- class sqlspec.migrations.AsyncMigrationTracker[source]¶
Bases:
BaseMigrationTracker[AsyncDriverAdapterBase]Asynchronous migration version tracker.
- async ensure_tracking_table(driver)[source]¶
Create the migration tracking table if it doesn’t exist.
Also checks for and adds any missing columns to support schema migrations.
- Parameters:
driver¶ (
AsyncDriverAdapterBase) – The database driver to use.- Return type:
- async get_current_version(driver)[source]¶
Get the latest applied migration version.
- Parameters:
driver¶ (
AsyncDriverAdapterBase) – The database driver to use.- Return type:
- Returns:
The current version number or None if no migrations applied.
- async get_applied_migrations(driver)[source]¶
Get all applied migrations in order.
- Parameters:
driver¶ (
AsyncDriverAdapterBase) – The database driver to use.- Return type:
- Returns:
List of migration records.
- async record_migration(driver, version, description, execution_time_ms, checksum)[source]¶
Record a successfully applied migration.
Parses version to determine type (sequential or timestamp) and auto-increments execution_sequence for application order tracking.
- Parameters:
- Return type:
- async remove_migration(driver, version)[source]¶
Remove a migration record (used during downgrade).
- Parameters:
driver¶ (
AsyncDriverAdapterBase) – The database driver to use.
- Return type:
- async update_version_record(driver, old_version, new_version)[source]¶
Update migration version record from timestamp to sequential.
Updates version_num and version_type while preserving execution_sequence, applied_at, and other tracking metadata. Used during fix command.
Idempotent: If the version is already updated, logs and continues without error. This allows fix command to be safely re-run after pulling changes.
- Parameters:
- Raises:
ValueError – If neither old_version nor new_version found in database.
- Return type:
- async replace_with_squash(driver, squashed_version, replaced_versions, description, checksum)[source]¶
Replace multiple migration records with a single squashed record.
Deletes all replaced version records and inserts a new record for the squashed migration with metadata about which versions it replaces.
- async is_squash_already_applied(driver, squashed_version, replaced_versions)[source]¶
Check if a squash operation has already been applied.
Determines if any of the replaced versions exist in the database, indicating that the original migrations were applied before the squash.
- Parameters:
- Return type:
- Returns:
True if any replaced version exists (squash already applied), False otherwise.
Loaders¶
- class sqlspec.migrations.BaseMigrationLoader[source]¶
Bases:
ABCAbstract base class for migration loaders.
- abstractmethod async get_up_sql(path)[source]¶
Load and return the ‘up’ SQL statements from a migration file.
- class sqlspec.migrations.SQLFileLoader[source]¶
Bases:
BaseMigrationLoaderLoader for SQL migration files.
- __init__(sql_loader=None)[source]¶
Initialize SQL file loader.
- Parameters:
sql_loader¶ (
SQLFileLoader|None) – Optional shared SQLFileLoader instance to reuse. If not provided, creates a new instance.
- async get_up_sql(path)[source]¶
Extract the ‘up’ SQL from a SQL migration file.
The SQL file must already be loaded via validate_migration_file() before calling this method. This design ensures the file is loaded exactly once during the migration process.
- class sqlspec.migrations.PythonFileLoader[source]¶
Bases:
BaseMigrationLoaderLoader for Python migration files.
- sqlspec.migrations.loaders.get_migration_loader(file_path, migrations_dir, project_root=None, context=None, sql_loader=None)[source]¶
Factory function to get appropriate loader for migration file.
- Parameters:
migrations_dir¶ (
Path) – Directory containing migration files.project_root¶ (
Path|None) – Optional project root directory for Python imports.context¶ (
Optional[typing.Any]) – Optional migration context to pass to Python migrations.sql_loader¶ (
SQLFileLoader|None) – Optional shared SQLFileLoader instance for SQL migrations. When provided, SQL files are loaded using this shared instance, avoiding redundant file parsing.
- Return type:
- Returns:
Appropriate loader instance for the file type.
- Raises:
MigrationLoadError – If file type is not supported.
Squashing¶
- class sqlspec.migrations.MigrationSquasher[source]¶
Bases:
objectCore squash engine for combining migrations.
Provides functionality to plan, validate, and execute migration squash operations. Combines multiple sequential migrations into a single file with merged UP/DOWN SQL.
- __init__(migrations_path, runner, template_settings=None)[source]¶
Initialize the migration squasher.
- Parameters:
runner¶ (
SyncMigrationRunner) – SyncMigrationRunner instance for loading migrations.template_settings¶ (
MigrationTemplateSettings|None) – Optional template settings for generating squashed file.
- plan_squash(start_version, end_version, description, *, allow_gaps=False, output_format='sql')[source]¶
Plan a squash operation for a range of migrations.
For homogeneous migrations (all SQL or all Python), returns a single plan. For mixed SQL/Python migrations, returns multiple plans - one per consecutive group of same-type migrations.
- Parameters:
- Return type:
- Returns:
List of SquashPlan objects with details of planned operations.
- Raises:
SquashValidationError – If validation fails (invalid range, gaps, etc.).
- extract_sql(migrations)[source]¶
Extract UP and DOWN SQL statements from migrations.
UP statements are accumulated in version order. DOWN statements are accumulated in REVERSE version order for proper rollback.
- generate_squashed_content(plan, up_sql, down_sql)[source]¶
Generate the content for a squashed migration file.
- generate_python_squash(plan, up_sql, down_sql)[source]¶
Generate Python migration file content instead of SQL.
Creates a Python migration file with up() and down() functions that return the SQL statements as lists.
- class sqlspec.migrations.SquashPlan[source]¶
Bases:
objectRepresents a planned squash operation.
- source_migrations¶
List of (version, path) tuples for migrations being squashed.
- target_version¶
The version string for the squashed migration.
- target_path¶
Output file path for the squashed migration.
- description¶
Combined description for the squashed migration.
- source_versions¶
List of version strings being replaced (for tracking table updates).
- __init__(source_migrations, target_version, target_path, description, source_versions)¶
Version Management¶
- class sqlspec.migrations.version.MigrationVersion[source]¶
Bases:
objectParsed migration version with structured comparison support.
- raw¶
Original version string (e.g., “0001”, “20251011120000”, “ext_litestar_0001”).
- type¶
Version format type (sequential or timestamp).
- sequence¶
Numeric value for sequential versions (e.g., 1, 2, 42).
- timestamp¶
Parsed datetime for timestamp versions (UTC).
- extension¶
Extension name for extension-prefixed versions (e.g., “litestar”).
- __lt__(other)[source]¶
Compare versions supporting mixed formats.
- Comparison Rules:
Extension migrations sort by extension name first, then version
Sequential < Timestamp (legacy migrations first)
Sequential vs Sequential: numeric comparison
Timestamp vs Timestamp: chronological comparison
- Parameters:
other¶ (
MigrationVersion) – Version to compare against.- Return type:
- Returns:
True if this version sorts before other.
- Raises:
TypeError – If comparing against non-MigrationVersion.
- __le__(other)[source]¶
Check if version is less than or equal to another.
- Parameters:
other¶ (
MigrationVersion) – Version to compare against.- Return type:
- Returns:
True if this version is less than or equal to other.
- __hash__()[source]¶
Hash version for use in sets and dicts.
- Return type:
- Returns:
Hash value based on raw version string.
- __repr__()[source]¶
Get string representation for debugging.
- Return type:
- Returns:
String representation with type and value.
- __init__(raw, type, sequence, timestamp, extension)¶
Context¶
- class sqlspec.migrations.context.MigrationContext[source]¶
Bases:
objectContext object passed to migration functions.
Provides runtime information about the database environment to migration functions, allowing them to generate dialect-specific SQL.
- config: Any | None = None¶
Database configuration object.
- dialect: str | None = None¶
Database dialect (e.g., ‘postgres’, ‘mysql’, ‘sqlite’).
- metadata: dict[str, Any] | None = None¶
Additional metadata for the migration.
- extension_config: dict[str, Any] | None = None¶
Extension-specific configuration options.
- driver: SyncDriverAdapterBase | AsyncDriverAdapterBase | None = None¶
Database driver instance (available during execution).
- classmethod from_config(config)[source]¶
Create context from database configuration.
- Parameters:
- Return type:
- Returns:
Migration context with dialect information.
- __init__(config=None, dialect=None, metadata=None, extension_config=None, driver=None, _execution_metadata=<factory>)¶
- property is_async_execution: bool¶
Check if migrations are running in an async execution context.
- Returns:
True if executing in an async context.
- property is_async_driver: bool¶
Check if the current driver is async.
- Returns:
True if driver supports async operations.
- property execution_mode: str¶
Get the current execution mode.
- Returns:
‘async’ if in async context, ‘sync’ otherwise.
Fixer¶
- class sqlspec.migrations.fix.MigrationFixer[source]¶
Bases:
objectHandles atomic migration file conversion operations.
Provides backup/rollback functionality and manages conversion from timestamp-based migration files to sequential format.
- plan_renames(conversion_map)[source]¶
Plan all file rename operations from conversion map.
Scans migration directory and builds list of MigrationRename objects for all files that need conversion. Validates no target collisions.
- Parameters:
conversion_map¶ (
dict[str,str]) – Dictionary mapping old versions to new versions.- Return type:
- Returns:
List of planned rename operations.
- Raises:
ValueError – If target file already exists or collision detected.
- create_backup()[source]¶
Create timestamped backup directory with all migration files.
- Return type:
- Returns:
Path to created backup directory.
- update_file_content(file_path, old_version, new_version)[source]¶
Update SQL query names and version comments in file content.
- Transforms query names and version metadata from old version to new version:
– name: migrate-{old_version}-up → – name: migrate-{new_version}-up – name: migrate-{old_version}-down → – name: migrate-{new_version}-down – Version: {old_version} → – Version: {new_version}
Creates version-specific regex patterns to avoid unintended replacements of other migrate-* patterns in the file.
- class sqlspec.migrations.fix.MigrationRename[source]¶
Bases:
objectRepresents a planned migration file rename operation.
- old_path¶
Current file path.
- new_path¶
Target file path after rename.
- old_version¶
Current version string.
- new_version¶
Target version string.
- needs_content_update¶
Whether file content needs updating. True for SQL files that contain query names.
- __init__(old_path, new_path, old_version, new_version, needs_content_update)¶
Templates¶
- class sqlspec.migrations.templates.MigrationTemplateSettings[source]¶
Bases:
objectResolved template configuration for a migration command context.
- property description_hints: TemplateDescriptionHints¶
Expose description extraction hints derived from the active profile.
- __init__(default_format, profile)¶
- class sqlspec.migrations.templates.MigrationTemplateProfile[source]¶
Bases:
objectConcrete template profile selected via configuration.
- __init__(name, title, sql, python)¶
Utilities¶
- sqlspec.migrations.create_migration_file(migrations_dir, version, message, file_type=None, *, config=None, template_settings=None)[source]¶
Create a new migration file from template.
- Return type:
- async sqlspec.migrations.drop_all(engine, version_table_name, metadata=None)[source]¶
Drop all tables from the database.
- Parameters:
- Raises:
NotImplementedError – Always raised.
- Return type: