Skip to content

Commit

Permalink
[#136] – Ensure all backends respect the log_queries option
Browse files Browse the repository at this point in the history
  • Loading branch information
cabol committed Mar 29, 2017
1 parent 507d33b commit 6850780
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 19 deletions.
2 changes: 1 addition & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,4 @@

%% == Shell ==

{shell, [{apps, [sumo_db]}]}.
{shell, [{apps, [sumo_db]}]}.
12 changes: 11 additions & 1 deletion src/adapters/sumo_store_mnesia.erl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
-spec init(term()) -> {ok, state()}.
init(Options) ->
DefaultOptions = parse(Options),
{ok, #{default_options => DefaultOptions}}.
LogQueries = application:get_env(sumo_db, log_queries, false),
{ok, #{default_options => DefaultOptions, log_queries => LogQueries}}.

-spec persist(Doc, State) -> Response when
Doc :: sumo_internal:doc(),
Expand Down Expand Up @@ -196,6 +197,7 @@ find_by(DocName, Conditions, [], Limit, Offset, State) ->
Schema = sumo_internal:get_schema(DocName),
Fields = schema_field_names(Schema),
Docs = [wakeup(result_to_doc(Result, Fields)) || Result <- Results],
_ = maybe_log_query(DocName, Conditions, Limit, Offset, MatchSpec, State),
{ok, Docs, State}
end;
find_by(_DocName, _Conditions, _Sort, _Limit, _Offset, State) ->
Expand Down Expand Up @@ -381,6 +383,14 @@ result_to_doc(Result, Fields) ->
transform_conditions(DocName, Conditions) ->
sumo_utils:transform_conditions(fun validate_date/1, DocName, Conditions, [date]).

%% @private
maybe_log_query(DocName, Conditions, Limit, Offset, MatchSpec, #{log_queries := true}) ->
Msg = "find_by(~p, ~p, [], ~p, ~p)~nMatchSpec: ~p",
Args = [DocName, Conditions, Limit, Offset, MatchSpec],
lager:debug(Msg, Args);
maybe_log_query(_, _, _, _, _, _) ->
ok.

%% @private
validate_date({FieldType, _, FieldValue}) ->
case {FieldType, sumo_utils:is_datetime(FieldValue)} of
Expand Down
31 changes: 19 additions & 12 deletions src/sumo_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,29 @@

-include_lib("stdlib/include/ms_transform.hrl").

%%%===================================================================
%%%=============================================================================
%%% Types
%%%===================================================================
%%%=============================================================================

-type doc_config() :: {DocName :: atom(), Store :: atom(), Props :: map()}.
-type doc_config() :: {
SchemaName :: sumo:schema_name(),
Store :: atom(),
Props :: map()
}.

-type event_config() :: {DocName :: atom(), EventHandler :: module()}.
-type event_config() :: {
SchemaName :: sumo:schema_name(),
EventHandler :: module()
}.

-export_type([
doc_config/0,
event_config/0
]).

%%%===================================================================
%%%=============================================================================
%%% API
%%%===================================================================
%%%=============================================================================

-spec init() -> ok.
init() ->
Expand Down Expand Up @@ -118,18 +125,18 @@ remove_event_managers(DocName, EventManagers) when is_list(EventManagers) ->
remove_event_managers(DocName, EventManagers) when is_atom(EventManagers) ->
remove_event_managers(DocName, [EventManagers]).

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

%% @private
do_init() ->
?MODULE = ets:new(?MODULE, [named_table, public, {read_concurrency, true}]),
Docs = application:get_env(sumo_db, docs, []),
Events = application:get_env(sumo_db, events, []),
UpdatedDocs = load_events(Docs, Events),
EvManagers = load_event_managers(UpdatedDocs),
Entries = [{'$event_managers', EvManagers} | UpdatedDocs],
EventManagers = load_event_managers(UpdatedDocs),
Entries = [{'$event_managers', EventManagers} | UpdatedDocs],
set_entries(Entries).

%% @private
Expand All @@ -145,8 +152,8 @@ load_entries(Docs, Events) ->
load_entries(Docs, Events, Fun) ->
UpdatedDocs = load_events(Docs, Events, Fun),
ok = set_entries(UpdatedDocs),
EvManagers = load_event_managers(get_docs()),
set_entries({'$event_managers', EvManagers}).
EventManagers = load_event_managers(get_docs()),
set_entries({'$event_managers', EventManagers}).

%% @private
load_events(Docs, Events) ->
Expand Down
8 changes: 4 additions & 4 deletions src/sumo_internal.erl
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@

-export_type([schema/0, doc/0, field/0]).

%%%===================================================================
%%%=============================================================================
%%% API
%%%===================================================================
%%%=============================================================================

%% @doc Returns a new schema.
-spec new_schema(sumo:schema_name(), [field()]) -> schema().
Expand Down Expand Up @@ -192,9 +192,9 @@ check_operator(Op) -> exit({unknown_operator, Op}).
report_overrun(Report) ->
lager:error("~p", [Report]).

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

%% @doc Returns field marked as ID for the given schema or doc name.
%% @private
Expand Down
19 changes: 18 additions & 1 deletion test/test.config
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,28 @@
{people, sumo_test_mnesia, #{module => sumo_test_people_mnesia}}
]},
{events, [
{'_', sumo_test_people_events_manager}
{people, sumo_test_people_events_manager}
]}
]},

{sasl, [
{sasl_error_logger, false}
]},

{lager, [
{colored, true},
{async_threshold, 200},
{async_threshold_window, 5},
{error_logger_hwm, 500},
{handlers, [
{lager_console_backend, [
debug,
{lager_default_formatter, [
color, time, " [", severity, "]",
" [", {module, ""}, ":", {function, ""}, ":", {line, ""}, "] ",
message, "\e[0m\n"
]}
]}
]}
]}
].

0 comments on commit 6850780

Please sign in to comment.