(diagram: Diagram)
| 57 | |
| 58 | // Fix problematic field names in the diagram before passing to SQL generator |
| 59 | const fixProblematicFieldNames = (diagram: Diagram): Diagram => { |
| 60 | const fixedTables = |
| 61 | diagram.tables?.map((table) => { |
| 62 | // Deep clone the table to avoid modifying the original |
| 63 | const newTable = { ...table }; |
| 64 | |
| 65 | // Fix field names if this is the "relation" table |
| 66 | if (table.name === 'relation') { |
| 67 | newTable.fields = table.fields.map((field) => { |
| 68 | // Create a new field to avoid modifying the original |
| 69 | const newField = { ...field }; |
| 70 | |
| 71 | // Fix the 'from' and 'to' fields which are SQL keywords |
| 72 | if (field.name === 'from') { |
| 73 | newField.name = 'source'; |
| 74 | } else if (field.name === 'to') { |
| 75 | newField.name = 'target'; |
| 76 | } |
| 77 | |
| 78 | return newField; |
| 79 | }); |
| 80 | } |
| 81 | |
| 82 | return newTable; |
| 83 | }) || []; |
| 84 | |
| 85 | // Update relationships to point to the renamed fields |
| 86 | const fixedRelationships = |
| 87 | diagram.relationships?.map((rel) => { |
| 88 | const relationTable = diagram.tables?.find( |
| 89 | (t) => t.name === 'relation' |
| 90 | ); |
| 91 | if (!relationTable) return rel; |
| 92 | |
| 93 | const newRel = { ...rel }; |
| 94 | |
| 95 | // Fix relationships that were pointing to the 'from' field |
| 96 | const fromField = relationTable.fields.find( |
| 97 | (f) => f.name === 'from' |
| 98 | ); |
| 99 | if (fromField && rel.targetFieldId === fromField.id) { |
| 100 | // We need to look up the renamed field in our fixed tables |
| 101 | const fixedRelationTable = fixedTables.find( |
| 102 | (t) => t.name === 'relation' |
| 103 | ); |
| 104 | const sourceField = fixedRelationTable?.fields.find( |
| 105 | (f) => f.name === 'source' |
| 106 | ); |
| 107 | if (sourceField) { |
| 108 | newRel.targetFieldId = sourceField.id; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // Fix relationships that were pointing to the 'to' field |
| 113 | const toField = relationTable.fields.find((f) => f.name === 'to'); |
| 114 | if (toField && rel.targetFieldId === toField.id) { |
| 115 | // We need to look up the renamed field in our fixed tables |
| 116 | const fixedRelationTable = fixedTables.find( |
no outgoing calls
no test coverage detected