Отсев баз через source.exclude_databases

list_databases() прячит базы по массиву строк: подстрока без регистра
или маска со * ? [. Фильтр — единственное место рождения списка, поэтому
одинаков для меню, пульта и list; явное имя в CLI не трогает.
This commit is contained in:
2026-08-20 06:02:55 +03:00
parent 9404d1dd08
commit 975934373b
5 changed files with 65 additions and 1 deletions
+24 -1
View File
@@ -7,6 +7,7 @@
from __future__ import annotations
import argparse
import fnmatch
import getpass
import importlib.util
import json
@@ -358,8 +359,28 @@ def psql_exec(tools: dict, node: dict, dbname: str, sql: str, what: str,
run(cmd, env, what, dry_run)
def excluded(name: str, patterns) -> bool:
"""Прячется ли база фильтром: подстрока без регистра, а со звёздочкой — маска."""
low = name.lower()
for raw in patterns or []:
pattern = str(raw).strip().lower()
if not pattern:
continue
if any(ch in pattern for ch in "*?["):
if fnmatch.fnmatch(low, pattern):
return True
elif pattern in low:
return True
return False
def list_databases(tools: dict, node: dict, label: str) -> list:
"""Список баз сервера: (имя, человекочитаемый размер)."""
"""Список баз сервера: (имя, человекочитаемый размер).
Отсев по node.exclude_databases делается здесь, в единственном месте, где список
вообще рождается: и меню, и пульт, и `list` получают его уже без мусорных баз.
Явное имя базы в CLI фильтр не трогает — оно списком не ходит.
"""
admin_db = node.get("maintenance_database") or node.get("database") or "postgres"
env = conn_env(node, label, admin_db)
sql = (
@@ -375,6 +396,8 @@ def list_databases(tools: dict, node: dict, label: str) -> list:
if not line.strip():
continue
name, _, size = line.partition("\t")
if excluded(name, node.get("exclude_databases")):
continue
rows.append((name, size or ""))
return rows