Skip to content

Commit

Permalink
dev(auth): some work
Browse files Browse the repository at this point in the history
  • Loading branch information
kitanoyoru committed Oct 6, 2023
1 parent fae6051 commit ad12de8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
21 changes: 18 additions & 3 deletions apps/auth/src/cli/commands/reset_database_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
import logging
import alembic.command

from contextlib import asynccontextmanager

import httpx

import hydra
from sqlalchemy import text
from sqlalchemy.ext.asyncio import AsyncEngine
from src import database

from src.config import AppConfig, DatabaseConfig
from src.constants import Constants
from src.crawler import load_repository_urls
from src.models import ModelType

logger = logging.getLogger(__name__)

Expand All @@ -35,6 +39,8 @@ async def _reset_db(db_config: DatabaseConfig):
alembic.command.stamp(config, "base", purge=True)
alembic.command.upgrade(config, "head")

engine = database.create_async_engine_from_conf(db_config)

# To populate db after shutdown
urls = load_repository_urls()

Expand All @@ -44,13 +50,22 @@ async def _reset_db(db_config: DatabaseConfig):
retries=Constants.HTTPX_RETRIES,
)
) as client:
tasks = [_import_repository(engine, client, url) for url in urls.values()]
tasks = [_import_repository(engine, client, model, url) for model, url in urls.items()]
await asyncio.gather(*tasks)


async def _import_repository(engine: Engine, client: https.AsyncClient, url: str):
...
async def _import_repository(engine: AsyncEngine, client: https.AsyncClient, model: str, url: str):
match model:
case ModelType.LOG.value:
async for log in load_logs(client, url):
async with _create_service(engine) as service:
service.save_log(log)


@asynccontextmanager
async def _create_service(engine: AsyncEngine):
async with service.from_engine(engine) as service:
yield service

@click.command()
@click.option("--sources-file", help="Sources file to populate database after migration")
Expand Down
9 changes: 9 additions & 0 deletions apps/auth/src/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from enum import IntEnum, auto

from .log import Base


class ModelType(IntEnum):
LOG = auto()


2 changes: 1 addition & 1 deletion apps/auth/src/models/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Base(DeclarativeBase):


class LogModel(Base):
__tablename__ = "event"
__tablename__ = "log"

id: Mapped[int] = mapped_column(Integer, primary_key=True)

Expand Down

0 comments on commit ad12de8

Please sign in to comment.