Really-amin commited on
Commit
2002286
·
verified ·
1 Parent(s): f26bf97

Upload 317 files

Browse files
Files changed (1) hide show
  1. database/__init__.py +16 -39
database/__init__.py CHANGED
@@ -1,19 +1,11 @@
1
  """Database package exports.
2
 
3
- This package exposes both the new SQLAlchemy-based ``DatabaseManager`` used by the
4
- API monitoring subsystem, and the legacy SQLite-backed database implementation
5
- used by the older crypto dashboard code.
6
-
7
- Compatibility guarantees
8
- ------------------------
9
- - Code that does ``from database import Database`` will receive the legacy
10
- implementation if ``database.py`` exists and defines either ``Database`` or
11
- ``CryptoDatabase``. Otherwise it will fall back to ``DatabaseManager``.
12
- - Code that does ``from database import CryptoDatabase`` will likewise receive
13
- the legacy implementation when available, with a safe fallback to
14
- ``DatabaseManager``.
15
- - ``from database import db_manager`` continues to work and returns the
16
- ``database.db_manager`` module.
17
  """
18
 
19
  from importlib import util as _importlib_util
@@ -21,50 +13,35 @@ from pathlib import Path as _Path
21
  from typing import Optional as _Optional
22
 
23
  from .db_manager import DatabaseManager
24
- from . import db_manager as db_manager # re-export module for tests and callers
25
-
26
 
27
  def _load_legacy_database() -> _Optional[type]:
28
- """
29
- Try to load the legacy database class from the root ``database.py`` module.
30
-
31
- We support both the older ``Database`` name (as mentioned in some docs)
32
- and the newer ``CryptoDatabase`` class name used in the current codebase.
33
- """
34
- root_module_path = _Path(__file__).resolve().parent.parent / "database.py"
35
-
36
- if not root_module_path.exists():
37
  return None
38
 
39
- spec = _importlib_util.spec_from_file_location("legacy_database", root_module_path)
40
  if spec is None or spec.loader is None:
41
  return None
42
 
43
  module = _importlib_util.module_from_spec(spec)
44
  try:
45
- spec.loader.exec_module(module) # type: ignore[union-attr]
46
  except Exception:
47
  # If loading the legacy module fails we silently fall back to DatabaseManager
48
  return None
49
 
50
- # Prefer a class called "Database" if present, but also support "CryptoDatabase"
51
- legacy_cls = getattr(module, "Database", None)
52
- if legacy_cls is None:
53
- legacy_cls = getattr(module, "CryptoDatabase", None)
54
-
55
- return legacy_cls
56
 
57
 
58
  _LegacyDatabase = _load_legacy_database()
59
 
60
  if _LegacyDatabase is not None:
61
- # On projects that still use the legacy implementation, both public names
62
- # point to the same underlying class.
63
  Database = _LegacyDatabase
64
- CryptoDatabase = _LegacyDatabase
65
  else:
66
- # If the legacy module is missing or broken, fall back to the new manager.
67
  Database = DatabaseManager
68
- CryptoDatabase = DatabaseManager
69
 
70
- __all__ = ["DatabaseManager", "Database", "CryptoDatabase", "db_manager"]
 
 
 
 
1
  """Database package exports.
2
 
3
+ This package exposes both the new SQLAlchemy-based ``DatabaseManager`` and the
4
+ legacy SQLite-backed ``Database`` class that the existing application modules
5
+ still import via ``from database import Database``. During the transition phase
6
+ we dynamically load the legacy implementation from the root ``database.py``
7
+ module (renamed here as ``legacy_database`` when importing) and fall back to the
8
+ new manager if that module is unavailable.
 
 
 
 
 
 
 
 
9
  """
10
 
11
  from importlib import util as _importlib_util
 
13
  from typing import Optional as _Optional
14
 
15
  from .db_manager import DatabaseManager
 
 
16
 
17
  def _load_legacy_database() -> _Optional[type]:
18
+ """Load the legacy Database class from the root-level ``database.py`` if it exists."""
19
+ legacy_path = _Path(__file__).resolve().parent.parent / "database.py"
20
+ if not legacy_path.exists():
 
 
 
 
 
 
21
  return None
22
 
23
+ spec = _importlib_util.spec_from_file_location("legacy_database", legacy_path)
24
  if spec is None or spec.loader is None:
25
  return None
26
 
27
  module = _importlib_util.module_from_spec(spec)
28
  try:
29
+ spec.loader.exec_module(module)
30
  except Exception:
31
  # If loading the legacy module fails we silently fall back to DatabaseManager
32
  return None
33
 
34
+ return getattr(module, "Database", None)
 
 
 
 
 
35
 
36
 
37
  _LegacyDatabase = _load_legacy_database()
38
 
39
  if _LegacyDatabase is not None:
 
 
40
  Database = _LegacyDatabase
 
41
  else:
 
42
  Database = DatabaseManager
 
43
 
44
+ __all__ = ["DatabaseManager", "Database", "CryptoDatabase"]
45
+
46
+ # Backward-compatible alias for older imports
47
+ CryptoDatabase = Database