From fcc1c92b33833e664eb476d8b6f7d0edac2e8048 Mon Sep 17 00:00:00 2001 From: Agent Zero Date: Tue, 4 Aug 2026 22:42:25 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20Migration=200104=20=E2=80=94=20check=20t?= =?UTF-8?q?able=20existence=20before=20ALTER,=20remove=20companies=20table?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0104_add_embedding_and_audit_timestamps.py | 31 ++++++++++++------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/alembic/versions/0104_add_embedding_and_audit_timestamps.py b/alembic/versions/0104_add_embedding_and_audit_timestamps.py index dc13834..2890353 100644 --- a/alembic/versions/0104_add_embedding_and_audit_timestamps.py +++ b/alembic/versions/0104_add_embedding_and_audit_timestamps.py @@ -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"]: