-
Notifications
You must be signed in to change notification settings - Fork 1.9k
feat: Improve migration #5391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: Improve migration #5391
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,76 +16,59 @@ | |
branch_labels = None | ||
depends_on = None | ||
|
||
MAX_PROMPT_LENGTH = 5_000_000 | ||
|
||
|
||
def upgrade() -> None: | ||
"""NOTE: Prompts without any Personas will just be lost.""" | ||
# Step 1: Add new columns to persona table (only if they don't exist) | ||
op.add_column( | ||
"persona", | ||
sa.Column("system_prompt", sa.String(length=MAX_PROMPT_LENGTH), nullable=True), | ||
) | ||
op.add_column( | ||
"persona", | ||
sa.Column("task_prompt", sa.String(length=MAX_PROMPT_LENGTH), nullable=True), | ||
) | ||
op.add_column( | ||
"persona", | ||
sa.Column( | ||
"datetime_aware", sa.Boolean(), nullable=False, server_default="true" | ||
), | ||
) | ||
|
||
# Check if columns exist before adding them | ||
connection = op.get_bind() | ||
inspector = sa.inspect(connection) | ||
existing_columns = [col["name"] for col in inspector.get_columns("persona")] | ||
|
||
if "system_prompt" not in existing_columns: | ||
op.add_column( | ||
"persona", sa.Column("system_prompt", sa.String(length=8000), nullable=True) | ||
) | ||
|
||
if "task_prompt" not in existing_columns: | ||
op.add_column( | ||
"persona", sa.Column("task_prompt", sa.String(length=8000), nullable=True) | ||
) | ||
|
||
if "datetime_aware" not in existing_columns: | ||
op.add_column( | ||
"persona", | ||
sa.Column( | ||
"datetime_aware", sa.Boolean(), nullable=False, server_default="true" | ||
), | ||
) | ||
|
||
# Step 2: Migrate data from prompt table to persona table (only if tables exist) | ||
existing_tables = inspector.get_table_names() | ||
|
||
if "prompt" in existing_tables and "persona__prompt" in existing_tables: | ||
# For personas that have associated prompts, copy the prompt data | ||
op.execute( | ||
""" | ||
UPDATE persona | ||
SET | ||
system_prompt = p.system_prompt, | ||
task_prompt = p.task_prompt, | ||
datetime_aware = p.datetime_aware | ||
FROM ( | ||
-- Get the first prompt for each persona (in case there are multiple) | ||
SELECT DISTINCT ON (pp.persona_id) | ||
pp.persona_id, | ||
pr.system_prompt, | ||
pr.task_prompt, | ||
pr.datetime_aware | ||
FROM persona__prompt pp | ||
JOIN prompt pr ON pp.prompt_id = pr.id | ||
) p | ||
WHERE persona.id = p.persona_id | ||
# For personas that have associated prompts, copy the prompt data | ||
op.execute( | ||
""" | ||
) | ||
UPDATE persona | ||
SET | ||
system_prompt = p.system_prompt, | ||
task_prompt = p.task_prompt, | ||
datetime_aware = p.datetime_aware | ||
FROM ( | ||
-- Get the first prompt for each persona (in case there are multiple) | ||
SELECT DISTINCT ON (pp.persona_id) | ||
pp.persona_id, | ||
pr.system_prompt, | ||
pr.task_prompt, | ||
pr.datetime_aware | ||
FROM persona__prompt pp | ||
JOIN prompt pr ON pp.prompt_id = pr.id | ||
) p | ||
WHERE persona.id = p.persona_id | ||
""" | ||
) | ||
|
||
# Step 3: Update chat_message references | ||
# Since chat messages referenced prompt_id, we need to update them to use persona_id | ||
# This is complex as we need to map from prompt_id to persona_id | ||
|
||
# Check if chat_message has prompt_id column | ||
chat_message_columns = [ | ||
col["name"] for col in inspector.get_columns("chat_message") | ||
] | ||
if "prompt_id" in chat_message_columns: | ||
op.execute( | ||
""" | ||
ALTER TABLE chat_message | ||
DROP CONSTRAINT IF EXISTS chat_message__prompt_fk | ||
""" | ||
) | ||
op.drop_column("chat_message", "prompt_id") | ||
# Step 3: Update chat_message references | ||
# Since chat messages referenced prompt_id, we need to update them to use persona_id | ||
# This is complex as we need to map from prompt_id to persona_id | ||
op.execute( | ||
""" | ||
ALTER TABLE chat_message | ||
DROP CONSTRAINT IF EXISTS chat_message__prompt_fk | ||
""" | ||
) | ||
op.drop_column("chat_message", "prompt_id") | ||
|
||
# Step 4: Handle personas without prompts - set default values if needed (always run this) | ||
op.execute( | ||
|
@@ -99,26 +82,24 @@ def upgrade() -> None: | |
) | ||
|
||
# Step 5: Drop the persona__prompt association table (if it exists) | ||
if "persona__prompt" in existing_tables: | ||
op.drop_table("persona__prompt") | ||
op.drop_table("persona__prompt") | ||
|
||
# Step 6: Drop the prompt table (if it exists) | ||
if "prompt" in existing_tables: | ||
op.drop_table("prompt") | ||
op.drop_table("prompt") | ||
|
||
|
||
# Step 7: Make system_prompt and task_prompt non-nullable after migration (only if they exist) | ||
op.alter_column( | ||
"persona", | ||
"system_prompt", | ||
existing_type=sa.String(length=8000), | ||
existing_type=sa.String(length=MAX_PROMPT_LENGTH), | ||
nullable=False, | ||
server_default=None, | ||
) | ||
|
||
op.alter_column( | ||
"persona", | ||
"task_prompt", | ||
existing_type=sa.String(length=8000), | ||
existing_type=sa.String(length=MAX_PROMPT_LENGTH), | ||
nullable=False, | ||
server_default=None, | ||
) | ||
|
@@ -132,8 +113,8 @@ def downgrade() -> None: | |
sa.Column("user_id", postgresql.UUID(as_uuid=True), nullable=True), | ||
sa.Column("name", sa.String(), nullable=False), | ||
sa.Column("description", sa.String(), nullable=False), | ||
sa.Column("system_prompt", sa.String(length=8000), nullable=False), | ||
sa.Column("task_prompt", sa.String(length=8000), nullable=False), | ||
sa.Column("system_prompt", sa.String(length=MAX_PROMPT_LENGTH), nullable=False), | ||
sa.Column("task_prompt", sa.String(length=MAX_PROMPT_LENGTH), nullable=False), | ||
sa.Column( | ||
"datetime_aware", sa.Boolean(), nullable=False, server_default="true" | ||
), | ||
|
55 changes: 55 additions & 0 deletions
55
backend/alembic/versions/b7ec9b5b505f_adjust_prompt_length.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
"""adjust prompt length | ||
Revision ID: b7ec9b5b505f | ||
Revises: abbfec3a5ac5 | ||
Create Date: 2025-09-10 18:51:15.629197 | ||
""" | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = "b7ec9b5b505f" | ||
down_revision = "abbfec3a5ac5" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
MAX_PROMPT_LENGTH = 5_000_000 | ||
|
||
|
||
def upgrade() -> None: | ||
# NOTE: need to run this since the previous migration PREVIOUSLY set the length to 8000 | ||
op.alter_column( | ||
"persona", | ||
"system_prompt", | ||
existing_type=sa.TEXT(), | ||
type_=sa.String(length=MAX_PROMPT_LENGTH), | ||
existing_nullable=False, | ||
) | ||
op.alter_column( | ||
"persona", | ||
"task_prompt", | ||
existing_type=sa.TEXT(), | ||
type_=sa.String(length=MAX_PROMPT_LENGTH), | ||
existing_nullable=False, | ||
) | ||
|
||
|
||
def downgrade() -> None: | ||
op.alter_column( | ||
"prompt", | ||
Weves marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
"system_prompt", | ||
existing_type=sa.String(length=MAX_PROMPT_LENGTH), | ||
type_=sa.TEXT(), | ||
existing_nullable=False, | ||
) | ||
op.alter_column( | ||
"prompt", | ||
"task_prompt", | ||
existing_type=sa.String(length=MAX_PROMPT_LENGTH), | ||
type_=sa.TEXT(), | ||
existing_nullable=False, | ||
) | ||
justin-tahara marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.