mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-04-29 02:46:25 +02:00
commit
f4e6c47d0b
2 changed files with 16 additions and 78 deletions
|
|
@ -6,9 +6,8 @@ Create Date: 2025-12-21
|
||||||
|
|
||||||
This migration:
|
This migration:
|
||||||
1. Migrates data from old 'chats' table to 'new_chat_threads' and 'new_chat_messages'
|
1. Migrates data from old 'chats' table to 'new_chat_threads' and 'new_chat_messages'
|
||||||
2. Drops the 'podcasts' table (podcast data is not migrated as per user request)
|
2. Drops the 'chats' table
|
||||||
3. Drops the 'chats' table
|
3. Removes the 'chattype' enum
|
||||||
4. Removes the 'chattype' enum
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
|
@ -92,7 +91,11 @@ def upgrade() -> None:
|
||||||
print(f"[Migration 49] Skipping empty chat {chat_id}")
|
print(f"[Migration 49] Skipping empty chat {chat_id}")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Create new thread
|
# Create new thread - truncate title to 500 chars (VARCHAR(500) limit)
|
||||||
|
thread_title = title or "Migrated Chat"
|
||||||
|
if len(thread_title) > 500:
|
||||||
|
thread_title = thread_title[:497] + "..."
|
||||||
|
|
||||||
result = connection.execute(
|
result = connection.execute(
|
||||||
sa.text("""
|
sa.text("""
|
||||||
INSERT INTO new_chat_threads
|
INSERT INTO new_chat_threads
|
||||||
|
|
@ -101,7 +104,7 @@ def upgrade() -> None:
|
||||||
RETURNING id
|
RETURNING id
|
||||||
"""),
|
"""),
|
||||||
{
|
{
|
||||||
"title": title or "Migrated Chat",
|
"title": thread_title,
|
||||||
"search_space_id": search_space_id,
|
"search_space_id": search_space_id,
|
||||||
"created_at": created_at,
|
"created_at": created_at,
|
||||||
},
|
},
|
||||||
|
|
@ -162,11 +165,7 @@ def upgrade() -> None:
|
||||||
|
|
||||||
print(f"[Migration 49] Successfully migrated {migrated_count} chats")
|
print(f"[Migration 49] Successfully migrated {migrated_count} chats")
|
||||||
|
|
||||||
# Drop podcasts table (FK references chats, so drop first)
|
# Drop chats table (podcasts table was already updated to remove chat_id FK)
|
||||||
print("[Migration 49] Dropping podcasts table...")
|
|
||||||
op.drop_table("podcasts")
|
|
||||||
|
|
||||||
# Drop chats table
|
|
||||||
print("[Migration 49] Dropping chats table...")
|
print("[Migration 49] Dropping chats table...")
|
||||||
op.drop_table("chats")
|
op.drop_table("chats")
|
||||||
|
|
||||||
|
|
@ -178,7 +177,7 @@ def upgrade() -> None:
|
||||||
|
|
||||||
|
|
||||||
def downgrade() -> None:
|
def downgrade() -> None:
|
||||||
"""Recreate old tables (data cannot be restored)."""
|
"""Recreate old chats table (data cannot be restored)."""
|
||||||
# Recreate chattype enum
|
# Recreate chattype enum
|
||||||
op.execute(
|
op.execute(
|
||||||
sa.text("""
|
sa.text("""
|
||||||
|
|
@ -209,32 +208,4 @@ def downgrade() -> None:
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
# Recreate podcasts table
|
print("[Migration 49 Downgrade] Chats table recreated (data not restored)")
|
||||||
op.create_table(
|
|
||||||
"podcasts",
|
|
||||||
sa.Column("id", sa.Integer(), primary_key=True, index=True),
|
|
||||||
sa.Column("title", sa.String(), nullable=False, index=True),
|
|
||||||
sa.Column("podcast_transcript", sa.JSON(), nullable=False, server_default="{}"),
|
|
||||||
sa.Column("file_location", sa.String(500), nullable=False, server_default=""),
|
|
||||||
sa.Column(
|
|
||||||
"chat_id",
|
|
||||||
sa.Integer(),
|
|
||||||
sa.ForeignKey("chats.id", ondelete="CASCADE"),
|
|
||||||
nullable=True,
|
|
||||||
),
|
|
||||||
sa.Column("chat_state_version", sa.BigInteger(), nullable=True),
|
|
||||||
sa.Column(
|
|
||||||
"search_space_id",
|
|
||||||
sa.Integer(),
|
|
||||||
sa.ForeignKey("searchspaces.id", ondelete="CASCADE"),
|
|
||||||
nullable=False,
|
|
||||||
),
|
|
||||||
sa.Column(
|
|
||||||
"created_at",
|
|
||||||
sa.TIMESTAMP(timezone=True),
|
|
||||||
nullable=False,
|
|
||||||
server_default=sa.func.now(),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
print("[Migration 49 Downgrade] Tables recreated (data not restored)")
|
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,8 @@ Create Date: 2024-12-22
|
||||||
|
|
||||||
This migration:
|
This migration:
|
||||||
1. Migrates data from old llm_configs table to new_llm_configs (preserving user configs)
|
1. Migrates data from old llm_configs table to new_llm_configs (preserving user configs)
|
||||||
2. Drops the old llm_configs table (no longer used)
|
2. Updates searchspaces to point to migrated configs
|
||||||
3. Removes the is_default column from new_llm_configs (roles now determine which config to use)
|
3. Drops the old llm_configs table (no longer used)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from alembic import op
|
from alembic import op
|
||||||
|
|
@ -47,7 +47,6 @@ def upgrade():
|
||||||
system_instructions,
|
system_instructions,
|
||||||
use_default_system_instructions,
|
use_default_system_instructions,
|
||||||
citations_enabled,
|
citations_enabled,
|
||||||
is_default,
|
|
||||||
search_space_id,
|
search_space_id,
|
||||||
created_at
|
created_at
|
||||||
)
|
)
|
||||||
|
|
@ -59,11 +58,10 @@ def upgrade():
|
||||||
lc.model_name,
|
lc.model_name,
|
||||||
lc.api_key,
|
lc.api_key,
|
||||||
lc.api_base,
|
lc.api_base,
|
||||||
COALESCE(lc.litellm_params, '{}'::jsonb),
|
COALESCE(lc.litellm_params::json, '{}'::json),
|
||||||
'' as system_instructions, -- Use defaults
|
'' as system_instructions, -- Use defaults
|
||||||
TRUE as use_default_system_instructions,
|
TRUE as use_default_system_instructions,
|
||||||
TRUE as citations_enabled,
|
TRUE as citations_enabled,
|
||||||
FALSE as is_default,
|
|
||||||
lc.search_space_id,
|
lc.search_space_id,
|
||||||
COALESCE(lc.created_at, NOW())
|
COALESCE(lc.created_at, NOW())
|
||||||
FROM llm_configs lc
|
FROM llm_configs lc
|
||||||
|
|
@ -130,23 +128,7 @@ def upgrade():
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
|
||||||
# STEP 3: Drop the is_default column from new_llm_configs
|
# STEP 3: Drop the old llm_configs table (data has been migrated)
|
||||||
# (role assignments now determine which config to use)
|
|
||||||
op.execute(
|
|
||||||
"""
|
|
||||||
DO $$
|
|
||||||
BEGIN
|
|
||||||
IF EXISTS (
|
|
||||||
SELECT 1 FROM information_schema.columns
|
|
||||||
WHERE table_name = 'new_llm_configs' AND column_name = 'is_default'
|
|
||||||
) THEN
|
|
||||||
ALTER TABLE new_llm_configs DROP COLUMN is_default;
|
|
||||||
END IF;
|
|
||||||
END$$;
|
|
||||||
"""
|
|
||||||
)
|
|
||||||
|
|
||||||
# STEP 4: Drop the old llm_configs table (data has been migrated)
|
|
||||||
op.execute("DROP TABLE IF EXISTS llm_configs CASCADE")
|
op.execute("DROP TABLE IF EXISTS llm_configs CASCADE")
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -213,7 +195,7 @@ def downgrade():
|
||||||
nlc.api_key,
|
nlc.api_key,
|
||||||
nlc.api_base,
|
nlc.api_base,
|
||||||
'English' as language, -- Default language
|
'English' as language, -- Default language
|
||||||
COALESCE(nlc.litellm_params, '{}'::jsonb),
|
COALESCE(nlc.litellm_params::jsonb, '{}'::jsonb),
|
||||||
nlc.search_space_id,
|
nlc.search_space_id,
|
||||||
nlc.created_at
|
nlc.created_at
|
||||||
FROM new_llm_configs nlc
|
FROM new_llm_configs nlc
|
||||||
|
|
@ -227,18 +209,3 @@ def downgrade():
|
||||||
END$$;
|
END$$;
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
|
||||||
# Add back the is_default column to new_llm_configs
|
|
||||||
op.execute(
|
|
||||||
"""
|
|
||||||
DO $$
|
|
||||||
BEGIN
|
|
||||||
IF NOT EXISTS (
|
|
||||||
SELECT 1 FROM information_schema.columns
|
|
||||||
WHERE table_name = 'new_llm_configs' AND column_name = 'is_default'
|
|
||||||
) THEN
|
|
||||||
ALTER TABLE new_llm_configs ADD COLUMN is_default BOOLEAN NOT NULL DEFAULT FALSE;
|
|
||||||
END IF;
|
|
||||||
END$$;
|
|
||||||
"""
|
|
||||||
)
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue