fix: sync plugin schemas with ORM models on startup

This commit is contained in:
Agent Zero
2026-08-07 00:44:10 +02:00
parent 2ebc64be47
commit 34d3ea2607
2 changed files with 290 additions and 48 deletions
+2 -48
View File
@@ -101,54 +101,8 @@ asyncio.run(set_password())
"
fi
echo "[prestart] Adding owner_id to plugin tables if missing..."
python3 -c "
import asyncio, os
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy import text
async def fix_owner_id():
db_url = os.environ.get('MIGRATION_DATABASE_URL', os.environ.get('DATABASE_URL', ''))
if not db_url:
print('[prestart] WARNING: No DB URL for owner_id fix')
return
engine = create_async_engine(db_url)
try:
async with engine.begin() as conn:
# Find all tables with tenant_id but without owner_id
result = await conn.execute(text(\"\"\"
SELECT t.table_name
FROM information_schema.tables t
JOIN information_schema.columns c ON t.table_name = c.table_name
WHERE t.table_schema = 'public'
AND c.column_name = 'tenant_id'
AND t.table_type = 'BASE TABLE'
AND NOT EXISTS (
SELECT 1 FROM information_schema.columns
WHERE table_name = t.table_name AND column_name = 'owner_id'
)
ORDER BY t.table_name
\"\"\"))
tables = [row[0] for row in result.fetchall()]
for table in tables:
await conn.execute(text(
f'ALTER TABLE {table} ADD COLUMN IF NOT EXISTS owner_id UUID REFERENCES users(id) ON DELETE SET NULL'
))
await conn.execute(text(
f'CREATE INDEX IF NOT EXISTS ix_{table}_owner ON {table} (owner_id)'
))
print(f'[prestart] Added owner_id to {table}')
if not tables:
print('[prestart] All tables already have owner_id.')
else:
print(f'[prestart] Added owner_id to {len(tables)} tables.')
except Exception as e:
print(f'[prestart] WARNING: owner_id fix failed: {e}')
finally:
await engine.dispose()
asyncio.run(fix_owner_id())
"
echo "[prestart] Syncing plugin table schemas..."
python3 /app/scripts/sync_plugin_schema.py
echo "[prestart] Seeding admin user if not exists..."
python3 /app/scripts/seed_admin.py || echo "[prestart] WARNING: Admin seed failed (may already exist)"