{
"event": "PostToolUse",
"tool_name": "Read",
"tool_response": {
"type": "text",
"file": {
"filePath": "\/var\/www\/scripts\/pipeline\/migrate_semantics.py",
"content": "#!\/usr\/bin\/env python3\n\"\"\"\nMigration Script: Unify semantic tables.\nPhase 1 of semantic infrastructure cleanup.\n\"\"\"\n\nimport os\nimport sys\n\n# Add parent directory for imports\nsys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))\n\nfrom db import db\n\n\ndef migrate_chunk_text_semantics():\n \"\"\"Migrate data from chunk_text_semantics to chunk_semantics.\"\"\"\n print(\"Migrating chunk_text_semantics -> chunk_semantics...\")\n\n # Get all chunk_text_semantics records\n cursor = db.execute(\"\"\"\n SELECT chunk_id, statement_form, intent, frame, is_negated, discourse_role\n FROM chunk_text_semantics\n \"\"\")\n records = cursor.fetchall()\n cursor.close()\n\n print(f\" Found {len(records)} records to migrate\")\n\n migrated = 0\n for rec in records:\n try:\n db.execute(\"\"\"\n UPDATE chunk_semantics\n SET statement_form = %s,\n intent = %s,\n frame = %s,\n is_negated = %s,\n discourse_role = %s\n WHERE chunk_id = %s\n \"\"\", (\n rec['statement_form'],\n rec['intent'],\n rec['frame'],\n rec['is_negated'],\n rec['discourse_role'],\n rec['chunk_id']\n ))\n migrated += 1\n except Exception as e:\n print(f\" Error migrating chunk_id {rec['chunk_id']}: {e}\")\n\n db.commit()\n print(f\" Migrated: {migrated}\")\n return migrated\n\n\ndef migrate_entity_knowledge_semantics():\n \"\"\"Migrate data from entity_knowledge_semantics to entity_semantics.\"\"\"\n print(\"Migrating entity_knowledge_semantics -> entity_semantics...\")\n\n # Get all entity_knowledge_semantics records\n cursor = db.execute(\"\"\"\n SELECT entity_id, chunk_id, semantic_role, properties,\n functional_category, context_meaning, confidence, model_used\n FROM entity_knowledge_semantics\n \"\"\")\n records = cursor.fetchall()\n cursor.close()\n\n print(f\" Found {len(records)} records to migrate\")\n\n migrated = 0\n for rec in records:\n try:\n # Insert or update entity_semantics with all fields\n db.execute(\"\"\"\n INSERT INTO entity_semantics\n (entity_id, chunk_id, context, semantic_role, properties,\n functional_category, confidence, model_used)\n VALUES (%s, %s, %s, %s, %s, %s, %s, %s)\n ON DUPLICATE KEY UPDATE\n chunk_id = VALUES(chunk_id),\n context = VALUES(context),\n semantic_role = VALUES(semantic_role),\n properties = VALUES(properties),\n functional_category = VALUES(functional_category),\n confidence = VALUES(confidence),\n model_used = VALUES(model_used)\n \"\"\", (\n rec['entity_id'],\n rec['chunk_id'],\n rec['context_meaning'],\n rec['semantic_role'],\n rec['properties'],\n rec['functional_category'],\n rec['confidence'],\n rec['model_used']\n ))\n migrated += 1\n except Exception as e:\n print(f\" Error migrating entity_id {rec['entity_id']}: {e}\")\n\n db.commit()\n print(f\" Migrated: {migrated}\")\n return migrated\n\n\ndef verify_migration():\n \"\"\"Verify migration was successful.\"\"\"\n print(\"\\nVerification:\")\n\n # Check chunk_semantics\n cursor = db.execute(\"\"\"\n SELECT COUNT(*) as total,\n SUM(CASE WHEN statement_form IS NOT NULL THEN 1 ELSE 0 END) as with_form\n FROM chunk_semantics\n \"\"\")\n result = cursor.fetchone()\n cursor.close()\n print(f\" chunk_semantics: {result['with_form']}\/{result['total']} have statement_form\")\n\n # Check entity_semantics\n cursor = db.execute(\"SELECT COUNT(*) as cnt FROM entity_semantics\")\n result = cursor.fetchone()\n cursor.close()\n print(f\" entity_semantics: {result['cnt']} records\")\n\n\ndef main():\n \"\"\"Run migration.\"\"\"\n print(\"=\" * 60)\n print(\"Semantic Infrastructure Migration\")\n print(\"=\" * 60)\n\n if not db.connect():\n print(\"ERROR: Could not connect to database\")\n return 1\n\n try:\n # Step 1: Migrate chunk semantics\n migrate_chunk_text_semantics()\n\n # Step 2: Migrate entity semantics\n migrate_entity_knowledge_semantics()\n\n # Verify\n verify_migration()\n\n print(\"\\n\" + \"=\" * 60)\n print(\"Migration complete!\")\n print(\"=\" * 60)\n\n finally:\n db.disconnect()\n\n return 0\n\n\nif __name__ == \"__main__\":\n sys.exit(main())\n",
"numLines": 162,
"startLine": 1,
"totalLines": 162
}
}
}