Ejemplo n.º 1
0
def creade_db(engine):
    engine = sa.create_engine(engine)
    metadata.create_all(engine)

    with engine.begin() as connection:
        for i, animal in enumerate(['cats', 'dogs', 'parrots'], start=1):
            statement = vote_results.insert().values(id=i,
                                                     name=animal,
                                                     votes=0)
            connection.execute(statement)
Ejemplo n.º 2
0
def create():
    engine = sa.create_engine('sqlite:///my_db.sqlite')
    metadata.create_all(engine)

    with engine.begin() as connection:
        for i, animal in enumerate(['cats', 'dogs', 'parrots'], start=1):
            statement = vote_results.insert().values(
                id=i,
                name=animal,
                votes=0
            )
            connection.execute(statement)
    print("\n********************************\n********* БАЗА СОЗДАНА *********\n********************************\n  ")
Ejemplo n.º 3
0
from fastapi import FastAPI

from app.db import metadata, engine, database
from app.api import ping, notes

metadata.create_all(engine)

app = FastAPI()


@app.on_event('startup')
async def startup():
    await database.connect()


@app.on_event('shutdown')
async def shutdown():
    await database.disconnect()


app.include_router(ping.router)
app.include_router(notes.router, prefix='/notes', tags=['notes'])
Ejemplo n.º 4
0
async def test_db():
    metadata.create_all(engine)
    await database.connect()
    yield database
    metadata.drop_all(engine)
    await database.disconnect()
Ejemplo n.º 5
0
def main():
    metadata.create_all(engine)
    command.stamp(config, "head")
Ejemplo n.º 6
0
import contextlib
from typing import List, Dict, Any, Mapping
from sqlalchemy import create_engine, Table
from databases import Database

from app.db import metadata
import app.config as cfg

SQLALCHEMY_DATABASE_URL = cfg.TEST_DATABASE_URL
engine = create_engine(SQLALCHEMY_DATABASE_URL)
metadata.create_all(bind=engine)
database = Database(SQLALCHEMY_DATABASE_URL)


async def reset_test_db():
    """
    Delete all rows from the database but keeps the schemas
    """

    with contextlib.closing(engine.connect()) as con:
        trans = con.begin()
        for table in reversed(metadata.sorted_tables):
            con.execute(table.delete())
            con.execute(f"ALTER SEQUENCE {table}_id_seq RESTART WITH 1")
        trans.commit()


async def populate_db(test_db: Database,
                      table: Table,
                      data: List[Dict[str, Any]],
                      remove_ids: bool = True) -> None: