import argparse
from dataactcore.models.errorInterface import ErrorInterface
from dataactcore.models.jobTrackerInterface import JobTrackerInterface
from dataactcore.models.userInterface import UserInterface
from dataactcore.models.validationInterface import ValidationInterface
from sqlalchemy import MetaData, Table
from sqlalchemy.orm import sessionmaker
from sqlalchemy.sql import update


def reset_alembic(alembic_version):

    engine_list = [
        ErrorInterface().engine,
        JobTrackerInterface().engine,
        UserInterface().engine,
        ValidationInterface().engine,
    ]

    for e in engine_list:
        Session = sessionmaker(bind=e)
        session = Session()
        metadata = MetaData(bind=e)
        alembic_table = Table('alembic_version', metadata, autoload=True)
        u = update(alembic_table)
        u = u.values({"version_num": alembic_version})
        session.execute(u)
        session.commit()

parser = argparse.ArgumentParser\
    (description="Reset alembic version tables across broker databases.")
parser.add_argument(
    'version', help="Version to set the Alembic migration tables to.")
v = vars(parser.parse_args())['version']
reset_alembic(v)


import argparse

from sqlalchemy import MetaData, Table
from sqlalchemy.sql import update

from dataactcore.interfaces.db import GlobalDB
from dataactvalidator.app import createApp


def reset_alembic(alembic_version):

    with createApp().app_context():
        db = GlobalDB.db()

        engine = db.engine
        sess = db.session
        metadata = MetaData(bind=engine)
        alembic_table = Table('alembic_version', metadata, autoload=True)
        u = update(alembic_table)
        u = u.values({"version_num": alembic_version})
        sess.execute(u)
        sess.commit()

parser = argparse.ArgumentParser\
    (description="Reset alembic version table.")
parser.add_argument(
    'version', help="Version to set the Alembic migration table to.")
v = vars(parser.parse_args())['version']
reset_alembic(v)


Remove db interfaces from the alembic version reset helper script.
Remove db interfaces from the alembic version reset helper script.

Arguably, this script is no longer especially useful now that we
only have a single database for the broker. That said, removed the
interfaces in case folks are still using it.

  import argparse
+ 
- from dataactcore.models.errorInterface import ErrorInterface
- from dataactcore.models.jobTrackerInterface import JobTrackerInterface
- from dataactcore.models.userInterface import UserInterface
- from dataactcore.models.validationInterface import ValidationInterface
  from sqlalchemy import MetaData, Table
- from sqlalchemy.orm import sessionmaker
  from sqlalchemy.sql import update
+ 
+ from dataactcore.interfaces.db import GlobalDB
+ from dataactvalidator.app import createApp
  
  
  def reset_alembic(alembic_version):
  
+     with createApp().app_context():
+         db = GlobalDB.db()
-     engine_list = [
-         ErrorInterface().engine,
-         JobTrackerInterface().engine,
-         UserInterface().engine,
-         ValidationInterface().engine,
-     ]
  
+         engine = db.engine
+         sess = db.session
-     for e in engine_list:
-         Session = sessionmaker(bind=e)
-         session = Session()
-         metadata = MetaData(bind=e)
+         metadata = MetaData(bind=engine)
          alembic_table = Table('alembic_version', metadata, autoload=True)
          u = update(alembic_table)
          u = u.values({"version_num": alembic_version})
-         session.execute(u)
+         sess.execute(u)
-         session.commit()
+         sess.commit()
  
  parser = argparse.ArgumentParser\
-     (description="Reset alembic version tables across broker databases.")
+     (description="Reset alembic version table.")
  parser.add_argument(
-     'version', help="Version to set the Alembic migration tables to.")
+     'version', help="Version to set the Alembic migration table to.")
  v = vars(parser.parse_args())['version']
  reset_alembic(v)
  
  
Remove db interfaces from the alembic version reset helper script.
## Code Before:
import argparse
from dataactcore.models.errorInterface import ErrorInterface
from dataactcore.models.jobTrackerInterface import JobTrackerInterface
from dataactcore.models.userInterface import UserInterface
from dataactcore.models.validationInterface import ValidationInterface
from sqlalchemy import MetaData, Table
from sqlalchemy.orm import sessionmaker
from sqlalchemy.sql import update


def reset_alembic(alembic_version):

    engine_list = [
        ErrorInterface().engine,
        JobTrackerInterface().engine,
        UserInterface().engine,
        ValidationInterface().engine,
    ]

    for e in engine_list:
        Session = sessionmaker(bind=e)
        session = Session()
        metadata = MetaData(bind=e)
        alembic_table = Table('alembic_version', metadata, autoload=True)
        u = update(alembic_table)
        u = u.values({"version_num": alembic_version})
        session.execute(u)
        session.commit()

parser = argparse.ArgumentParser\
    (description="Reset alembic version tables across broker databases.")
parser.add_argument(
    'version', help="Version to set the Alembic migration tables to.")
v = vars(parser.parse_args())['version']
reset_alembic(v)


## Instruction:
Remove db interfaces from the alembic version reset helper script.
## Code After:
import argparse

from sqlalchemy import MetaData, Table
from sqlalchemy.sql import update

from dataactcore.interfaces.db import GlobalDB
from dataactvalidator.app import createApp


def reset_alembic(alembic_version):

    with createApp().app_context():
        db = GlobalDB.db()

        engine = db.engine
        sess = db.session
        metadata = MetaData(bind=engine)
        alembic_table = Table('alembic_version', metadata, autoload=True)
        u = update(alembic_table)
        u = u.values({"version_num": alembic_version})
        sess.execute(u)
        sess.commit()

parser = argparse.ArgumentParser\
    (description="Reset alembic version table.")
parser.add_argument(
    'version', help="Version to set the Alembic migration table to.")
v = vars(parser.parse_args())['version']
reset_alembic(v)

