From 6fe4384d91f29f2e4cfb09f8428f6c1edc9dfabc Mon Sep 17 00:00:00 2001 From: Reditus <33814724+ReditusDraco@users.noreply.github.com> Date: Thu, 21 May 2026 06:27:58 +0100 Subject: [PATCH] Add db_owner setting to reassign table ownership to game server DB user for backup compatibility --- app/config.py | 1 + app/factory.py | 8 ++++++-- app/services/chat.py | 5 ++++- app/services/database.py | 7 ++++++- settings.yaml.example | 1 + 5 files changed, 18 insertions(+), 4 deletions(-) diff --git a/app/config.py b/app/config.py index 73044c1..19f1ab1 100644 --- a/app/config.py +++ b/app/config.py @@ -37,6 +37,7 @@ DEFAULTS = { 'schema': 'dune', 'min_connections': 2, 'max_connections': 10, + 'owner': 'dune', }, 'kubernetes': { 'namespace': '', diff --git a/app/factory.py b/app/factory.py index 3de08a5..05c4729 100644 --- a/app/factory.py +++ b/app/factory.py @@ -191,7 +191,8 @@ def create_app(settings_path=None): db_service = DatabaseService( db_config, min_conn=settings['database']['min_connections'], - max_conn=settings['database']['max_connections'] + max_conn=settings['database']['max_connections'], + db_owner=settings['database'].get('owner', 'dune') ) logging.debug(f"DatabaseService initialized: host={db_config['host']}, port={db_config['port']}, user={db_config['user']}") db_service.ensure_tables() @@ -224,7 +225,10 @@ def create_app(settings_path=None): player_svc = PlayerService(db_service) vehicle_svc = VehicleService(db_service) - chat_svc = ChatService(db_service, k8s_service, ssh_service, static_cache) + chat_svc = ChatService( + db_service, k8s_service, ssh_service, static_cache, + db_owner=settings['database'].get('owner', 'dune') + ) logging.debug("ChatService initialized") admin_svc = AdminService(db_service, ssh_service) logging.debug("AdminService initialized") diff --git a/app/services/chat.py b/app/services/chat.py index 49d3d85..a316fdd 100644 --- a/app/services/chat.py +++ b/app/services/chat.py @@ -8,11 +8,12 @@ logger = logging.getLogger(__name__) class ChatService: - def __init__(self, db_service, k8s_service, ssh_service, cache): + def __init__(self, db_service, k8s_service, ssh_service, cache, db_owner='dune'): self.db = db_service self.k8s = k8s_service self.ssh = ssh_service self.cache = cache + self.db_owner = db_owner self.ensured_table = False def ensure_history_table(self): @@ -34,6 +35,8 @@ class ChatService: ) """) self.db.execute("CREATE INDEX IF NOT EXISTS idx_chat_history_timestamp ON dune.chat_history (timestamp DESC)") + if self.db_owner: + self.db.execute("ALTER TABLE IF EXISTS dune.chat_history OWNER TO %s", (self.db_owner,)) self.ensured_table = True logger.info("Chat history table ready") return True diff --git a/app/services/database.py b/app/services/database.py index 4b8e3af..e881c57 100644 --- a/app/services/database.py +++ b/app/services/database.py @@ -10,10 +10,11 @@ logger = logging.getLogger(__name__) class DatabaseService: - def __init__(self, db_config, min_conn=2, max_conn=10): + def __init__(self, db_config, min_conn=2, max_conn=10, db_owner='dune'): self.db_config = db_config self.min_conn = min_conn self.max_conn = max_conn + self.db_owner = db_owner self.pool = None def init_pool(self): @@ -175,6 +176,10 @@ class DatabaseService: created_at TIMESTAMP NOT NULL DEFAULT NOW() ) """) + if self.db_owner: + cur.execute("ALTER TABLE IF EXISTS dune.player_ips OWNER TO %s", (self.db_owner,)) + cur.execute("ALTER TABLE IF EXISTS dune.bans OWNER TO %s", (self.db_owner,)) + cur.execute("ALTER TABLE IF EXISTS dune.player_actions OWNER TO %s", (self.db_owner,)) conn.commit() logger.info("Dashboard tables ensured (player_ips, bans, player_actions)") except Exception as e: diff --git a/settings.yaml.example b/settings.yaml.example index 2f74119..91c2b72 100644 --- a/settings.yaml.example +++ b/settings.yaml.example @@ -37,6 +37,7 @@ database: schema: "dune" min_connections: 2 max_connections: 10 + owner: "dune" # Game server DB user — tables are reassigned to this owner for backup compatibility # Kubernetes kubernetes: