Skip to content

Commit

Permalink
make auto view creation work for all the views
Browse files Browse the repository at this point in the history
  • Loading branch information
mark2600 authored and fenollp committed Dec 16, 2016
1 parent 36e39e0 commit 8d9939c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
22 changes: 21 additions & 1 deletion core/kazoo_data/src/kz_datamgr.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1100,7 +1100,16 @@ get_results(DbName, DesignDoc) ->

get_results(DbName, DesignDoc, Options) when ?VALID_DBNAME ->
Opts = maybe_add_doc_type_from_view(DesignDoc, Options),
kzs_view:get_results(kzs_plan:plan(DbName, Opts), DbName, DesignDoc, Options);
Plan = kzs_plan:plan(DbName, Opts),

case kzs_view:get_results(Plan, DbName, DesignDoc, Options) of
{'error', 'not_found'} ->
maybe_create_view(DbName, Plan, DesignDoc, Options);

Other ->
Other
end;

get_results(DbName, DesignDoc, Options) ->
case maybe_convert_dbname(DbName) of
{'ok', Db} -> get_results(Db, DesignDoc, Options);
Expand All @@ -1111,6 +1120,17 @@ get_results_count(DbName, DesignDoc, Options) ->
Opts = maybe_add_doc_type_from_view(DesignDoc, Options),
kzs_view:get_results_count(kzs_plan:plan(DbName, Opts), DbName, DesignDoc, Options).

-spec maybe_create_view(ne_binary(), map(), ne_binary(), view_options()) -> get_results_return().
maybe_create_view(DbName, Plan, DesignDoc, Options) ->
case props:get_value('view_json', Options) of
'undefined' ->
{'error', 'not_found'};

ViewJson ->
db_view_update(DbName, ViewJson),
kzs_view:get_results(Plan, DbName, DesignDoc, Options)
end.

-spec get_result_keys(ne_binary(), ne_binary(), view_options()) ->
{'ok', ne_binaries()} | data_error().
get_result_keys(DbName, DesignDoc, Options) ->
Expand Down
10 changes: 2 additions & 8 deletions core/kazoo_modb/src/kazoo_modb.erl
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ strip_modb_options(ViewOptions) ->
is_modb_option({'year', _}) -> 'true';
is_modb_option({'month', _}) -> 'true';
is_modb_option({'create_db', _}) -> 'true';
is_modb_option({'view_json', _}) -> 'true';
is_modb_option(_) -> 'false'.

-spec get_results_not_found(ne_binary(), ne_binary(), view_options(), integer()) ->
Expand All @@ -81,7 +80,7 @@ get_results_not_found(Account, View, ViewOptions, Retry) ->
EncodedMODb = kz_util:format_account_modb(AccountMODb, 'encoded'),
case kz_datamgr:db_exists(EncodedMODb, View) of
'true' ->
refresh_views(AccountMODb, ViewOptions),
refresh_views(AccountMODb),
get_results(Account, View, ViewOptions, Retry-1);
'false' ->
get_results_missing_db(Account, View, ViewOptions, Retry)
Expand Down Expand Up @@ -327,15 +326,10 @@ is_account_deleted(AccountId) ->

-spec refresh_views(ne_binary()) -> 'ok'.
refresh_views(AccountMODb) ->
refresh_views(AccountMODb, []).

-spec refresh_views(ne_binary(), list()) -> 'ok'.
refresh_views(AccountMODb, Options) ->
lager:debug("refresh views on modb ~p", [AccountMODb]),
EncodedMODb = kz_util:format_account_modb(AccountMODb, 'encoded'),
Views = get_modb_views(),
ExtraViews = props:get_value('view_json', Options, []),
_ = kapps_util:update_views(EncodedMODb, Views++ExtraViews, 'true'),
_ = kapps_util:update_views(EncodedMODb, Views, 'true'),
'ok'.

-spec get_modb_views() -> kz_proplist().
Expand Down

0 comments on commit 8d9939c

Please sign in to comment.