Skip to content

Commit

Permalink
refactor final commands
Browse files Browse the repository at this point in the history
  • Loading branch information
majsan committed Jan 30, 2024
1 parent f4735b8 commit ec9a101
Show file tree
Hide file tree
Showing 26 changed files with 72 additions and 308 deletions.
12 changes: 4 additions & 8 deletions karp-backend/src/karp/cliapp/subapps/database_subapp.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import typer # noqa: D100, I001

from karp.main.migrations import use_cases as migration_usecases

import typer
from karp.main.migrations.use_cases import run_migrations_down, run_migrations_up

subapp = typer.Typer()

Expand All @@ -10,16 +8,14 @@
def migrations_up( # noqa: ANN201, D103
ctx: typer.Context,
):
uc = migration_usecases.RunningMigrationsUp()
uc.execute(migration_usecases.RunMigrationsUp())
run_migrations_up()


@subapp.command(name="down")
def migrations_down( # noqa: ANN201, D103
ctx: typer.Context,
):
uc = migration_usecases.RunningMigrationsDown()
uc.execute(migration_usecases.RunMigrationsDown())
run_migrations_down()


def init_app(app: typer.Typer) -> None: # noqa: D103
Expand Down
22 changes: 10 additions & 12 deletions karp-backend/src/karp/cliapp/subapps/entry_repo_subapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
from tabulate import tabulate
import typer

from karp import lex
from karp.lex_core.value_objects import UniqueId # noqa: F401
from karp.command_bus import CommandBus
from karp.lex_core.value_objects.unique_id import UniqueIdStr # noqa: F401
from karp.lex_core.commands import CreateEntryRepository
from karp.cliapp.typer_injector import inject_from_ctx
from karp.lex_infrastructure import SqlListEntryRepos, SqlReadOnlyEntryRepoRepository
from karp.resource_commands import ResourceCommands

subapp = typer.Typer()

