anchor.diff:      case ns_config:search_node(node(), Config, config_version) of
           {value, CurrentVersion} ->
               [];
  -        false ->
  -            [{set, {node, node(), config_version}, {1,7,1}} |
  -             upgrade_config_from_1_7_to_1_7_1()];
  -        {value, {1,7,1}} ->
  -            [{set, {node, node(), config_version}, {1,7,2}} |
  -             upgrade_config_from_1_7_1_to_1_7_2(Config)];
  -        {value, {1,7,2}} ->
  -            [{set, {node, node(), config_version}, {1,8,0}} |
  -             upgrade_config_from_1_7_2_to_1_8_0(Config)];
  -        {value, {1,8,0}} ->
  -            [{set, {node, node(), config_version}, {1,8,1}} |
  -             upgrade_config_from_1_8_0_to_1_8_1(Config)];
  -        {value, {1,8,1}} ->
  -            [{set, {node, node(), config_version}, {2,0}} |
  -             upgrade_config_from_1_8_1_to_2_0(Config)];
  -        {value, {2,0}} ->
  -            [{set, {node, node(), config_version}, {2,2,0}} |
  -             upgrade_config_from_2_0_to_2_2_0(Config)];
  -        {value, {2,2,0}} ->
  -            [{set, {node, node(), config_version}, {2,3,0}} |
  -             upgrade_config_from_2_2_0_to_2_3_0(Config)];
           {value, {2,3,0}} ->
               [{set, {node, node(), config_version}, {3,0}} |
                upgrade_config_from_2_3_0_to_3_0(Config)];
  
