Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Tradunsky
Copy link
Contributor

@Tradunsky Tradunsky commented Sep 28, 2023

tl;dr; Mitigates the issue: #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
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant