Skip to content

Commit

Permalink
Merge pull request #753 from 2600hz/KAZOO-3301
Browse files Browse the repository at this point in the history
KAZOO-3301 override_existing_document in copy_doc/move_doc
  • Loading branch information
James Aimonetti committed Feb 5, 2015
2 parents f41555c + 1b91a2f commit 7dc5886
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 24 deletions.
60 changes: 40 additions & 20 deletions applications/fax/src/fax_maintenance.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,65 @@
%% ====================================================================
%% API functions
%% ====================================================================
-export([migrate/0, migrate/1]).
-export([migrate/0, migrate/1, migrate/2]).
-export([flush/0]).

-export([restart_job/1 , update_job/2]).
-export([account_jobs/1, account_jobs/2]).
-export([faxbox_jobs/1, faxbox_jobs/2]).
-export([pending_jobs/0, active_jobs/0]).

-define(DEFAULT_MIGRATE_OPTIONS, []).
-define(OVERRIDE_DOCS, ['override_existing_document']).

-spec migrate() -> 'ok'.
migrate() ->
Accounts = whapps_util:get_all_accounts(),
Total = length(Accounts),
lists:foldr(fun(A, C) -> migrate_faxes_fold(A, C, Total) end, 1, Accounts),
lists:foldr(fun(A, C) -> migrate_faxes_fold(A, C, Total,?DEFAULT_MIGRATE_OPTIONS) end, 1, Accounts),
'ok'.

-spec migrate(ne_binaries() | ne_binary()) -> 'ok'.
migrate([]) -> 'ok';
migrate(<<"override_existing_documents">>) ->
Accounts = whapps_util:get_all_accounts(),
Total = length(Accounts),
lists:foldr(fun(A, C) -> migrate_faxes_fold(A, C, Total, ?OVERRIDE_DOCS) end, 1, Accounts),
'ok';
migrate([Account|Accounts]) ->
_ = migrate_faxes(Account),
_ = migrate_faxes(Account, ?DEFAULT_MIGRATE_OPTIONS),
migrate(Accounts);
migrate(Account) ->
migrate_faxes(Account).
migrate_faxes(Account, ?DEFAULT_MIGRATE_OPTIONS).

-spec migrate(ne_binaries() | ne_binary(), ne_binary() | wh_proplist()) -> 'ok'.
migrate([], _) -> 'ok';
migrate(Accounts, <<"override_existing_documents">>) ->
migrate(Accounts, ?OVERRIDE_DOCS);
migrate(Accounts, Option) when is_binary(Option)->
migrate(Accounts, ?DEFAULT_MIGRATE_OPTIONS);
migrate([Account|Accounts], Options) when is_list(Options) ->
_ = migrate_faxes(Account, Options),
migrate(Accounts, Options);
migrate(Account, Options) when is_list(Options) ->
migrate_faxes(Account, Options).

%% ====================================================================
%% Internal functions
%% ====================================================================

migrate_faxes_fold(AccountDb, Current, Total) ->
migrate_faxes_fold(AccountDb, Current, Total, Options) ->
io:format("migrating faxes in database (~p/~p) '~s'~n", [Current, Total, AccountDb]),
_ = migrate_faxes(AccountDb),
_ = migrate_faxes(AccountDb, Options),
Current + 1.

-spec migrate_faxes(atom() | string() | binary()) -> 'ok'.
migrate_faxes(Account) when not is_binary(Account) ->
migrate_faxes(wh_util:to_binary(Account));
migrate_faxes(Account) ->
-spec migrate_faxes(atom() | string() | binary(), wh_proplist()) -> 'ok'.
migrate_faxes(Account, Options) when not is_binary(Account) ->
migrate_faxes(wh_util:to_binary(Account), Options);
migrate_faxes(Account, Options) ->
migrate_private_media(Account),
recover_private_media(Account),
migrate_faxes_to_modb(Account).
migrate_faxes_to_modb(Account, Options).

