def create_app(): app = FastAPI(title=settings.PROJECT_NAME, openapi_url=f"{settings.API_PREFIX}/v1/openapi.json", version=1.0) app.mount(settings.STATIC_URL, StaticFiles(directory=settings.STATIC_PATH), name="static") # Set all CORS enabled origins if settings.BACKEND_CORS_ORIGINS: app.add_middleware( CORSMiddleware, allow_origins=[ str(origin) for origin in settings.BACKEND_CORS_ORIGINS ], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # initialize database init_db() # start APScheduler sched.start() append_routes(app) return app
def setup_db() -> None: session = SessionLocal() if not database_exists(engine.url): create_database(engine.url) else: Base.metadata.drop_all(engine) init_db(session) session.close()
def init() -> None: try: db = SessionLocal() # Try to create session to check if DB is awake db.execute("SELECT 1") init_db(db) except Exception as e: logger.error(e) raise e
def database_initialization(db): # Before running tests alembic_cfg = alembic.config.Config('alembic.ini') alembic_cfg.attributes['configure_logger'] = False # Database upgrade alembic.command.upgrade(alembic_cfg, 'head') init_db(db) yield # After running tests db.commit() db.close() engine.dispose() alembic.command.downgrade(alembic_cfg, '6e523d653806')
def create_test_database(): try: if database_exists(engine.url): drop_database(engine.url) create_database( engine.url, template="template_postgis" ) # Create the test database. p = os.path.join(os.getcwd(), "alembic.ini") m = os.path.join(os.getcwd(), "alembic") alembic_config = Config(p) # Run the migrations. alembic_config.set_main_option("script_location", m) alembic_config.attributes["configure_logger"] = False with engine.begin() as session: alembic_config.attributes["connection"] = session command.upgrade(alembic_config, "head") session = next(app.dependency_overrides[get_db]()) init_db(session) yield # Run the tests. finally: drop_database(engine.url) # Drop the test database.
def init() -> None: init_db()
def init(): with get_db_session() as db_session: init_db(db_session)
def init(): init_db(db_session)
def init(): init_db()
from fastapi import FastAPI from app.db.init_db import init_db from app.db.session import Session from starlette.requests import Request from app.api.router import api_router from app import salt import bcrypt app = FastAPI(title='Task manager') init_db() @app.on_event("startup") async def startup_event(): salt.confirm_salt = bcrypt.gensalt() @app.middleware("http") async def db_session_middleware(request: Request, call_next): request.state.db = Session() response = await call_next(request) request.state.db.close() return response app.include_router(api_router)
def init(): try: init_db() except Exception as e: logger.error(e) raise e
# CORS origins = [] # Set all CORS enabled origins if config.BACKEND_CORS_ORIGINS: origins_raw = config.BACKEND_CORS_ORIGINS.split(",") for origin in origins_raw: use_origin = origin.strip() origins.append(use_origin) app.add_middleware( CORSMiddleware, allow_origins=origins, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ), # ROUTES app.include_router(api_routers, prefix=config.API_V1_STR) # MIDDLEWARES app.add_middleware(DBConnection) app.add_middleware(DBException) app.add_middleware(FieldValidation) # Function to create a initial super_user from app.db.init_db import init_db from app.db.session import db_session init_db(db_session)
def init() -> None: db = SessionLocal() init_db(db) # if the default-store does not exist, create it and some more # sample data code = settings.DEFAULT_STORE if crud.store.get_by_code(db, code=code): return # create default-store store_in = StoreCreate(code=code) default_store = crud.store.create(db=db, obj_in=store_in) # create mm-berlin store_in = StoreCreate(code="mm-berlin") mm_berlin = crud.store.create(db=db, obj_in=store_in) # create some funny brands brand_in = BrandCreate(name="Cat co.") cat_brand = crud.brand.create(db=db, obj_in=brand_in) brand_in = BrandCreate(name="Dog inc.") dog_brand = crud.brand.create(db=db, obj_in=brand_in) # create some funny categories category_in = ProductCategoryCreate(name="cat-toys") cat_category = crud.product_category.create(db=db, obj_in=category_in) category_in = ProductCategoryCreate(name="dog-toys") dog_category = crud.product_category.create(db=db, obj_in=category_in) # create some funny products product_in = ProductCreate(name="piece of string", category_id=cat_category.id, brand_id=cat_brand.id) cat_product_1 = crud.product.create(db=db, obj_in=product_in) product_in = ProductCreate(name="leaf", category_id=cat_category.id, brand_id=cat_brand.id) cat_product_2 = crud.product.create(db=db, obj_in=product_in) product_in = ProductCreate(name="stick", category_id=dog_category.id, brand_id=dog_brand.id) dog_product_1 = crud.product.create(db=db, obj_in=product_in) product_in = ProductCreate(name="ball", category_id=dog_category.id, brand_id=dog_brand.id) dog_product_2 = crud.product.create(db=db, obj_in=product_in) # stock the shops with our supply :) # first cat-products stock_level_in = StockLevelCreate(product_id=cat_product_1.id, store_id=default_store.id, amount=10) crud.stock_level.create(db=db, obj_in=stock_level_in) stock_level_in = StockLevelCreate(product_id=cat_product_2.id, store_id=default_store.id, amount=20) crud.stock_level.create(db=db, obj_in=stock_level_in) stock_level_in = StockLevelCreate(product_id=cat_product_1.id, store_id=mm_berlin.id, amount=5) crud.stock_level.create(db=db, obj_in=stock_level_in) stock_level_in = StockLevelCreate(product_id=cat_product_2.id, store_id=mm_berlin.id, amount=8) crud.stock_level.create(db=db, obj_in=stock_level_in) # now dog-products stock_level_in = StockLevelCreate(product_id=dog_product_1.id, store_id=default_store.id, amount=4) crud.stock_level.create(db=db, obj_in=stock_level_in) stock_level_in = StockLevelCreate(product_id=dog_product_2.id, store_id=default_store.id, amount=23) crud.stock_level.create(db=db, obj_in=stock_level_in) stock_level_in = StockLevelCreate(product_id=dog_product_1.id, store_id=mm_berlin.id, amount=7) # unfortunately mm-berlin run out of balls :( crud.stock_level.create(db=db, obj_in=stock_level_in) stock_level_in = StockLevelCreate(product_id=dog_product_2.id, store_id=mm_berlin.id, amount=0) crud.stock_level.create(db=db, obj_in=stock_level_in)
def init() -> None: db = TestingSessionLocal() init_db(db)
def main() -> None: logger.info('Begin seed') db = SessionLocal() init_db(db) logger.info('End seed')
def init_data(): db = SessionLocal() init_db(db)
def db_init(): db = SessionLocal() init_db(db)
def setup(): init_db(db_session)
def initialise(ctx): """Create initial data""" logger.info("Creating initial data") db = SessionLocal() init_db(db) logger.info("Initial data created")
def init() -> None: db = AsyncSessionLocal init_db(db)
def init() -> None: db = Session() init_db(db)
def init() -> None: db = SessionLocal() init_db(db)
from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from app.core.config import settings from app.db.init_db import init_db # We use the same database simply for easier developing engine = create_engine( settings.SQLALCHEMY_DATABASE_URI, pool_pre_ping=True, ) TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) # Initalize the database whenever we restart the server init_db(TestingSessionLocal())