Files
portainer-core/services/library-desk/setup_wiki_triggers.py
T
jpmschweitzerandClaude Opus 4.5 b74b5bc2c8 feat(library-desk): add wiki change detection via PostgreSQL LISTEN/NOTIFY
- Add WikiChangeListener service for PostgreSQL notifications
- Add webhooks router for HTTP webhook fallback
- Add asyncpg dependency for PostgreSQL async support
- Include setup scripts and documentation for triggers

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-10 21:35:18 +01:00

92 lines
2.9 KiB
Python

#!/usr/bin/env python3
"""
Setup PostgreSQL triggers in Wiki.js database for change detection.
This script creates database triggers that emit NOTIFY events when
pages are created, updated, or deleted in Wiki.js.
Run this once to enable automatic processing of user-edited pages.
"""
import asyncio
import asyncpg
import sys
from src.config import get_settings
# Import the corrected trigger SQL from the listener module
from src.services.wiki_change_listener import SETUP_TRIGGERS_SQL
async def setup_triggers():
"""Install database triggers in Wiki.js PostgreSQL database."""
settings = get_settings()
print("=" * 70)
print("Wiki.js Database Trigger Setup")
print("=" * 70)
print()
print(f"Connecting to Wiki.js database at {settings.wikijs_db_host}:{settings.wikijs_db_port}")
print(f"Database: {settings.wikijs_db_name}")
print(f"User: {settings.wikijs_db_user}")
print()
try:
# Connect to Wiki.js database
connection = await asyncpg.connect(
host=settings.wikijs_db_host,
port=settings.wikijs_db_port,
user=settings.wikijs_db_user,
password=settings.wikijs_db_password,
database=settings.wikijs_db_name
)
print("✓ Connected to Wiki.js database")
print()
print("Installing triggers...")
print()
# Execute setup SQL
await connection.execute(SETUP_TRIGGERS_SQL)
# Verify triggers were created
triggers = await connection.fetch("""
SELECT trigger_name, event_manipulation
FROM information_schema.triggers
WHERE event_object_table = 'pages'
ORDER BY trigger_name
""")
if triggers:
print("✓ Triggers installed successfully:")
print()
for trigger in triggers:
print(f" - {trigger['trigger_name']} ({trigger['event_manipulation']})")
print()
print("=" * 70)
print("Setup complete!")
print("=" * 70)
print()
print("Next steps:")
print(" 1. Restart library-desk service to activate the listener")
print(" 2. Edit a page in Wiki.js to test")
print(" 3. Check library-desk logs for processing messages")
print()
else:
print("✗ No triggers found after installation")
sys.exit(1)
await connection.close()
except Exception as e:
print(f"✗ Error: {e}")
print()
print("Common issues:")
print(" - Check database connection settings in .env")
print(" - Ensure database user has CREATE FUNCTION and CREATE TRIGGER permissions")
print(" - Verify Wiki.js database is accessible from library-desk service")
print()
sys.exit(1)
if __name__ == "__main__":
asyncio.run(setup_triggers())