Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """ | |
| Quick script to verify Supabase rules storage is working. | |
| Run this to check if rules are being saved to Supabase. | |
| """ | |
| import os | |
| import sys | |
| from pathlib import Path | |
| # Load environment variables from .env file | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| # Add backend to path | |
| backend_dir = Path(__file__).resolve().parent | |
| sys.path.insert(0, str(backend_dir)) | |
| from backend.api.storage.rules_store import RulesStore | |
| def main(): | |
| print("=" * 60) | |
| print("Supabase Rules Storage Verification") | |
| print("=" * 60) | |
| # Check environment variables | |
| supabase_url = os.getenv("SUPABASE_URL") | |
| supabase_key = os.getenv("SUPABASE_SERVICE_KEY") | |
| print("\n1. Checking Environment Variables:") | |
| if supabase_url: | |
| print(f" β SUPABASE_URL is set: {supabase_url[:50]}...") | |
| else: | |
| print(" β SUPABASE_URL is not set") | |
| print(" Add it to your .env file: SUPABASE_URL=https://your-project.supabase.co") | |
| if supabase_key: | |
| print(f" β SUPABASE_SERVICE_KEY is set: {supabase_key[:20]}...") | |
| else: | |
| print(" β SUPABASE_SERVICE_KEY is not set") | |
| print(" Add it to your .env file: SUPABASE_SERVICE_KEY=your_service_role_key") | |
| if not supabase_url or not supabase_key: | |
| print("\nβ οΈ Supabase credentials are missing!") | |
| print(" Rules will be saved to SQLite instead.") | |
| print(" See SUPABASE_SETUP.md for setup instructions.") | |
| print("\n To use Supabase:") | |
| print(" 1. Add SUPABASE_URL and SUPABASE_SERVICE_KEY to your .env file") | |
| print(" 2. Create the admin_rules table in Supabase (see supabase_admin_rules_table.sql)") | |
| print(" 3. Restart your application") | |
| return | |
| # Initialize RulesStore | |
| print("\n2. Initializing RulesStore:") | |
| try: | |
| store = RulesStore(auto_create_table=True) | |
| print(f" β RulesStore initialized") | |
| print(f" π¦ Using Supabase: {store.use_supabase}") | |
| if not store.use_supabase: | |
| print(" β οΈ RulesStore is using SQLite, not Supabase!") | |
| print(" Check that:") | |
| print(" - SUPABASE_URL and SUPABASE_SERVICE_KEY are correct") | |
| print(" - Supabase Python client is installed: pip install supabase") | |
| return | |
| except Exception as e: | |
| print(f" β Failed to initialize RulesStore: {e}") | |
| return | |
| # Test adding a rule | |
| print("\n3. Testing Rule Storage:") | |
| test_tenant = "test_verification" | |
| test_rule = "Test rule for Supabase verification" | |
| try: | |
| # Delete test rule if it exists | |
| store.delete_rule(test_tenant, test_rule) | |
| # Add test rule | |
| success = store.add_rule( | |
| test_tenant, | |
| test_rule, | |
| severity="medium", | |
| description="Verification test rule" | |
| ) | |
| if success: | |
| print(f" β Successfully added test rule to Supabase") | |
| else: | |
| print(f" β Failed to add rule to Supabase") | |
| return | |
| # Retrieve rule | |
| rules = store.get_rules(test_tenant) | |
| if test_rule in rules: | |
| print(f" β Successfully retrieved rule from Supabase") | |
| print(f" π Found {len(rules)} rule(s) for tenant '{test_tenant}'") | |
| else: | |
| print(f" β Rule not found after adding") | |
| return | |
| # Get detailed rules | |
| detailed_rules = store.get_rules_detailed(test_tenant) | |
| if detailed_rules: | |
| print(f" β Successfully retrieved detailed rules") | |
| for rule in detailed_rules: | |
| if rule['rule'] == test_rule: | |
| print(f" π Rule details:") | |
| print(f" - Pattern: {rule.get('pattern', 'N/A')}") | |
| print(f" - Severity: {rule.get('severity', 'N/A')}") | |
| print(f" - Enabled: {rule.get('enabled', 'N/A')}") | |
| # Cleanup test rule | |
| store.delete_rule(test_tenant, test_rule) | |
| print(f" π§Ή Cleaned up test rule") | |
| except Exception as e: | |
| print(f" β Error during test: {e}") | |
| import traceback | |
| traceback.print_exc() | |
| return | |
| print("\n" + "=" * 60) | |
| print("β All checks passed! Rules are being saved to Supabase.") | |
| print("=" * 60) | |
| if __name__ == "__main__": | |
| main() | |