def check_db_revision(
    engine: Engine,
    prefix: str = core_db.DEFAULT_DATABASE_PREFIX,
    prior_prefix: Optional[str] = None,
):
    """
    Check if database schema is at the expected revision.

    Args:
        engine: SQLAlchemy engine to check.

        prefix: Prefix used for table names including alembic_version in the
            current code.

        prior_prefix: Table prefix used in the previous version of the
            database. Before this configuration was an option, the prefix was
            equivalent to "".
    """

    if not isinstance(prefix, str):
        raise ValueError("prefix must be a string")

    if prefix == prior_prefix:
        raise ValueError(
            "prior_prefix and prefix cannot be the same. Use None for prior_prefix if it is unknown."
        )

    ins = sqlalchemy.inspect(engine)
    tables = ins.get_table_names()

    # Get all tables we could have made for alembic version. Other apps might
    # also have made these though.
    version_tables = [t for t in tables if t.endswith("alembic_version")]

    if prior_prefix is not None:
        # Check if tables using the old/empty prefix exist.
        if prior_prefix + "alembic_version" in version_tables:
            raise db_exceptions.DatabaseVersionException.reconfigured(
                prior_prefix=prior_prefix
            )
    else:
        # Check if the new/expected version table exists.

        if prefix + "alembic_version" not in version_tables:
            # If not, lets try to figure out the prior prefix.

            if len(version_tables) > 0:
                if len(version_tables) > 1:
                    # Cannot figure out prior prefix if there is more than one
                    # version table.
                    raise ValueError(
                        f"Found multiple alembic_version tables: {version_tables}. "
                        "Cannot determine prior prefix. "
                        "Please specify it using the `prior_prefix` argument."
                    )

                # Guess prior prefix as the single one with version table name.
                raise db_exceptions.DatabaseVersionException.reconfigured(
                    prior_prefix=version_tables[0].replace(
                        "alembic_version", ""
                    )
                )

    if is_legacy_sqlite(engine):
        logger.info("Found legacy SQLite file: %s", engine.url)
        raise db_exceptions.DatabaseVersionException.behind()

    revisions = db_migrations.DbRevisions.load(engine, prefix=prefix)

    if revisions.current is None:
        logger.debug("Creating database")
        db_migrations.upgrade_db(
            engine, revision="head", prefix=prefix
        )  # create automatically if it doesn't exist

    elif revisions.in_sync:
        logger.debug("Database schema is up to date: %s", revisions)

    elif revisions.behind:
        raise db_exceptions.DatabaseVersionException.behind()

    elif revisions.ahead:
        raise db_exceptions.DatabaseVersionException.ahead()

    else:
        raise NotImplementedError(
            f"Cannot handle database revisions: {revisions}"
        )

    Check if database schema is at the expected revision.

    Args:
        engine: SQLAlchemy engine to check.

        prefix: Prefix used for table names including alembic_version in the
            current code.

        prior_prefix: Table prefix used in the previous version of the
            database. Before this configuration was an option, the prefix was
            equivalent to "".
    
src/core/trulens/core/database/utils.py