Source code for sqlspec.migrations.loaders

"""Migration loader implementations for SQLSpec.

This module provides loader classes for different migration file formats.
"""

import abc
import inspect
import sys
import types
from collections.abc import Callable, Iterator
from contextlib import contextmanager
from pathlib import Path
from typing import Any, Final, cast

from sqlspec.loader import SQLFileLoader as CoreSQLFileLoader

__all__ = ("BaseMigrationLoader", "MigrationLoadError", "PythonFileLoader", "SQLFileLoader", "get_migration_loader")

PROJECT_ROOT_MARKERS: Final[list[str]] = ["pyproject.toml", ".git", "setup.cfg", "setup.py"]


class MigrationLoadError(Exception):
    """Exception raised when migration loading fails."""


class BaseMigrationLoader(abc.ABC):
    """Abstract base class for migration loaders."""

    __slots__ = ()

    @abc.abstractmethod
    async def get_up_sql(self, path: Path) -> list[str]:
        """Load and return the 'up' SQL statements from a migration file.

        Args:
            path: Path to the migration file.

        Returns:
            List of SQL statements to execute for upgrade.

        Raises:
            MigrationLoadError: If loading fails.
        """
        ...
@abc.abstractmethod async def get_down_sql(self, path: Path) -> list[str]: """Load and return the 'down' SQL statements from a migration file. Args: path: Path to the migration file. Returns: List of SQL statements to execute for downgrade. Empty list if no downgrade is available. Raises: MigrationLoadError: If loading fails. """ ...