This commit is contained in:
Timothy Jaeryang Baek committed 2026-05-14 14:06:32 +09:00
1 parent 9717ada92f
commit d7cfc1e46a
3 files changed
+49 -13

No files matched your search

@@ -16,17 +16,33 @@ depends_on = None
def upgrade(): def upgrade():
conn = op.get_bind()
inspector = sa.inspect(conn)
def _idx_exists(table, idx_name):
return any(i['name'] == idx_name for i in inspector.get_indexes(table))
# Chat table indexes # Chat table indexes
if not _idx_exists('chat', 'folder_id_idx'):
op.create_index('folder_id_idx', 'chat', ['folder_id']) op.create_index('folder_id_idx', 'chat', ['folder_id'])
if not _idx_exists('chat', 'user_id_pinned_idx'):
op.create_index('user_id_pinned_idx', 'chat', ['user_id', 'pinned']) op.create_index('user_id_pinned_idx', 'chat', ['user_id', 'pinned'])
if not _idx_exists('chat', 'user_id_archived_idx'):
op.create_index('user_id_archived_idx', 'chat', ['user_id', 'archived']) op.create_index('user_id_archived_idx', 'chat', ['user_id', 'archived'])
if not _idx_exists('chat', 'updated_at_user_id_idx'):
op.create_index('updated_at_user_id_idx', 'chat', ['updated_at', 'user_id']) op.create_index('updated_at_user_id_idx', 'chat', ['updated_at', 'user_id'])
if not _idx_exists('chat', 'folder_id_user_id_idx'):
op.create_index('folder_id_user_id_idx', 'chat', ['folder_id', 'user_id']) op.create_index('folder_id_user_id_idx', 'chat', ['folder_id', 'user_id'])
# Tag table index # Tag table index
if not _idx_exists('tag', 'user_id_idx'):
op.create_index('user_id_idx', 'tag', ['user_id']) op.create_index('user_id_idx', 'tag', ['user_id'])
# Function table index # Function table index (only if is_global column exists — added by a later migration)
conn = op.get_bind()
inspector = sa.inspect(conn)
func_cols = {c['name'] for c in inspector.get_columns('function')}
if 'is_global' in func_cols and not _idx_exists('function', 'is_global_idx'):
op.create_index('is_global_idx', 'function', ['is_global']) op.create_index('is_global_idx', 'function', ['is_global'])
@@ -20,11 +20,23 @@ depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None: def upgrade() -> None:
conn = op.get_bind() conn = op.get_bind()
inspector = sa.inspect(conn)
existing_tables = set(inspector.get_table_names())
# Step 1: Read existing data from OLD table (schema likely command as PK) # If the final state already exists (prompt has 'id' PK + prompt_history exists),
# We use batch_alter previously, but we want to move to new table. # the migration completed successfully on a prior run — nothing to do.
# We need to assume the OLD structure. if 'prompt_history' in existing_tables and 'prompt_new' not in existing_tables:
# prompt_history exists and prompt_new was already renamed → done
prompt_cols = {c['name'] for c in inspector.get_columns('prompt')}
if 'id' in prompt_cols and 'version_id' in prompt_cols:
return
# Step 1: Read existing data from OLD table (schema: command as PK)
# Only read if the old-schema prompt table still exists (has 'command' but no 'version_id')
existing_prompts = []
if 'prompt' in existing_tables and 'prompt_new' not in existing_tables:
prompt_cols = {c['name'] for c in inspector.get_columns('prompt')}
if 'command' in prompt_cols and 'version_id' not in prompt_cols:
old_prompt_table = sa.table( old_prompt_table = sa.table(
'prompt', 'prompt',
sa.column('command', sa.Text()), sa.column('command', sa.Text()),
@@ -34,8 +46,6 @@ def upgrade() -> None:
sa.column('timestamp', sa.BigInteger()), sa.column('timestamp', sa.BigInteger()),
sa.column('access_control', sa.JSON()), sa.column('access_control', sa.JSON()),
) )
# Check if table exists/read data
try: try:
existing_prompts = conn.execute( existing_prompts = conn.execute(
sa.select( sa.select(
@@ -48,10 +58,10 @@ def upgrade() -> None:
) )
).fetchall() ).fetchall()
except Exception: except Exception:
# Fallback if table doesn't exist (new install)
existing_prompts = [] existing_prompts = []
# Step 2: Create new prompt table with 'id' as PRIMARY KEY # Step 2: Create new prompt table with 'id' as PRIMARY KEY (if not already created)
if 'prompt_new' not in existing_tables:
op.create_table( op.create_table(
'prompt_new', 'prompt_new',
sa.Column('id', sa.Text(), primary_key=True), sa.Column('id', sa.Text(), primary_key=True),
@@ -69,7 +79,8 @@ def upgrade() -> None:
sa.Column('updated_at', sa.BigInteger(), nullable=False), sa.Column('updated_at', sa.BigInteger(), nullable=False),
) )
# Step 3: Create prompt_history table # Step 3: Create prompt_history table (if not already created)
if 'prompt_history' not in existing_tables:
op.create_table( op.create_table(
'prompt_history', 'prompt_history',
sa.Column('id', sa.Text(), primary_key=True), sa.Column('id', sa.Text(), primary_key=True),
@@ -81,7 +92,8 @@ def upgrade() -> None:
sa.Column('created_at', sa.BigInteger(), nullable=False), sa.Column('created_at', sa.BigInteger(), nullable=False),
) )
# Step 4: Migrate data # Step 4: Migrate data (only if we have old data to migrate)
if existing_prompts:
prompt_new_table = sa.table( prompt_new_table = sa.table(
'prompt_new', 'prompt_new',
sa.column('id', sa.Text()), sa.column('id', sa.Text()),
@@ -161,7 +173,12 @@ def upgrade() -> None:
) )
) )
# Step 5: Replace old table with new one # Step 5: Replace old table with new one (only if prompt_new exists)
# Re-check tables after potential creation above
inspector.clear_cache()
current_tables = set(inspector.get_table_names())
if 'prompt_new' in current_tables:
if 'prompt' in current_tables:
op.drop_table('prompt') op.drop_table('prompt')
op.rename_table('prompt_new', 'prompt') op.rename_table('prompt_new', 'prompt')
@@ -158,9 +158,12 @@ def upgrade() -> None:
if col_name not in user_columns: if col_name not in user_columns:
op.add_column('user', sa.Column(col_name, col_type, nullable=True)) op.add_column('user', sa.Column(col_name, col_type, nullable=True))
# Convert info (TEXT/JSONField) → JSON # Convert info (TEXT/JSONField) → JSON (skip if already JSON)
user_col_types = {c['name']: c['type'] for c in inspector.get_columns('user')}
if isinstance(user_col_types.get('info'), sa.Text):
_convert_column_to_json('user', 'info') _convert_column_to_json('user', 'info')
# Convert settings (TEXT/JSONField) → JSON # Convert settings (TEXT/JSONField) → JSON (skip if already JSON)
if isinstance(user_col_types.get('settings'), sa.Text):
_convert_column_to_json('user', 'settings') _convert_column_to_json('user', 'settings')
# ── Create api_key table (idempotent) ───────────────────────────── # ── Create api_key table (idempotent) ─────────────────────────────