Coverage for src/pydal2sql_core/types.py: 100%
43 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-08-05 17:25 +0200
« prev ^ index » next coverage.py v7.4.4, created at 2024-08-05 17:25 +0200
1"""
2Contains types for core.py.
3"""
5import typing
6import warnings
7from typing import Any
9import pydal
10from pydal.adapters import SQLAdapter as _SQLAdapter
11from witchery import Empty
13SUPPORTED_DATABASE_TYPES = typing.Literal["psycopg2", "sqlite3", "pymysql"]
14DATABASE_ALIASES_PSQL = typing.Literal["postgresql", "postgres", "psql"]
15DATABASE_ALIASES_SQLITE = typing.Literal["sqlite"]
16DATABASE_ALIASES_MYSQL = typing.Literal["mysql"]
18DATABASE_ALIASES = DATABASE_ALIASES_PSQL | DATABASE_ALIASES_SQLITE | DATABASE_ALIASES_MYSQL
19SUPPORTED_DATABASE_TYPES_WITH_ALIASES = SUPPORTED_DATABASE_TYPES | DATABASE_ALIASES
21_SUPPORTED_OUTPUT_FORMATS = typing.Literal["default", "edwh-migrate"]
22SUPPORTED_OUTPUT_FORMATS = _SUPPORTED_OUTPUT_FORMATS | None
23DEFAULT_OUTPUT_FORMAT: SUPPORTED_OUTPUT_FORMATS = "default"
26class SQLAdapter(_SQLAdapter): # type: ignore
27 """
28 Typing friendly version of pydal's SQL Adapter.
29 """
32empty = Empty()
35class CustomAdapter(SQLAdapter):
36 """
37 Adapter that prevents actual queries.
38 """
40 drivers = ("sqlite3",)
42 def id_query(self, _: Any) -> Empty: # pragma: no cover
43 """
44 Normally generates table._id != None.
45 """
46 warnings.warn("Prevented attempt to execute query while migrating.")
47 return empty
49 def execute(self, *_: Any, **__: Any) -> Empty:
50 """
51 Normally executes an SQL query on the adapter.
52 """
53 warnings.warn("Prevented attempt to execute query while migrating.")
54 return empty
56 @property
57 def cursor(self) -> Empty:
58 """
59 Trying to connect to the database.
60 """
61 warnings.warn("Prevented attempt to execute query while migrating.")
62 return empty
65class DummyDAL(pydal.DAL): # type: ignore
66 """
67 Subclass of DAL that disables committing.
68 """
70 def commit(self) -> None:
71 """
72 Do Nothing.
73 """
75 def __getattribute__(self, item: str) -> Any:
76 """
77 Replace dal._adapter with a custom adapter that doesn't run queries.
78 """
79 if item == "_adapter":
80 return CustomAdapter(self, "", adapter_args={"driver": "sqlite3"}, driver_args="")
82 return super().__getattribute__(item)
84 def __call__(self, *_: Any, **__: Any) -> Empty:
85 """
86 Prevents calling db() and thus creating a query.
87 """
88 return empty
91try:
92 import typedal
94 class DummyTypeDAL(typedal.TypeDAL, DummyDAL):
95 """
96 Variant of DummyDAL for TypeDAL.
97 """
99 def __init__(self, *args: Any, **settings: Any) -> None:
100 """
101 Force TypeDAL to ignore project/env settings.
102 """
103 # dummy typedal should not look at these settings:
104 settings["use_pyproject"] = False
105 settings["use_env"] = False
106 if not settings.get("folder"):
107 settings["folder"] = "/tmp/typedal2sql"
109 super().__init__(*args, **settings)
111except ImportError: # pragma: no cover
112 DummyTypeDAL = DummyDAL # type: ignore