fix: Migration 0104 — check table existence before ALTER, remove companies table
This commit is contained in:
@@ -35,17 +35,22 @@ def upgrade() -> None:
|
||||
)
|
||||
|
||||
# 2. Add embedding columns to other tables (from plugin migration 0002)
|
||||
for table in ["mails", "companies", "files", "calendar_entries"]:
|
||||
for table in ["mails", "files", "calendar_entries"]:
|
||||
result = conn.execute(sa.text(
|
||||
f"SELECT column_name FROM information_schema.columns "
|
||||
f"WHERE table_name = '{table}' AND column_name = 'embedding'"
|
||||
))
|
||||
if result.fetchone() is None:
|
||||
op.execute(f"ALTER TABLE {table} ADD COLUMN embedding vector(768)")
|
||||
op.execute(
|
||||
f"CREATE INDEX IF NOT EXISTS ix_{table}_embedding "
|
||||
f"ON {table} USING hnsw(embedding vector_cosine_ops)"
|
||||
)
|
||||
# Check if table exists
|
||||
table_exists = conn.execute(sa.text(
|
||||
f"SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = '{table}')"
|
||||
)).scalar()
|
||||
if table_exists:
|
||||
op.execute(f"ALTER TABLE {table} ADD COLUMN embedding vector(768)")
|
||||
op.execute(
|
||||
f"CREATE INDEX IF NOT EXISTS ix_{table}_embedding "
|
||||
f"ON {table} USING hnsw(embedding vector_cosine_ops)"
|
||||
)
|
||||
|
||||
# Tags use 384-dim embeddings
|
||||
result = conn.execute(sa.text(
|
||||
@@ -53,11 +58,15 @@ def upgrade() -> None:
|
||||
"WHERE table_name = 'tags' AND column_name = 'embedding'"
|
||||
))
|
||||
if result.fetchone() is None:
|
||||
op.execute("ALTER TABLE tags ADD COLUMN embedding vector(384)")
|
||||
op.execute(
|
||||
"CREATE INDEX IF NOT EXISTS ix_tags_embedding "
|
||||
"ON tags USING hnsw(embedding vector_cosine_ops)"
|
||||
)
|
||||
table_exists = conn.execute(sa.text(
|
||||
"SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'tags')"
|
||||
)).scalar()
|
||||
if table_exists:
|
||||
op.execute("ALTER TABLE tags ADD COLUMN embedding vector(384)")
|
||||
op.execute(
|
||||
"CREATE INDEX IF NOT EXISTS ix_tags_embedding "
|
||||
"ON tags USING hnsw(embedding vector_cosine_ops)"
|
||||
)
|
||||
|
||||
# 3. Add timestamp columns to audit_log (if not exists)
|
||||
for col in ["created_at", "updated_at", "deleted_at"]:
|
||||
|
||||
Reference in New Issue
Block a user