def helper(self): """Building a fresh helper every time This is convenient because every time e.g. a revision is added to our version directory, `helper.scr` does not know that. """ return AlembicHelper(self.connection)
def test_stamped_schema_is_ok(self): self.helper.stamp() # self.helper sticks to the old versions helper = AlembicHelper(self.connection) assert helper.running_version assert helper.running_version == helper.desired_version strategist = MockedStrategist(helper) assert strategist.determine_schema_strategy() == strategist.run
def test_stamped_schema_is_ok(self): self.helper.stamp() # self.helper sticks to the old versions helper = AlembicHelper(self.connection) self.assertTrue(helper.running_version) self.assertEqual(helper.running_version, helper.desired_version) strategist = MockedStrategist(helper) self.assertEqual(strategist.determine_schema_strategy(), strategist.run)
def server_run(args): app = make_app(args.debug) wait_for_db: bool = args.wait_for_database connection_string = get_connection_string() connection = try_create_connection(connection_string, wait_for_db, app.logger) state = AlembicHelper(connection) strategy = SchemaStrategist(state).determine_schema_strategy() strategy() @app.before_request def get_time(): g.request_time = time.time() @app.teardown_request def time_response(exception=None): request_time = g.pop('request_time', None) if request_time: time_taken = time.time() - request_time if time_taken > 0.5: app.logger.warn( "Response took {duration} seconds for request {path}". format(path=request.full_path, duration=time_taken)) engine = create_engine(connection_string, echo=args.profile) set_scoped_session( scoped_session(sessionmaker(bind=engine), scopefunc=lambda: _request_ctx_stack.top)) def lookup_translation(): ctx = _request_ctx_stack.top if ctx is None: return None translations = getattr(ctx, 'pycroft_translations', None) if translations is None: translations = Translations() for module in (pycroft, web): os.path.dirname(module.__file__) dirname = os.path.join(ctx.app.root_path, 'translations') translations.merge(Translations.load(dirname, [get_locale()])) ctx.pycroft_translations = translations return translations set_translation_lookup(lookup_translation) app.config.from_pyfile('flask.cfg') if args.profile: app.config['PROFILE'] = True app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions=[30]) app.run(debug=args.debug, port=args.port, host=args.host, threaded=True)
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) if not SchemaStrategist(state).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.")
def old_schema_needs_upgrade(self): self.helper.stamp(revision='aaaaaa') # something != the head revision helper = AlembicHelper(self.connection) strategist = MockedStrategist(helper) self.assertEqual(strategist.determine_schema_strategy(), strategist.upgrade)
def setUp(self): super().setUp() _, self.connection = get_engine_and_connection() self.connection.execute("DROP TABLE IF EXISTS alembic_version") self.helper = AlembicHelper(self.connection)
def prepare_server(args) -> tuple[PycroftFlask, Callable]: """returns both the prepared app and a callback executing `app.run`""" if args.echo: logging.basicConfig(stream=sys.stdout, level=logging.DEBUG) logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO) logging.getLogger('sqlalchemy.pool').setLevel(logging.DEBUG) app = make_app(args.debug) logging.getLogger('pycroft').addHandler(default_handler) wait_for_db: bool = args.wait_for_database connection_string = get_connection_string() connection, engine = try_create_connection(connection_string, wait_for_db, app.logger, reflections=False) state = AlembicHelper(connection) if args.force_schema_create: strategy = SchemaStrategist(state).create_then_stamp else: strategy = SchemaStrategist(state).determine_schema_strategy() strategy() @app.before_request def get_time(): g.request_time = time.time() @app.teardown_request def time_response(exception=None): request_time = g.pop('request_time', None) if request_time: time_taken = time.time() - request_time if time_taken > 0.5: app.logger.warning( "Response took %s seconds for request %s", time_taken, request.full_path, ) connection, engine = try_create_connection(connection_string, wait_for_db, app.logger, args.profile) set_scoped_session(scoped_session(sessionmaker(bind=engine), scopefunc=lambda: _request_ctx_stack.top)) def lookup_translation(): ctx = _request_ctx_stack.top if ctx is None: return None translations = getattr(ctx, 'pycroft_translations', None) if translations is None: translations = Translations() for module in (pycroft, web): os.path.dirname(module.__file__) dirname = os.path.join(ctx.app.root_path, 'translations') translations.merge(Translations.load(dirname, [get_locale()])) ctx.pycroft_translations = translations return translations set_translation_lookup(lookup_translation) app.config.from_pyfile('flask.cfg') if args.profile: app.config['PROFILE'] = True app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions=[30]) return app, lambda: app.run(debug=args.debug, port=args.port, host=args.host, threaded=True)