From c15a1b278a8cc729dbeb94733186254099ee3b09 Mon Sep 17 00:00:00 2001 From: cabol Date: Mon, 24 Aug 2015 13:34:37 -0500 Subject: [PATCH 1/9] [#182] Fixed sumo_store_riak to support date/datetime fields. --- Makefile | 7 ++- rebar.config | 19 +++--- src/sumo_store_riak.erl | 73 ++++++++++++++++++++++- test/sumo_basic_SUITE.erl | 22 ++++++- test/sumo_test_people_riak.erl | 106 +++++++++++++++++++++++++++------ 5 files changed, 193 insertions(+), 34 deletions(-) diff --git a/Makefile b/Makefile index 0bc7e43..f1bd491 100644 --- a/Makefile +++ b/Makefile @@ -2,16 +2,17 @@ PROJECT = sumo_db CONFIG ?= test/test.config -DEPS = lager emysql emongo tirerl epgsql worker_pool riakc uuid +DEPS = lager emysql emongo tirerl epgsql worker_pool riakc uuid iso8601 dep_lager = git https://github.com/basho/lager.git 2.1.1 dep_emysql = git https://github.com/Eonblast/Emysql.git v0.4.1 dep_emongo = git https://github.com/inaka/emongo.git v0.2.1 dep_tirerl = git https://github.com/inaka/tirerl 0.1.7 dep_epgsql = git https://github.com/epgsql/epgsql 2.0.0 -dep_worker_pool = git https://github.com/inaka/worker_pool.git 1.0.2 +dep_worker_pool = https https://github.com/inaka/worker_pool.git 1.0.2 dep_riakc = git https://github.com/inaka/riak-erlang-client.git 2.1.1-dialyzed -dep_uuid = git git://github.com/okeuday/uuid.git v1.4.0 +dep_uuid = git https://github.com/okeuday/uuid.git v1.4.0 +dep_iso8601 = git https://github.com/kivra/erlang_iso8601.git 1.1.2 TEST_DEPS = mixer dep_mixer = git git://github.com/inaka/mixer.git 0.1.2 diff --git a/rebar.config b/rebar.config index 8efbf4f..e7e0778 100644 --- a/rebar.config +++ b/rebar.config @@ -19,15 +19,16 @@ warn_untyped_record, debug_info ]}. {deps, [ - {lager, ".*", {git, "git://github.com/basho/lager.git", "2.1.1"}}, - {emysql, ".*", {git, "git://github.com/Eonblast/Emysql.git", "v0.4.1"}}, - {emongo, ".*", {git, "git://github.com/inaka/emongo.git", "v0.2.1"}}, - {tirerl, ".*", {git, "git://github.com/inaka/tirerl", "0.1.7"}}, - {epgsql, ".*", {git, "git://github.com/epgsql/epgsql", "2.0.0"}}, - {worker_pool, ".*", {git, "git://github.com/inaka/worker_pool.git", "1.0.2"}}, - {riakc, ".*", {git, "git://github.com/inaka/riak-erlang-client", "2.1.1-dialyzed"}}, - {uuid, ".*", {git, "git://github.com/okeuday/uuid.git", "v1.4.0"}}, - {mixer, ".*", {git, "git://github.com/inaka/mixer", "0.1.2"}} + {lager, ".*", {git, "https://github.com/basho/lager.git", "2.1.1"}}, + {emysql, ".*", {git, "https://github.com/Eonblast/Emysql.git", "v0.4.1"}}, + {emongo, ".*", {git, "https://github.com/inaka/emongo.git", "v0.2.1"}}, + {tirerl, ".*", {git, "https://github.com/inaka/tirerl", "0.1.7"}}, + {epgsql, ".*", {git, "https://github.com/epgsql/epgsql", "2.0.0"}}, + {worker_pool, ".*", {git, "https://github.com/inaka/worker_pool.git", "1.0.2"}}, + {riakc, ".*", {git, "https://github.com/inaka/riak-erlang-client", "2.1.1-dialyzed"}}, + {uuid, ".*", {git, "https://github.com/okeuday/uuid.git", "v1.4.0"}}, + {mixer, ".*", {git, "https://github.com/inaka/mixer", "0.1.2"}}, + {iso8601, ".*", {git, "https://github.com/kivra/erlang_iso8601.git", "1.1.2"}} ]}. {xref_warnings, true}. {xref_checks, [undefined_function_calls, undefined_functions, locals_not_used, deprecated_function_calls, deprecated_functions]}. diff --git a/src/sumo_store_riak.erl b/src/sumo_store_riak.erl index d778fa9..ea50216 100644 --- a/src/sumo_store_riak.erl +++ b/src/sumo_store_riak.erl @@ -113,7 +113,7 @@ init(Opts) -> ) -> sumo_store:result(sumo_internal:doc(), state()). persist(Doc, #state{conn = Conn, bucket = Bucket, put_opts = Opts} = State) -> - {Id, NewDoc} = new_doc(Doc, State), + {Id, NewDoc} = new_doc(sleep(Doc), State), case update_map(Conn, Bucket, Id, doc_to_rmap(NewDoc), Opts) of {error, Error} -> {error, Error, State}; @@ -223,6 +223,7 @@ find_by(DocName, Conditions, Limit, Offset, State) when is_list(Conditions) -> find_by(DocName, Conditions, Limit, Offset, State) -> find_by_query(DocName, Conditions, Limit, Offset, State). +%% @private find_by_id_field(DocName, Key, State) -> #state{conn = Conn, bucket = Bucket, get_opts = Opts} = State, case fetch_map(Conn, Bucket, to_bin(Key), Opts) of @@ -235,11 +236,13 @@ find_by_id_field(DocName, Key, State) -> {error, Error, State} end. +%% @private find_by_query(DocName, Conditions, undefined, undefined, State) -> #state{conn = Conn, index = Index} = State, Query = build_query(Conditions), find_all_by_query(DocName, Conn, Index, Query, State); +%% @private find_by_query(DocName, Conditions, Limit, Offset, State) -> #state{conn = Conn, index = Index} = State, Query = build_query(Conditions), @@ -248,6 +251,7 @@ find_by_query(DocName, Conditions, Limit, Offset, State) -> {error, Error} -> {error, Error, State} end. +%% @private find_all_by_query(DocName, Conn, Index, Query, State) -> FirstQuery = case search_docs_by(DocName, Conn, Index, Query, 0, 0) of @@ -306,7 +310,7 @@ map_to_rmap(Map) -> sumo:schema_name(), riakc_map:crdt_map() ) -> sumo_internal:doc(). rmap_to_doc(DocName, RMap) -> - sumo_internal:new_doc(DocName, rmap_to_map(RMap)). + wakeup(sumo_internal:new_doc(DocName, rmap_to_map(RMap))). -spec rmap_to_map(riakc_map:crdt_map()) -> map(). rmap_to_map(RMap) -> @@ -365,6 +369,69 @@ build_query(Conditions) -> %% Private API. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% @private +sleep(Doc) -> + DTFields = datetime_fields(Doc), + Encode = + fun({FieldName, FieldValue}, Acc) -> + case {FieldName, is_datetime(FieldValue)} of + {datetime, true} -> + sumo_internal:set_field(FieldName, iso8601:format(FieldValue), Acc); + {date, true} -> + DateTime = {FieldValue, {0, 0, 0}}, + sumo_internal:set_field(FieldName, iso8601:format(DateTime), Acc); + _ -> + Acc + end + end, + lists:foldl(Encode, Doc, DTFields). + +%% @private +wakeup(Doc) -> + DTFields = datetime_fields(Doc), + Decode = + fun({FieldName, FieldValue}, Acc) -> + case FieldName of + datetime when FieldValue /= <<"undefined">> -> + sumo_internal:set_field(FieldName, iso8601:parse(FieldValue), Acc); + date when FieldValue /= <<"undefined">> -> + {Date, _} = iso8601:parse(FieldValue), + sumo_internal:set_field(FieldName, Date, Acc); + _ -> + Acc + end + end, + lists:foldl(Decode, Doc, DTFields). + +%% @private +datetime_fields(Doc) -> + DocName = sumo_internal:doc_name(Doc), + Schema = sumo_internal:get_schema(DocName), + SchemaFields = sumo_internal:schema_fields(Schema), + Filter = + fun(Field, Acc) -> + case sumo_internal:field_type(Field) of + T when T =:= datetime; T =:= date -> + FieldName = sumo_internal:field_name(Field), + FieldValue = sumo_internal:get_field(FieldName, Doc), + [{FieldName, FieldValue} | Acc]; + _ -> + Acc + end + end, + lists:foldl(Filter, [], SchemaFields). + +%% @private +is_datetime(DT) -> + case DT of + {{_, _, _} = Date, {_, _, _}} -> + calendar:valid_date(Date); + {_, _, _} -> + calendar:valid_date(DT); + _ -> + false + end. + %% @private doc_id(Doc) -> DocName = sumo_internal:doc_name(Doc), @@ -423,7 +490,7 @@ kv_to_doc(DocName, KV) -> NK = normalize_doc_fields(K), sumo_internal:set_field(to_atom(NK), V, Acc) end, - lists:foldl(F, sumo_internal:new_doc(DocName), KV). + wakeup(lists:foldl(F, sumo_internal:new_doc(DocName), KV)). %% @private normalize_doc_fields(Src) -> diff --git a/test/sumo_basic_SUITE.erl b/test/sumo_basic_SUITE.erl index cfd290f..20a588c 100644 --- a/test/sumo_basic_SUITE.erl +++ b/test/sumo_basic_SUITE.erl @@ -12,7 +12,8 @@ find_all/1, find_by/1, delete_all/1, - delete/1 + delete/1, + check_datetime_fields/1 ]). -define(EXCLUDED_FUNS, @@ -70,6 +71,11 @@ delete_all(_Config) -> delete(_Config) -> run_all_stores(fun delete_module/1). +check_datetime_fields(_Config) -> + lists:foreach( + fun do_check_datetime_fields/1, + [sumo_test_people_riak]). + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%% Internal functions %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -139,6 +145,20 @@ delete_module(Module) -> 1 = length(All) - length(NewAll). +do_check_datetime_fields(Module) -> + [P0] = sumo:find_by(Module, [{name, <<"A">>}]), + P1 = sumo:find(Module, Module:id(P0)), + [P2 | _] = sumo:find_all(Module), + + {Date, _} = calendar:universal_time(), + + Date = Module:date(P0), + {Date, {_, _, _}} = Module:datetime(P0), + Date = Module:date(P1), + {Date, {_, _, _}} = Module:datetime(P1), + Date = Module:date(P2), + {Date, {_, _, _}} = Module:datetime(P2). + %%% Helper -spec run_all_stores(fun()) -> ok. diff --git a/test/sumo_test_people_riak.erl b/test/sumo_test_people_riak.erl index 5d7694c..ecd5637 100644 --- a/test/sumo_test_people_riak.erl +++ b/test/sumo_test_people_riak.erl @@ -1,22 +1,34 @@ -module(sumo_test_people_riak). --behavior(sumo_doc). - --include_lib("mixer/include/mixer.hrl"). --mixin([{sumo_test_people, - [sumo_wakeup/1, - sumo_sleep/1, - new/2, - new/3, - new/4, - name/1, - id/1, - age/1 - ] - } - ]). - --export([sumo_schema/0]). +%%% sumo_db callbacks +-export([ sumo_schema/0 + , sumo_wakeup/1 + , sumo_sleep/1 + ]). + +-export([ new/2 + , new/3 + , new/4 + , name/1 + , id/1 + , age/1 + , date/1 + , datetime/1 + ]). + +-record(person, {id :: integer(), + name :: string(), + last_name :: string(), + age :: integer(), + address :: string(), + date :: calendar:date(), + datetime :: calendar:datetime()}). + +-type person() :: #person{}. + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%% sumo_doc callbacks +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -spec sumo_schema() -> sumo:schema(). sumo_schema() -> @@ -25,6 +37,64 @@ sumo_schema() -> sumo:new_field(name, string, [{length, 255}, not_null]), sumo:new_field(last_name, string, [{length, 255}, not_null]), sumo:new_field(age, integer), - sumo:new_field(address, string, [{length, 255}]) + sumo:new_field(address, string, [{length, 255}]), + sumo:new_field(date, date), + sumo:new_field(datetime, datetime) ], sumo:new_schema(?MODULE, Fields). + +-spec sumo_sleep(person()) -> sumo:doc(). +sumo_sleep(Person) -> + #{id => Person#person.id, + name => Person#person.name, + last_name => Person#person.last_name, + age => Person#person.age, + address => Person#person.address, + date => Person#person.date, + datetime => Person#person.datetime}. + +-spec sumo_wakeup(sumo:doc()) -> person(). +sumo_wakeup(Person) -> + #person{ + id = maps:get(id, Person), + name = maps:get(name, Person), + last_name = maps:get(last_name, Person), + age = maps:get(age, Person), + address = maps:get(address, Person), + date = maps:get(date, Person), + datetime = maps:get(datetime, Person) + }. + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%% Exported +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +new(Name, LastName) -> + new(Name, LastName, undefined). + +new(Name, LastName, Age) -> + new(Name, LastName, Age, undefined). + +new(Name, LastName, Age, Address) -> + Datetime = {Date, _} = calendar:universal_time(), + #person{name = Name, + last_name = LastName, + age = Age, + address = Address, + date = Date, + datetime = Datetime}. + +name(Person) -> + Person#person.name. + +id(Person) -> + Person#person.id. + +age(Person) -> + Person#person.age. + +date(Person) -> + Person#person.date. + +datetime(Person) -> + Person#person.datetime. From 725f4eebe545ca8ccc76ce9115a0cb34cdf95eb5 Mon Sep 17 00:00:00 2001 From: cabol Date: Mon, 24 Aug 2015 14:11:29 -0500 Subject: [PATCH 2/9] [#182] Fixed deps in Makefile. --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index f1bd491..657dd82 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ dep_emysql = git https://github.com/Eonblast/Emysql.git v0.4.1 dep_emongo = git https://github.com/inaka/emongo.git v0.2.1 dep_tirerl = git https://github.com/inaka/tirerl 0.1.7 dep_epgsql = git https://github.com/epgsql/epgsql 2.0.0 -dep_worker_pool = https https://github.com/inaka/worker_pool.git 1.0.2 +dep_worker_pool = git https://github.com/inaka/worker_pool.git 1.0.2 dep_riakc = git https://github.com/inaka/riak-erlang-client.git 2.1.1-dialyzed dep_uuid = git https://github.com/okeuday/uuid.git v1.4.0 dep_iso8601 = git https://github.com/kivra/erlang_iso8601.git 1.1.2 From 9f25ef969eeab3136672d3418851c810c037ca4b Mon Sep 17 00:00:00 2001 From: cabol Date: Mon, 24 Aug 2015 17:01:04 -0500 Subject: [PATCH 3/9] [#182] Fixed bug in internal sleep/wakeup functions within sumo_store_riak. Fixed PR comments. --- src/sumo_store_riak.erl | 28 ++++++------- test/sumo_basic_SUITE.erl | 22 +++++------ test/sumo_test_people_riak.erl | 72 +++++++++++++++++----------------- test/sumo_test_utils.erl | 5 +++ 4 files changed, 65 insertions(+), 62 deletions(-) diff --git a/src/sumo_store_riak.erl b/src/sumo_store_riak.erl index ea50216..1157aac 100644 --- a/src/sumo_store_riak.erl +++ b/src/sumo_store_riak.erl @@ -373,8 +373,8 @@ build_query(Conditions) -> sleep(Doc) -> DTFields = datetime_fields(Doc), Encode = - fun({FieldName, FieldValue}, Acc) -> - case {FieldName, is_datetime(FieldValue)} of + fun({FieldName, FieldType, FieldValue}, Acc) -> + case {FieldType, is_datetime(FieldValue)} of {datetime, true} -> sumo_internal:set_field(FieldName, iso8601:format(FieldValue), Acc); {date, true} -> @@ -390,8 +390,8 @@ sleep(Doc) -> wakeup(Doc) -> DTFields = datetime_fields(Doc), Decode = - fun({FieldName, FieldValue}, Acc) -> - case FieldName of + fun({FieldName, FieldType, FieldValue}, Acc) -> + case FieldType of datetime when FieldValue /= <<"undefined">> -> sumo_internal:set_field(FieldName, iso8601:parse(FieldValue), Acc); date when FieldValue /= <<"undefined">> -> @@ -410,11 +410,12 @@ datetime_fields(Doc) -> SchemaFields = sumo_internal:schema_fields(Schema), Filter = fun(Field, Acc) -> - case sumo_internal:field_type(Field) of + FieldType = sumo_internal:field_type(Field), + case FieldType of T when T =:= datetime; T =:= date -> FieldName = sumo_internal:field_name(Field), FieldValue = sumo_internal:get_field(FieldName, Doc), - [{FieldName, FieldValue} | Acc]; + [{FieldName, FieldType, FieldValue} | Acc]; _ -> Acc end @@ -422,15 +423,12 @@ datetime_fields(Doc) -> lists:foldl(Filter, [], SchemaFields). %% @private -is_datetime(DT) -> - case DT of - {{_, _, _} = Date, {_, _, _}} -> - calendar:valid_date(Date); - {_, _, _} -> - calendar:valid_date(DT); - _ -> - false - end. +is_datetime({{_, _, _} = Date, {_, _, _}}) -> + calendar:valid_date(Date); +is_datetime({_, _, _} = Date) -> + calendar:valid_date(Date); +is_datetime(_) -> + false. %% @private doc_id(Doc) -> diff --git a/test/sumo_basic_SUITE.erl b/test/sumo_basic_SUITE.erl index 20a588c..bbf95a5 100644 --- a/test/sumo_basic_SUITE.erl +++ b/test/sumo_basic_SUITE.erl @@ -13,7 +13,7 @@ find_by/1, delete_all/1, delete/1, - check_datetime_fields/1 + check_proper_dates/1 ]). -define(EXCLUDED_FUNS, @@ -71,10 +71,10 @@ delete_all(_Config) -> delete(_Config) -> run_all_stores(fun delete_module/1). -check_datetime_fields(_Config) -> +check_proper_dates(_Config) -> lists:foreach( - fun do_check_datetime_fields/1, - [sumo_test_people_riak]). + fun check_proper_dates_module/1, + sumo_test_utils:people_with_proper_dates()). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%% Internal functions @@ -145,19 +145,19 @@ delete_module(Module) -> 1 = length(All) - length(NewAll). -do_check_datetime_fields(Module) -> +check_proper_dates_module(Module) -> [P0] = sumo:find_by(Module, [{name, <<"A">>}]), P1 = sumo:find(Module, Module:id(P0)), [P2 | _] = sumo:find_all(Module), {Date, _} = calendar:universal_time(), - Date = Module:date(P0), - {Date, {_, _, _}} = Module:datetime(P0), - Date = Module:date(P1), - {Date, {_, _, _}} = Module:datetime(P1), - Date = Module:date(P2), - {Date, {_, _, _}} = Module:datetime(P2). + Date = Module:birthdate(P0), + {Date, {_, _, _}} = Module:created_at(P0), + Date = Module:birthdate(P1), + {Date, {_, _, _}} = Module:created_at(P1), + Date = Module:birthdate(P2), + {Date, {_, _, _}} = Module:created_at(P2). %%% Helper diff --git a/test/sumo_test_people_riak.erl b/test/sumo_test_people_riak.erl index ecd5637..fb22f9d 100644 --- a/test/sumo_test_people_riak.erl +++ b/test/sumo_test_people_riak.erl @@ -12,17 +12,17 @@ , name/1 , id/1 , age/1 - , date/1 - , datetime/1 + , birthdate/1 + , created_at/1 ]). --record(person, {id :: integer(), - name :: string(), - last_name :: string(), - age :: integer(), - address :: string(), - date :: calendar:date(), - datetime :: calendar:datetime()}). +-record(person, {id :: integer(), + name :: string(), + last_name :: string(), + age :: integer(), + address :: string(), + birthdate :: calendar:date(), + created_at :: calendar:datetime()}). -type person() :: #person{}. @@ -33,36 +33,36 @@ -spec sumo_schema() -> sumo:schema(). sumo_schema() -> Fields = - [sumo:new_field(id, string, [id, {length, 255}, not_null]), - sumo:new_field(name, string, [{length, 255}, not_null]), - sumo:new_field(last_name, string, [{length, 255}, not_null]), - sumo:new_field(age, integer), - sumo:new_field(address, string, [{length, 255}]), - sumo:new_field(date, date), - sumo:new_field(datetime, datetime) + [sumo:new_field(id, string, [id, {length, 255}, not_null]), + sumo:new_field(name, string, [{length, 255}, not_null]), + sumo:new_field(last_name, string, [{length, 255}, not_null]), + sumo:new_field(age, integer), + sumo:new_field(address, string, [{length, 255}]), + sumo:new_field(birthdate, date), + sumo:new_field(created_at, datetime) ], sumo:new_schema(?MODULE, Fields). -spec sumo_sleep(person()) -> sumo:doc(). sumo_sleep(Person) -> - #{id => Person#person.id, - name => Person#person.name, - last_name => Person#person.last_name, - age => Person#person.age, - address => Person#person.address, - date => Person#person.date, - datetime => Person#person.datetime}. + #{id => Person#person.id, + name => Person#person.name, + last_name => Person#person.last_name, + age => Person#person.age, + address => Person#person.address, + birthdate => Person#person.birthdate, + created_at => Person#person.created_at}. -spec sumo_wakeup(sumo:doc()) -> person(). sumo_wakeup(Person) -> #person{ - id = maps:get(id, Person), - name = maps:get(name, Person), - last_name = maps:get(last_name, Person), - age = maps:get(age, Person), - address = maps:get(address, Person), - date = maps:get(date, Person), - datetime = maps:get(datetime, Person) + id = maps:get(id, Person), + name = maps:get(name, Person), + last_name = maps:get(last_name, Person), + age = maps:get(age, Person), + address = maps:get(address, Person), + birthdate = maps:get(birthdate, Person), + created_at = maps:get(created_at, Person) }. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -81,8 +81,8 @@ new(Name, LastName, Age, Address) -> last_name = LastName, age = Age, address = Address, - date = Date, - datetime = Datetime}. + birthdate = Date, + created_at = Datetime}. name(Person) -> Person#person.name. @@ -93,8 +93,8 @@ id(Person) -> age(Person) -> Person#person.age. -date(Person) -> - Person#person.date. +birthdate(Person) -> + Person#person.birthdate. -datetime(Person) -> - Person#person.datetime. +created_at(Person) -> + Person#person.created_at. diff --git a/test/sumo_test_utils.erl b/test/sumo_test_utils.erl index 5cf6da7..afd09aa 100644 --- a/test/sumo_test_utils.erl +++ b/test/sumo_test_utils.erl @@ -9,6 +9,7 @@ , people_with_like/0 , people_with_numeric_sort/0 , sleep_if_required/1 + , people_with_proper_dates/0 ]). -spec start_apps() -> ok. @@ -54,3 +55,7 @@ sleep_if_required(Module) -> sumo_test_people_riak -> timer:sleep(5000); _ -> ok end. + +-spec people_with_proper_dates() -> [atom()]. +people_with_proper_dates() -> + all_people() -- [sumo_test_people_postgres]. From f63eab2fae960058d45abca4dd56589ec213574d Mon Sep 17 00:00:00 2001 From: Brujo Benavides Date: Wed, 26 Aug 2015 15:10:17 -0300 Subject: [PATCH 4/9] Dependency upgrade --- Makefile | 16 +++++++++------- rebar.config | 10 +++++----- test/sumo_test_utils.erl | 12 ++++++------ 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index 657dd82..99bdecd 100644 --- a/Makefile +++ b/Makefile @@ -2,17 +2,19 @@ PROJECT = sumo_db CONFIG ?= test/test.config -DEPS = lager emysql emongo tirerl epgsql worker_pool riakc uuid iso8601 +DEPS = lager uuid emysql emongo tirerl epgsql worker_pool riakc iso8601 +SHELL_DEPS = sync +dep_sync = git https://github.com/inaka/sync.git 0.1.3 dep_lager = git https://github.com/basho/lager.git 2.1.1 -dep_emysql = git https://github.com/Eonblast/Emysql.git v0.4.1 +dep_emysql = git https://github.com/inaka/Emysql.git 0.4.2 dep_emongo = git https://github.com/inaka/emongo.git v0.2.1 -dep_tirerl = git https://github.com/inaka/tirerl 0.1.7 +dep_tirerl = git https://github.com/inaka/tirerl 77d17a7 dep_epgsql = git https://github.com/epgsql/epgsql 2.0.0 -dep_worker_pool = git https://github.com/inaka/worker_pool.git 1.0.2 -dep_riakc = git https://github.com/inaka/riak-erlang-client.git 2.1.1-dialyzed -dep_uuid = git https://github.com/okeuday/uuid.git v1.4.0 -dep_iso8601 = git https://github.com/kivra/erlang_iso8601.git 1.1.2 +dep_worker_pool = git https://github.com/inaka/worker_pool.git 1.0.3 +dep_riakc = git https://github.com/inaka/riak-erlang-client.git 2.1.1-R18 +dep_uuid = git https://github.com/okeuday/uuid.git 31f408f4ef +dep_iso8601 = git https://github.com/zerotao/erlang_iso8601.git 0d14540 TEST_DEPS = mixer dep_mixer = git git://github.com/inaka/mixer.git 0.1.2 diff --git a/rebar.config b/rebar.config index e7e0778..55f2e4f 100644 --- a/rebar.config +++ b/rebar.config @@ -20,13 +20,13 @@ ]}. {deps, [ {lager, ".*", {git, "https://github.com/basho/lager.git", "2.1.1"}}, - {emysql, ".*", {git, "https://github.com/Eonblast/Emysql.git", "v0.4.1"}}, + {emysql, ".*", {git, "https://github.com/inaka/Emysql.git", "0.4.2"}}, {emongo, ".*", {git, "https://github.com/inaka/emongo.git", "v0.2.1"}}, - {tirerl, ".*", {git, "https://github.com/inaka/tirerl", "0.1.7"}}, + {tirerl, ".*", {git, "https://github.com/inaka/tirerl", "77d17a7"}}, {epgsql, ".*", {git, "https://github.com/epgsql/epgsql", "2.0.0"}}, - {worker_pool, ".*", {git, "https://github.com/inaka/worker_pool.git", "1.0.2"}}, - {riakc, ".*", {git, "https://github.com/inaka/riak-erlang-client", "2.1.1-dialyzed"}}, - {uuid, ".*", {git, "https://github.com/okeuday/uuid.git", "v1.4.0"}}, + {worker_pool, ".*", {git, "https://github.com/inaka/worker_pool.git", "1.0.3"}}, + {riakc, ".*", {git, "https://github.com/inaka/riak-erlang-client", "2.1.1-R18"}}, + {uuid, ".*", {git, "https://github.com/okeuday/uuid.git", "v1.5.0"}}, {mixer, ".*", {git, "https://github.com/inaka/mixer", "0.1.2"}}, {iso8601, ".*", {git, "https://github.com/kivra/erlang_iso8601.git", "1.1.2"}} ]}. diff --git a/test/sumo_test_utils.erl b/test/sumo_test_utils.erl index afd09aa..320c496 100644 --- a/test/sumo_test_utils.erl +++ b/test/sumo_test_utils.erl @@ -14,13 +14,13 @@ -spec start_apps() -> ok. start_apps() -> - application:ensure_all_started(emysql), - application:ensure_all_started(epgsql), - application:ensure_all_started(emongo), - application:ensure_all_started(tirerl), + {ok, _} = application:ensure_all_started(emysql), + {ok, _} = application:ensure_all_started(epgsql), + {ok, _} = application:ensure_all_started(emongo), + {ok, _} = application:ensure_all_started(tirerl), mnesia:create_schema([node()]), - application:ensure_all_started(mnesia), - application:ensure_all_started(sumo_db). + {ok, _} = application:ensure_all_started(mnesia), + {ok, _} = application:ensure_all_started(sumo_db). -spec all_people() -> [atom()]. all_people() -> From 29bf6b4f1df5d506f24b110964b3908b552d63b7 Mon Sep 17 00:00:00 2001 From: Brujo Benavides Date: Wed, 26 Aug 2015 15:53:38 -0300 Subject: [PATCH 5/9] [#182] Almost unified people structure among different stores --- test/sumo_test_people.erl | 25 ++++-- test/sumo_test_people_elasticsearch.erl | 24 ++++-- test/sumo_test_people_mnesia.erl | 28 ++++--- test/sumo_test_people_mongo.erl | 24 ++++-- test/sumo_test_people_mysql.erl | 24 ++++-- test/sumo_test_people_pgsql.erl | 26 +++--- test/sumo_test_people_riak.erl | 102 +++++------------------- 7 files changed, 120 insertions(+), 133 deletions(-) diff --git a/test/sumo_test_people.erl b/test/sumo_test_people.erl index 00a68f6..de08dee 100644 --- a/test/sumo_test_people.erl +++ b/test/sumo_test_people.erl @@ -8,13 +8,15 @@ sumo_sleep/1 ]). --export([new/2, new/3, new/4, name/1, id/1, age/1]). +-export([new/2, new/3, new/4, name/1, id/1, age/1, birthdate/1, created_at/1]). -record(person, {id :: integer(), name :: string(), last_name :: string(), age :: integer(), - address :: string()}). + address :: string(), + birthdate :: calendar:date(), + created_at :: calendar:datetime()}). -type person() :: #person{}. @@ -28,7 +30,9 @@ sumo_sleep(Person) -> name => Person#person.name, last_name => Person#person.last_name, age => Person#person.age, - address => Person#person.address}. + address => Person#person.address, + birthdate => Person#person.birthdate, + created_at => Person#person.created_at}. -spec sumo_wakeup(sumo:doc()) -> person(). sumo_wakeup(Person) -> @@ -37,7 +41,9 @@ sumo_wakeup(Person) -> name = maps:get(name, Person), last_name = maps:get(last_name, Person), age = maps:get(age, Person), - address = maps:get(address, Person) + address = maps:get(address, Person), + birthdate = maps:get(birthdate, Person), + created_at = maps:get(created_at, Person) }. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -54,10 +60,13 @@ new(Name, LastName, Age) -> age = Age}. new(Name, LastName, Age, Address) -> + Datetime = {Date, _} = calendar:universal_time(), #person{name = Name, last_name = LastName, age = Age, - address = Address}. + address = Address, + birthdate = Date, + created_at = Datetime}. name(Person) -> Person#person.name. @@ -67,3 +76,9 @@ id(Person) -> age(Person) -> Person#person.age. + +birthdate(Person) -> + Person#person.birthdate. + +created_at(Person) -> + Person#person.created_at. diff --git a/test/sumo_test_people_elasticsearch.erl b/test/sumo_test_people_elasticsearch.erl index 7b7f8b2..355b687 100644 --- a/test/sumo_test_people_elasticsearch.erl +++ b/test/sumo_test_people_elasticsearch.erl @@ -11,20 +11,28 @@ new/4, name/1, id/1, - age/1 + age/1, + birthdate/1, + created_at/1 ] } ]). -export([sumo_schema/0]). +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%% sumo_doc callbacks +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + -spec sumo_schema() -> sumo:schema(). sumo_schema() -> - Fields = - [sumo:new_field(id, string, [id, not_null, auto_increment]), - sumo:new_field(name, string, [{length, 255}, not_null]), - sumo:new_field(last_name, string, [{length, 255}, not_null]), - sumo:new_field(age, integer), - sumo:new_field(address, string, [{length, 255}]) + Fields = + [sumo:new_field(id, integer, [id, auto_increment]), + sumo:new_field(name, string, [{length, 255}, not_null]), + sumo:new_field(last_name, string, [{length, 255}, not_null]), + sumo:new_field(age, integer), + sumo:new_field(address, string, [{length, 255}]), + sumo:new_field(birthdate, date), + sumo:new_field(created_at, datetime) ], - sumo:new_schema(?MODULE, Fields). + sumo:new_schema(?MODULE, Fields). diff --git a/test/sumo_test_people_mnesia.erl b/test/sumo_test_people_mnesia.erl index 959cfe6..2249b3b 100644 --- a/test/sumo_test_people_mnesia.erl +++ b/test/sumo_test_people_mnesia.erl @@ -11,24 +11,28 @@ new/4, name/1, id/1, - age/1 + age/1, + birthdate/1, + created_at/1 ] } ]). -export([sumo_schema/0]). +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%% sumo_doc callbacks +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + -spec sumo_schema() -> sumo:schema(). sumo_schema() -> - Fields = - [sumo:new_field(id, integer, [id, auto_increment]), - sumo:new_field(name, string, [{length, 255}, not_null]), - sumo:new_field(last_name, string, [{length, 255}, not_null]), - sumo:new_field(age, integer, [index]), - sumo:new_field(address, string, [{length, 255}]), - sumo:new_field(birthday, date), - sumo:new_field(created, datetime), - sumo:new_field(description, text), - sumo:new_field(profile_image, binary) + Fields = + [sumo:new_field(id, integer, [id, auto_increment]), + sumo:new_field(name, string, [{length, 255}, not_null]), + sumo:new_field(last_name, string, [{length, 255}, not_null]), + sumo:new_field(age, integer), + sumo:new_field(address, string, [{length, 255}]), + sumo:new_field(birthdate, date), + sumo:new_field(created_at, datetime) ], - sumo:new_schema(?MODULE, Fields). + sumo:new_schema(?MODULE, Fields). diff --git a/test/sumo_test_people_mongo.erl b/test/sumo_test_people_mongo.erl index 1c06e23..cd508d5 100644 --- a/test/sumo_test_people_mongo.erl +++ b/test/sumo_test_people_mongo.erl @@ -11,20 +11,28 @@ new/4, name/1, id/1, - age/1 + age/1, + birthdate/1, + created_at/1 ] } ]). -export([sumo_schema/0]). +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%% sumo_doc callbacks +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + -spec sumo_schema() -> sumo:schema(). sumo_schema() -> - Fields = - [sumo:new_field(id, integer, [id, not_null, auto_increment]), - sumo:new_field(name, string, [{length, 255}, not_null]), - sumo:new_field(last_name, string, [{length, 255}, not_null]), - sumo:new_field(age, integer), - sumo:new_field(address, string, [{length, 255}]) + Fields = + [sumo:new_field(id, integer, [id, auto_increment]), + sumo:new_field(name, string, [{length, 255}, not_null]), + sumo:new_field(last_name, string, [{length, 255}, not_null]), + sumo:new_field(age, integer), + sumo:new_field(address, string, [{length, 255}]), + sumo:new_field(birthdate, date), + sumo:new_field(created_at, datetime) ], - sumo:new_schema(?MODULE, Fields). + sumo:new_schema(?MODULE, Fields). diff --git a/test/sumo_test_people_mysql.erl b/test/sumo_test_people_mysql.erl index 01d81a7..1f9cd43 100644 --- a/test/sumo_test_people_mysql.erl +++ b/test/sumo_test_people_mysql.erl @@ -11,20 +11,28 @@ new/4, name/1, id/1, - age/1 + age/1, + birthdate/1, + created_at/1 ] } ]). -export([sumo_schema/0]). +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%% sumo_doc callbacks +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + -spec sumo_schema() -> sumo:schema(). sumo_schema() -> - Fields = - [sumo:new_field(id, integer, [id, not_null, auto_increment]), - sumo:new_field(name, string, [{length, 255}, not_null]), - sumo:new_field(last_name, string, [{length, 255}, not_null]), - sumo:new_field(age, integer), - sumo:new_field(address, string, [{length, 255}]) + Fields = + [sumo:new_field(id, integer, [id, auto_increment]), + sumo:new_field(name, string, [{length, 255}, not_null]), + sumo:new_field(last_name, string, [{length, 255}, not_null]), + sumo:new_field(age, integer), + sumo:new_field(address, string, [{length, 255}]), + sumo:new_field(birthdate, date), + sumo:new_field(created_at, datetime) ], - sumo:new_schema(?MODULE, Fields). + sumo:new_schema(?MODULE, Fields). diff --git a/test/sumo_test_people_pgsql.erl b/test/sumo_test_people_pgsql.erl index 16676c3..fde189a 100644 --- a/test/sumo_test_people_pgsql.erl +++ b/test/sumo_test_people_pgsql.erl @@ -11,24 +11,30 @@ new/4, name/1, id/1, - age/1 + age/1, + birthdate/1, + created_at/1 ] } ]). -export([sumo_schema/0]). +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%% sumo_doc callbacks +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + -spec sumo_schema() -> sumo:schema(). sumo_schema() -> - Fields = - [sumo:new_field(id, integer, [id, auto_increment]), - sumo:new_field(name, string, [{length, 255}, not_null]), - sumo:new_field(last_name, string, [{length, 255}, not_null]), - sumo:new_field(age, integer), - sumo:new_field(address, string, [{length, 255}]), - sumo:new_field(birthday, date), - sumo:new_field(created, datetime), + Fields = + [sumo:new_field(id, integer, [id, auto_increment]), + sumo:new_field(name, string, [{length, 255}, not_null]), + sumo:new_field(last_name, string, [{length, 255}, not_null]), + sumo:new_field(age, integer), + sumo:new_field(address, string, [{length, 255}]), + sumo:new_field(birthdate, date), + sumo:new_field(created_at, datetime), sumo:new_field(description, text), sumo:new_field(profile_image, binary) ], - sumo:new_schema(?MODULE, Fields). + sumo:new_schema(?MODULE, Fields). diff --git a/test/sumo_test_people_riak.erl b/test/sumo_test_people_riak.erl index fb22f9d..0432a79 100644 --- a/test/sumo_test_people_riak.erl +++ b/test/sumo_test_people_riak.erl @@ -1,30 +1,24 @@ -module(sumo_test_people_riak). -%%% sumo_db callbacks --export([ sumo_schema/0 - , sumo_wakeup/1 - , sumo_sleep/1 - ]). - --export([ new/2 - , new/3 - , new/4 - , name/1 - , id/1 - , age/1 - , birthdate/1 - , created_at/1 - ]). - --record(person, {id :: integer(), - name :: string(), - last_name :: string(), - age :: integer(), - address :: string(), - birthdate :: calendar:date(), - created_at :: calendar:datetime()}). - --type person() :: #person{}. +-behavior(sumo_doc). + +-include_lib("mixer/include/mixer.hrl"). +-mixin([{sumo_test_people, + [sumo_wakeup/1, + sumo_sleep/1, + new/2, + new/3, + new/4, + name/1, + id/1, + age/1, + birthdate/1, + created_at/1 + ] + } + ]). + +-export([sumo_schema/0]). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%% sumo_doc callbacks @@ -33,7 +27,7 @@ -spec sumo_schema() -> sumo:schema(). sumo_schema() -> Fields = - [sumo:new_field(id, string, [id, {length, 255}, not_null]), + [sumo:new_field(id, integer, [id, auto_increment]), sumo:new_field(name, string, [{length, 255}, not_null]), sumo:new_field(last_name, string, [{length, 255}, not_null]), sumo:new_field(age, integer), @@ -42,59 +36,3 @@ sumo_schema() -> sumo:new_field(created_at, datetime) ], sumo:new_schema(?MODULE, Fields). - --spec sumo_sleep(person()) -> sumo:doc(). -sumo_sleep(Person) -> - #{id => Person#person.id, - name => Person#person.name, - last_name => Person#person.last_name, - age => Person#person.age, - address => Person#person.address, - birthdate => Person#person.birthdate, - created_at => Person#person.created_at}. - --spec sumo_wakeup(sumo:doc()) -> person(). -sumo_wakeup(Person) -> - #person{ - id = maps:get(id, Person), - name = maps:get(name, Person), - last_name = maps:get(last_name, Person), - age = maps:get(age, Person), - address = maps:get(address, Person), - birthdate = maps:get(birthdate, Person), - created_at = maps:get(created_at, Person) - }. - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%% Exported -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -new(Name, LastName) -> - new(Name, LastName, undefined). - -new(Name, LastName, Age) -> - new(Name, LastName, Age, undefined). - -new(Name, LastName, Age, Address) -> - Datetime = {Date, _} = calendar:universal_time(), - #person{name = Name, - last_name = LastName, - age = Age, - address = Address, - birthdate = Date, - created_at = Datetime}. - -name(Person) -> - Person#person.name. - -id(Person) -> - Person#person.id. - -age(Person) -> - Person#person.age. - -birthdate(Person) -> - Person#person.birthdate. - -created_at(Person) -> - Person#person.created_at. From 75f6d1648073e60ce6101a906318af8ea2ea8763 Mon Sep 17 00:00:00 2001 From: Brujo Benavides Date: Thu, 27 Aug 2015 17:20:09 -0300 Subject: [PATCH 6/9] [#182] Now everyone uses calendar:date() --- src/sumo_store_mysql.erl | 4 ++++ test/sumo_basic_SUITE.erl | 4 +--- test/sumo_test_people.erl | 19 +++++++------------ 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/src/sumo_store_mysql.erl b/src/sumo_store_mysql.erl index 880314c..31af30d 100644 --- a/src/sumo_store_mysql.erl +++ b/src/sumo_store_mysql.erl @@ -429,6 +429,10 @@ build_docs(DocName, #result_packet{rows = Rows, field_list = Fields}) -> [build_doc(sumo_internal:new_doc(DocName), FieldNames, Row) || Row <- Rows]. build_doc(Doc, [], []) -> Doc; +build_doc(Doc, [FieldName|FieldNames], [{date, Value}|Values]) -> + build_doc(sumo_internal:set_field(FieldName, Value, Doc), FieldNames, Values); +build_doc(Doc, [FieldName|FieldNames], [{datetime, Value}|Values]) -> + build_doc(sumo_internal:set_field(FieldName, Value, Doc), FieldNames, Values); build_doc(Doc, [FieldName|FieldNames], [Value|Values]) -> build_doc(sumo_internal:set_field(FieldName, Value, Doc), FieldNames, Values). diff --git a/test/sumo_basic_SUITE.erl b/test/sumo_basic_SUITE.erl index bbf95a5..634953a 100644 --- a/test/sumo_basic_SUITE.erl +++ b/test/sumo_basic_SUITE.erl @@ -72,9 +72,7 @@ delete(_Config) -> run_all_stores(fun delete_module/1). check_proper_dates(_Config) -> - lists:foreach( - fun check_proper_dates_module/1, - sumo_test_utils:people_with_proper_dates()). + run_all_stores(fun check_proper_dates_module/1). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%% Internal functions diff --git a/test/sumo_test_people.erl b/test/sumo_test_people.erl index de08dee..fe30a83 100644 --- a/test/sumo_test_people.erl +++ b/test/sumo_test_people.erl @@ -50,23 +50,18 @@ sumo_wakeup(Person) -> %%% Exported %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -new(Name, LastName) -> - #person{name = Name, - last_name = LastName}. +new(Name, LastName) -> new(Name, LastName, undefined). -new(Name, LastName, Age) -> - #person{name = Name, - last_name = LastName, - age = Age}. +new(Name, LastName, Age) -> new(Name, LastName, Age, undefined). new(Name, LastName, Age, Address) -> Datetime = {Date, _} = calendar:universal_time(), #person{name = Name, - last_name = LastName, - age = Age, - address = Address, - birthdate = Date, - created_at = Datetime}. + last_name = LastName, + age = Age, + address = Address, + birthdate = Date, + created_at = Datetime}. name(Person) -> Person#person.name. From 215f341f6a79e60855ad05d99a289d401a06d620 Mon Sep 17 00:00:00 2001 From: Brujo Benavides Date: Thu, 27 Aug 2015 18:15:15 -0300 Subject: [PATCH 7/9] Version upgrade for elasticsearch --- Makefile | 2 +- rebar.config | 2 +- src/sumo_store_elasticsearch.erl | 19 +++++++++++++++++-- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 99bdecd..935b0a9 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ dep_sync = git https://github.com/inaka/sync.git 0.1.3 dep_lager = git https://github.com/basho/lager.git 2.1.1 dep_emysql = git https://github.com/inaka/Emysql.git 0.4.2 dep_emongo = git https://github.com/inaka/emongo.git v0.2.1 -dep_tirerl = git https://github.com/inaka/tirerl 77d17a7 +dep_tirerl = git https://github.com/inaka/tirerl 7ac7d57a24 dep_epgsql = git https://github.com/epgsql/epgsql 2.0.0 dep_worker_pool = git https://github.com/inaka/worker_pool.git 1.0.3 dep_riakc = git https://github.com/inaka/riak-erlang-client.git 2.1.1-R18 diff --git a/rebar.config b/rebar.config index 55f2e4f..f8609d1 100644 --- a/rebar.config +++ b/rebar.config @@ -22,7 +22,7 @@ {lager, ".*", {git, "https://github.com/basho/lager.git", "2.1.1"}}, {emysql, ".*", {git, "https://github.com/inaka/Emysql.git", "0.4.2"}}, {emongo, ".*", {git, "https://github.com/inaka/emongo.git", "v0.2.1"}}, - {tirerl, ".*", {git, "https://github.com/inaka/tirerl", "77d17a7"}}, + {tirerl, ".*", {git, "https://github.com/inaka/tirerl", "7ac7d57a24"}}, {epgsql, ".*", {git, "https://github.com/epgsql/epgsql", "2.0.0"}}, {worker_pool, ".*", {git, "https://github.com/inaka/worker_pool.git", "1.0.3"}}, {riakc, ".*", {git, "https://github.com/inaka/riak-erlang-client", "2.1.1-R18"}}, diff --git a/src/sumo_store_elasticsearch.erl b/src/sumo_store_elasticsearch.erl index 513106d..8f5b50c 100644 --- a/src/sumo_store_elasticsearch.erl +++ b/src/sumo_store_elasticsearch.erl @@ -74,7 +74,7 @@ persist(Doc, #{index := Index, pool_name := PoolName} = State) -> IdField = sumo_internal:id_field_name(DocName), Id = sumo_internal:get_field(IdField, Doc), - Fields = sumo_internal:doc_fields(Doc), + Fields = normalize_fields(sumo_internal:doc_fields(Doc)), Doc1 = case Id of @@ -236,8 +236,23 @@ build_mapping(MappingType, Fields) -> fun (Field, Acc) -> Name = sumo_internal:field_name(Field), - FieldType = sumo_internal:field_type(Field), + FieldType = normalize_type(sumo_internal:field_type(Field)), maps:put(Name, #{type => FieldType}, Acc) end, Properties = lists:foldl(Fun, #{}, Fields), maps:from_list([{MappingType, #{properties => Properties}}]). + +normalize_type(date) -> binary; +normalize_type(datetime) -> binary; +normalize_type(Type) -> Type. + +normalize_fields(Doc) -> + FieldList = maps:to_list(Doc), + lists:foldl( + fun ({K, {_, _, _} = FieldValue}, AccDoc) -> + maps:put(K, iso8601:format({FieldValue, {0, 0, 0}}), AccDoc); + ({K, {{_, _, _}, {_, _, _}} = FieldValue}, AccDoc) -> + maps:put(K, iso8601:format(FieldValue), AccDoc); + ({K, _FieldValue}, AccDoc) -> + AccDoc + end, Doc, FieldList). From a22636789d99e4aecb31a32e1dea3b7f1bdb4b7b Mon Sep 17 00:00:00 2001 From: Brujo Benavides Date: Fri, 28 Aug 2015 16:24:26 -0300 Subject: [PATCH 8/9] [#182] Normalizing tests, postponing ES to #187 --- test/sumo_basic_SUITE.erl | 4 +++- test/sumo_config_SUITE.erl | 6 ++++-- test/sumo_test_people_elasticsearch.erl | 8 ++++---- test/sumo_test_utils.erl | 2 +- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/test/sumo_basic_SUITE.erl b/test/sumo_basic_SUITE.erl index 634953a..bbf95a5 100644 --- a/test/sumo_basic_SUITE.erl +++ b/test/sumo_basic_SUITE.erl @@ -72,7 +72,9 @@ delete(_Config) -> run_all_stores(fun delete_module/1). check_proper_dates(_Config) -> - run_all_stores(fun check_proper_dates_module/1). + lists:foreach( + fun check_proper_dates_module/1, + sumo_test_utils:people_with_proper_dates()). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%% Internal functions diff --git a/test/sumo_config_SUITE.erl b/test/sumo_config_SUITE.erl index b4b6121..9685106 100644 --- a/test/sumo_config_SUITE.erl +++ b/test/sumo_config_SUITE.erl @@ -28,8 +28,10 @@ -spec all() -> [atom()]. all() -> - Exports = ?MODULE:module_info(exports), - [F || {F, _} <- Exports, not lists:member(F, ?EXCLUDED_FUNS)]. + case lists:member(sumo_test_people_mysql, sumo_test_utils:all_people()) of + true -> [check_overrun_handler]; + false -> [] + end. -spec init_per_suite(config()) -> config(). init_per_suite(Config) -> diff --git a/test/sumo_test_people_elasticsearch.erl b/test/sumo_test_people_elasticsearch.erl index 355b687..d2cb6ad 100644 --- a/test/sumo_test_people_elasticsearch.erl +++ b/test/sumo_test_people_elasticsearch.erl @@ -27,12 +27,12 @@ -spec sumo_schema() -> sumo:schema(). sumo_schema() -> Fields = - [sumo:new_field(id, integer, [id, auto_increment]), + [sumo:new_field(id, string, [id]), sumo:new_field(name, string, [{length, 255}, not_null]), sumo:new_field(last_name, string, [{length, 255}, not_null]), sumo:new_field(age, integer), - sumo:new_field(address, string, [{length, 255}]), - sumo:new_field(birthdate, date), - sumo:new_field(created_at, datetime) + sumo:new_field(address, string, [{length, 255}]) %, + % sumo:new_field(birthdate, date), + % sumo:new_field(created_at, datetime) ], sumo:new_schema(?MODULE, Fields). diff --git a/test/sumo_test_utils.erl b/test/sumo_test_utils.erl index 320c496..811a137 100644 --- a/test/sumo_test_utils.erl +++ b/test/sumo_test_utils.erl @@ -58,4 +58,4 @@ sleep_if_required(Module) -> -spec people_with_proper_dates() -> [atom()]. people_with_proper_dates() -> - all_people() -- [sumo_test_people_postgres]. + all_people() -- [sumo_test_people_elasticsearch]. From 37c458c0f4cd48652361b9797f74be8803dda702 Mon Sep 17 00:00:00 2001 From: Brujo Benavides Date: Fri, 28 Aug 2015 17:11:45 -0300 Subject: [PATCH 9/9] [#182] @gadgetci comments --- src/sumo_store_elasticsearch.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sumo_store_elasticsearch.erl b/src/sumo_store_elasticsearch.erl index 8f5b50c..f186ed3 100644 --- a/src/sumo_store_elasticsearch.erl +++ b/src/sumo_store_elasticsearch.erl @@ -253,6 +253,6 @@ normalize_fields(Doc) -> maps:put(K, iso8601:format({FieldValue, {0, 0, 0}}), AccDoc); ({K, {{_, _, _}, {_, _, _}} = FieldValue}, AccDoc) -> maps:put(K, iso8601:format(FieldValue), AccDoc); - ({K, _FieldValue}, AccDoc) -> + ({_K, _FieldValue}, AccDoc) -> AccDoc end, Doc, FieldList).