-spec migrate_private_media(ne_binary()) -> 'ok'.
-spec migrate_private_media(ne_binary(), wh_json:object(), ne_binary()) -> 'ok'.
Expand Down Expand Up @@ -117,11 +137,11 @@ recover_private_media(AccountDb, Doc, _MediaType) ->
_ = couch_mgr:save_doc(AccountDb, wh_json:set_value(<<"pvt_type">>, <<"private_media">>, Doc)),
'ok'.

-spec migrate_faxes_to_modb(ne_binary()) -> 'ok'.
-spec maybe_migrate_fax_to_modb(ne_binary(), wh_json:object()) -> 'ok'.
-spec migrate_fax_to_modb(ne_binary(), ne_binary(), wh_json:object()) -> 'ok'.
-spec migrate_faxes_to_modb(ne_binary(), wh_proplist()) -> 'ok'.
-spec maybe_migrate_fax_to_modb(ne_binary(), wh_json:object(), wh_proplist()) -> 'ok'.
-spec migrate_fax_to_modb(ne_binary(), ne_binary(), wh_json:object(), wh_proplist()) -> 'ok'.

migrate_faxes_to_modb(Account) ->
migrate_faxes_to_modb(Account, Options) ->
AccountDb = case couch_mgr:db_exists(Account) of
'true' -> Account;
'false' -> wh_util:format_account_id(Account, 'encoded')
Expand All @@ -130,13 +150,13 @@ migrate_faxes_to_modb(Account) ->
case couch_mgr:get_results(AccountDb, <<"maintenance/listing_by_type">>, ViewOptions) of
{'ok', []} -> io:format("no fax docs in db for fax migration ~s~n", [AccountDb]);
{'ok', JObjs3}->
_ = [maybe_migrate_fax_to_modb(AccountDb, JObj) || JObj <- JObjs3],
_ = [maybe_migrate_fax_to_modb(AccountDb, JObj, Options) || JObj <- JObjs3],
'ok';
{'error', _}=E3 ->
io:format("unable to fetch fax docs in db ~s: ~p~n", [AccountDb, E3])
end.

maybe_migrate_fax_to_modb(AccountDb, JObj) ->
maybe_migrate_fax_to_modb(AccountDb, JObj, Options) ->
DocId = wh_json:get_value(<<"id">>, JObj),
case couch_mgr:open_doc(AccountDb, DocId) of
{'ok', Doc} ->
Expand All @@ -149,13 +169,13 @@ maybe_migrate_fax_to_modb(AccountDb, JObj) ->
'false' -> 'ok'
end;
_Attachments ->
migrate_fax_to_modb(AccountDb, DocId, Doc)
migrate_fax_to_modb(AccountDb, DocId, Doc, Options)
end;
{'error', E} ->
io:format("unable to get document ~s for fax migration : ~p",[DocId, E])
end.

migrate_fax_to_modb(AccountDb, DocId, JObj) ->
migrate_fax_to_modb(AccountDb, DocId, JObj, Options) ->
Timestamp = wh_json:get_integer_value(<<"pvt_created">>, JObj, wh_util:current_tstamp()),
{{Year, Month, _}, _} = calendar:gregorian_seconds_to_datetime(Timestamp),
AccountMODb = kazoo_modb:get_modb(AccountDb, Year, Month),
Expand All @@ -167,7 +187,7 @@ migrate_fax_to_modb(AccountDb, DocId, JObj) ->
>>,
io:format("moving doc ~s/~s to ~s/~s~n",[AccountDb, DocId, AccountMODb, FaxId]),
kazoo_modb:create(AccountMODb),
case couch_mgr:move_doc(AccountDb, DocId, FaxMODb, FaxId, []) of
case couch_mgr:move_doc(AccountDb, DocId, FaxMODb, FaxId, Options) of
{'ok', _JObj} -> io:format("document ~s moved to ~s~n",[DocId, FaxId]);
{'error', Error} -> io:format("error ~p moving document ~s to ~s~n",[Error, DocId, FaxId])
end.
Expand Down
15 changes: 13 additions & 2 deletions core/whistle-1.0.0/src/props.erl
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ delete_keys([_|_]=Ks, Props) -> lists:foldl(fun ?MODULE:delete/2, Props, Ks).
is_defined(Key, Props) ->
case lists:keyfind(Key, 1, Props) of
{Key,_} -> 'true';
_ -> 'false'
'false' -> lists:member(Key, Props)
end.

