Skip to content

Commit 6ccd5ee

Browse files
committed
feat(migrations): enhance database migration checks and logging
- Added logic to check the current database revision before running migrations. - Improved logging to indicate whether migrations are being run or if the database is already up to date. - Updated the prefix management in the guild config to ensure cache is invalidated when prefixes are deleted or updated. - Introduced a method to check if prefix override is enabled via environment variables in the settings.
1 parent 8f651d9 commit 6ccd5ee

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

src/tux/database/migrations/runner.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,23 @@ async def upgrade_head_if_needed() -> None:
8383
def run_upgrade():
8484
"""Run the upgrade in a separate thread with timeout."""
8585
cfg = _build_alembic_config()
86-
logger.info("🔄 Running database migrations...")
86+
logger.info("🔄 Checking database migrations...")
8787
try:
88-
command.upgrade(cfg, "head")
89-
logger.info("✅ Database migrations completed")
88+
# Check current revision first
89+
current_rev = command.current(cfg)
90+
logger.debug(f"Current database revision: {current_rev}")
91+
92+
# Check if we need to upgrade
93+
head_rev = command.heads(cfg)
94+
logger.debug(f"Head revision: {head_rev}")
95+
96+
# Only run upgrade if we're not already at head
97+
if current_rev != head_rev:
98+
logger.info("🔄 Running database migrations...")
99+
command.upgrade(cfg, "head")
100+
logger.info("✅ Database migrations completed")
101+
else:
102+
logger.info("✅ Database is already up to date")
90103
return True
91104
except Exception as e:
92105
# Check if this is a database connection error

src/tux/modules/guild/config.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,10 @@ async def config_set_prefix(
348348

349349
await self.db_config.update_guild_prefix(interaction.guild.id, prefix)
350350

351+
# Update the prefix cache
352+
if self.bot.prefix_manager:
353+
await self.bot.prefix_manager.set_prefix(interaction.guild.id, prefix)
354+
351355
await interaction.followup.send(
352356
embed=EmbedCreator.create_embed(
353357
bot=self.bot,
@@ -380,6 +384,10 @@ async def config_clear_prefix(
380384

381385
await self.db_config.delete_guild_prefix(interaction.guild.id)
382386

387+
# Update the prefix cache to use default prefix
388+
if self.bot.prefix_manager:
389+
self.bot.prefix_manager.invalidate_cache(interaction.guild.id)
390+
383391
await interaction.followup.send(
384392
embed=EmbedCreator.create_embed(
385393
bot=self.bot,

src/tux/shared/config/settings.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,15 @@ def get_prefix(self) -> str:
139139
"""Get command prefix for current environment."""
140140
return self.BOT_INFO.PREFIX
141141

142+
def is_prefix_override_enabled(self) -> bool:
143+
"""Check if prefix override is enabled by environment variable.
144+
145+
Returns True if BOT_INFO__PREFIX was explicitly set in environment variables,
146+
indicating the user wants to override all database prefix settings.
147+
"""
148+
149+
return "BOT_INFO__PREFIX" in os.environ
150+
142151
def is_debug_enabled(self) -> bool:
143152
"""Check if debug mode is enabled."""
144153
return self.DEBUG

0 commit comments

Comments
 (0)