Coverage for frappe_manager / site_manager / modules / bench_database.py: 47%
34 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-07-02 18:13 +0530
« prev ^ index » next coverage.py v7.13.5, created at 2026-07-02 18:13 +0530
1"""
2BenchDatabase Module
4Handles database operations for the bench including:
5- Database connection information retrieval
6- Database and user removal
7- Common site config synchronization
8"""
10from pathlib import Path
11from typing import TYPE_CHECKING
13from frappe_manager.output_manager import OutputHandler
14from frappe_manager.output_manager.rich_output import RichOutputHandler
15from frappe_manager.utils.helpers import get_container_name_prefix, get_bench_connection_config
16from frappe_manager.utils.site import get_bench_db_connection_info
18if TYPE_CHECKING:
19 from frappe_manager.services_manager.services import ServicesManager
22class BenchDatabase:
23 """
24 Manages database operations for a bench.
26 Responsibilities:
27 - Get database connection information
28 - Remove database and user from global-db
29 - Sync common site config with database settings
30 """
32 def __init__(
33 self,
34 bench_name: str,
35 bench_path: Path,
36 services: "ServicesManager",
37 set_common_bench_config_fn,
38 output_handler: OutputHandler | None = None,
39 ):
40 """
41 Initialize BenchDatabase module.
43 Args:
44 bench_name: Name of the bench
45 bench_path: Path to bench directory
46 services: Services manager instance
47 set_common_bench_config_fn: Callable to set common bench config
48 output_handler: Optional output handler for displaying information
49 """
50 self.bench_name = bench_name
51 self.bench_path = bench_path
52 self.services = services
53 self.set_common_bench_config = set_common_bench_config_fn
54 self.output = output_handler or RichOutputHandler()
56 def get_connection_info(self) -> dict:
57 """
58 Get database connection information for the bench.
60 Returns:
61 dict: Database connection info containing name, user, password, host, port
62 """
63 return get_bench_db_connection_info(self.bench_name, self.bench_path)
65 def remove_database_and_user(self):
66 """
67 Remove database and user from global-db for this bench.
69 This function removes both the database and user associated with the bench
70 from the global database service.
71 """
72 bench_db_info = self.get_connection_info()
73 self.output.change_head("Removing bench db and db users from global-db")
75 if "name" in bench_db_info:
76 db_name = bench_db_info["name"]
77 db_user = bench_db_info["user"]
79 # Remove database
80 if not self.services.database_manager.check_db_exists(db_name):
81 self.output.warning(f"global-db: Bench db [blue]{db_name}[/blue] not found. Skipping..")
82 else:
83 self.services.database_manager.remove_db(db_name)
84 self.output.print(f"global-db: Removed bench db [blue]{db_name}[/blue]")
86 # Remove user
87 if not self.services.database_manager.check_user_exists(db_user):
88 self.output.warning(f"global-db: Bench db user [blue]{db_user}[/blue] not found. Skipping..")
89 else:
90 self.services.database_manager.remove_user(db_user, remove_all_host=True)
91 self.output.print(f"global-db: Removed bench db users [blue]{db_user}[/blue]")
93 def sync_common_site_config(self, services_db_host: str, services_db_port: int):
94 """
95 Sync the common site configuration with the global database information.
97 This function sets the common site configuration data including the socketio port,
98 database host and port, and the Redis cache, queue, and socketio URLs.
100 Args:
101 services_db_host: Database host from services
102 services_db_port: Database port from services
103 """
104 container_prefix = get_container_name_prefix(self.bench_name)
105 common_site_config_data = get_bench_connection_config(container_prefix, services_db_host, services_db_port)
106 common_site_config_data["socketio_port"] = "80"
107 self.set_common_bench_config(common_site_config_data)