From 557287671432c4b72387df17a471d35d13b84037 Mon Sep 17 00:00:00 2001 From: Waket Zheng Date: Wed, 11 Dec 2024 13:26:18 +0800 Subject: [PATCH] fix: NonExistentKey when running `aerich init` without `[tool]` section in config file (#381) * fix: NonExistentKey when running `aerich init` without `[tool]` section in config file * docs: update changelog --- CHANGELOG.md | 1 + aerich/cli.py | 5 ++++- tests/test_sqlite_migrate.py | 6 ++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ff66ca..7c7f91d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### [0.8.1](Unreleased) #### Fixed +- Fix NonExistentKey when running `aerich init` without `[tool]` section in config file. (#284) - Fix configuration file reading error when containing Chinese characters. (#286) - sqlite: failed to create/drop index. (#302) - PostgreSQL: Cannot drop constraint after deleting or rename FK on a model. (#378) diff --git a/aerich/cli.py b/aerich/cli.py index 25aeb8c..b5d446d 100644 --- a/aerich/cli.py +++ b/aerich/cli.py @@ -190,7 +190,10 @@ async def init(ctx: Context, tortoise_orm, location, src_folder) -> None: table["tortoise_orm"] = tortoise_orm table["location"] = location table["src_folder"] = src_folder - doc["tool"]["aerich"] = table + try: + doc["tool"]["aerich"] = table + except KeyError: + doc["tool"] = {"aerich": table} config_path.write_text(tomlkit.dumps(doc)) diff --git a/tests/test_sqlite_migrate.py b/tests/test_sqlite_migrate.py index e1478a0..568f273 100644 --- a/tests/test_sqlite_migrate.py +++ b/tests/test_sqlite_migrate.py @@ -230,3 +230,9 @@ def test_sqlite_migrate(tmp_path: Path) -> None: p.unlink() run_aerich("aerich init-db") assert db_file.exists() + + # init without '[tool]' section in pyproject.toml + config_file = Path("pyproject.toml") + config_file.write_text('[project]\nname = "project"') + run_aerich("init -t settings.TORTOISE_ORM") + assert "[tool.aerich]" in config_file.read_text()