Coverage for src/pydal2sql_core/types.py: 100%
37 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-11-20 11:58 +0100
« prev ^ index » next coverage.py v7.2.7, created at 2023-11-20 11:58 +0100
1"""
2Contains types for core.py.
3"""
4import typing
5import warnings
6from typing import Any
8import pydal
9from pydal.adapters import SQLAdapter as _SQLAdapter
10from witchery import Empty
12SUPPORTED_DATABASE_TYPES = typing.Literal["psycopg2", "sqlite3", "pymysql"]
13DATABASE_ALIASES_PSQL = typing.Literal["postgresql", "postgres", "psql"]
14DATABASE_ALIASES_SQLITE = typing.Literal["sqlite"]
15DATABASE_ALIASES_MYSQL = typing.Literal["mysql"]
17DATABASE_ALIASES = DATABASE_ALIASES_PSQL | DATABASE_ALIASES_SQLITE | DATABASE_ALIASES_MYSQL
18SUPPORTED_DATABASE_TYPES_WITH_ALIASES = SUPPORTED_DATABASE_TYPES | DATABASE_ALIASES
20_SUPPORTED_OUTPUT_FORMATS = typing.Literal["default", "edwh-migrate"]
21SUPPORTED_OUTPUT_FORMATS = _SUPPORTED_OUTPUT_FORMATS | None
22DEFAULT_OUTPUT_FORMAT: SUPPORTED_OUTPUT_FORMATS = "default"
25class SQLAdapter(_SQLAdapter): # type: ignore
26 """
27 Typing friendly version of pydal's SQL Adapter.
28 """
31empty = Empty()
34class CustomAdapter(SQLAdapter):
35 """
36 Adapter that prevents actual queries.
37 """
39 drivers = ("sqlite3",)
41 def id_query(self, _: Any) -> Empty: # pragma: no cover
42 """
43 Normally generates table._id != None.
44 """
45 warnings.warn("Prevented attempt to execute query while migrating.")
46 return empty
48 def execute(self, *_: Any, **__: Any) -> Empty:
49 """
50 Normally executes an SQL query on the adapter.
51 """
52 warnings.warn("Prevented attempt to execute query while migrating.")
53 return empty
55 @property
56 def cursor(self) -> Empty:
57 """
58 Trying to connect to the database.
59 """
60 warnings.warn("Prevented attempt to execute query while migrating.")
61 return empty
64class DummyDAL(pydal.DAL): # type: ignore
65 """
66 Subclass of DAL that disables committing.
67 """
69 def commit(self) -> None:
70 """
71 Do Nothing.
72 """
74 def __getattribute__(self, item: str) -> Any:
75 """
76 Replace dal._adapter with a custom adapter that doesn't run queries.
77 """
78 if item == "_adapter":
79 return CustomAdapter(self, "", adapter_args={"driver": "sqlite3"}, driver_args="")
81 return super().__getattribute__(item)
83 def __call__(self, *_: Any, **__: Any) -> Empty:
84 """
85 Prevents calling db() and thus creating a query.
86 """
87 return empty
90try:
91 import typedal
93 class DummyTypeDAL(typedal.TypeDAL, DummyDAL):
94 """
95 Variant of DummyDAL for TypeDAL.
96 """
98except ImportError: # pragma: no cover
99 DummyTypeDAL = DummyDAL # type: ignore