From 1bbc5cc33e6cf5bb41fb821fafc5a0ae58abf562 Mon Sep 17 00:00:00 2001 From: alex Date: Mon, 16 Dec 2024 12:25:49 +0100 Subject: [PATCH] Changes: - add "" around db names so bad names with spaces at end or start can be identified - check that no bad templates can be generated --- edgy/cli/templates/default/script.py.mako | 6 +-- edgy/cli/templates/plain/script.py.mako | 2 - edgy/cli/templates/url/script.py.mako | 4 +- tests/cli/main_multidb_nonidentifier.py | 63 +++++++++++++++++++++++ tests/cli/test_multidb_templates.py | 30 +++++++++++ tests/settings/multidb_nonidentifier.py | 7 +++ 6 files changed, 104 insertions(+), 8 deletions(-) create mode 100644 tests/cli/main_multidb_nonidentifier.py create mode 100644 tests/settings/multidb_nonidentifier.py diff --git a/edgy/cli/templates/default/script.py.mako b/edgy/cli/templates/default/script.py.mako index 97910855..ebf76472 100644 --- a/edgy/cli/templates/default/script.py.mako +++ b/edgy/cli/templates/default/script.py.mako @@ -34,8 +34,6 @@ def downgrade(engine_name: str = "") -> None: fn = globals().get(f"downgrade{hash_to_identifier(engine_name)}") if fn is not None: fn() - - <% from edgy import monkay db_names = monkay.settings.migrate_databases @@ -48,13 +46,13 @@ def downgrade(engine_name: str = "") -> None: def ${f"upgrade{hash_to_identifier(db_name or '')}"}(): # Migration of: - # ${db_name or 'main database'} + # ${f'"{db_name}"' if db_name else 'main database'} ${context.get(f"{db_name or ''}_upgrades", "pass")} def ${f"downgrade{hash_to_identifier(db_name or '')}"}(): # Migration of: - # ${db_name or 'main database'} + # ${f'"{db_name}"' if db_name else 'main database'} ${context.get(f"{db_name or ''}_downgrades", "pass")} % endfor diff --git a/edgy/cli/templates/plain/script.py.mako b/edgy/cli/templates/plain/script.py.mako index 2ff9196e..4b691307 100644 --- a/edgy/cli/templates/plain/script.py.mako +++ b/edgy/cli/templates/plain/script.py.mako @@ -27,8 +27,6 @@ def downgrade(engine_name: str = "") -> None: fn = globals().get(f"downgrade_{engine_name}") if fn is not None: fn() - - <% from edgy import monkay db_names = monkay.settings.migrate_databases diff --git a/edgy/cli/templates/url/script.py.mako b/edgy/cli/templates/url/script.py.mako index eb2bd8cf..d7a77e42 100644 --- a/edgy/cli/templates/url/script.py.mako +++ b/edgy/cli/templates/url/script.py.mako @@ -65,13 +65,13 @@ def downgrade(url: Optional[DatabaseURL] = None) -> None: def ${f"upgrade{hash_to_identifier(url_for_name(db_name))}"}(): # Migration of: - # ${url_for_name(db_name)} (${db_name or 'main database'}) + # ${url_for_name(db_name)} (${f'"{db_name}"' if db_name else 'main database'}) ${context.get(f"{db_name or ''}_upgrades", "pass")} def ${f"downgrade{hash_to_identifier(url_for_name(db_name))}"}(): # Migration of: - # ${url_for_name(db_name)} (${db_name or 'main database'}) + # ${url_for_name(db_name)} (${f'"{db_name}"' if db_name else 'main database'}) ${context.get(f"{db_name or ''}_downgrades", "pass")} % endfor diff --git a/tests/cli/main_multidb_nonidentifier.py b/tests/cli/main_multidb_nonidentifier.py new file mode 100644 index 00000000..b2d1ee47 --- /dev/null +++ b/tests/cli/main_multidb_nonidentifier.py @@ -0,0 +1,63 @@ +import os + +import pytest + +import edgy +from edgy import Instance +from edgy.contrib.permissions import BasePermission +from tests.settings import TEST_ALTERNATIVE_DATABASE, TEST_DATABASE + +pytestmark = pytest.mark.anyio +models = edgy.Registry( + database=TEST_DATABASE, + extra={"ano ther ": TEST_ALTERNATIVE_DATABASE}, + with_content_type=True, +) +basedir = os.path.abspath(os.path.dirname(__file__)) + + +class User(edgy.StrictModel): + name = edgy.fields.CharField(max_length=100) + + class Meta: + registry = models + + +class Group(edgy.StrictModel): + name = edgy.fields.CharField(max_length=100) + users = edgy.fields.ManyToMany("User", embed_through=False) + + class Meta: + registry = models + + +class Permission(BasePermission): + users = edgy.fields.ManyToMany("User", embed_through=False) + groups = edgy.fields.ManyToMany("Group", embed_through=False) + name_model: str = edgy.fields.CharField(max_length=100, null=True) + obj = edgy.fields.ForeignKey("ContentType", null=True) + + class Meta: + registry = models + unique_together = [("name", "name_model", "obj")] + + +class Signal(edgy.StrictModel): + user = edgy.fields.ForeignKey(User, no_constraint=True) + signal_type = edgy.fields.CharField(max_length=100) + database = models.extra["ano ther "] + + class Meta: + registry = models + + +class Unrelated(edgy.StrictModel): + name = edgy.fields.CharField(max_length=100) + database = models.extra["ano ther "] + content_type = edgy.fields.ExcludeField() + + class Meta: + registry = models + + +edgy.monkay.set_instance(Instance(registry=models)) diff --git a/tests/cli/test_multidb_templates.py b/tests/cli/test_multidb_templates.py index 93d6a0cc..e1da8703 100644 --- a/tests/cli/test_multidb_templates.py +++ b/tests/cli/test_multidb_templates.py @@ -124,6 +124,36 @@ async def test_migrate_upgrade_multidb(app_flag, template_param): assert f.readline().strip() == "# Default env template" +@pytest.mark.parametrize( + "template_param", + [" -t default", " -t plain", " -t url"], + ids=["default", "plain", "url"], +) +async def test_multidb_nonidentifier(template_param): + os.chdir(base_path) + assert not (base_path / "migrations").exists() + (o, e, ss) = await arun_cmd( + "tests.cli.main_multidb_nonidentifier", + f"edgy init{template_param}", + with_app_environment=True, + extra_env={"EDGY_SETTINGS_MODULE": "tests.settings.multidb_nonidentifier.TestSettings"}, + ) + assert ss == 0 + assert b'Extra database name: "ano ther " starts or ends with whitespace characters.' in e + + (o, e, ss) = await arun_cmd( + "tests.cli.main_multidb_nonidentifier", + "edgy makemigrations", + with_app_environment=True, + extra_env={"EDGY_SETTINGS_MODULE": "tests.settings.multidb_nonidentifier.TestSettings"}, + ) + if "plain" in template_param: + assert ss == 1 + else: + assert ss == 0 + assert b"No changes in schema detected" not in o + + @pytest.mark.parametrize("app_flag", ["explicit", "explicit_env"]) @pytest.mark.parametrize( "template_param", diff --git a/tests/settings/multidb_nonidentifier.py b/tests/settings/multidb_nonidentifier.py new file mode 100644 index 00000000..7b5cc5c4 --- /dev/null +++ b/tests/settings/multidb_nonidentifier.py @@ -0,0 +1,7 @@ +from typing import Union + +from edgy.conf.global_settings import EdgySettings + + +class TestSettings(EdgySettings): + migrate_databases: list[Union[str, None]] = [None, "ano ther "]