Skip to content

Commit

Permalink
Changes:
Browse files Browse the repository at this point in the history
- add "" around db names so bad names with spaces at end or start can be
  identified
- check that no bad templates can be generated
  • Loading branch information
devkral committed Dec 16, 2024
1 parent 7df1b17 commit 1bbc5cc
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 8 deletions.
6 changes: 2 additions & 4 deletions edgy/cli/templates/default/script.py.mako
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
2 changes: 0 additions & 2 deletions edgy/cli/templates/plain/script.py.mako
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions edgy/cli/templates/url/script.py.mako
Original file line number Diff line number Diff line change
Expand Up @@ -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
63 changes: 63 additions & 0 deletions tests/cli/main_multidb_nonidentifier.py
Original file line number Diff line number Diff line change
@@ -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))
30 changes: 30 additions & 0 deletions tests/cli/test_multidb_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 7 additions & 0 deletions tests/settings/multidb_nonidentifier.py
Original file line number Diff line number Diff line change
@@ -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 "]

0 comments on commit 1bbc5cc

Please sign in to comment.