-spec unique(wh_proplist()) -> wh_proplist().
Expand Down Expand Up @@ -386,4 +386,15 @@ insert_values_test() ->
?assertEqual(3, get_value(c, P1)),
?assertEqual('true', get_value(d, P1)).

-endif.
is_defined_test() ->
Tests = [{[], 'foo', 'false'}
,{['foo'], 'foo', 'true'}
,{['foo'], 'bar', 'false'}
,{[{'foo', 'bar'}], 'foo', 'true'}
,{[{'foo', 'bar'}], 'bar', 'false'}
],
lists:foreach(fun({Props, Key, Expected}) ->
?assertEqual(Expected, is_defined(Key, Props))
end, Tests).

-endif.
23 changes: 21 additions & 2 deletions core/whistle_couch-1.0.0/src/couch_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@
,db_classifications/0
]).

-define(DELETE_KEYS, [<<"_rev">>, <<"id">>, <<"_attachments">>]).

-type copy_function() :: fun((server(), ne_binary(), wh_json:object(), wh_proplist()) ->
{'ok', wh_json:object()} | couchbeam_error()).
-export_type([copy_function/0]).
-define(COPY_DOC_OVERRIDE_PROPERTY, 'override_existing_document').

%%------------------------------------------------------------------------------
%% @public
%% @doc
Expand Down Expand Up @@ -923,7 +930,11 @@ doc_acct_id(Db, Doc) ->
AccountId -> AccountId
end.

-define(DELETE_KEYS, [<<"_rev">>, <<"id">>, <<"_attachments">>]).


-spec default_copy_function(boolean()) -> copy_function().
default_copy_function('true') -> fun ensure_saved/4;
default_copy_function('false') -> fun save_doc/4.

-spec copy_doc(server(), copy_doc(), wh_proplist()) ->
{'ok', wh_json:object()} |
Expand All @@ -937,6 +948,14 @@ copy_doc(#server{}=Conn, #wh_copy_doc{source_dbname = SourceDb
copy_doc(#server{}=Conn, #wh_copy_doc{dest_doc_id='undefined'}=CopySpec, Options) ->
copy_doc(Conn, CopySpec#wh_copy_doc{dest_doc_id=wh_util:rand_hex_binary(16)}, Options);
copy_doc(#server{}=Conn, CopySpec, Options) ->
SaveFun = default_copy_function(props:is_defined(?COPY_DOC_OVERRIDE_PROPERTY, Options)),
copy_doc(Conn, CopySpec, SaveFun, props:delete(?COPY_DOC_OVERRIDE_PROPERTY, Options)).


-spec copy_doc(server(), copy_doc(), copy_function(), wh_proplist()) ->
{'ok', wh_json:object()} |
couchbeam_error().
copy_doc(#server{}=Conn, CopySpec, CopyFun, Options) ->
#wh_copy_doc{source_dbname = SourceDbName
,source_doc_id = SourceDocId
,dest_dbname = DestDbName
Expand All @@ -946,7 +965,7 @@ copy_doc(#server{}=Conn, CopySpec, Options) ->
{'ok', SourceDoc} ->
Props = [{<<"_id">>, DestDocId}],
DestinationDoc = wh_json:set_values(Props,wh_json:delete_keys(?DELETE_KEYS, SourceDoc)),
case save_doc(Conn, DestDbName, DestinationDoc, Options) of
case CopyFun(Conn, DestDbName, DestinationDoc, Options) of
{'ok', _JObj} ->
Attachments = wh_json:get_value(<<"_attachments">>, SourceDoc, wh_json:new()),
copy_attachments(Conn, CopySpec, wh_json:get_values(Attachments));
Expand Down

0 comments on commit 7dc5886

Please sign in to comment.