| 1 | package sessioncatalog |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | ) |
| 7 | |
| 8 | // removeRemappedSessionIdentity removes a projection whose access spelling is |
| 9 | // unchanged but whose filesystem identity moved, for example after a symlink |
| 10 | // retarget. The caller reinserts the current identity in the same transaction. |
| 11 | func removeRemappedSessionIdentity(ctx context.Context, tx *sql.Tx, path, pathKey string) ([]TopicKey, error) { |
| 12 | rows, err := tx.QueryContext(ctx, `SELECT scope,workspace_root,workspace_root_key,topic_id FROM catalog_sessions |
| 13 | WHERE path=? AND path_key<>? AND topic_id<>''`, path, pathKey) |
| 14 | if err != nil { |
| 15 | return nil, err |
| 16 | } |
| 17 | affected := []TopicKey{} |
| 18 | for rows.Next() { |
| 19 | var key TopicKey |
| 20 | if err := rows.Scan(&key.Scope, &key.WorkspaceRoot, &key.workspaceKey, &key.TopicID); err != nil { |
| 21 | _ = rows.Close() |
| 22 | return nil, err |
| 23 | } |
| 24 | affected = append(affected, key) |
| 25 | } |
| 26 | if err := rows.Close(); err != nil { |
| 27 | return nil, err |
| 28 | } |
| 29 | if _, err := tx.ExecContext(ctx, `DELETE FROM catalog_sessions WHERE path=? AND path_key<>?`, path, pathKey); err != nil { |
| 30 | return nil, err |
| 31 | } |
| 32 | return affected, nil |
| 33 | } |
| 34 | |
| 35 | func (c *Catalog) removeRemappedDirectoryIdentity(ctx context.Context, tx *sql.Tx, path, pathKey string) error { |
| 36 | rows, err := tx.QueryContext(ctx, `SELECT DISTINCT s.scope,s.workspace_root,s.workspace_root_key,s.topic_id |
| 37 | FROM catalog_sessions s JOIN catalog_directories d ON d.path_key=s.directory_key |
| 38 | WHERE d.path=? AND d.path_key<>? AND s.topic_id<>''`, path, pathKey) |
| 39 | if err != nil { |
| 40 | return err |
| 41 | } |
| 42 | affected := []TopicKey{} |
| 43 | for rows.Next() { |
| 44 | var key TopicKey |
| 45 | if err := rows.Scan(&key.Scope, &key.WorkspaceRoot, &key.workspaceKey, &key.TopicID); err != nil { |
| 46 | _ = rows.Close() |
| 47 | return err |
| 48 | } |
| 49 | affected = append(affected, key) |
| 50 | } |
| 51 | if err := rows.Close(); err != nil { |
| 52 | return err |
| 53 | } |
| 54 | if _, err := tx.ExecContext(ctx, `DELETE FROM catalog_sessions WHERE directory_key IN ( |
| 55 | SELECT path_key FROM catalog_directories WHERE path=? AND path_key<>? |
| 56 | )`, path, pathKey); err != nil { |
| 57 | return err |
| 58 | } |
| 59 | if _, err := tx.ExecContext(ctx, `DELETE FROM catalog_directories WHERE path=? AND path_key<>?`, path, pathKey); err != nil { |
| 60 | return err |
| 61 | } |
| 62 | for _, key := range affected { |
| 63 | if err := c.recomputeTopic(ctx, tx, key); err != nil { |
| 64 | return err |
| 65 | } |
| 66 | } |
| 67 | return nil |
| 68 | } |
| 69 | |
| 70 | func removeRemappedTopicIdentity(ctx context.Context, tx *sql.Tx, key TopicKey, rootKey string) error { |
| 71 | _, err := tx.ExecContext(ctx, `DELETE FROM catalog_topics |
| 72 | WHERE scope=? AND workspace_root=? AND topic_id=? AND workspace_root_key<>?`, |
| 73 | key.Scope, key.WorkspaceRoot, key.TopicID, rootKey) |
| 74 | return err |
| 75 | } |
| 76 | |
| 77 | func removeRemappedFoldedTopicIdentity(ctx context.Context, tx *sql.Tx, key TopicKey, rootKey string) error { |
| 78 | _, err := tx.ExecContext(ctx, `DELETE FROM catalog_folded_topics |
| 79 | WHERE scope=? AND workspace_root=? AND topic_id=? AND workspace_root_key<>?`, |
| 80 | key.Scope, key.WorkspaceRoot, key.TopicID, rootKey) |
| 81 | return err |
| 82 | } |
| 83 |