IntegraChat / SUPABASE_SETUP.md
nothingworry's picture
feat: Enhance admin rules with file upload, drag-and-drop, chunk processing, and improved UI
a477044
|
raw
history blame
3.45 kB

Supabase Setup for Admin Rules

This guide will help you set up Supabase to store admin rules instead of SQLite.

Step 1: Create the Table in Supabase

  1. Go to your Supabase Dashboard

  2. Open SQL Editor

    • Click on "SQL Editor" in the left sidebar
    • Click "New query"
  3. Run the SQL Script

    • Copy the contents of supabase_admin_rules_table.sql
    • Paste it into the SQL Editor
    • Click "Run" to execute

    This will create:

    • admin_rules table with all necessary columns
    • Indexes for performance
    • Row Level Security (RLS) policies
    • Automatic timestamp updates

Step 2: Configure Environment Variables

Make sure your .env file has Supabase credentials:

SUPABASE_URL=https://your-project.supabase.co
SUPABASE_SERVICE_KEY=your_service_role_key_here

Important: Use the Service Role Key (not the anon key) for full access.

To find your keys:

  1. Go to Supabase Dashboard β†’ Settings β†’ API
  2. Copy the "Project URL" β†’ SUPABASE_URL
  3. Copy the "service_role" key β†’ SUPABASE_SERVICE_KEY

Step 3: Verify Setup

The RulesStore will automatically use Supabase if:

  • SUPABASE_URL is set
  • SUPABASE_SERVICE_KEY is set
  • Supabase Python client is installed (pip install supabase)

If Supabase is not configured, it will fall back to SQLite automatically.

Step 4: Test the Integration

You can test if rules are being saved to Supabase:

from backend.api.storage.rules_store import RulesStore

store = RulesStore()
print(f"Using Supabase: {store.use_supabase}")

# Add a test rule
store.add_rule("test_tenant", "Test rule", severity="high")
print("Rule added!")

# Get rules
rules = store.get_rules("test_tenant")
print(f"Rules: {rules}")

Step 5: View Rules in Supabase

  1. Go to Supabase Dashboard β†’ Table Editor
  2. Select the admin_rules table
  3. You should see all your rules with tenant isolation

Migration from SQLite

If you have existing rules in SQLite and want to migrate:

  1. Export from SQLite:

    import sqlite3
    conn = sqlite3.connect('data/admin_rules.db')
    cursor = conn.execute("SELECT * FROM admin_rules")
    rules = cursor.fetchall()
    
  2. Import to Supabase:

    from backend.api.storage.rules_store import RulesStore
    store = RulesStore(use_supabase=True)
    for rule in rules:
        store.add_rule(rule['tenant_id'], rule['rule'], 
                      pattern=rule.get('pattern'),
                      severity=rule.get('severity', 'medium'))
    

Troubleshooting

Rules not appearing in Supabase

  • Check that RLS policies allow your service role to read/write
  • Verify environment variables are set correctly
  • Check Supabase logs for errors

Fallback to SQLite

  • If Supabase credentials are missing, it automatically uses SQLite
  • Check your .env file has correct values
  • Restart your FastAPI server after changing .env

Permission Errors

  • Make sure you're using the service_role key (not anon key)
  • Check RLS policies in Supabase allow service role access

Benefits of Using Supabase

βœ… Scalability - Handle millions of rules
βœ… Multi-region - Global availability
βœ… Backups - Automatic backups
βœ… Real-time - Can subscribe to changes
βœ… Security - Row Level Security built-in
βœ… Analytics - Built-in query performance monitoring