Beispiel #1
0
def use_on_disk_postgresql_database(declarative_base: DeclarativeMeta) -> None:
    """Connects SQLAlchemy to a local test postgres server. Should be called after the test database and user have
    already been initialized.

    This includes:
    1. Create all tables in the newly created sqlite database
    2. Bind the global SessionMaker to the new database engine
    """
    if declarative_base not in DECLARATIVE_BASES:
        raise ValueError(f"Unexpected declarative base: {declarative_base}.")

    SQLAlchemyEngineManager.init_engine_for_postgres_instance(
        db_url=on_disk_postgres_db_url(), schema_base=declarative_base)
def setup_scoped_sessions(app: Flask, db_url: URL) -> Engine:
    engine = SQLAlchemyEngineManager.init_engine_for_postgres_instance(
        database_key=SQLAlchemyDatabaseKey.for_schema(SchemaType.CASE_TRIAGE),
        db_url=db_url,
    )
    session_factory = sessionmaker(bind=engine)
    app.scoped_session = flask_scoped_session(session_factory, app)
    return engine
Beispiel #3
0
def use_on_disk_postgresql_database(declarative_base: DeclarativeMeta) -> None:
    """Connects SQLAlchemy to a local test postgres server. Should be called after the test database and user have
    already been initialized. In Travis, these are set up via commands in the .travis.yaml, locally, you must have
    first called start_on_disk_postgresql_database().

    This includes:
    1. Create all tables in the newly created sqlite database
    2. Bind the global SessionMaker to the new database engine
    """
    if environment.in_gae():
        raise ValueError('Running test-only code in Google App Engine.')

    if declarative_base not in DECLARATIVE_BASES:
        raise ValueError(f"Unexpected declarative base: {declarative_base}.")

    SQLAlchemyEngineManager.init_engine_for_postgres_instance(
        db_url=
        f'postgresql://{TEST_POSTGRES_USER_NAME}:@localhost:5432/{TEST_POSTGRES_DB_NAME}',
        schema_base=declarative_base)
Beispiel #4
0
    def setUp(self) -> None:
        self.db_key = SQLAlchemyDatabaseKey.for_schema(SchemaType.CASE_TRIAGE)
        self.env_vars = (
            local_postgres_helpers.update_local_sqlalchemy_postgres_env_vars())

        # We need to build up the database using the migrations instead of
        # by just loading from the internal representation because the different
        # methods induce different orders.
        # The migration order is the one seen in staging/prod, as well as what
        # we do in development.
        engine = SQLAlchemyEngineManager.init_engine_for_postgres_instance(
            database_key=self.db_key,
            db_url=local_postgres_helpers.on_disk_postgres_db_url(),
        )
        with runner(
            {
                "file": self.db_key.alembic_file,
                "script_location": self.db_key.migrations_location,
            },
                engine,
        ) as r:
            r.migrate_up_to("head")
def setup_scoped_sessions(app: Flask, db_url: str) -> None:
    engine = SQLAlchemyEngineManager.init_engine_for_postgres_instance(
        db_url, CaseTriageBase)
    session_factory = sessionmaker(bind=engine)
    app.scoped_session = flask_scoped_session(session_factory, app)