From 5bdaa32a9e66f09fbbcadf3d4d9a475a67361492 Mon Sep 17 00:00:00 2001 From: Patrick Schneeweis Date: Fri, 24 Jul 2020 09:17:19 -0400 Subject: [PATCH 1/5] Decorator for closing db connection after func run --- aerich/cli.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/aerich/cli.py b/aerich/cli.py index 4feb041..4da07c1 100644 --- a/aerich/cli.py +++ b/aerich/cli.py @@ -1,3 +1,4 @@ +import functools import json import os import sys @@ -27,6 +28,16 @@ class Color(str, Enum): parser = ConfigParser() +def close_db(func): + @functools.wraps(func) + async def close_db_inner(*args, **kwargs): + result = await func(*args, **kwargs) + await Tortoise.close_connections() + return result + + return close_db_inner + + @click.group(context_settings={"help_option_names": ["-h", "--help"]}) @click.version_option(__version__, "-V", "--version") @click.option( From 9bd96a9487245f4675eb911336aa407417ecc3ab Mon Sep 17 00:00:00 2001 From: Patrick Schneeweis Date: Fri, 24 Jul 2020 09:22:57 -0400 Subject: [PATCH 2/5] Use close_db decorator on command functions --- aerich/cli.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/aerich/cli.py b/aerich/cli.py index 4da07c1..3dfa6ac 100644 --- a/aerich/cli.py +++ b/aerich/cli.py @@ -81,6 +81,7 @@ async def cli(ctx: Context, config, app, name): @cli.command(help="Generate migrate changes file.") @click.option("--name", default="update", show_default=True, help="Migrate name.") @click.pass_context +@close_db async def migrate(ctx: Context, name): config = ctx.obj["config"] location = ctx.obj["location"] @@ -95,6 +96,7 @@ async def migrate(ctx: Context, name): @cli.command(help="Upgrade to latest version.") @click.pass_context +@close_db async def upgrade(ctx: Context): config = ctx.obj["config"] app = ctx.obj["app"] @@ -121,6 +123,7 @@ async def upgrade(ctx: Context): @cli.command(help="Downgrade to previous version.") @click.pass_context +@close_db async def downgrade(ctx: Context): app = ctx.obj["app"] config = ctx.obj["config"] @@ -143,6 +146,7 @@ async def downgrade(ctx: Context): @cli.command(help="Show current available heads in migrate location.") @click.pass_context +@close_db async def heads(ctx: Context): app = ctx.obj["app"] versions = Migrate.get_all_version_files() @@ -207,6 +211,7 @@ async def init( show_default=True, ) @click.pass_context +@close_db async def init_db(ctx: Context, safe): config = ctx.obj["config"] location = ctx.obj["location"] From 21001c0eda0e8b8e4764dd45b3282a81969ee712 Mon Sep 17 00:00:00 2001 From: Patrick Schneeweis Date: Fri, 24 Jul 2020 09:24:43 -0400 Subject: [PATCH 3/5] Make history command into async function The close_db decorator requires async functions --- aerich/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aerich/cli.py b/aerich/cli.py index 3dfa6ac..b637f4c 100644 --- a/aerich/cli.py +++ b/aerich/cli.py @@ -161,7 +161,7 @@ async def heads(ctx: Context): @cli.command(help="List all migrate items.") @click.pass_context -def history(ctx): +async def history(ctx): versions = Migrate.get_all_version_files() for version in versions: click.secho(version, fg=Color.green) From b97ce0ff2f81d76889b73cb625ca38d7e4bb6856 Mon Sep 17 00:00:00 2001 From: Patrick Schneeweis Date: Fri, 24 Jul 2020 09:26:29 -0400 Subject: [PATCH 4/5] Use close_db decorator on history command --- aerich/cli.py | 1 + 1 file changed, 1 insertion(+) diff --git a/aerich/cli.py b/aerich/cli.py index b637f4c..3cb6ee0 100644 --- a/aerich/cli.py +++ b/aerich/cli.py @@ -161,6 +161,7 @@ async def heads(ctx: Context): @cli.command(help="List all migrate items.") @click.pass_context +@close_db async def history(ctx): versions = Migrate.get_all_version_files() for version in versions: From dfe13ea250bfa719f1c133b1f2c134dee078f775 Mon Sep 17 00:00:00 2001 From: Patrick Schneeweis Date: Sat, 25 Jul 2020 15:22:05 -0400 Subject: [PATCH 5/5] add type hint --- aerich/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aerich/cli.py b/aerich/cli.py index 3cb6ee0..3a2f5cd 100644 --- a/aerich/cli.py +++ b/aerich/cli.py @@ -162,7 +162,7 @@ async def heads(ctx: Context): @cli.command(help="List all migrate items.") @click.pass_context @close_db -async def history(ctx): +async def history(ctx: Context): versions = Migrate.get_all_version_files() for version in versions: click.secho(version, fg=Color.green)