Skip to content

Commit 925e877

Browse files
zjwu0522claude
andcommitted
🐛 fix: remove duplicate _setup_database method
Remove the first duplicate _setup_database method, keeping the second one which has better error checking (checks for "ERROR" in stderr instead of just non-zero return code from pg_restore). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 4e6b79f commit 925e877

1 file changed

Lines changed: 0 additions & 59 deletions

File tree

src/mcp_services/postgres/postgres_state_manager.py

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -77,65 +77,6 @@ def _test_connection(self):
7777
conn = psycopg2.connect(**self.conn_params, database="postgres")
7878
conn.close()
7979

80-
def _setup_database(self):
81-
"""Setup all required databases by downloading and restoring from backup."""
82-
databases = ['employees', 'chinook', 'dvdrental', 'sports', 'lego']
83-
84-
for db_name in databases:
85-
if not self._database_exists(db_name):
86-
logger.info(f"Setting up {db_name} database...")
87-
88-
# Path to backup file
89-
backup_dir = Path(__file__).parent.parent.parent.parent / "postgres_state"
90-
backup_file = backup_dir / f"{db_name}.backup"
91-
92-
# Download backup if not exists
93-
if not backup_file.exists():
94-
backup_dir.mkdir(parents=True, exist_ok=True)
95-
logger.info(f"Downloading {db_name} backup...")
96-
try:
97-
import urllib.request
98-
urllib.request.urlretrieve(
99-
f'https://storage.mcpmark.ai/postgres/{db_name}.backup',
100-
str(backup_file)
101-
)
102-
logger.info(f"{db_name} backup downloaded")
103-
except Exception as e:
104-
logger.warning(f"Failed to download {db_name} backup: {e}")
105-
continue
106-
107-
# Create database
108-
try:
109-
self._create_empty_database(db_name)
110-
logger.info(f"Created {db_name} database")
111-
except Exception as e:
112-
logger.warning(f"Failed to create {db_name} database: {e}")
113-
continue
114-
115-
# Restore from backup
116-
env = os.environ.copy()
117-
env['PGPASSWORD'] = self.password
118-
119-
try:
120-
result = subprocess.run([
121-
'pg_restore',
122-
'-h', str(self.host),
123-
'-p', str(self.port),
124-
'-U', self.username,
125-
'-d', db_name,
126-
'-v',
127-
str(backup_file)
128-
], env=env, capture_output=True, text=True)
129-
130-
if result.returncode != 0:
131-
logger.warning(f"pg_restore had errors for {db_name}: {result.stderr}")
132-
else:
133-
logger.info(f"{db_name} database restored successfully")
134-
except Exception as e:
135-
logger.warning(f"Failed to restore {db_name} database: {e}")
136-
else:
137-
logger.debug(f"{db_name} database already exists")
138-
13980
def _setup_database(self):
14081
"""Setup all required databases by downloading and restoring from backup."""
14182
databases = ['employees', 'chinook', 'dvdrental', 'sports', 'lego']

0 commit comments

Comments
 (0)