AutoPackAI / beebot

An Autonomous AI Agent that works
MIT License
403 stars 71 forks source link

#17: Fail fast when migrations dir cannot be resolved #22

Open Tradunsky opened 1 year ago

Tradunsky commented 1 year ago

tl;dr; Mitigates the issue: https://github.com/AutoPackAI/beebot/issues/17

Details: When application is running by IDE generated quick run play button: зображення

It automatically sets working directory to beebot/initiator sub-directory of the repository, because such entry point is located in that directory. At the same time, when yoyo reads migrations:

migrations = read_migrations("migrations")

it does use relative to the working directory path, however, even if such path does not exist, the library silently returns empty MigrationList: зображення which leads to missing schema and fails later in the code path that accesses the entity.

This approach fails fast preventing application from running on the unexpected working directory.

Alternative approach would be to resolve migrations directory relative to database_models.py:

    migrations_dir = "migrations"
    models_dir = os.path.dirname(__file__)
    migrations = Path(models_dir).parent.parent.resolve().joinpath(migrations_dir).absolute()
    if not migrations.exists():
        raise FileNotFoundError(f"\"{migrations_dir}\" directory not found. ")
    migrations_dir = str(migrations)
    migrations = read_migrations(migrations_dir)

With this approach the part responsible for migrations will successfully locate migrations in regardless of working directory: зображення At the same time, the code becomes coupled to the database_models.py and migrations locations and also does not fix possibility for other places in code to rely on the repository layout.

Best practice in general would be to locate migrations within source code module, so it can be referenced from the source code root as python modules do right now.

Please let me know if this simple fail fast solution enough or you prefer anything else.

Tradunsky commented 1 year ago

@erik-megarad tagging you here as I cannot add reviewers.