fixed all phase 0 issues Enhance error handling for ModuleEnableSettings retrieval

- Added a check for the existence of the ModuleEnableSettings table before attempting to retrieve or fixed all phase 0 create settings for an account.
- Implemented logging and a user-friendly error response if the table does not exist, prompting the user to run the necessary migration.
- Updated migration to create the ModuleEnableSettings table using raw SQL to avoid model resolution issues.
This commit is contained in:
IGNY8 VPS (Salman)
2025-11-16 21:16:35 +00:00
parent fc6dd5623a
commit 51cd021f85
2 changed files with 50 additions and 32 deletions

View File

@@ -1,37 +1,39 @@
# Generated manually for Phase 0: Module Enable Settings # Generated manually for Phase 0: Module Enable Settings
# Using RunSQL to create table directly to avoid model resolution issues with new unified API model
from django.db import migrations, models from django.db import migrations
class Migration(migrations.Migration): class Migration(migrations.Migration):
dependencies = [ dependencies = [
('igny8_core_modules_system', '0006_alter_systemstatus_unique_together_and_more'), ('system', '0006_alter_systemstatus_unique_together_and_more'),
('igny8_core_auth', '0008_passwordresettoken_alter_industry_options_and_more'), ('igny8_core_auth', '0008_passwordresettoken_alter_industry_options_and_more'),
] ]
operations = [ operations = [
migrations.CreateModel( # Create table using raw SQL to avoid model resolution issues
name='ModuleEnableSettings', # The model state is automatically discovered from models.py
fields=[ migrations.RunSQL(
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), sql="""
('planner_enabled', models.BooleanField(default=True, help_text='Enable Planner module')), CREATE TABLE IF NOT EXISTS igny8_module_enable_settings (
('writer_enabled', models.BooleanField(default=True, help_text='Enable Writer module')), id BIGSERIAL PRIMARY KEY,
('thinker_enabled', models.BooleanField(default=True, help_text='Enable Thinker module')), planner_enabled BOOLEAN NOT NULL DEFAULT TRUE,
('automation_enabled', models.BooleanField(default=True, help_text='Enable Automation module')), writer_enabled BOOLEAN NOT NULL DEFAULT TRUE,
('site_builder_enabled', models.BooleanField(default=True, help_text='Enable Site Builder module')), thinker_enabled BOOLEAN NOT NULL DEFAULT TRUE,
('linker_enabled', models.BooleanField(default=True, help_text='Enable Linker module')), automation_enabled BOOLEAN NOT NULL DEFAULT TRUE,
('optimizer_enabled', models.BooleanField(default=True, help_text='Enable Optimizer module')), site_builder_enabled BOOLEAN NOT NULL DEFAULT TRUE,
('publisher_enabled', models.BooleanField(default=True, help_text='Enable Publisher module')), linker_enabled BOOLEAN NOT NULL DEFAULT TRUE,
('account', models.ForeignKey(on_delete=models.CASCADE, to='igny8_core_auth.account', db_column='tenant_id')), optimizer_enabled BOOLEAN NOT NULL DEFAULT TRUE,
], publisher_enabled BOOLEAN NOT NULL DEFAULT TRUE,
options={ tenant_id BIGINT NOT NULL REFERENCES igny8_tenants(id) ON DELETE CASCADE,
'db_table': 'igny8_module_enable_settings', created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
}, updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
), );
migrations.AddConstraint( CREATE INDEX IF NOT EXISTS igny8_module_enable_settings_tenant_id_idx ON igny8_module_enable_settings(tenant_id);
model_name='moduleenablesettings', CREATE INDEX IF NOT EXISTS igny8_module_enable_settings_account_created_idx ON igny8_module_enable_settings(tenant_id, created_at);
constraint=models.UniqueConstraint(fields=('account',), name='unique_account_module_enable_settings'), CREATE UNIQUE INDEX IF NOT EXISTS unique_account_module_enable_settings ON igny8_module_enable_settings(tenant_id);
""",
reverse_sql="DROP TABLE IF EXISTS igny8_module_enable_settings CASCADE;",
), ),
] ]

View File

@@ -354,6 +354,8 @@ class ModuleEnableSettingsViewSet(AccountModelViewSet):
request=request request=request
) )
# Check if table exists (migration might not have been run)
try:
# Get or create settings for account (one per account) # Get or create settings for account (one per account)
try: try:
settings = ModuleEnableSettings.objects.get(account=account) settings = ModuleEnableSettings.objects.get(account=account)
@@ -363,6 +365,20 @@ class ModuleEnableSettingsViewSet(AccountModelViewSet):
serializer = self.get_serializer(settings) serializer = self.get_serializer(settings)
return success_response(data=serializer.data, request=request) return success_response(data=serializer.data, request=request)
except Exception as db_error:
# Check if it's a "table does not exist" error
error_str = str(db_error)
if 'does not exist' in error_str.lower() or 'relation' in error_str.lower():
import logging
logger = logging.getLogger(__name__)
logger.error(f"ModuleEnableSettings table does not exist. Migration 0007_add_module_enable_settings needs to be run: {error_str}")
return error_response(
error='Module enable settings table not found. Please run migration: python manage.py migrate igny8_core_modules_system 0007',
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
request=request
)
# Re-raise other database errors
raise
except Exception as e: except Exception as e:
import traceback import traceback
error_trace = traceback.format_exc() error_trace = traceback.format_exc()