import sqlite3
from typing import Optional


def get_previous_sha(db: sqlite3.Connection) -> Optional[str]:
    """Gets the latest inserted SHA."""
    result = db.execute(
        # Use ROWID as a free, auto-incrementing, primary key.
        'SELECT sha FROM metric_data ORDER BY ROWID DESC LIMIT 1',
    ).fetchone()

    return result[0] if result else None