Skip to content

Update before_doc_update/2 to before_doc_update/3#1808

Merged
jiangphcn merged 1 commit intomasterfrom
before_doc_update
Jan 5, 2019
Merged

Update before_doc_update/2 to before_doc_update/3#1808
jiangphcn merged 1 commit intomasterfrom
before_doc_update

Conversation

@jiangphcn
Copy link
Contributor

Overview

Pass update type to before_doc_update/3 to allow additional check. The possible update type includes

  • interactive_edit
  • replicated_changes

Testing recommendations

make check skip_deps+=couch_epi apps=couch tests=callback_test_

Related Issues or Pull Requests

Checklist

  • Code is written and works correctly;
  • Changes are covered by tests;
  • Documentation reflects the changes;

%% fake db is expensive to create so we only do it if we have to
Db = fabric_util:fake_db(DbName, Opts),
[couch_replicator_manager:before_doc_update(Doc, Db) || Doc <- Docs];
[couch_replicator_manager:before_doc_update(Doc, Db, []) || Doc <- Docs];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to update description of the PR the update type can be either:

  • replicated_changes
  • interactive_updates

[Doc1, _Db] when is_function(Fun) -> Fun(Doc1, Db);
[Doc1, _Db] -> Doc1
case with_pipe(before_doc_update, [Doc0, Db, UpdateType]) of
[Doc1, _Db, UpdateType] when is_function(Fun) ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are matching the same variable UpdateType which means plugin wouldn't be able to change update type. Is there any reason to allow it? I think you need to change this line to [Doc1, _Db] when is_function(Fun) ->.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is still an issue

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @iilyak for double check. For this case, I tried to use [Doc1, _Db] when is_function(Fun) -> here and [Doc, Db]. in https://github.com/cloudant/showroom/pull/212/commits/7706fba5776466ba23963213ace114d85068e777#diff-984e3fb55ca9cd5d963758ec7cdc91e0L43. But this causes that db instance can't start.

So I try to pass all variables here [Doc, Db, _UpdateType]. and use [Doc, Db, _UpdateType]. in https://github.com/cloudant/showroom/pull/212/commits/7706fba5776466ba23963213ace114d85068e777#diff-984e3fb55ca9cd5d963758ec7cdc91e0R40. My test works fine now https://github.com/cloudant/showroom/pull/212#issuecomment-449840808

I noticed that _Db is still passed to Fun even if it is not changed. Thinking that we need the same case for _UpdateType.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am sorry I think I confused you by my wrong example. I meant it should be [Doc1, _Db, _UpdateType]. The UpdateTypename cannot be used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem, Ilya. I think that I got your point now. Totally make sense and I should use different name after calling with_pipe/2. The change was reflected in recent commit.

[Doc1, _Db] -> Doc1
case with_pipe(before_doc_update, [Doc0, Db, UpdateType]) of
[Doc1, _Db, _UpdateType] when is_function(Fun) ->
Fun(Doc1, Db, _UpdateType);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh. I didn't notice earlier that you are passing _UpdateType to Fun. In this case it is better to rename it into UpdateType1 or something. According to erlang's conventions (roughly).

  • Use new name for variable when the value need to be changed (usually the integer is added as suffix)
  • Use _Name style for variables you don't care about.

In this case we do care about the variable since we are passing it to another function. Also we cannot use UpdateType since the value returned from with_pipe can differ from the passed value. In this case we would have a badmatch.


-spec before_doc_update(#doc{}, Db::any()) -> #doc{}.
before_doc_update(#doc{id = <<?DESIGN_DOC_PREFIX, _/binary>>} = Doc, _Db) ->
-spec before_doc_update(#doc{}, Db::any(), any()) -> #doc{}.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we shouldn't use any() here. By using any we disable dializer. Try defining update_type() in couch_db and refer to it using couch_db:update_type().

diff --git a/src/couch/src/couch_db.erl b/src/couch/src/couch_db.erl
index 1f8bc42..43ed562 100644
--- a/src/couch/src/couch_db.erl
+++ b/src/couch/src/couch_db.erl
@@ -135,6 +135,10 @@
 -include_lib("couch/include/couch_db.hrl").
 -include("couch_db_int.hrl").

+-type update_type()
+    :: replicated_changes
+        | interactive_edit.
+

%% fake db is expensive to create so we only do it if we have to
Db = fabric_util:fake_db(DbName, Opts),
[couch_replicator_docs:before_doc_update(Doc, Db) || Doc <- Docs];
[couch_replicator_docs:before_doc_update(Doc, Db, interactive_updates)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. I couldn't find where interactive_updates is handled. I think it is typo and should be interactive_edit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Because this is for placeholder and I directly use #1808 (comment). I already change it to replicated_changes.

%% fake db is expensive to create so we only do it if we have to
Db = fabric_util:fake_db(DbName, Opts),
[couch_users_db:before_doc_update(Doc, Db) || Doc <- Docs];
[couch_users_db:before_doc_update(Doc, Db, interactive_updates)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be interactive_edit unless interactive_updates is a new thing defined in some other dependent PR I am not aware of.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, change it to interactive_edit

@jiangphcn
Copy link
Contributor Author

Thanks @iilyak for careful review. I addressed your comments. I will rebase and merge them if getting approval later.

Copy link
Contributor

@iilyak iilyak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 after squashing of all commits into one.

  - Pass UpdateType to before_doc_update/3
@jiangphcn jiangphcn merged commit 72b788e into master Jan 5, 2019
@jiangphcn jiangphcn deleted the before_doc_update branch January 5, 2019 07:40
@wohali wohali mentioned this pull request Feb 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants