code: def downgrade(
      *,
      to_revision,
      from_revision=None,
      show_sql_only=False,
      session: Session = NEW_SESSION,
  ):
      """
      Downgrade the airflow metastore schema to a prior version.
  
      :param to_revision: The alembic revision to downgrade *to*.
      :param show_sql_only: if True, print sql statements but do not run them
      :param from_revision: if supplied, alembic revision to dawngrade *from*. This may only
          be used in conjunction with ``sql=True`` because if we actually run the commands,
          we should only downgrade from the *current* revision.
      :param session: sqlalchemy session for connection to airflow metadata database
      """
      if from_revision and not show_sql_only:
          raise ValueError(
              "`from_revision` can't be combined with `sql=False`. When actually "
              "applying a downgrade (instead of just generating sql), we always "
              "downgrade from current revision."
          )
  
      if not settings.SQL_ALCHEMY_CONN:
          raise RuntimeError("The settings.SQL_ALCHEMY_CONN not set.")
  
      # alembic adds significant import time, so we import it lazily
      from alembic import command
  
      log.info("Attempting downgrade to revision %s", to_revision)
      config = _get_alembic_config()
  
      with create_global_lock(session=session, lock=DBLocks.MIGRATIONS):
          if show_sql_only:
              log.warning(
                  "Generating sql scripts for manual migration."
              )
              if not from_revision:
                  from_revision = _get_current_revision(session)
              revision_range = f"{from_revision}:{to_revision}"
              _offline_migration(
                  command.downgrade,
                  config=config,
                  revision=revision_range,
              )
          else:
              log.info("Applying downgrade migrations.")
              command.downgrade(
                  config, revision=to_revision, sql=show_sql_only
              )
  
imports: from __future__ import annotations
  import contextlib
  import enum
  import json
  import logging
  import os
  import sys
  import time
  import warnings
  from dataclasses import dataclass
  from tempfile import gettempdir
  from typing import TYPE_CHECKING, Callable, Generator, Iterable
  from sqlalchemy import Table, and_, column, delete, exc, func, inspect, literal, or_, select, table, text, tuple_
  import airflow
  from airflow import settings
  from airflow.configuration import conf
  from airflow.exceptions import AirflowException
  from airflow.models import import_all_models
  from airflow.utils import helpers
  from airflow.utils.session import NEW_SESSION, create_session, provide_session
label_desc: Use image node_id 37 for calling a global function with example usage: downgrade() without return types