fix: Migration 0104 — check table existence before ALTER, remove companies table

This commit is contained in:
Agent Zero
2026-08-04 22:42:25 +02:00
parent 6881e8abde
commit fcc1c92b33
@@ -35,17 +35,22 @@ def upgrade() -> None:
) )
# 2. Add embedding columns to other tables (from plugin migration 0002) # 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( result = conn.execute(sa.text(
f"SELECT column_name FROM information_schema.columns " f"SELECT column_name FROM information_schema.columns "
f"WHERE table_name = '{table}' AND column_name = 'embedding'" f"WHERE table_name = '{table}' AND column_name = 'embedding'"
)) ))
if result.fetchone() is None: if result.fetchone() is None:
op.execute(f"ALTER TABLE {table} ADD COLUMN embedding vector(768)") # Check if table exists
op.execute( table_exists = conn.execute(sa.text(
f"CREATE INDEX IF NOT EXISTS ix_{table}_embedding " f"SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = '{table}')"
f"ON {table} USING hnsw(embedding vector_cosine_ops)" )).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 # Tags use 384-dim embeddings
result = conn.execute(sa.text( result = conn.execute(sa.text(
@@ -53,11 +58,15 @@ def upgrade() -> None:
"WHERE table_name = 'tags' AND column_name = 'embedding'" "WHERE table_name = 'tags' AND column_name = 'embedding'"
)) ))
if result.fetchone() is None: if result.fetchone() is None:
op.execute("ALTER TABLE tags ADD COLUMN embedding vector(384)") table_exists = conn.execute(sa.text(
op.execute( "SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'tags')"
"CREATE INDEX IF NOT EXISTS ix_tags_embedding " )).scalar()
"ON tags USING hnsw(embedding vector_cosine_ops)" 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) # 3. Add timestamp columns to audit_log (if not exists)
for col in ["created_at", "updated_at", "deleted_at"]: for col in ["created_at", "updated_at", "deleted_at"]: