------------------------------------------------------------------- saveViewCollection persists the provided View collection changes: - deletes the old related SQL view (if any) - creates a new SQL view with the latest newCollection.Options.Query - generates new feilds list based on newCollection.O
(app App, newCollection, oldCollection *Collection)
| 259 | // |
| 260 | // This method returns an error if newCollection is not a "view". |
| 261 | func saveViewCollection(app App, newCollection, oldCollection *Collection) error { |
| 262 | if !newCollection.IsView() { |
| 263 | return errors.New("not a view collection") |
| 264 | } |
| 265 | |
| 266 | return app.RunInTransaction(func(txApp App) error { |
| 267 | query := newCollection.ViewQuery |
| 268 | |
| 269 | // generate collection fields from the query |
| 270 | viewFields, err := txApp.CreateViewFields(query) |
| 271 | if err != nil { |
| 272 | return err |
| 273 | } |
| 274 | |
| 275 | // delete old renamed view |
| 276 | if oldCollection != nil { |
| 277 | if err := txApp.DeleteView(oldCollection.Name); err != nil { |
| 278 | return err |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | // wrap view query if necessary |
| 283 | query, err = normalizeViewQueryId(txApp, query) |
| 284 | if err != nil { |
| 285 | return fmt.Errorf("failed to normalize view query id: %w", err) |
| 286 | } |
| 287 | |
| 288 | // (re)create the view |
| 289 | if err := txApp.SaveView(newCollection.Name, query); err != nil { |
| 290 | return err |
| 291 | } |
| 292 | |
| 293 | newCollection.Fields = viewFields |
| 294 | |
| 295 | return txApp.Save(newCollection) |
| 296 | }) |
| 297 | } |
| 298 | |
| 299 | // normalizeViewQueryId wraps (if necessary) the provided view query |
| 300 | // with a subselect to ensure that the id column is a text since |
no test coverage detected
searching dependent graphs…