title: Getting Alembic Database Version Programmatically
body: <p>I'm trying to figure out how to get the version of my database using Alembic. I've already gotten the database set up to use alembic and successfully performed both an upgrade and downgrade on it. I now want to get this version from my own python script.</p>
  
  <p>I attempted to create a function for doing this</p>
  
  <pre><code>def get_current_database_version():
      path = os.path.join(os.path.dirname(__file__), os.path.pardir)
      alembic_cfg = Config(os.path.join(path, 'alembic.ini'))
      current_rev = command.current(alembic_cfg, head_only=True)
      return current_rev
  </code></pre>
  
  <p>This function returned a <code>NoSectionError: No section: 'formatters'</code></p>
  
  <p>I then went to my alembic.ini file to check that it had a formatters area. This is my alembic.ini file:</p>
  
  <pre><code># A generic, single database configuration.
  
  [alembic]
  # path to migration scripts
  script_location = alembic
  pyramid_config_file = ../../development.ini
  
  # template used to generate migration files
  # file_template = %%(rev)s_%%(slug)s
  
  # max length of characters to apply to the
  # "slug" field
  #truncate_slug_length = 40
  
  # set to 'true' to run the environment during
  # the 'revision' command, regardless of autogenerate
  # revision_environment = false
  
  # set to 'true' to allow .pyc and .pyo files without
  # a source .py file to be detected as revisions in the
  # versions/ directory
  # sourceless = false
  
  sqlalchemy.url = sqlite:///%(here)s/mgo.sqlite
  
  
  # Logging configuration
  [loggers]
  keys = root,sqlalchemy,alembic
  
  [handlers]
  keys = console
  
  [formatters]
  keys = generic
  
  [logger_root]
  level = WARN
  handlers = console
  qualname =
  
  [logger_sqlalchemy]
  level = WARN
  handlers =
  qualname = sqlalchemy.engine
  
  [logger_alembic]
  level = INFO
  handlers =
  qualname = alembic
  
  [handler_console]
  class = StreamHandler
  args = (sys.stderr,)
  level = NOTSET
  formatter = generic
  
  [formatter_generic]
  format = %(levelname)-5.5s [%(name)s] %(message)s
  datefmt = %H:%M:%S
  </code></pre>
  
  <p>Anyone know what I'm doing wrong? Thanks</p>
  
  <p>Edit:</p>
  
  <p>Here is my attempt to use MigrationContext to solve the problem:</p>
  
  <pre><code>def get_database_revision():
      engine = create_engine("sqlite:///../mgo.db")
      conn = engine.connect()
      context = MigrationContext.configure(conn)
      current_rev = context.get_current_revision()
      return current_rev
  </code></pre>
  
  <p>It connects but returns none. Using sqlite browser I can see that the version in the database is not set to none.</p>