Skip to content

Commit

Permalink
qf-import import database (#2858)
Browse files Browse the repository at this point in the history
* qf-import import database

not suitable for large databases

* qf-import make fmt

* qf-import more fmt
  • Loading branch information
lazedo authored and k-anderson committed Dec 2, 2016
1 parent 6523afe commit 23b7263
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
5 changes: 5 additions & 0 deletions core/kazoo_couch/src/kazoo_couch.erl
Expand Up @@ -23,6 +23,7 @@
,db_info/1, db_info/2
,db_exists/2
,db_archive/3
,db_import/3
,db_list/2
]).

Expand Down Expand Up @@ -110,6 +111,10 @@ db_exists(Server, DbName) ->
db_archive(Server, DbName, Filename) ->
kz_couch_db:db_archive(Server, DbName, Filename).

-spec db_import(kz_data:connection(), ne_binary(), ne_binary()) -> any().
db_import(Server, DbName, Filename) ->
kz_couch_db:db_import(Server, DbName, Filename).

-spec db_list(kz_data:connection(), kz_data:options()) -> any().
db_list(Server, Options) ->
db_list(version(Server), Server, Options).
Expand Down
21 changes: 21 additions & 0 deletions core/kazoo_couch/src/kz_couch_db.erl
Expand Up @@ -20,6 +20,7 @@
,db_info/2
,db_exists/2
,db_archive/3
,db_import/3
,db_list/2
]).

Expand Down Expand Up @@ -163,6 +164,26 @@ archive_docs(File, [Doc|Docs]) ->
'ok' = file:write(File, [kz_json:encode(Doc), $,, $\n]),
archive_docs(File, Docs).

-spec db_import(server(), ne_binary(), ne_binary()) ->
'ok' |
couchbeam_error().
db_import(#server{}=Conn, DbName, Filename) ->
case file:read_file(Filename) of
{'ok', Text} -> do_db_import(Conn, DbName, kz_json:decode(Text));
Error -> Error
end.

-spec do_db_import(server(), ne_binary(), kz_json:objects()) ->
'ok' |
couchbeam_error().
do_db_import(#server{}=Conn, DbName, Docs) ->
JObjs0 = [kz_json:delete_keys([<<"_rev">>, <<"_attachments">>], kz_json:get_value(<<"doc">>, Doc)) || Doc <- Docs],
JObjs = [JObj || JObj <- JObjs0, filter_views(kz_json:get_value(<<"_id">>, JObj))],
kz_couch_doc:save_docs(Conn, DbName, JObjs, []).

filter_views(<<"_design", _/binary>>) -> 'false';
filter_views(_Id) -> true.

%% Internal DB-related functions -----------------------------------------------

-spec do_db_compact(db()) -> boolean().
Expand Down
4 changes: 2 additions & 2 deletions core/kazoo_data/src/kz_datamgr.erl
Expand Up @@ -522,10 +522,10 @@ db_archive(DbName, Filename) ->

-spec db_import(ne_binary(), file:filename_all()) -> 'ok' | data_error().
db_import(DbName, ArchiveFile) when ?VALID_DBNAME ->
kzs_db:db_archive(kzs_plan:plan(DbName), DbName, ArchiveFile);
kzs_db:db_import(kzs_plan:plan(DbName), DbName, ArchiveFile);
db_import(DbName, ArchiveFile) ->
case maybe_convert_dbname(DbName) of
{'ok', Db} -> db_archive(Db, ArchiveFile);
{'ok', Db} -> db_import(Db, ArchiveFile);
{'error', _}=E -> E
end.

Expand Down

0 comments on commit 23b7263

Please sign in to comment.