| 350 | } |
| 351 | |
| 352 | func InitializeViews(ctx context.Context, ds sgbucket.DataStore) error { |
| 353 | collection, isCBServerCollection := ds.(*base.Collection) |
| 354 | if isCBServerCollection && !collection.IsDefaultScopeCollection() { |
| 355 | return fmt.Errorf("Can not initialize views on a non-default Couchbase Server collection") |
| 356 | } |
| 357 | |
| 358 | viewStore, ok := ds.(sgbucket.ViewStore) |
| 359 | if !ok { |
| 360 | return fmt.Errorf("Datastore does not support views") |
| 361 | } |
| 362 | |
| 363 | // Check whether design docs are already present |
| 364 | ddocsExist := checkExistingDDocs(ctx, viewStore) |
| 365 | |
| 366 | // If not present, install design docs and views |
| 367 | if !ddocsExist { |
| 368 | base.InfofCtx(ctx, base.KeyAll, "Design docs for current view version (%s) do not exist - creating...", DesignDocVersion) |
| 369 | if err := installViews(ctx, viewStore); err != nil { |
| 370 | return err |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | // Wait for views to be indexed and available |
| 375 | return WaitForViews(ctx, ds) |
| 376 | } |
| 377 | |
| 378 | func checkExistingDDocs(ctx context.Context, viewStore sgbucket.ViewStore) bool { |
| 379 | |