positive.code:      {set, PortServersK, NewPSDefaults},
       {set, McdKey, NewMcdConfig},
       {set, JTKey, {NewJsonTemplateConfig}}].
  
  search_sub_key(Config, Key, Subkey) ->
      case ns_config:search(Config, Key) of
          false ->
              false;
          {value, List} ->
              case lists:keyfind(Subkey, 1, List) of
                  false ->
                      false;
                  {Subkey, X} ->
                      X
              end
      end.
  
  upgrade_if_equals(Config, DefaultConfig, Key, Subkey, Value) ->
      Upgrade = case search_sub_key(Config, Key, Subkey) of
                    false ->
                        true;
                    X ->
                        X =:= Value
                end,
      case Upgrade of
          true ->
              {_, UpgradeValue} = lists:keyfind(Key, 1, DefaultConfig),
              [{set, Key, UpgradeValue}];
          false ->
              []
      end.
  
  do_upgrade_config_from_2_0_to_2_2_0(Config, DefaultConfig, DataDir) ->
      OldDataDir = filename:join(DataDir, "data"),
  
      OldVersionPwFile = filename:join(OldDataDir, ?ISASL_PW),
      OldVersionNsLog = filename:join(OldDataDir, ?NS_LOG),
  
      MayBePwFile = upgrade_if_equals(Config, DefaultConfig, {node, node(), isasl}, path,
                                      OldVersionPwFile),
      MayBeNsLog = upgrade_if_equals(Config, DefaultConfig, {node, node(), ns_log}, filename,
                                     OldVersionNsLog),
  
      MayBePwFile ++ MayBeNsLog.
  
  delete_file(Filename) ->
      case file:delete(Filename) of
          {error,enoent} ->
              ok;
          ok ->
              ok;
          Err ->
              ?log_error("Cannot delete file ~p. Error: ~p", [Filename, Err]),
              Err
      end.
  
  rename_file(Source, Dest) ->
      case file:rename(Source, Dest) of
          {error,enoent} ->
              ok;
          ok ->
              ok;
          Err ->
              ?log_error("Cannot rename file from ~p to ~p. Error: ~p", [Source, Dest, Err]),
              Err
      end.
  
  do_upgrade_files_from_2_0_to_2_2_0(DataDir) ->
      OldDataDir = filename:join(DataDir, "data"),
  
      %% this file will be regenerated, so just delete it
      %% from the old location if it is there
      OldVersionPwFile = filename:join(OldDataDir, ?ISASL_PW),
      delete_file(OldVersionPwFile),
  
      OldVersionNsLog = filename:join(OldDataDir, ?NS_LOG),
      NsLog = filename:join(DataDir, ?NS_LOG),
  
      case filelib:is_file(NsLog) of
          true ->
              delete_file(OldVersionNsLog);
          false ->
              rename_file(OldVersionNsLog, NsLog)
      end.
  
  upgrade_1_7_1_to_1_7_2_test() ->
      DefaultCfg = [{rest, [{port, 8091}]},
                    {{node, node(), rest},
                     [{port, 8091},
                      {port_meta, global}]}],
  
      OldCfg0 = [{nodes_wanted, [node()]},
                 {{node, node(), rest}, [{port, 9000}]}],
      Ref0 = [{set, rest, [{port, 9000}]},
              {set, {node, node(), rest},
               [{port, 9000},
                {port_meta, global}]}],
  
      Res0 = do_upgrade_config_from_1_7_1_to_1_7_2([OldCfg0], DefaultCfg),
      ?assertEqual(lists:sort(Ref0), lists:sort(Res0)),
  
  
      OldCfg1 = [],
      Ref1 = [{set, rest, [{port, 8091}]},
              {set, {node, node(), rest},
               [{port, 8091},
                {port_meta, global}]}],
  
      Res1 = do_upgrade_config_from_1_7_1_to_1_7_2([OldCfg1], DefaultCfg),
      ?assertEqual(lists:sort(Ref1), lists:sort(Res1)),
  
      OldCfg2 = [{nodes_wanted, [node(), other_node]},
                 {{node, node(), rest}, [{port, 9000}]},
                 {{node, other_node, rest}, [{port, 9001}]}],
      Ref2 = [{set, rest, [{port, 8091}]},
              {set, {node, node(), rest},
               [{port, 9000}, {port_meta, local}]}],
  
      Res2 = do_upgrade_config_from_1_7_1_to_1_7_2([OldCfg2], DefaultCfg),
      ?assertEqual(lists:sort(Ref2), lists:sort(Res2)),
  
      OldCfg3 = [{nodes_wanted, [node(), other_node]},
                 {{node, node(), rest}, [{port, 9000}]},
                 {{node, other_node, rest}, [{port, 9000}]}],
      Ref3 = [{set, rest, [{port, 9000}]},
              {set, {node, node(), rest},
               [{port, 9000},
                {port_meta, global}]}],
  
      Res3 = do_upgrade_config_from_1_7_1_to_1_7_2([OldCfg3], DefaultCfg),
      ?assertEqual(lists:sort(Ref3), lists:sort(Res3)),
  
      OldCfg4 = [{nodes_wanted, [node(), other_node]},
                 {rest, [{port, 9000}]},
                 {{node, node(), rest}, [{port, 9000}]},
                 {{node, other_node, rest},
                  [{port, 9000}, {port_meta, global}]}],
      Ref4 = [{set, rest, [{port, 9000}]},
              {set, {node, node(), rest},
               [{port, 9000},
                {port_meta, global}]}],
  
      Res4 = do_upgrade_config_from_1_7_1_to_1_7_2([OldCfg4], DefaultCfg),
      ?assertEqual(lists:sort(Ref4), lists:sort(Res4)).
  
  upgrade_1_7_2_to_1_8_0_test() ->
      DefaultCfg = [{{node, node(), port_servers},
                     [{moxi, "/opt/couchbase/bin/moxi something"},
                      {memcached, "/opt/couchbase/bin/memcached something"}]}],
  
      OldCfg0 = [{nodes_wanted, [node()]},
                 {{node, node(), rest}, [{port, 9000}]},
                 {port_servers, [{moxi, "moxi something"},
                                 {memcached, "memcached something"}]}],
      Res0 = do_upgrade_config_from_1_7_2_to_1_8_0([OldCfg0], DefaultCfg),
      ?assertEqual([{set, {node, node(), port_servers},
                     [{moxi, "/opt/couchbase/bin/moxi something"},
                      {memcached, "/opt/couchbase/bin/memcached something"}]}],
                     lists:sort(Res0)),
  
      OldCfg1 = [{{node,node(),memcached},
                  [{dbdir,"/opt/membase/var/lib/membase/data"},
                   {port,11210},
                   {bucket_engine,"/opt/membase/lib/memcached/bucket_engine.so"},
                   {engines,
                    [{membase,
                      [{engine,"/opt/membase/lib/memcached/ep.so"},
                       {initfile,"/opt/membase/etc/membase/init.sql"},
                       {static_config_string,"foo"}]},
                     {memcached,
                      [{engine,"/opt/membase/lib/memcached/default_engine.so"},
                       {static_config_string,"bar"}]}]},
                   {verbosity,[]}]},
                 {otp,[{cookie,shouldnotbechanged}]},
                 {directory,"/opt/membase/var/lib/membase/config"},
                 {port_servers, [{moxi, "/opt/membase/bin/moxi something"},
                                 {memcached,
                                  "/opt/membase/bin/memcached something"}]}],
      RefCfg1 = [{set, {node,node(),memcached},
                  [{dbdir,"/opt/membase/var/lib/membase/data"},
                   {port,11210},
                   {bucket_engine,"/opt/couchbase/lib/memcached/bucket_engine.so"},
                   {engines,
                    [{membase,
                      [{engine,"/opt/couchbase/lib/memcached/ep.so"},
                       {initfile,"/opt/couchbase/etc/membase/init.sql"},
                       {static_config_string,"foo"}]},
                     {memcached,
                      [{engine,"/opt/couchbase/lib/memcached/default_engine.so"},
                       {static_config_string,"bar"}]}]},
                   {verbosity,[]}]},
                 {set, {node, node(), port_servers},
                  [{moxi, "/opt/couchbase/bin/moxi something"},
                   {memcached, "/opt/couchbase/bin/memcached something"}]}],
      Res1 = do_upgrade_config_from_1_7_2_to_1_8_0([OldCfg1], DefaultCfg),
      ?assertEqual(lists:sort(RefCfg1), lists:sort(Res1)),
  
      OldCfg2 = [{nodes_wanted, [node()]},
                 {{node, node(), rest}, [{port, 9000}]}],
      Res2 = do_upgrade_config_from_1_7_2_to_1_8_0([OldCfg2], DefaultCfg),
      ?assertEqual([{set, {node, node(), port_servers},
                     [{moxi, "/opt/couchbase/bin/moxi something"},
                      {memcached, "/opt/couchbase/bin/memcached something"}]}],
                   lists:sort(Res2)),
      ok.
  
  update_binaries_cfg_pathes_and_add_dedicated_port_test() ->
      DefaultCfg = [{{node, node(), port_servers}, default_port_servers},
                    {{node, node(), isasl}, default_isasl},
                    {{node, node(), ns_log}, ns_log_default},
                    {something_else, else_something},
                    {{node, node(), memcached},
                     [{dedicated_port, 1234},
                      {port, 1235},
                      {other_crap, crap},
                      {bucket_engine, new_bucketen_cfg},
                      {engines, new_engines_cfg}]}],
      OldCfg1 = [[{memcached, [{bucket_engine, old_bucketen_cfg},
                               {some_field, field_some},
                               {engines, old_engines_cfg}]}]],
      RV = lists:sort(update_binaries_cfg_pathes_and_add_dedicated_port(OldCfg1, DefaultCfg)),
      ?assertEqual(lists:sort(
                     [{set, {node, node(), port_servers}, default_port_servers},
                      {set, {node, node(), isasl}, default_isasl},
                      {set, {node, node(), ns_log}, ns_log_default},
                      {set, {node, node(), memcached}, [{dedicated_port, 1234},
                                                        {bucket_engine, new_bucketen_cfg},
                                                        {engines, new_engines_cfg},
                                                        {some_field, field_some}]}]),
                   RV),
      ok.
  
  upgrade_1_8_1_to_2_0_test() ->
      Cfg = [[{{node, node(), capi_port}, something},
              {remote_clusters, foobar},
              {{node, node(), memcached}, [{engines, old_engines},
                                           {something, something}]},
              {{node, node(), port_servers}, old_port_servers}]],
      DefaultCfg = [{{node, node(), capi_port}, somethingelse},
                    {remote_clusters, foobar_2},
                    {autocompaction, compaction_something},
                    {{node, node(), compaction_daemon}, compaction_daemon_settings},
                    {{node, node(), memcached},
                     [{log_path, log_path},
                      {log_prefix, log_prefix},
                      {log_generations, log_generations},
                      {log_rotation_period, log_rotation_period},
                      {engines, new_engines},
                      {something_else, something_else}]},
                    {{node, node(), port_servers}, new_port_servers}],
      Result = do_upgrade_config_from_1_8_1_to_2_0(Cfg, DefaultCfg),
      ?assertEqual([{set, autocompaction, compaction_something},
                    {set, {node, node(), compaction_daemon}, compaction_daemon_settings},
                    {set, {node, node(), memcached},
                     [{log_path, log_path},
                      {log_prefix, log_prefix},
                      {log_generations, log_generations},
                      {log_rotation_period, log_rotation_period},
                      {engines, new_engines},
                      {something, something}]},
                    {set, {node, node(), port_servers}, new_port_servers}],
                   Result),
      Cfg2 = [[{remote_clusters, foobar},
               {{node, node(), compaction_daemon}, compaction_daemon_existing},
               {{node, node(), memcached}, [{something, something}]}]],
      Result2 = do_upgrade_config_from_1_8_1_to_2_0(Cfg2, DefaultCfg),
      ?assertEqual([{set, {node, node(), capi_port}, somethingelse},
                    {set, autocompaction, compaction_something},
                    {set, {node, node(), memcached},
                     [{log_path, log_path},
                      {log_prefix, log_prefix},
                      {log_generations, log_generations},
                      {log_rotation_period, log_rotation_period},
                      {engines, new_engines},
                      {something, something}]},
                    {set, {node, node(), port_servers}, new_port_servers}],
                   Result2).
  
  upgrade_2_0_to_2_2_0_test() ->
      DataDir = "/data/n_0",
      OldDataDir = filename:join(DataDir, "data"),
      DefaultCfg = [{{node, node(), ns_log}, default_ns_log},
                    {{node, node(), isasl}, default_isasl}],
      Cfg1 = [[{{node, node(), ns_log}, [{filename, filename:join("/somewhere/else", ?NS_LOG)}]},
               {{node, node(), isasl}, [{path, filename:join("/somewhere/else", ?ISASL_PW)}]}
              ]],
      Result1 = do_upgrade_config_from_2_0_to_2_2_0(Cfg1, DefaultCfg, DataDir),
      ?assertEqual([], Result1),
  
      Cfg2 = [[{{node, node(), ns_log}, [{filename, filename:join(OldDataDir, ?NS_LOG)}]},
               {{node, node(), isasl}, [{path, filename:join(OldDataDir, ?ISASL_PW)}]}
              ]],
      Result2 = do_upgrade_config_from_2_0_to_2_2_0(Cfg2, DefaultCfg, DataDir),
      ?assertEqual([
                    {set, {node, node(), isasl}, default_isasl},
                    {set, {node, node(), ns_log}, default_ns_log}
                   ], Result2).
  
  upgrade_2_2_0_to_2_3_0_test() ->
      Key = {node, node(), memcached},
      Cfg = [[{Key,
             [{log_generations, 10},
              {log_cyclesize, 1024*1024*100}]}]],
      UpgradedCfg = upgrade_config_from_2_2_0_to_2_3_0(Cfg),
      ?assertMatch([{set, Key,
                     [{log_generations, 20},
                      {log_cyclesize, 1024*1024*10},
                      {admin_pass, _}]}], UpgradedCfg).
  
  upgrade_2_3_0_to_3_0_test() ->
      Cfg = [[{some_key, some_value},
  
negative.code:                 {[], [{log_path, Path}]}
          end,
  
      [{{node, node(), config_version}, get_current_version()},
       {directory, path_config:component_path(data, "config")},
       {{node, node(), is_enterprise}, IsEnterprise},
       {{node, node(), ldap_enabled}, LdapEnabled},
       {index_aware_rebalance_disabled, false},
  