Source code for sqlspec.migrations.fix

"""Migration file fix operations for converting timestamp to sequential versions.

This module provides utilities to convert timestamp-format migration files to
sequential format, supporting the hybrid versioning workflow where development
uses timestamps and production uses sequential numbers.
"""

import re
from dataclasses import dataclass
from pathlib import Path

from sqlspec.migrations._backup import create_backup, remove_backup, restore_backup
from sqlspec.utils.logging import get_logger

__all__ = ("MigrationFixer", "MigrationRename")

logger = get_logger(__name__)


@dataclass
class MigrationRename:
    """Represents a planned migration file rename operation.

    Attributes:
        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.
    """

    old_path: Path
    new_path: Path
    old_version: str
    new_version: str
    needs_content_update: bool
class MigrationFixer: """Handles atomic migration file conversion operations. Provides backup/rollback functionality and manages conversion from timestamp-based migration files to sequential format. """ def __init__(self, migrations_path: Path) -> None: """Initialize migration fixer. Args: migrations_path: Path to migrations directory. """ self.migrations_path = migrations_path self.backup_path: Path | None = None