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

Add a migration param to config class for automatic database migration. #58

Open
hakancelikdev opened this issue Jul 19, 2023 · 1 comment

Comments

@hakancelikdev
Copy link
Owner

class PyPIPackageModel(DbmModel):
    username: str

    class Config:
        table_name = "usernames"
        unique_together = ("username",)
class PyPIPackageModel(DbmModel):
    user: str

    class Config:
        table_name = "usernames"
        unique_together = ("user",)
        migration = True
    def set_database_header(self):
        ann = get_obj_annotations(obj=self.model)
        db_headers = bytes(str({key: DATABASE_HEADER_MAPPING[value] for key, value in ann.items()}), "utf-8")

        with self as db:
            database_header: bytes | None
            if (database_header := db.get(DATABASE_HEADER_NAME, None)) is None or (database_header is not None and self.model._config.migration is True):
                db[DATABASE_HEADER_NAME] = db_headers

            else:
                # TODO: migrations
                assert database_header == db_headers, f"Database headers are not equal: '{database_header}' != '{db_headers}'"  # type: ignore[str-bytes-safe]  # noqa: E501

        setattr(self, DATABASE_HEADER_NAME, ann)
@hakancelikdev
Copy link
Owner Author

The place that makes operations on the db should be separated from the place that makes pythonic and the place that models the data.

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

No branches or pull requests

1 participant