@@ -24,7 +24,7 @@ type DBInterface interface {
2424 UpdateServerDetails (id int64 , name , url string ) error
2525
2626 // Tool Management
27- UpsertTool (tool models.Tool ) ( added bool , err error )
27+ UpsertTool (tool models.Tool ) error
2828 ListTools () ([]models.Tool , error )
2929 ListToolsByServerID (serverID int64 ) ([]models.Tool , error )
3030 RemoveToolsByServerID (serverID int64 ) error
@@ -80,7 +80,7 @@ func (db *DB) ensureSchema() error {
8080 name TEXT NOT NULL,
8181 description TEXT,
8282 created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
83- updated_at DATETIME DEFAULT CURRENT_TIMESTAMP, -- Added this column
83+ updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
8484 FOREIGN KEY (source_server_id) REFERENCES mcp_servers(id) ON DELETE CASCADE,
8585 UNIQUE (source_server_id, external_id)
8686 );
@@ -156,7 +156,7 @@ func (db *DB) RemoveServer(id int64) error {
156156 return fmt .Errorf ("failed to get rows affected for remove server ID %d: %w" , id , err )
157157 }
158158 if rowsAffected == 0 {
159- return fmt . Errorf ( "server with ID %d not found for removal" , id )
159+ return nil
160160 }
161161 return nil
162162}
@@ -173,7 +173,6 @@ func (db *DB) UpdateServerStatus(id int64, state models.ConnectionState, lastErr
173173
174174// UpdateServerDetails updates the name and URL for a server.
175175func (db * DB ) UpdateServerDetails (id int64 , name , url string ) error {
176- // Add validation if needed (e.g., check for URL uniqueness if changing)
177176 query := `UPDATE mcp_servers SET name = ?, url = ? WHERE id = ?`
178177 _ , err := db .Exec (query , name , url , id )
179178 if err != nil {
@@ -185,46 +184,36 @@ func (db *DB) UpdateServerDetails(id int64, name, url string) error {
185184// --- Tool CRUD ---
186185
187186// UpsertTool inserts a new tool or updates an existing one based on external_id and source_server_id.
188- // It returns true if a new row was inserted, false if an existing row was updated.
189- func (db * DB ) UpsertTool (tool models.Tool ) (added bool , err error ) {
187+ func (db * DB ) UpsertTool (tool models.Tool ) (err error ) {
190188 tx , err := db .Beginx ()
191189 if err != nil {
192- return false , fmt .Errorf ("failed to begin transaction for upsert tool: %w" , err )
190+ return fmt .Errorf ("failed to begin transaction for upsert tool: %w" , err )
193191 }
194- // Ensure rollback on error
192+ // Ensure rollback on error, commit on success
195193 defer func () {
196- if p := recover (); p != nil {
197- _ = tx .Rollback ()
198- panic (p ) // Re-panic after rollback
199- } else if err != nil {
200- _ = tx .Rollback () // Rollback on normal error
201- } else {
202- err = tx .Commit () // Commit on success
203- if err != nil {
204- err = fmt .Errorf ("failed to commit transaction for upsert tool: %w" , err )
194+ if err != nil {
195+ // Rollback only if the error is not nil on exit
196+ if rbErr := tx .Rollback (); rbErr != nil {
197+ // Log or wrap the rollback error if necessary, but prioritize the original error
198+ fmt .Printf ("ERROR: Failed to rollback transaction after error: %v (original error: %v)\n " , rbErr , err )
205199 }
200+ return // Keep the original error
201+ }
202+ // Commit if err is nil
203+ err = tx .Commit ()
204+ if err != nil {
205+ err = fmt .Errorf ("failed to commit transaction for upsert tool: %w" , err )
206206 }
207207 }()
208208
209- // 1. Check if the tool already exists
210- var exists int
211- checkQuery := `SELECT COUNT(*) FROM tools WHERE external_id = ? AND source_server_id = ?`
212- err = tx .Get (& exists , checkQuery , tool .ExternalID , tool .SourceServerID )
213- if err != nil && err != sql .ErrNoRows { // Allow ErrNoRows here, though count should return 0
214- err = fmt .Errorf ("failed to check tool existence: %w" , err )
215- return // Defer will rollback
216- }
217-
218- added = (exists == 0 )
219-
220- // 2. Perform the UPSERT
209+ // Perform the UPSERT
221210 upsertQuery := `
222211 INSERT INTO tools (external_id, source_server_id, name, description, created_at, updated_at)
223- VALUES (:external_id, :source_server_id, :name, :description, :created_at, :updated_at)
224- ON CONFLICT(external_id, source_server_id) DO UPDATE SET
225- name = excluded.name,
226- description = excluded.description,
227- updated_at = excluded.updated_at
212+ VALUES (:external_id, :source_server_id, :name, :description, :created_at, :updated_at)
213+ ON CONFLICT(external_id, source_server_id) DO UPDATE SET
214+ name = excluded.name,
215+ description = excluded.description,
216+ updated_at = excluded.updated_at
228217 `
229218 now := time .Now ().UTC ()
230219 tool .CreatedAt = now // Set creation time (only used on INSERT)
@@ -245,8 +234,8 @@ func (db *DB) UpsertTool(tool models.Tool) (added bool, err error) {
245234 return // Defer will rollback
246235 }
247236
248- // Defer will commit if err is nil
249- return added , err
237+ // Defer handles commit/rollback
238+ return err
250239}
251240
252241// ListTools retrieves all tools from the database.
0 commit comments