-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): Adds drop-all option to the litestar cli (#226)
* feat(cli): Adds drop-all option to the litestar cli * fix(mypy): CI failure * tests(cli): Add tests for drop-all * chore: remove unnecessary import * feat: updated tests * fix: 3.8 and 3.9 support * chore: revert later * revert: "chore: revert later" This reverts commit 4091abd. * chore: 3.8/3.9 support * chore: add eval type check * chore: fix lint * feat: updated linting --------- Co-authored-by: Alc-Alc <alc@localhost> Co-authored-by: Cody Fincher <cody.fincher@gmail.com>
- Loading branch information
1 parent
da87543
commit d3f8cfd
Showing
11 changed files
with
326 additions
and
231 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from litestar.cli._utils import console | ||
from sqlalchemy import Engine, MetaData, Table | ||
from typing_extensions import TypeIs | ||
|
||
if TYPE_CHECKING: | ||
from sqlalchemy.ext.asyncio import AsyncEngine | ||
|
||
|
||
async def drop_all(engine: AsyncEngine | Engine, version_table_name: str, metadata: MetaData) -> None: | ||
def _is_sync(engine: Engine | AsyncEngine) -> TypeIs[Engine]: | ||
return isinstance(engine, Engine) | ||
|
||
def _drop_tables_sync(engine: Engine) -> None: | ||
console.rule("[bold red]Connecting to database backend.") | ||
with engine.begin() as db: | ||
console.rule("[bold red]Dropping the db", align="left") | ||
metadata.drop_all(db) | ||
console.rule("[bold red]Dropping the version table", align="left") | ||
Table(version_table_name, metadata).drop(db, checkfirst=True) | ||
console.rule("[bold yellow]Successfully dropped all objects", align="left") | ||
|
||
async def _drop_tables_async(engine: AsyncEngine) -> None: | ||
console.rule("[bold red]Connecting to database backend.", align="left") | ||
async with engine.begin() as db: | ||
console.rule("[bold red]Dropping the db", align="left") | ||
await db.run_sync(metadata.drop_all) | ||
console.rule("[bold red]Dropping the version table", align="left") | ||
await db.run_sync(Table(version_table_name, metadata).drop, checkfirst=True) | ||
console.rule("[bold yellow]Successfully dropped all objects", align="left") | ||
|
||
if _is_sync(engine): | ||
return _drop_tables_sync(engine) | ||
return await _drop_tables_async(engine) |
This file contains 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 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 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 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 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.