/
kapps_maintenance.erl
1367 lines (1235 loc) · 54.5 KB
/
kapps_maintenance.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%%%-------------------------------------------------------------------
%%% @copyright (C) 2012-2017, 2600Hz INC
%%% @doc
%%%
%%% @end
%%% @contributors
%%% Karl Anderson
%%% James Aimonetti
%%%-------------------------------------------------------------------
-module(kapps_maintenance).
-export([rebuild_token_auth/0
,rebuild_token_auth/1
]).
-export([migrate/0
,migrate/1
,migrate_to_4_0/0
]).
-export([parallel_migrate/1
,parallel_migrate/2
]).
-export([find_invalid_acccount_dbs/0]).
-export([refresh/0, refresh/1
,refresh_account_db/1
,refresh_ratedeck_db/1
]).
-export([blocking_refresh/0
,blocking_refresh/1
]).
-export([remove_depreciated_databases/0]).
-export([ensure_aggregate_devices/0
,ensure_aggregate_device/1
]).
-export([cleanup_aggregated_devices/0
,cleanup_aggregated_device/1
]).
-export([cleanup_aggregated_accounts/0
,cleanup_aggregated_account/1
,remove_aggregated_account/1
]).
-export([migrate_limits/0, migrate_limits/1]).
-export([migrate_media/0, migrate_media/1]).
-export([purge_doc_type/2, purge_doc_type/3]).
-export([call_id_status/1, call_id_status/2]).
-export([get_all_account_views/0]).
-export([cleanup_voicemail_media/1]).
-export([cleanup_orphan_modbs/0]).
-export([delete_system_media_references/0]).
-export([migrate_system/0]).
-export([validate_system_config/1, cleanup_system_config/1, validate_system_configs/0, cleanup_system_configs/0]).
-export([bind/3, unbind/3]).
-export([flush_account_views/0]).
-export([flush_getby_cache/0]).
-include_lib("kazoo_number_manager/include/knm_phone_number.hrl").
-include_lib("kazoo_caches/include/kazoo_caches.hrl").
-include("kazoo_apps.hrl").
binding('migrate') -> <<"maintenance.migrate">>;
binding('refresh') -> <<"maintenance.refresh">>;
binding('refresh_account') -> <<"maintenance.refresh.account">>;
binding({Common, Specific}) when is_atom(Common), is_binary(Specific) ->
CommonPath = binding(Common),
<<CommonPath/binary, ".", Specific/binary>>.
-spec bind(atom() | {atom(), binary()}, module(), atom()) -> any().
-spec unbind(atom() | {atom(), binary()}, module(), atom()) -> any().
bind(Event, M, F) -> kazoo_bindings:bind(binding(Event), M, F).
unbind(Event, M, F) -> kazoo_bindings:unbind(binding(Event), M, F).
-define(DEVICES_CB_LIST, <<"devices/crossbar_listing">>).
-define(MAINTENANCE_VIEW_FILE, <<"views/maintenance.json">>).
-define(RESELLER_VIEW_FILE, <<"views/reseller.json">>).
-define(FAXES_VIEW_FILE, <<"views/faxes.json">>).
-define(FAXBOX_VIEW_FILE, <<"views/faxbox.json">>).
-define(ACCOUNTS_AGG_VIEW_FILE, <<"views/accounts.json">>).
-define(ACCOUNTS_AGG_NOTIFY_VIEW_FILE, <<"views/notify.json">>).
-define(SEARCH_VIEW_FILE, <<"views/search.json">>).
-define(VMBOX_VIEW, <<"vmboxes/crossbar_listing">>).
-define(PMEDIA_VIEW, <<"media/listing_private_media">>).
-define(VIEW_NUMBERS_ACCOUNT, <<"_design/numbers">>).
%%--------------------------------------------------------------------
%% @public
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec rebuild_token_auth() -> 'ok'.
rebuild_token_auth() ->
rebuild_token_auth(5 * ?MILLISECONDS_IN_SECOND).
-spec rebuild_token_auth(text() | integer()) -> 'ok'.
rebuild_token_auth(Pause) ->
_ = kz_datamgr:db_delete(?KZ_TOKEN_DB),
timer:sleep(kz_term:to_integer(Pause)),
refresh(?KZ_TOKEN_DB),
'ok'.
%%--------------------------------------------------------------------
%% @public
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec migrate_to_4_0() -> no_return.
migrate_to_4_0() ->
%% Number migration
kazoo_number_manager_maintenance:migrate(),
%% Voicemail migration
kazoo_voicemail_maintenance:migrate(),
no_return.
%%--------------------------------------------------------------------
%% @public
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec migrate() -> 'no_return'.
migrate() ->
migrate(2 * ?MILLISECONDS_IN_SECOND).
-spec migrate(text() | integer()) -> 'no_return'.
migrate(Pause) ->
_ = migrate_system(),
_ = kapps_config:migrate(),
Databases = get_databases(),
_ = migrate(Pause, Databases),
%% Migrate settings for kazoo_media
io:format("running media migrations...~n"),
_ = kazoo_media_maintenance:migrate(),
'no_return'.
-spec migrate(text() | integer(), ne_binaries()) -> 'no_return'.
migrate(Pause, Databases) ->
Accounts = [kz_util:format_account_id(Db, 'encoded')
|| Db <- Databases,
kapps_util:is_account_db(Db)
],
io:format("updating dbs...~n"),
_ = refresh(Databases, Pause),
%% Remove depreciated dbs
io:format("removing depreciated databases...~n"),
_ = remove_depreciated_databases(Databases),
kazoo_bindings:map(binding('migrate'), [Accounts]),
'no_return'.
-spec parallel_migrate(text() | integer()) -> 'no_return'.
parallel_migrate(Workers) ->
parallel_migrate(Workers, 2 * ?MILLISECONDS_IN_SECOND).
-spec parallel_migrate(text() | integer(), text() | integer()) -> 'no_return'.
parallel_migrate(Workers, Pause) ->
_ = migrate_system(),
_ = kapps_config:migrate(),
{Accounts, Others} = lists:partition(fun kapps_util:is_account_db/1, get_databases()),
AccountDbs = [kz_util:format_account_db(Db) || Db <- Accounts],
OtherSplit = kz_term:to_integer(length(Others) / kz_term:to_integer(Workers)),
AccountSplit = kz_term:to_integer(length(AccountDbs) / kz_term:to_integer(Workers)),
SplitDbs = split(AccountSplit, AccountDbs, OtherSplit, Others, []),
parallel_migrate(Pause, SplitDbs, []).
-type split_results() :: [{ne_binaries(), ne_binaries()}].
-spec split(integer(), ne_binaries(), integer(), ne_binaries(), split_results()) -> split_results().
split(_, [], _, [], Results) -> Results;
split(AccountSplit, Accounts, OtherSplit, Others, Results) ->
{OtherDbs, RemainingOthers} = split(OtherSplit, Others),
{AccountDbs, RemainingAccounts} = split(AccountSplit, Accounts),
NewResults = [{AccountDbs, OtherDbs}|Results],
split(AccountSplit, RemainingAccounts, OtherSplit, RemainingOthers, NewResults).
-spec split(integer(), [any()]) -> {[any()],[any()]}.
split(Count, List) ->
case length(List) >= Count of
'false' -> {List, []};
'true' -> lists:split(Count, List)
end.
-spec parallel_migrate(integer(), split_results(), references()) -> 'no_return'.
parallel_migrate(_, [], Refs) -> wait_for_parallel_migrate(Refs);
parallel_migrate(Pause, [{Accounts, Others}|Remaining], Refs) ->
Self = self(),
Dbs = lists:sort(fun get_database_sort/2, lists:usort(Accounts ++ Others)),
Ref = make_ref(),
_Pid = kz_util:spawn_link(fun parallel_migrate_worker/4, [Ref, Pause, Dbs, Self]),
parallel_migrate(Pause, Remaining, [Ref|Refs]).
-spec parallel_migrate_worker(reference(), integer(), ne_binaries(), pid()) -> reference().
parallel_migrate_worker(Ref, Pause, Databases, Parent) ->
_ = (catch migrate(Pause, Databases)),
Parent ! Ref.
-spec wait_for_parallel_migrate(references()) -> 'no_return'.
wait_for_parallel_migrate([]) ->
%% Migrate settings for kazoo_media
io:format("running media migrations...~n"),
_ = kazoo_media_maintenance:migrate(),
'no_return';
wait_for_parallel_migrate([Ref|Refs]) ->
receive
Ref -> wait_for_parallel_migrate(Refs)
end.
%%--------------------------------------------------------------------
%% @private
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec blocking_refresh() -> 'no_return'.
blocking_refresh() -> refresh().
-spec blocking_refresh(text() | non_neg_integer()) -> 'no_return'.
blocking_refresh(Pause) ->
Databases = get_databases(),
refresh(Databases, Pause).
%%--------------------------------------------------------------------
%% @public
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec refresh() -> 'no_return'.
-spec refresh(ne_binary() | nonempty_string()) -> 'ok' | 'remove'.
-spec refresh(ne_binaries(), text() | non_neg_integer()) -> 'no_return'.
-spec refresh(ne_binaries(), non_neg_integer(), non_neg_integer()) -> 'no_return'.
refresh() ->
Databases = get_databases(),
_ = flush_account_views(),
refresh(Databases, 2 * ?MILLISECONDS_IN_SECOND).
refresh(Databases, Pause) ->
Total = length(Databases),
refresh(Databases, kz_term:to_integer(Pause), Total).
refresh([], _, _) -> 'no_return';
refresh([Database|Databases], Pause, Total) ->
io:format("~p (~p/~p) refreshing database '~s'~n"
,[self(), length(Databases) + 1, Total, Database]),
_ = refresh(Database),
_ = case Pause < 1 of
'false' -> timer:sleep(Pause);
'true' -> 'ok'
end,
refresh(Databases, Pause, Total).
-spec get_databases() -> ne_binaries().
get_databases() ->
{'ok', Databases} = kz_datamgr:db_info(),
lists:sort(fun get_database_sort/2, lists:usort(Databases ++ ?KZ_SYSTEM_DBS)).
-spec get_database_sort(ne_binary(), ne_binary()) -> boolean().
get_database_sort(Db1, Db2) ->
kzs_util:db_priority(Db1) < kzs_util:db_priority(Db2).
refresh(?KZ_CONFIG_DB) ->
kz_datamgr:db_create(?KZ_CONFIG_DB),
kz_datamgr:revise_doc_from_file(?KZ_CONFIG_DB, 'teletype', <<"views/notifications.json">>),
kz_datamgr:revise_doc_from_file(?KZ_CONFIG_DB, 'crossbar', <<"views/system_configs.json">>),
cleanup_invalid_notify_docs(),
delete_system_media_references(),
accounts_config_deprecate_timezone_for_default_timezone();
refresh(?KZ_DATA_DB) ->
kz_datamgr:revise_docs_from_folder(?KZ_DATA_DB, 'kazoo_data', <<"views">>);
refresh(?KZ_OAUTH_DB) ->
kz_datamgr:db_create(?KZ_OAUTH_DB),
kazoo_oauth_maintenance:register_common_providers();
refresh(?KZ_AUTH_DB) ->
kz_datamgr:db_create(?KZ_AUTH_DB),
kazoo_auth_maintenance:register_common_providers(),
kazoo_auth_maintenance:refresh();
refresh(?KZ_WEBHOOKS_DB=Part) ->
kazoo_bindings:map(binding({'refresh', Part}), []);
refresh(?KZ_OFFNET_DB=Part) ->
kazoo_bindings:map(binding({'refresh', Part}), []);
refresh(?KZ_SERVICES_DB) ->
kz_datamgr:db_create(?KZ_SERVICES_DB),
kazoo_services_maintenance:refresh();
refresh(?KZ_SIP_DB) ->
kz_datamgr:db_create(?KZ_SIP_DB),
Views = [kapps_util:get_view_json('kazoo_apps', ?MAINTENANCE_VIEW_FILE)
,kapps_util:get_view_json('registrar', <<"credentials.json">>)
,kapps_util:get_view_json('crossbar', <<"views/resources.json">>)
],
kapps_util:update_views(?KZ_SIP_DB, Views, 'true');
refresh(?KZ_SCHEMA_DB) ->
kz_datamgr:db_create(?KZ_SCHEMA_DB),
lager:notice("starting system schemas update"),
kz_datamgr:suppress_change_notice(),
kz_datamgr:revise_docs_from_folder(?KZ_SCHEMA_DB, 'crossbar', "schemas"),
kz_datamgr:enable_change_notice(),
lager:notice("finished system schemas update"),
'ok';
refresh(?KZ_MEDIA_DB) ->
kz_datamgr:db_create(?KZ_MEDIA_DB),
kazoo_media_maintenance:refresh(),
'ok';
refresh(?KZ_RATES_DB) ->
refresh_ratedeck_db(?KZ_RATES_DB);
refresh(?KZ_ANONYMOUS_CDR_DB) ->
kz_datamgr:db_create(?KZ_ANONYMOUS_CDR_DB),
_ = kz_datamgr:revise_doc_from_file(?KZ_ANONYMOUS_CDR_DB, 'cdr', <<"cdr.json">>),
'ok';
refresh(?KZ_DEDICATED_IP_DB) ->
kz_datamgr:db_create(?KZ_DEDICATED_IP_DB),
kz_ip_utils:refresh_database();
refresh(?KZ_ACCOUNTS_DB) ->
kz_datamgr:db_create(?KZ_ACCOUNTS_DB),
Views = [kapps_util:get_view_json('kazoo_apps', ?MAINTENANCE_VIEW_FILE)
,kapps_util:get_view_json('kazoo_apps', ?ACCOUNTS_AGG_VIEW_FILE)
,kapps_util:get_view_json('kazoo_apps', ?SEARCH_VIEW_FILE)
],
kapps_util:update_views(?KZ_ACCOUNTS_DB, Views, 'true'),
'ok';
refresh(?KZ_FAXES_DB) ->
kz_datamgr:db_create(?KZ_FAXES_DB),
_ = kz_datamgr:revise_doc_from_file(?KZ_FAXES_DB, 'fax', ?FAXES_VIEW_FILE),
_ = kz_datamgr:revise_doc_from_file(?KZ_FAXES_DB, 'fax', ?FAXBOX_VIEW_FILE),
'ok';
refresh(?KZ_PORT_REQUESTS_DB) ->
kz_datamgr:db_create(?KZ_PORT_REQUESTS_DB),
_ = kz_datamgr:revise_doc_from_file(?KZ_PORT_REQUESTS_DB, 'crossbar', <<"views/port_requests.json">>),
_ = kz_util:spawn(fun knm_port_request:migrate/0),
'ok';
refresh(?KZ_ACDC_DB) ->
kz_datamgr:db_create(?KZ_ACDC_DB),
_ = kz_datamgr:revise_doc_from_file(?KZ_ACDC_DB, 'crossbar', <<"views/acdc.json">>),
'ok';
refresh(?KZ_CCCPS_DB) ->
kz_datamgr:db_create(?KZ_CCCPS_DB),
_ = kz_datamgr:revise_doc_from_file(?KZ_CCCPS_DB, 'crossbar', <<"views/cccps.json">>),
'ok';
refresh(?KZ_TOKEN_DB) ->
_ = kz_datamgr:db_create(?KZ_TOKEN_DB),
kz_datamgr:revise_doc_from_file(?KZ_TOKEN_DB, 'crossbar', "views/token_auth.json"),
'ok';
refresh(?KZ_ALERTS_DB) ->
_ = kz_datamgr:db_create(?KZ_ALERTS_DB),
kz_datamgr:revise_doc_from_file(?KZ_ALERTS_DB, 'crossbar', "views/alerts.json"),
'ok';
refresh(?KZ_TASKS_DB) ->
_ = kz_datamgr:db_create(?KZ_TASKS_DB),
_ = kz_datamgr:revise_views_from_folder(?KZ_TASKS_DB, 'tasks'),
'ok';
refresh(?KZ_PENDING_NOTIFY_DB) ->
_ = kz_datamgr:db_create(?KZ_PENDING_NOTIFY_DB),
kz_datamgr:revise_doc_from_file(?KZ_PENDING_NOTIFY_DB, 'crossbar', "views/pending_notify.json"),
'ok';
refresh(Database) when is_binary(Database) ->
case kz_datamgr:db_classification(Database) of
'account' -> refresh_account_db(Database);
'modb' -> kazoo_modb:refresh_views(Database);
'numbers' -> kazoo_number_manager_maintenance:refresh_numbers_db(Database);
'system' ->
kz_datamgr:db_create(Database),
'ok';
'ratedeck' -> refresh_ratedeck_db(Database);
_Else -> 'ok'
end.
%%--------------------------------------------------------------------
%% @private
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec cleanup_invalid_notify_docs() -> 'ok'.
cleanup_invalid_notify_docs() ->
_ = kz_datamgr:db_archive(<<"system_config">>),
case kz_datamgr:all_docs(?KZ_CONFIG_DB, ['include_docs']) of
{'ok', JObjs} -> cleanup_invalid_notify_docs(JObjs);
{'error', _R} ->
lager:warning("unable to fetch all system config docs: ~p", [_R])
end.
-spec cleanup_invalid_notify_docs(kz_json:objects()) -> 'ok'.
cleanup_invalid_notify_docs([]) -> 'ok';
cleanup_invalid_notify_docs([JObj|JObjs]) ->
Id = kz_json:get_value(<<"id">>, JObj),
Doc = kz_json:get_value(<<"doc">>, JObj),
Type = kz_json:get_value(<<"pvt_type">>, Doc),
_ = maybe_remove_invalid_notify_doc(Type, Id, Doc),
cleanup_invalid_notify_docs(JObjs).
-spec maybe_remove_invalid_notify_doc(ne_binary(), ne_binary(), kz_json:object()) -> 'ok'.
maybe_remove_invalid_notify_doc(<<"notification">>, <<"notification", _/binary>>, _) -> 'ok';
maybe_remove_invalid_notify_doc(<<"notification">>, _, JObj) ->
_ = kz_datamgr:del_doc(?KZ_CONFIG_DB, JObj),
'ok';
maybe_remove_invalid_notify_doc(_Type, _Id, _Doc) -> 'ok'.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Remove system_config/accounts timezone key and use only
%% default_timezone
%% @end
%%--------------------------------------------------------------------
-spec accounts_config_deprecate_timezone_for_default_timezone() -> 'ok'.
-spec accounts_config_deprecate_timezone_for_default_timezone(kz_json:object()) -> 'ok'.
accounts_config_deprecate_timezone_for_default_timezone() ->
case kz_datamgr:open_cache_doc(?KZ_CONFIG_DB, <<"accounts">>) of
{'ok', AccountsConfig} ->
accounts_config_deprecate_timezone_for_default_timezone(AccountsConfig);
{'error', E} ->
lager:warning("unable to fetch system_config/accounts: ~p", [E])
end.
accounts_config_deprecate_timezone_for_default_timezone(AccountsConfig) ->
PublicFields = kz_doc:public_fields(AccountsConfig),
case kz_json:get_keys(PublicFields) of
[] -> 'ok';
Keys ->
MigratedConfig = deprecate_timezone_for_default_timezone(Keys, AccountsConfig),
kz_datamgr:save_doc(?KZ_CONFIG_DB, MigratedConfig),
'ok'
end.
-spec deprecate_timezone_for_default_timezone(kz_json:keys(), kz_json:object()) ->
kz_json:object().
deprecate_timezone_for_default_timezone(Nodes, AccountsConfig) ->
lists:foldl(fun deprecate_timezone_for_node/2, AccountsConfig, Nodes).
-spec deprecate_timezone_for_node(kz_json:key(), kz_json:object()) ->
kz_json:object().
-spec deprecate_timezone_for_node(kz_json:key(), kz_json:object(), api_ne_binary(), api_ne_binary()) ->
kz_json:object().
deprecate_timezone_for_node(Node, AccountsConfig) ->
Timezone = kz_json:get_value([Node, <<"timezone">>], AccountsConfig),
DefaultTimezone = kz_json:get_value([Node, <<"default_timezone">>], AccountsConfig),
deprecate_timezone_for_node(Node, AccountsConfig, Timezone, DefaultTimezone).
deprecate_timezone_for_node(_Node, AccountsConfig, 'undefined', _Default) ->
AccountsConfig;
deprecate_timezone_for_node(Node, AccountsConfig, Timezone, 'undefined') ->
io:format("setting default timezone to ~s for node ~s~n", [Timezone, Node]),
kz_json:set_value([Node, <<"default_timezone">>]
,Timezone
,kz_json:delete_key([Node, <<"timezone">>], AccountsConfig)
);
deprecate_timezone_for_node(Node, AccountsConfig, _Timezone, _Default) ->
kz_json:delete_key([Node, <<"timezone">>], AccountsConfig).
-spec refresh_ratedeck_db(ne_binary()) -> 'ok'.
refresh_ratedeck_db(RateDb) ->
kz_datamgr:db_create(RateDb),
kz_datamgr:revise_docs_from_folder(RateDb, 'hotornot', "views"),
_ = kz_datamgr:revise_doc_from_file(RateDb, 'crossbar', <<"views/rates.json">>),
kz_datamgr:load_fixtures_from_folder(RateDb, 'hotornot'),
'ok'.
%%--------------------------------------------------------------------
%% @public
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec refresh_account_db(ne_binary()) -> 'ok'.
refresh_account_db(Database) ->
AccountDb = kz_util:format_account_id(Database, 'encoded'),
AccountId = kz_util:format_account_id(Database, 'raw'),
_ = remove_depreciated_account_views(AccountDb),
_ = ensure_account_definition(AccountDb, AccountId),
%% ?VIEW_NUMBERS_ACCOUNT gets updated/created in KNM maintenance
AccountViews =
case kz_datamgr:open_doc(AccountDb, ?VIEW_NUMBERS_ACCOUNT) of
{error,_} ->
lists:keydelete(?VIEW_NUMBERS_ACCOUNT, 1, get_all_account_views());
{ok, ViewJObj} ->
ViewListing = {?VIEW_NUMBERS_ACCOUNT, ViewJObj},
lists:keyreplace(?VIEW_NUMBERS_ACCOUNT, 1, get_all_account_views(), ViewListing)
end,
_ = kapps_util:update_views(AccountDb, AccountViews, 'true'),
_ = kazoo_number_manager_maintenance:update_number_services_view(AccountDb),
kapps_account_config:migrate(AccountDb),
_ = kazoo_bindings:map(binding({'refresh_account', AccountDb}), AccountId),
'ok'.
-spec remove_depreciated_account_views(ne_binary()) -> 'ok'.
remove_depreciated_account_views(AccountDb) ->
_ = kz_datamgr:del_doc(AccountDb, <<"_design/limits">>),
_ = kz_datamgr:del_doc(AccountDb, <<"_design/sub_account_reps">>),
'ok'.
-spec ensure_account_definition(ne_binary(), ne_binary()) -> 'ok'.
ensure_account_definition(AccountDb, AccountId) ->
case kz_datamgr:open_doc(AccountDb, AccountId) of
{'error', 'not_found'} -> get_definition_from_accounts(AccountDb, AccountId);
{'ok', _} -> 'ok'
end.
-spec get_definition_from_accounts(ne_binary(), ne_binary()) -> 'ok'.
get_definition_from_accounts(AccountDb, AccountId) ->
case kz_datamgr:open_doc(?KZ_ACCOUNTS_DB, AccountId) of
{'ok', JObj} -> kz_datamgr:ensure_saved(AccountDb, kz_doc:delete_revision(JObj));
{'error', 'not_found'} ->
io:format(" account ~s is missing its local account definition, and not in the accounts db~n"
,[AccountId]),
_ = kz_datamgr:db_archive(AccountDb),
maybe_delete_db(AccountDb)
end.
-spec flush_account_views() -> 'ok'.
flush_account_views() ->
put('account_views', 'undefined').
-spec get_all_account_views() -> kz_datamgr:views_listing().
get_all_account_views() ->
case get('account_views') of
'undefined' ->
Views = fetch_all_account_views(),
put('account_views', Views),
Views;
Views -> Views
end.
-spec fetch_all_account_views() -> kz_datamgr:views_listing().
fetch_all_account_views() ->
[kapps_util:get_view_json('kazoo_apps', ?MAINTENANCE_VIEW_FILE)
,kapps_util:get_view_json('conference', <<"views/conference.json">>)
,kapps_util:get_view_json('webhooks', <<"webhooks.json">>)
|kapps_util:get_views_json('crossbar', "account")
++ kapps_util:get_views_json('callflow', "views")
].
%%--------------------------------------------------------------------
%% @public
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec remove_depreciated_databases() -> 'ok'.
remove_depreciated_databases() ->
Databases = get_databases(),
remove_depreciated_databases(Databases).
-spec remove_depreciated_databases(ne_binaries()) -> 'ok'.
remove_depreciated_databases([]) -> 'ok';
remove_depreciated_databases([Database|Databases]) ->
_ = case kz_datamgr:db_classification(Database) of
'deprecated' ->
io:format(" archive and remove depreciated database ~s~n", [Database]),
_ = kz_datamgr:db_archive(Database),
maybe_delete_db(Database);
_Else -> 'ok'
end,
remove_depreciated_databases(Databases).
%%--------------------------------------------------------------------
%% @public
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec cleanup_aggregated_accounts() -> 'ok'.
cleanup_aggregated_accounts() ->
case kz_datamgr:all_docs(?KZ_ACCOUNTS_DB, []) of
{'ok', JObjs} -> cleanup_aggregated_accounts(JObjs);
_ -> 'ok'
end.
-spec cleanup_aggregated_accounts(kz_json:objects()) -> 'ok'.
cleanup_aggregated_accounts([]) -> 'ok';
cleanup_aggregated_accounts([JObj|JObjs]) ->
_ = case kz_doc:id(JObj) of
<<"_design", _/binary>> -> 'ok';
AccountId ->
io:format(" verifing ~s doc ~s~n", [?KZ_ACCOUNTS_DB, AccountId]),
cleanup_aggregated_account(AccountId)
end,
cleanup_aggregated_accounts(JObjs).
-spec cleanup_aggregated_account(ne_binary()) -> 'ok'.
cleanup_aggregated_account(Account) ->
AccountDb = kz_util:format_account_id(Account, 'encoded'),
AccountId = kz_util:format_account_id(Account, 'raw'),
case kz_datamgr:open_doc(AccountDb, AccountId) of
{'error', 'not_found'} -> remove_aggregated_account(AccountDb);
_Else -> 'ok'
end.
-spec remove_aggregated_account(ne_binary()) -> 'ok'.
remove_aggregated_account(Account) ->
AccountId = kz_util:format_account_id(Account, 'raw'),
{'ok', JObj} = kz_datamgr:open_doc(?KZ_ACCOUNTS_DB, AccountId),
io:format(" removing invalid ~s doc ~s~n", [?KZ_ACCOUNTS_DB, AccountId]),
_ = kz_datamgr:del_doc(?KZ_ACCOUNTS_DB, JObj),
'ok'.
%%--------------------------------------------------------------------
%% @private
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec cleanup_aggregated_devices() -> 'ok'.
cleanup_aggregated_devices() ->
case kz_datamgr:all_docs(?KZ_SIP_DB, []) of
{'ok', JObjs} -> cleanup_aggregated_devices(JObjs);
_ -> 'ok'
end.
-spec cleanup_aggregated_devices(kz_json:objects()) -> 'ok'.
cleanup_aggregated_devices([]) -> 'ok';
cleanup_aggregated_devices([JObj|JObjs]) ->
_ = case kz_doc:id(JObj) of
<<"_design", _/binary>> -> 'ok';
DocId ->
io:format(" verifing ~s doc ~s~n", [?KZ_SIP_DB, DocId]),
cleanup_aggregated_device(DocId)
end,
cleanup_aggregated_devices(JObjs).
-spec cleanup_aggregated_device(ne_binary()) -> 'ok'.
cleanup_aggregated_device(DocId) ->
{'ok', JObj} = kz_datamgr:open_doc(?KZ_SIP_DB, DocId),
case kz_json:get_first_defined([<<"pvt_account_db">>
,<<"pvt_account_id">>
], JObj)
of
'undefined' -> 'ok';
Account ->
AccountDb = kz_util:format_account_id(Account, 'encoded'),
AccountId = kz_util:format_account_id(Account, 'raw'),
verify_aggregated_device(AccountDb, AccountId, JObj)
end.
-spec verify_aggregated_device(ne_binary(), ne_binary(), kz_json:object()) -> 'ok'.
verify_aggregated_device(AccountDb, AccountId, JObj) ->
case kz_datamgr:open_doc(AccountDb, AccountId) of
{'error', 'not_found'} ->
io:format(" removing ~s doc ~s referencing missing db ~s~n"
,[?KZ_SIP_DB, AccountId, AccountDb]),
_ = kz_datamgr:del_doc(?KZ_SIP_DB, JObj),
'ok';
_Else -> 'ok'
end.
%%--------------------------------------------------------------------
%% @public
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec cleanup_voicemail_media(ne_binary()) ->
{'ok', kz_json:objects()} |
{'error', any()}.
cleanup_voicemail_media(Account) ->
AccountDb = kz_util:format_account_id(Account, 'encoded'),
Medias = get_medias(Account),
Messages = get_messages(Account),
ExtraMedia = Medias -- Messages,
case kz_datamgr:del_docs(AccountDb, ExtraMedia) of
{'ok', _}=Res -> Res;
{'error', _E}=Err ->
lager:error("could not delete docs ~p: ~p", [ExtraMedia, _E]),
Err
end.
-spec cleanup_orphan_modbs() -> 'ok'.
cleanup_orphan_modbs() ->
AccountMODbs = kapps_util:get_all_account_mods('encoded'),
AccountIds = kapps_util:get_all_accounts('raw'),
DeleteOrphaned = fun (AccountMODb) ->
kazoo_modb:maybe_delete(AccountMODb, AccountIds)
end,
lists:foreach(DeleteOrphaned, AccountMODbs).
-spec get_messages(ne_binary()) -> ne_binaries().
get_messages(Account) ->
AccountDb = kz_util:format_account_id(Account, 'encoded'),
ViewOptions = ['include_docs'],
case kz_datamgr:get_results(AccountDb, ?VMBOX_VIEW, ViewOptions) of
{'ok', ViewRes} ->
lists:foldl(fun extract_messages/2, [], ViewRes);
{'error', _E} ->
lager:error("coumd not load view ~p: ~p", [?VMBOX_VIEW, _E]),
[]
end.
-spec extract_messages(kz_json:objects() | kz_json:object(), ne_binaries()) -> ne_binaries().
extract_messages([], CurMessages) -> CurMessages;
extract_messages([Mess|Messages], CurMessages) ->
extract_messages(Messages, [kz_json:get_value(<<"media_id">>, Mess)|CurMessages]);
extract_messages(JObj, CurMessages) ->
Messages = kz_json:get_value([<<"doc">>, <<"messages">>], JObj, []),
extract_messages(Messages, CurMessages).
-spec get_medias(ne_binary()) -> ne_binaries().
get_medias(Account) ->
AccountDb = kz_util:format_account_id(Account, 'encoded'),
ViewOptions = [],
case kz_datamgr:get_results(AccountDb, ?PMEDIA_VIEW, ViewOptions) of
{'ok', ViewRes} -> [kz_doc:id(JObj) || JObj<- ViewRes];
{'error', _E} ->
lager:error("could not load view ~p: ~p", [?PMEDIA_VIEW, _E]),
[]
end.
%%--------------------------------------------------------------------
%% @public
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec migrate_limits() -> 'ok'.
-spec migrate_limits(atom() | string() | binary()) -> 'ok'.
migrate_limits() ->
migrate_all_limits(kapps_util:get_all_accounts()).
-spec migrate_all_limits(ne_binaries()) -> 'ok'.
migrate_all_limits(Accounts) ->
Total = length(Accounts),
lists:foldr(fun(A, C) -> migrate_limits_fold(A, C, Total) end, 1, Accounts),
'ok'.
migrate_limits_fold(AccountDb, Current, Total) ->
io:format("checking limits doc in database (~p/~p) '~s'~n", [Current, Total, AccountDb]),
_ = case kz_datamgr:open_doc(AccountDb, <<"limits">>) of
{'error', 'not_found'} -> migrate_limits(AccountDb);
_Else -> 'ok'
end,
Current + 1.
migrate_limits(Account) when not is_binary(Account) ->
migrate_limits(kz_term:to_binary(Account));
migrate_limits(Account) ->
TStamp = kz_time:current_tstamp(),
TwowayTrunks = kapps_config:get_integer(<<"jonny5">>, <<"default_twoway_trunks">>),
InboundTrunks = kapps_config:get_integer(<<"jonny5">>, <<"default_inbound_trunks">>),
AccountDb = case kz_datamgr:db_exists(Account) of
'true' -> Account;
'false' -> kz_util:format_account_id(Account, 'encoded')
end,
{TT, IT} = clean_trunkstore_docs(AccountDb, TwowayTrunks, InboundTrunks),
JObj = kz_json:from_list(
[{<<"_id">>, <<"limits">>}
,{<<"twoway_trunks">>, TT}
,{<<"inbound_trunks">>, IT}
,{<<"pvt_account_db">>, AccountDb}
,{<<"pvt_account_id">>, kz_util:format_account_id(Account, 'raw')}
,{<<"pvt_type">>, <<"limits">>}
,{<<"pvt_created">>, TStamp}
,{<<"pvt_modified">>, TStamp}
,{<<"pvt_vsn">>, 1}
]),
_ = kz_datamgr:save_doc(AccountDb, JObj),
'ok'.
-spec clean_trunkstore_docs(ne_binary(), integer(), integer()) ->
{integer(), integer()}.
clean_trunkstore_docs(AccountDb, TwowayTrunks, InboundTrunks) ->
ViewOptions = ['include_docs'
,{'reduce', 'false'}
],
case kz_datamgr:get_results(AccountDb, <<"trunkstore/crossbar_listing">>, ViewOptions) of
{'ok', JObjs} -> clean_trunkstore_docs(AccountDb, JObjs, TwowayTrunks, InboundTrunks);
{'error', _}=E -> E
end.
-spec clean_trunkstore_docs(ne_binary(), kz_json:objects(), integer(), integer()) ->
{integer(), integer()}.
clean_trunkstore_docs(_, [], Trunks, InboundTrunks) ->
{Trunks, InboundTrunks};
clean_trunkstore_docs(AccountDb, [JObj|JObjs], Trunks, InboundTrunks) ->
Doc = kz_json:get_value(<<"doc">>, JObj),
%% if there are no servers and it was created by jonny5 softdelete the doc
_ = case kz_json:get_ne_value(<<"servers">>, Doc) =:= 'undefined'
andalso kz_json:get_ne_value(<<"pvt_created_by">>, Doc) =:= <<"jonny5">>
of
'true' -> kz_datamgr:save_doc(AccountDb, kz_doc:set_soft_deleted(Doc, 'true'));
'false' -> 'ok'
end,
NewTrunks = case kz_json:get_integer_value([<<"account">>, <<"trunks">>], Doc, 0) of
OldTrunks when OldTrunks > Trunks -> OldTrunks;
_ -> Trunks
end,
NewInboundTrunks =
case kz_json:get_integer_value([<<"account">>, <<"inbound_trunks">>], Doc, 0) of
OldInboundTrunks when OldInboundTrunks > InboundTrunks -> OldInboundTrunks;
_ -> Trunks
end,
clean_trunkstore_docs(AccountDb, JObjs, NewTrunks, NewInboundTrunks).
%%--------------------------------------------------------------------
%% @public
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec migrate_media() -> 'ok'.
-spec migrate_media(atom() | string() | binary()) -> 'ok'.
migrate_media() ->
Accounts = kapps_util:get_all_accounts(),
Total = length(Accounts),
lists:foldr(fun(A, C) -> migrate_media_fold(A, C, Total) end, 1, Accounts),
'ok'.
migrate_media_fold(AccountDb, Current, Total) ->
io:format("migrating media in database (~p/~p) '~s'", [Current, Total, AccountDb]),
_ = migrate_media(AccountDb),
Current + 1.
migrate_media(Account) when not is_binary(Account) ->
migrate_media(kz_term:to_binary(Account));
migrate_media(Account) ->
AccountDb = case kz_datamgr:db_exists(Account) of
'true' -> Account;
'false' -> kz_util:format_account_id(Account, 'encoded')
end,
case kz_datamgr:get_results(AccountDb, <<"media/listing_by_name">>, []) of
{'ok', []} -> io:format("no public media files in db ~s~n", [AccountDb]);
{'ok', [_|_]=JObjs1}->
_ = [migrate_attachment(AccountDb, JObj) || JObj <- JObjs1],
'ok';
{'error', _}=E1 ->
io:format("unable to fetch media files in db ~s: ~p~n", [AccountDb, E1])
end,
case kz_datamgr:get_results(AccountDb, <<"media/listing_private_media">>, []) of
{'ok', []} -> io:format("no private media files in db ~s~n", [AccountDb]);
{'ok', JObjs2}->
_ = [migrate_attachment(AccountDb, JObj) || JObj <- JObjs2],
'ok';
{'error', _}=E2 ->
io:format("unable to fetch private media files in db ~s: ~p~n", [AccountDb, E2])
end.
%%--------------------------------------------------------------------
%% @public
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec ensure_aggregate_devices() -> 'ok'.
ensure_aggregate_devices() ->
ensure_aggregate_devices(kapps_util:get_all_accounts()).
-spec ensure_aggregate_devices(ne_binaries()) -> 'ok'.
ensure_aggregate_devices([]) -> 'ok';
ensure_aggregate_devices([Account|Accounts]) ->
_ = ensure_aggregate_device(Account),
ensure_aggregate_devices(Accounts).
-spec ensure_aggregate_device(ne_binary()) -> 'ok'.
ensure_aggregate_device(Account) ->
AccountDb = kz_util:format_account_id(Account, 'encoded'),
AccountRealm = kz_util:get_account_realm(AccountDb),
case kz_datamgr:get_results(AccountDb, ?DEVICES_CB_LIST, ['include_docs']) of
{'ok', Devices} ->
_ = remove_aggregate_devices(AccountDb, AccountRealm, Devices),
refresh_account_devices(AccountDb, AccountRealm, Devices);
{'error', _} -> 'ok'
end.
-spec refresh_account_devices(ne_binary(), ne_binary(), kz_json:objects()) -> 'ok'.
refresh_account_devices(AccountDb, AccountRealm, Devices) ->
_ = [kapps_util:add_aggregate_device(AccountDb, kz_json:get_value(<<"doc">>, Device))
|| Device <- Devices,
should_aggregate_device(AccountRealm, kz_json:get_value(<<"doc">>, Device))
],
'ok'.
-spec should_aggregate_device(ne_binary(), kz_json:object()) -> boolean().
should_aggregate_device(AccountRealm, Device) ->
kz_device:sip_realm(Device, AccountRealm) =/= AccountRealm
orelse kz_device:sip_ip(Device) =/= 'undefined'.
-spec remove_aggregate_devices(ne_binary(), ne_binary(), kz_json:objects()) -> 'ok'.
remove_aggregate_devices(AccountDb, AccountRealm, Devices) ->
_ = [kapps_util:rm_aggregate_device(AccountDb, kz_json:get_value(<<"doc">>, Device))
|| Device <- Devices,
should_remove_aggregate(AccountRealm, kz_json:get_value(<<"doc">>, Device))
],
'ok'.
-spec should_remove_aggregate(ne_binary(), kz_json:object()) -> boolean().
should_remove_aggregate(AccountRealm, Device) ->
kz_device:sip_realm(Device, AccountRealm) =:= AccountRealm
andalso kz_device:sip_ip(Device) =:= 'undefined'.
%%--------------------------------------------------------------------
%% @public
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec find_invalid_acccount_dbs() -> ne_binaries().
find_invalid_acccount_dbs() ->
lists:foldr(fun find_invalid_acccount_dbs_fold/2, [], kapps_util:get_all_accounts()).
find_invalid_acccount_dbs_fold(AccountDb, Acc) ->
AccountId = kz_util:format_account_id(AccountDb, 'raw'),
case kz_datamgr:open_doc(AccountDb, AccountId) of
{'error', 'not_found'} -> [AccountDb|Acc];
{'ok', _} -> Acc
end.
%%--------------------------------------------------------------------
%% @private
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec maybe_migrate_attachments(ne_binary(), ne_binary(), kz_json:object()) -> any().
maybe_migrate_attachments(AccountDb, Id, JObj) ->
case kz_doc:attachments(JObj) of
'undefined' ->
io:format("media doc ~s/~s has no attachments, removing~n", [AccountDb, Id]),
kz_datamgr:save_doc(AccountDb, kz_doc:set_soft_deleted(JObj, 'true'));
Attachments ->
_ = [catch migrate_attachment(AccountDb, JObj, K, V)
|| {K,V} <- kz_json:to_proplist(Attachments)
]
end.
-spec migrate_attachment(ne_binary(), kz_json:object()) -> 'ok'.
migrate_attachment(AccountDb, ViewJObj) ->
Id = kz_doc:id(ViewJObj),
_ = case kz_datamgr:open_doc(AccountDb, Id) of
{'error', _}=E1 ->
io:format("unable to open media for attachment migration ~s/~s: ~p~n", [AccountDb, Id, E1]);
{'ok', JObj1} ->
maybe_migrate_attachments(AccountDb, Id, JObj1)
end,
%% we must reopen the doc since the _attachments has changed or we will effectively remove all attachments!
case kz_datamgr:open_doc(AccountDb, Id) of
{'error', _}=E2 ->
io:format("unable to open media for depreciated field removal ~s/~s: ~p~n", [AccountDb, Id, E2]);
{'ok', JObj2} ->
remove_deprecated_attachment_properties(AccountDb, Id, JObj2)
end.
remove_deprecated_attachment_properties(AccountDb, Id, JObj) ->
J = kz_json:delete_keys([<<"status">>, <<"content_size">>, <<"size">>
,<<"content_type">>, <<"content_length">>
,<<"format">>, <<"sample">>, <<"media_type">>
], JObj),
Result = case (J =/= JObj)
andalso kz_json:get_value(<<"source_id">>, J)
of
'false' -> 'ignore';
'undefined' ->
kz_datamgr:save_doc(AccountDb, kz_json:set_value(<<"media_source">>, <<"upload">>, J));
_Else ->
kz_datamgr:save_doc(AccountDb, kz_json:set_value(<<"media_source">>, <<"recording">>, J))
end,
case Result of
'ignore' -> 'ok';
{'ok', _} ->
io:format("removed depreciated properties from ~s/~s~n", [AccountDb, Id]);
{'error', _}=E3 ->
io:format("removal of depreciated properties from ~s/~s failed: ~p~n", [AccountDb, Id, E3])
end.
%%--------------------------------------------------------------------
%% @private
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec migrate_attachment(ne_binary(), kz_json:object(), ne_binary(), kz_json:object()) -> 'ok'.
migrate_attachment(AccountDb, JObj, Attachment, MetaData) ->
DocCT = kz_json:get_value(<<"content_type">>, JObj),
MetaCT = kz_json:get_value(<<"content_type">>, MetaData),
Migrations = [fun({A, MCT}) -> maybe_update_attachment_content_type(A, MCT, DocCT) end
,fun maybe_add_extension/1
],
Migrate = lists:foldl(fun(F, Acc) -> F(Acc) end
,{Attachment, MetaCT}
,Migrations
),
maybe_update_attachment(AccountDb, kz_doc:id(JObj), {Attachment, MetaCT}, Migrate).
maybe_update_attachment_content_type(A, MCT, DocCT) ->
case {is_audio_content(DocCT), is_audio_content(MCT)} of
{_, 'true'} -> {A, MCT};
{'true', 'false'} -> {A, DocCT};
{'false', 'false'} -> {A, find_attachment_content_type(A)}
end.
-spec find_attachment_content_type(ne_binary()) -> ne_binary().