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

Use aerich for database migrations #1110

Merged
merged 7 commits into from
Oct 2, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Minor fixes for database migrations with aerich
bigherc18 committed Oct 1, 2024
commit a4d588df74aa713a25eb17f0a21947312d7d7eb9
7 changes: 2 additions & 5 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -7,8 +7,5 @@
},
"files.associations": {
"*.env.default": "shellscript"
},
"cSpell.words": [
"aerich"
]
}
}
}
4 changes: 2 additions & 2 deletions src/dipdup/aerich.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from typing import TYPE_CHECKING


if TYPE_CHECKING:
from aerich import Command as AerichCommand
from collections.abc import Iterable
from pathlib import Path
from types import ModuleType

from aerich import Command as AerichCommand


async def create_aerich_command(db_url: str, package: str, migrations_dir: 'Path') -> 'AerichCommand':
"""Create and return an `AerichCommand` instance
9 changes: 5 additions & 4 deletions src/dipdup/cli.py
Original file line number Diff line number Diff line change
@@ -590,11 +590,12 @@ async def schema(ctx: click.Context) -> None:
migrations_dir = DipDupPackage(config.package_path).migrations

if not migrations_dir.exists():
echo(
f"""Database migrations is not initialize at {migrations_dir}.
Run `dipdup schema init` or just run `dipdup run` and it'll be initialized automatically."""
from dipdup.exceptions import ProjectPackageError

raise ProjectPackageError(
f"""Database migrations are not initialized at {migrations_dir}.
Run `dipdup schema init` or `dipdup run` to the run the indexer and it'll be initialized automatically."""
)
raise click.Abort()

from dipdup.aerich import create_aerich_command

4 changes: 1 addition & 3 deletions src/dipdup/database.py
Original file line number Diff line number Diff line change
@@ -69,14 +69,12 @@ async def tortoise_wrapper(

model_modules: dict[str, Iterable[str | ModuleType]] = {
'int_models': ['dipdup.models'],
'models': ['dipdup.models'],
}

if models:
if not models.endswith('.models'):
models += '.models'
# NOTE: The only way we can append to the list without getting yelled at by mypy
model_modules['models'] = [models, *model_modules['models']]
model_modules['models'] = [models, 'dipdup.models']

# NOTE: Must be called before entering Tortoise context
decimal_precision = decimal_precision or guess_decimal_precision(models)
1 change: 1 addition & 0 deletions src/dipdup/dipdup.py
Original file line number Diff line number Diff line change
@@ -940,6 +940,7 @@ async def _initialize_migrations(self) -> None:
raise
except FileExistsError as e:
from pathlib import Path

if Path(e.filename).is_relative_to(migrations_dir):
_logger.debug("Database migrations already initialized at '%s'", migrations_dir)

2 changes: 1 addition & 1 deletion src/dipdup/exceptions.py
Original file line number Diff line number Diff line change
@@ -76,7 +76,7 @@ class DatasourceError(Error):
def _help(self) -> str:
return f"""
`{self.datasource}` datasource returned an error:

"{self.msg}"

See https://dipdup.io/docs/getting-started/datasources