title: How to directly access SQLite database to delete alembic_version table?
body: <p>I am building a Flask web app, and at some point accidentally deleted a migration file for my SQLite database. I am using SQLAlchemy in Flask for database commands, and flask-alembic for migrations. I am trying to delete the "alembic_version" file that contains the reference to the missing migration file, but am having trouble doing so.</p>
  
  <p>I deleted the migration file a while ago, but didn't notice until trying to run a migration on my database recently with:</p>
  
  <pre><code>$ flask db migrate
  </code></pre>
  
  <p>and I received the following error message:</p>
  
  <pre><code>INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
  INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
  ERROR [root] Error: Can't locate revision identified by '44ab999461f7'
  </code></pre>
  
  <p>Okay, so I believe that means I deleted the migration file, except the alembic_version table that contains the current migration version <em>thinks</em> it still exists and is trying to reference it. My thought is to delete the entry in alembic_version, then I will re-initialize the migrations folder and migrate with:</p>
  
  <pre><code>$ flask db init
  $ flask db migrate
  </code></pre>
  
  <h2><strong>Attempt 1</strong></h2>
  
  <p>So I need to directly access my SQLite database, and remove the table. First I tried using this code to remove all tables:</p>
  
  <pre><code>#drop alembic_version table
  if db.engine.dialect.has_table(db.engine, 'alembic_version'):
      version = db.Table('alembic_version', db.metadata,autoload_with=db.engine)
      version.drop()
  </code></pre>
  
  <p>found in this <a href="https://github.com/inveniosoftware/invenio-db/issues/72" rel="nofollow noreferrer">thread</a>.</p>
  
  <p>When running this function I get the error:</p>
  
  <pre><code>sqlalchemy.exc.UnboundExecutionError: Table object 'alembic_version' is not bound to an Engine or Connection.  Execution can not proceed without a database to execute against.
  </code></pre>
  
  <p>Okay, so there is an issue with the engine configuration, or something. My engine creation is in my __init__.py file, and uses memory:</p>
  
  <pre><code>engine = create_engine('sqlite://')
  </code></pre>
  
  <p>I am not well versed in engine configuration, and at this point I'm stuck. I don't know if I need to do this outside of my context processor? Maybe it doesn't pull in the engine info? Maybe I need to bind the engine?</p>
  
  <h2><strong>Attempt 2</strong></h2>
  
  <p>After looking through information about SQLAlchemy engine connection, found <a href="https://docs.sqlalchemy.org/en/13/core/engines.html" rel="nofollow noreferrer">here</a> and <a href="https://docs.sqlalchemy.org/en/13/core/connections.html" rel="nofollow noreferrer">here</a>, I tried directly dropping the table using my flask shell context processor:</p>
  
  <pre><code>@app.shell_context_processor
  def make_shell_context():
          return {'db':db,'User':User}
  </code></pre>
  
  <pre><code>from sqlalchemy import create_engine
  engine=create_engine('sqlite://')
  engine
  Engine(sqlite://)
  engine.execute('DROP TABLE IF EXISTS alembic_version')
  &lt;sqlalchemy.engine.result.ResultProxy object at 0x0000028EFEA9A588&gt;
  exit()
  </code></pre>
  
  <p>But it didn't work, because I can query another table in the database and it returns a full table. Just to be sure though, I tried to migrate again:</p>
  
  <pre><code>$ flask db migrate
  </code></pre>
  
  <p>results in the same error as above:</p>
  
  <pre><code>INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
  INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
  ERROR [root] Error: Can't locate revision identified by '44ab999461f7'
  </code></pre>
  
  <p>So not tables were actually dropped. The same questions arise from attempt 1, do I need to do this outside of my context processor? Do I need to do more engine configuration?</p>
  
  <p>Any help or direction is greatly appreciated.</p>