package db import ( "database/sql" ) // PersistExcalidrawIDs writes the assignments returned by the exporter // back onto the corresponding rows. Idempotent: only updates rows whose // excalidraw_id is currently NULL (the first export "owns" the id; later // exports reuse it so mxdrw's collab cursors / undo history survive). // // Caller passes one map per kind; keys are the in-project row ids, // values are the 21-char Excalidraw element ids the exporter minted. func (s *Store) PersistExcalidrawIDs(projectID int64, frames, devices, ports, ios, cables, clamps map[int64]string, ) error { tx, err := s.db.Begin() if err != nil { return err } defer tx.Rollback() if err := updateExIDs(tx, "frames", projectID, frames); err != nil { return err } if err := updateExIDs(tx, "devices", projectID, devices); err != nil { return err } if err := updateExIDs(tx, "ports", projectID, ports); err != nil { return err } if err := updateExIDs(tx, "io_markers", projectID, ios); err != nil { return err } if err := updateExIDs(tx, "cables", projectID, cables); err != nil { return err } if err := updateExIDs(tx, "clamps", projectID, clamps); err != nil { return err } return tx.Commit() } func updateExIDs(tx *sql.Tx, table string, projectID int64, m map[int64]string) error { if len(m) == 0 { return nil } stmt, err := tx.Prepare( `UPDATE ` + table + ` SET excalidraw_id = ? WHERE id = ? AND project_id = ? AND excalidraw_id IS NULL`, ) if err != nil { return err } defer stmt.Close() for id, exID := range m { if _, err := stmt.Exec(exID, id, projectID); err != nil { return err } } return nil }