Expand All @@ -21,18 +19,18 @@ def create(infile: typer.FileBinaryRead, ctx: typer.Context): # noqa: ANN201, D
except Exception as err: # noqa: BLE001
typer.echo(f"Error reading file '{infile.name}': {err!s}")
raise typer.Exit(123) from err
create_entry_repo = CreateEntryRepository.from_dict(
data,

resource_commands = inject_from_ctx(ResourceCommands, ctx)
name = data.pop("resource_id")
entry_repo = resource_commands.create_entry_repository(
name=name,
connection_str=data.pop("connection_str", None),
config=data,
user="local admin",
message="Entry repository created",
)

bus = inject_from_ctx(CommandBus, ctx) # type: ignore [misc]

bus.dispatch(create_entry_repo)

print(
f"Entry repository '{create_entry_repo.name}' with id '{create_entry_repo.id}' created."
)
print(f"Entry repository '{name}' with id '{entry_repo.entity_id}' created.")


@subapp.command()
Expand Down
7 changes: 0 additions & 7 deletions karp-backend/src/karp/command_bus/__init__.py

This file was deleted.

35 changes: 0 additions & 35 deletions karp-backend/src/karp/command_bus/core.py

This file was deleted.

40 changes: 6 additions & 34 deletions karp-backend/src/karp/lex/__init__.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,16 @@
import injector # noqa: I001

from karp.command_bus import CommandHandler
from karp.lex.application.repositories import (
EntryUowRepositoryUnitOfWork,
ResourceUnitOfWork,
)
from karp.lex_core.commands import (
CreateEntryRepository,
)
from karp.lex_core import commands
from karp.lex.domain.value_objects import EntrySchema
from karp.lex.application.use_cases import (
CreatingEntryRepo,
)
from karp.lex.application.queries import (
EntryDto,
EntryRepoDto,
GetHistoryDto,
HistoryDto,
EntryRepoDto,
)

from karp.lex.application.repositories import (
EntryUowRepositoryUnitOfWork,
ResourceUnitOfWork,
)
from karp.lex.domain.value_objects import EntrySchema

__all__ = [
# modules
"commands",
# commands
"CreateEntryRepository",
# use cases
"CreatingEntryRepo",
# dtos
"EntryDto",
"GetHistoryDto",
Expand All @@ -38,14 +21,3 @@
# value objects
"EntrySchema",
]


class Lex(injector.Module): # noqa: D101
@injector.provider
def create_entry_repository( # noqa: D102
self,
uow: EntryUowRepositoryUnitOfWork,
) -> CommandHandler[CreateEntryRepository]:
return CreatingEntryRepo(
entry_repo_uow=uow,
)
8 changes: 0 additions & 8 deletions karp-backend/src/karp/lex/application/use_cases/__init__.py

This file was deleted.

This file was deleted.

6 changes: 2 additions & 4 deletions karp-backend/src/karp/main/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import logging # noqa: F811
import asgi_correlation_id

from karp.lex import Lex
from karp.lex_infrastructure import GenericLexInfrastructure, LexInfrastructure
from karp.search_infrastructure import (
GenericSearchInfrastructure,
Expand All @@ -22,7 +21,7 @@
)
from karp.main import config, modules
from karp.main.modules import (
CommandBusMod,
CommandsMod,
Db,
EventBusMod,
ElasticSearchMod,
Expand Down Expand Up @@ -77,10 +76,9 @@ def _setup_dependency_injection(
return injector.Injector(
[
Db(engine),
CommandBusMod(),
CommandsMod(),
EventBusMod(),
ElasticSearchMod(es_url),
Lex(),
LexInfrastructure(),
GenericLexInfrastructure(),
Search(),
Expand Down
44 changes: 12 additions & 32 deletions karp-backend/src/karp/main/migrations/use_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,21 @@

from alembic.config import Config as AlembicConfig
import alembic
import pydantic

from karp import command_bus

alembic_cfg = AlembicConfig(Path(__file__).parent / "alembic.ini")
alembic_cfg.set_main_option("script_location", str(Path(__file__).parent))

class RunMigrationsUp(pydantic.BaseModel): # noqa: D101
to_revision: str = "head"

def run_migrations_up():
alembic.command.upgrade(
alembic_cfg,
"head",
)

class RunMigrationsDown(pydantic.BaseModel): # noqa: D101
to_revision: str = "base"


class RunningMigrationsBase: # noqa: D101
def __init__(self): # noqa: D107, ANN204
self.alembic_cfg = AlembicConfig(Path(__file__).parent / "alembic.ini")
self.alembic_cfg.set_main_option("script_location", str(Path(__file__).parent))


class RunningMigrationsUp( # noqa: D101
RunningMigrationsBase,
command_bus.CommandHandler[RunMigrationsUp],
):
def execute(self, command: RunMigrationsUp): # noqa: ANN201, D102
alembic.command.upgrade(
self.alembic_cfg,
command.to_revision,
)


class RunningMigrationsDown( # noqa: D101
RunningMigrationsBase, command_bus.CommandHandler[RunMigrationsDown]
):
def execute(self, command: RunMigrationsDown): # noqa: ANN201, D102
alembic.command.downgrade(
self.alembic_cfg,
command.to_revision,
)
def run_migrations_down():
alembic.command.downgrade(
alembic_cfg,
"base",
)
7 changes: 1 addition & 6 deletions karp-backend/src/karp/main/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
from sqlalchemy.engine import Connection, Engine
from sqlalchemy.orm import Session

from karp.command_bus import CommandBus, InjectorCommandBus
from karp.foundation.events import EventBus, InjectorEventBus
from karp.auth_infrastructure import (
AuthInfrastructure,
Expand Down Expand Up @@ -77,11 +76,7 @@ def get(self, key: Type[T], provider: Provider[T]) -> Provider[T]: # noqa: D102
request = injector.ScopeDecorator(RequestScope)


class CommandBusMod(injector.Module): # noqa: D101
@injector.provider
def command_bus(self, inj: injector.Injector) -> CommandBus: # noqa: D102
return InjectorCommandBus(inj)

class CommandsMod(injector.Module): # noqa: D101
@injector.provider
def entry_commands(
self, resource_uow: ResourceUnitOfWork, entry_repo_uow: EntryUowRepositoryUnitOfWork
Expand Down
22 changes: 22 additions & 0 deletions karp-backend/src/karp/resource_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from karp.lex.application.dtos import ResourceDto
from karp.lex.domain import entities
from karp.lex.domain.errors import IntegrityError, NoSuchEntryRepository, ResourceNotFound
from karp.lex_core.value_objects import make_unique_id
from karp.timings import utc_now

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -115,3 +116,24 @@ def delete_resource(self, resource_id, user, message):
uow.repo.save(entry_repo)
uow.post_on_commit(events)
uow.commit()


def create_entry_repository(self, name, config, user, message, connection_str=None):

entry_repo, events = self.entry_repo_uow.create(
id=make_unique_id(),
name=name,
config=config,
connection_str=connection_str,
user=user,
timestamp=utc_now(),
message=message,
)

logger.debug("Created entry repo", extra={"entry_repo": entry_repo})
with self.entry_repo_uow as uow:
logger.debug("Saving...")
uow.repo.save(entry_repo)
uow.post_on_commit(events)
uow.commit()
return entry_repo
2 changes: 0 additions & 2 deletions karp-backend/src/karp/search/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import injector

from karp.command_bus import CommandHandler
from karp.foundation.events import EventHandler
from karp.lex.domain import events as lex_events
from karp.search.domain import commands
Expand Down Expand Up @@ -30,7 +29,6 @@
from karp.search_infrastructure.transformers.generic_entry_transformer import (
GenericEntryTransformer,
)
from karp.search_infrastructure.transformers.generic_pre_processor import GenericPreProcessor


class Search(injector.Module): # noqa: D101
Expand Down
1 change: 0 additions & 1 deletion karp-backend/src/karp/search/application/use_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from karp.foundation import events as foundation_events

from karp import command_bus
from karp.lex.domain import events as lex_events
from karp.lex.application.queries import EntryDto
from karp.search.application.repositories import IndexUnitOfWork
Expand Down
4 changes: 2 additions & 2 deletions karp-lex-core/src/karp/lex_core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__version__ = "0.5.1" # noqa: D104


from karp.lex_core import commands, value_objects
from karp.lex_core import value_objects
from karp.lex_core.dtos import EntryDto, GenericEntryDto

__all__ = ["EntryDto", "GenericEntryDto", "value_objects", "commands"]
__all__ = ["EntryDto", "GenericEntryDto", "value_objects"]
7 changes: 0 additions & 7 deletions karp-lex-core/src/karp/lex_core/commands/__init__.py

This file was deleted.

Loading

0 comments on commit ec9a101

Please sign in to comment.