Skip to content

Commit e1e5ef3

Browse files
authored
Add caching and filtering to gen_models. (#888)
Adds the following flags to `tools/gen_models.py`: - `-k` allows filtering for tests, similar to pytest - `--cache` and `--no-cache` which allows skipping schemas which have not changed - If caching, the schemas are copied to `.gen_models_cache`
1 parent cf71fec commit e1e5ef3

File tree

4 files changed

+289
-41
lines changed

4 files changed

+289
-41
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@ docs/_build
3636
/uv.lock
3737
.dir-locals.el
3838
.envrc
39+
.gen_models_cache

gel/_testbase.py

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import pickle
3636
import re
3737
import shutil
38-
import site
3938
import subprocess
4039
import sys
4140
import tempfile
@@ -65,12 +64,6 @@
6564
_unset = object()
6665

6766

68-
SITE_PACKAGES: typing.Final[pathlib.Path] = pathlib.Path(
69-
site.getsitepackages()[0]
70-
)
71-
MODELS_DEST: typing.Final[pathlib.Path] = SITE_PACKAGES / "models"
72-
73-
7467
@contextlib.contextmanager
7568
def silence_asyncio_long_exec_warning():
7669
def flt(log_record):
@@ -635,7 +628,7 @@ def get_database_name(cls):
635628
return dbname.lower()
636629

637630
@classmethod
638-
def get_schema_texts(cls) -> list[str]:
631+
def get_combined_schemas(cls) -> str:
639632
schema_texts: list[str] = []
640633

641634
# Look at all SCHEMA entries and potentially create multiple
@@ -647,7 +640,11 @@ def get_schema_texts(cls) -> list[str]:
647640
if schema_text := cls.get_schema_text(name):
648641
schema_texts.append(schema_text)
649642

650-
return schema_texts
643+
return "\n\n".join(st for st in schema_texts)
644+
645+
@classmethod
646+
def is_schema_field(cls, field: str) -> bool:
647+
return bool(re.match(r"^SCHEMA(?:_(\w+))?", field))
651648

652649
@classmethod
653650
def get_schema_text(cls, field: str) -> str | None:
@@ -678,12 +675,11 @@ def get_schema_text(cls, field: str) -> str | None:
678675
@classmethod
679676
def get_setup_script(cls):
680677
script = ""
681-
schema = "\n\n".join(st for st in cls.get_schema_texts())
682678

683679
# Don't wrap the script into a transaction here, so that
684680
# potentially it's easier to stitch multiple such scripts
685681
# together in a fashion similar to what `edb inittestdb` does.
686-
script += f"\nSTART MIGRATION TO {{ {schema} }};"
682+
script += f"\nSTART MIGRATION TO {{ {cls.get_combined_schemas()} }};"
687683
script += f"\nPOPULATE MIGRATION; \nCOMMIT MIGRATION;"
688684

689685
if cls.SETUP:
@@ -763,6 +759,7 @@ def adapt_call(cls, result):
763759

764760

765761
class BaseModelTestCase(DatabaseTestCase):
762+
SCHEMA: str
766763
DEFAULT_MODULE = "default"
767764

768765
client: typing.ClassVar[gel.Client]
@@ -790,7 +787,7 @@ def setUpClass(cls):
790787

791788
cls.tmp_model_dir = tempfile.TemporaryDirectory(**td_kwargs)
792789

793-
model_from_file, model_name = cls._model_info()
790+
_, model_name = cls._model_info()
794791

795792
if cls.orm_debug:
796793
print(cls.tmp_model_dir.name)
@@ -830,15 +827,6 @@ def setUpClass(cls):
830827
finally:
831828
gen_client.terminate()
832829

833-
if not model_from_file:
834-
# This is a direct schema, let's copy it to the site paths
835-
site_model_dir = MODELS_DEST / model_name
836-
if site_model_dir.exists():
837-
shutil.rmtree(site_model_dir)
838-
shutil.copytree(
839-
model_output_dir, site_model_dir, dirs_exist_ok=True
840-
)
841-
842830
sys.path.insert(0, cls.tmp_model_dir.name)
843831

844832
import models

tests/test_model_generator.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6583,6 +6583,8 @@ def test_model_gen_direct_01(self):
65836583
def test_model_gen_direct_02(self):
65846584
from models.TestModelGenDirect import default
65856585

6586+
# This still works because the actual test runs with a model generated
6587+
# after SETUP.
65866588
bar = default.Bar(x=12)
65876589
self.client.sync(bar)
65886590

0 commit comments

Comments
 (0)