
import os

from flask import _request_ctx_stack, g, request
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker

from pycroft.model import session
from pycroft.model.session import set_scoped_session
from scripts.schema import AlembicHelper, SchemaStrategist
from pycroft.lib import traffic


def main():
    try:
        connection_string = os.environ['PYCROFT_DB_URI']
    except KeyError:
        raise RuntimeError("Environment variable PYCROFT_DB_URI must be "
                           "set to an SQLAlchemy connection string.")

    engine = create_engine(connection_string)
    connection = engine.connect()
    state = AlembicHelper(connection)
    strategy = SchemaStrategist(state).determine_schema_strategy()
    strategy()

    engine = create_engine(connection_string)
    set_scoped_session(scoped_session(sessionmaker(bind=engine),
                                      scopefunc=lambda: _request_ctx_stack.top))

    print("Starting synchronization of exceeded traffic limits.")
    traffic.sync_exceeded_traffic_limits()
    session.session.commit()
    print("Finished synchronization.")


if __name__ == "__main__":
    main()


import os

from flask import _request_ctx_stack, g, request
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker

from pycroft.model import session
from pycroft.model.session import set_scoped_session
from scripts.schema import AlembicHelper, SchemaStrategist
from pycroft.lib import traffic


def main():
    try:
        connection_string = os.environ['PYCROFT_DB_URI']
    except KeyError:
        raise RuntimeError("Environment variable PYCROFT_DB_URI must be "
                           "set to an SQLAlchemy connection string.")

    engine = create_engine(connection_string)
    connection = engine.connect()
    state = AlembicHelper(connection)
    strategist = SchemaStrategist(state)
    is_up_to_date = strategist.helper.running_version == strategist.helper.desired_version
    if not is_up_to_date:
        print("Schema is not up to date!")
        return

    set_scoped_session(scoped_session(sessionmaker(bind=engine),
                                      scopefunc=lambda: _request_ctx_stack.top))

    print("Starting synchronization of exceeded traffic limits.")
    traffic.sync_exceeded_traffic_limits()
    session.session.commit()
    print("Finished synchronization.")


if __name__ == "__main__":
    main()

Add schema version check to sync script
Add schema version check to sync script


  
  import os
  
  from flask import _request_ctx_stack, g, request
  from sqlalchemy import create_engine
  from sqlalchemy.orm import scoped_session, sessionmaker
  
  from pycroft.model import session
  from pycroft.model.session import set_scoped_session
  from scripts.schema import AlembicHelper, SchemaStrategist
  from pycroft.lib import traffic
  
  
  def main():
      try:
          connection_string = os.environ['PYCROFT_DB_URI']
      except KeyError:
          raise RuntimeError("Environment variable PYCROFT_DB_URI must be "
                             "set to an SQLAlchemy connection string.")
  
      engine = create_engine(connection_string)
      connection = engine.connect()
      state = AlembicHelper(connection)
-     strategy = SchemaStrategist(state).determine_schema_strategy()
-     strategy()
+     strategist = SchemaStrategist(state)
+     is_up_to_date = strategist.helper.running_version == strategist.helper.desired_version
+     if not is_up_to_date:
+         print("Schema is not up to date!")
+         return
  
-     engine = create_engine(connection_string)
      set_scoped_session(scoped_session(sessionmaker(bind=engine),
                                        scopefunc=lambda: _request_ctx_stack.top))
  
      print("Starting synchronization of exceeded traffic limits.")
      traffic.sync_exceeded_traffic_limits()
      session.session.commit()
      print("Finished synchronization.")
  
  
  if __name__ == "__main__":
      main()
  
Add schema version check to sync script
## Code Before:

import os

from flask import _request_ctx_stack, g, request
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker

from pycroft.model import session
from pycroft.model.session import set_scoped_session
from scripts.schema import AlembicHelper, SchemaStrategist
from pycroft.lib import traffic


def main():
    try:
        connection_string = os.environ['PYCROFT_DB_URI']
    except KeyError:
        raise RuntimeError("Environment variable PYCROFT_DB_URI must be "
                           "set to an SQLAlchemy connection string.")

    engine = create_engine(connection_string)
    connection = engine.connect()
    state = AlembicHelper(connection)
    strategy = SchemaStrategist(state).determine_schema_strategy()
    strategy()

    engine = create_engine(connection_string)
    set_scoped_session(scoped_session(sessionmaker(bind=engine),
                                      scopefunc=lambda: _request_ctx_stack.top))

    print("Starting synchronization of exceeded traffic limits.")
    traffic.sync_exceeded_traffic_limits()
    session.session.commit()
    print("Finished synchronization.")


if __name__ == "__main__":
    main()

## Instruction:
Add schema version check to sync script
## Code After:

import os

from flask import _request_ctx_stack, g, request
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker

from pycroft.model import session
from pycroft.model.session import set_scoped_session
from scripts.schema import AlembicHelper, SchemaStrategist
from pycroft.lib import traffic


def main():
    try:
        connection_string = os.environ['PYCROFT_DB_URI']
    except KeyError:
        raise RuntimeError("Environment variable PYCROFT_DB_URI must be "
                           "set to an SQLAlchemy connection string.")

    engine = create_engine(connection_string)
    connection = engine.connect()
    state = AlembicHelper(connection)
    strategist = SchemaStrategist(state)
    is_up_to_date = strategist.helper.running_version == strategist.helper.desired_version
    if not is_up_to_date:
        print("Schema is not up to date!")
        return

    set_scoped_session(scoped_session(sessionmaker(bind=engine),
                                      scopefunc=lambda: _request_ctx_stack.top))

    print("Starting synchronization of exceeded traffic limits.")
    traffic.sync_exceeded_traffic_limits()
    session.session.commit()
    print("Finished synchronization.")


if __name__ == "__main__":
    main()
