-
Notifications
You must be signed in to change notification settings - Fork 0
Switch database to Postgres #9
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9a85d9d
feat: migrate db from sqlite to postgres
ttnsy 1806e8e
refactor: change database config to use DATABASE_URL
ttnsy eda5c1a
refactor: update pre-commit hooks and test setup after postgres migra…
ttnsy 8951a74
chore: reconstruct DB URL, use in memory sqlite for test & change pos…
ttnsy 559c20b
fix: reconfig compose.yml, use explicit alias in DatabaseSettings, rm…
ttnsy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,32 @@ | ||
| from pathlib import Path | ||
| from typing import Literal | ||
|
|
||
| from pydantic import field_validator | ||
| from pydantic_settings import BaseSettings | ||
| from pydantic import Field | ||
| from pydantic_settings import BaseSettings, SettingsConfigDict | ||
|
|
||
| UPLOAD_DIR = Path("data") | ||
|
|
||
| # Used to catch missing DB_PATH. | ||
| # If this value is still set, it means DB_PATH wasn't configured. | ||
| DEFAULT_DB_PLACEHOLDER = "__MISSING_DB_PATH__" | ||
|
|
||
| class DatabaseSettings(BaseSettings): | ||
| model_config = SettingsConfigDict( | ||
| env_file=".env", extra="ignore" | ||
| ) | ||
|
|
||
| class Settings(BaseSettings): | ||
| db_path: str = DEFAULT_DB_PLACEHOLDER | ||
| database_engine: Literal["sqlite", "postgres"] = Field(default="sqlite") | ||
| database_host: str | ||
| database_port: int | ||
| database_user: str | ||
| database_password: str | ||
| database_db: str | ||
|
|
||
| @field_validator("db_path", mode="before") | ||
| @classmethod | ||
| def validate_db_path(cls, value): | ||
| if value == DEFAULT_DB_PLACEHOLDER: | ||
| raise ValueError("You must set DB_PATH in your environment or .env file.") | ||
| return value | ||
| @property | ||
| def database_url(self) -> str: | ||
| if self.database_engine == "sqlite": | ||
| return "sqlite:///:memory:" | ||
| return ( | ||
| f"postgresql+psycopg://{self.database_user}:{self.database_password}" | ||
| f"@{self.database_host}:{self.database_port}/{self.database_db}" | ||
| ) | ||
|
|
||
|
|
||
| settings = Settings() | ||
| settings = DatabaseSettings() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,17 @@ | ||
| from sqlalchemy.pool import StaticPool | ||
| from sqlmodel import create_engine | ||
|
|
||
| from app.config import settings | ||
|
|
||
| connect_args = {"check_same_thread": False} | ||
| engine = create_engine( | ||
| f"sqlite:///{settings.db_path}", echo=True, connect_args=connect_args | ||
| ) | ||
| if settings.database_engine == "sqlite": | ||
| engine = create_engine( | ||
| settings.database_url, | ||
| echo=True, | ||
| connect_args={"check_same_thread": False}, | ||
| poolclass=StaticPool, | ||
| ) | ||
| else: | ||
| engine = create_engine( | ||
| settings.database_url, | ||
| echo=True, | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.