mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-05-01 20:03:30 +02:00
feat: fixed migration for electric-sql
This commit is contained in:
parent
5712336feb
commit
7a9a14aa66
4 changed files with 84 additions and 78 deletions
|
|
@ -8,7 +8,6 @@ services:
|
||||||
volumes:
|
volumes:
|
||||||
- postgres_data:/var/lib/postgresql/data
|
- postgres_data:/var/lib/postgresql/data
|
||||||
- ./scripts/docker/postgresql.conf:/etc/postgresql/postgresql.conf:ro
|
- ./scripts/docker/postgresql.conf:/etc/postgresql/postgresql.conf:ro
|
||||||
- ./scripts/docker/init-electric-user.sql:/docker-entrypoint-initdb.d/init-electric-user.sql:ro
|
|
||||||
environment:
|
environment:
|
||||||
- POSTGRES_USER=${POSTGRES_USER:-postgres}
|
- POSTGRES_USER=${POSTGRES_USER:-postgres}
|
||||||
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-postgres}
|
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-postgres}
|
||||||
|
|
|
||||||
|
|
@ -1,11 +0,0 @@
|
||||||
-- Electrify tables for Electric SQL sync
|
|
||||||
-- This tells Electric SQL which tables to sync
|
|
||||||
-- Run this after running migrations
|
|
||||||
|
|
||||||
-- Electrify notifications table
|
|
||||||
ALTER TABLE notifications ENABLE ELECTRIC;
|
|
||||||
|
|
||||||
-- You can electrify other tables as needed:
|
|
||||||
-- ALTER TABLE documents ENABLE ELECTRIC;
|
|
||||||
-- ALTER TABLE logs ENABLE ELECTRIC;
|
|
||||||
|
|
||||||
|
|
@ -1,30 +0,0 @@
|
||||||
-- Create Electric SQL replication user
|
|
||||||
-- This script is run during PostgreSQL initialization
|
|
||||||
|
|
||||||
DO $$
|
|
||||||
BEGIN
|
|
||||||
IF NOT EXISTS (SELECT FROM pg_user WHERE usename = 'electric') THEN
|
|
||||||
CREATE USER electric WITH REPLICATION PASSWORD 'electric_password';
|
|
||||||
END IF;
|
|
||||||
END
|
|
||||||
$$;
|
|
||||||
|
|
||||||
-- Grant necessary permissions
|
|
||||||
GRANT CONNECT ON DATABASE surfsense TO electric;
|
|
||||||
GRANT USAGE ON SCHEMA public TO electric;
|
|
||||||
GRANT SELECT ON ALL TABLES IN SCHEMA public TO electric;
|
|
||||||
GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO electric;
|
|
||||||
|
|
||||||
-- Grant permissions on future tables
|
|
||||||
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO electric;
|
|
||||||
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON SEQUENCES TO electric;
|
|
||||||
|
|
||||||
-- Create the publication that Electric SQL expects
|
|
||||||
-- Electric will add tables to this publication when shapes are subscribed
|
|
||||||
DO $$
|
|
||||||
BEGIN
|
|
||||||
IF NOT EXISTS (SELECT FROM pg_publication WHERE pubname = 'electric_publication_default') THEN
|
|
||||||
CREATE PUBLICATION electric_publication_default;
|
|
||||||
END IF;
|
|
||||||
END
|
|
||||||
$$;
|
|
||||||
|
|
@ -4,7 +4,7 @@ Revision ID: 62
|
||||||
Revises: 61
|
Revises: 61
|
||||||
|
|
||||||
Creates notifications table and sets up Electric SQL replication
|
Creates notifications table and sets up Electric SQL replication
|
||||||
(REPLICA IDENTITY FULL and publication) for notifications,
|
(user, publication, REPLICA IDENTITY FULL) for notifications,
|
||||||
search_source_connectors, and documents tables.
|
search_source_connectors, and documents tables.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
@ -45,45 +45,93 @@ def upgrade() -> None:
|
||||||
op.create_index("ix_notifications_created_at", "notifications", ["created_at"])
|
op.create_index("ix_notifications_created_at", "notifications", ["created_at"])
|
||||||
op.create_index("ix_notifications_user_read", "notifications", ["user_id", "read"])
|
op.create_index("ix_notifications_user_read", "notifications", ["user_id", "read"])
|
||||||
|
|
||||||
# Set up Electric SQL replication for real-time sync tables
|
# =====================================================
|
||||||
# Set REPLICA IDENTITY FULL (required by Electric SQL for replication)
|
# Electric SQL Setup - User and Publication
|
||||||
# This logs full row data for UPDATE/DELETE operations in the WAL
|
# =====================================================
|
||||||
op.execute("ALTER TABLE notifications REPLICA IDENTITY FULL;")
|
|
||||||
op.execute("ALTER TABLE search_source_connectors REPLICA IDENTITY FULL;")
|
# Create Electric SQL replication user if not exists
|
||||||
op.execute("ALTER TABLE documents REPLICA IDENTITY FULL;")
|
|
||||||
|
|
||||||
# Add tables to Electric SQL publication for replication if publication exists
|
|
||||||
op.execute(
|
op.execute(
|
||||||
"""
|
"""
|
||||||
DO $$
|
DO $$
|
||||||
BEGIN
|
BEGIN
|
||||||
IF EXISTS (SELECT 1 FROM pg_publication WHERE pubname = 'electric_publication_default') THEN
|
IF NOT EXISTS (SELECT FROM pg_user WHERE usename = 'electric') THEN
|
||||||
-- Add notifications if not already added
|
CREATE USER electric WITH REPLICATION PASSWORD 'electric_password';
|
||||||
IF NOT EXISTS (
|
END IF;
|
||||||
SELECT 1 FROM pg_publication_tables
|
END
|
||||||
WHERE pubname = 'electric_publication_default'
|
$$;
|
||||||
AND tablename = 'notifications'
|
"""
|
||||||
) THEN
|
)
|
||||||
ALTER PUBLICATION electric_publication_default ADD TABLE notifications;
|
|
||||||
END IF;
|
# Grant necessary permissions to electric user
|
||||||
|
op.execute(
|
||||||
-- Add search_source_connectors if not already added
|
"""
|
||||||
IF NOT EXISTS (
|
DO $$
|
||||||
SELECT 1 FROM pg_publication_tables
|
DECLARE
|
||||||
WHERE pubname = 'electric_publication_default'
|
db_name TEXT := current_database();
|
||||||
AND tablename = 'search_source_connectors'
|
BEGIN
|
||||||
) THEN
|
EXECUTE format('GRANT CONNECT ON DATABASE %I TO electric', db_name);
|
||||||
ALTER PUBLICATION electric_publication_default ADD TABLE search_source_connectors;
|
END
|
||||||
END IF;
|
$$;
|
||||||
|
"""
|
||||||
-- Add documents if not already added
|
)
|
||||||
IF NOT EXISTS (
|
op.execute("GRANT USAGE ON SCHEMA public TO electric;")
|
||||||
SELECT 1 FROM pg_publication_tables
|
op.execute("GRANT SELECT ON ALL TABLES IN SCHEMA public TO electric;")
|
||||||
WHERE pubname = 'electric_publication_default'
|
op.execute("GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO electric;")
|
||||||
AND tablename = 'documents'
|
op.execute("ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO electric;")
|
||||||
) THEN
|
op.execute("ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON SEQUENCES TO electric;")
|
||||||
ALTER PUBLICATION electric_publication_default ADD TABLE documents;
|
|
||||||
END IF;
|
# Create the publication if not exists
|
||||||
|
op.execute(
|
||||||
|
"""
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
IF NOT EXISTS (SELECT FROM pg_publication WHERE pubname = 'electric_publication_default') THEN
|
||||||
|
CREATE PUBLICATION electric_publication_default;
|
||||||
|
END IF;
|
||||||
|
END
|
||||||
|
$$;
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
# =====================================================
|
||||||
|
# Electric SQL Setup - Table Configuration
|
||||||
|
# =====================================================
|
||||||
|
|
||||||
|
# Set REPLICA IDENTITY FULL (required by Electric SQL for replication)
|
||||||
|
op.execute("ALTER TABLE notifications REPLICA IDENTITY FULL;")
|
||||||
|
op.execute("ALTER TABLE search_source_connectors REPLICA IDENTITY FULL;")
|
||||||
|
op.execute("ALTER TABLE documents REPLICA IDENTITY FULL;")
|
||||||
|
|
||||||
|
# Add tables to Electric SQL publication for replication
|
||||||
|
op.execute(
|
||||||
|
"""
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
-- Add notifications if not already added
|
||||||
|
IF NOT EXISTS (
|
||||||
|
SELECT 1 FROM pg_publication_tables
|
||||||
|
WHERE pubname = 'electric_publication_default'
|
||||||
|
AND tablename = 'notifications'
|
||||||
|
) THEN
|
||||||
|
ALTER PUBLICATION electric_publication_default ADD TABLE notifications;
|
||||||
|
END IF;
|
||||||
|
|
||||||
|
-- Add search_source_connectors if not already added
|
||||||
|
IF NOT EXISTS (
|
||||||
|
SELECT 1 FROM pg_publication_tables
|
||||||
|
WHERE pubname = 'electric_publication_default'
|
||||||
|
AND tablename = 'search_source_connectors'
|
||||||
|
) THEN
|
||||||
|
ALTER PUBLICATION electric_publication_default ADD TABLE search_source_connectors;
|
||||||
|
END IF;
|
||||||
|
|
||||||
|
-- Add documents if not already added
|
||||||
|
IF NOT EXISTS (
|
||||||
|
SELECT 1 FROM pg_publication_tables
|
||||||
|
WHERE pubname = 'electric_publication_default'
|
||||||
|
AND tablename = 'documents'
|
||||||
|
) THEN
|
||||||
|
ALTER PUBLICATION electric_publication_default ADD TABLE documents;
|
||||||
END IF;
|
END IF;
|
||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue