Просмотр исходного кода

refactor: introduce extra macro to be overridden in different repos

The idea is to only use this new macro for introducing extra schema modules that are
idiosyncratic to the repo itself, thus avoiding missing some schema modules when
overridding `?EE_SCHEMA_MODULES` as is done today.
Thales Macedo Garitezi 1 год назад
Родитель
Сommit
d4b464fb62

+ 1 - 1
apps/emqx_enterprise/src/emqx_enterprise.app.src

@@ -1,6 +1,6 @@
 {application, emqx_enterprise, [
     {description, "EMQX Enterprise Edition"},
-    {vsn, "0.2.3"},
+    {vsn, "0.2.4"},
     {registered, []},
     {applications, [
         kernel,

+ 4 - 2
apps/emqx_enterprise/src/emqx_enterprise_schema.erl

@@ -13,6 +13,8 @@
 -export([upgrade_raw_conf/1]).
 -export([injected_fields/0]).
 
+%% DO NOT REMOVE.  This is used in other repositories.
+-define(EXTRA_SCHEMA_MODULES, []).
 -define(EE_SCHEMA_MODULES, [
     emqx_license_schema,
     emqx_schema_registry_schema,
@@ -108,7 +110,7 @@ fields("log_audit_handler") ->
             )}
     ] ++ CommonConfs1;
 fields(Name) ->
-    ee_delegate(fields, ?EE_SCHEMA_MODULES, Name).
+    ee_delegate(fields, ?EXTRA_SCHEMA_MODULES ++ ?EE_SCHEMA_MODULES, Name).
 
 translations() ->
     emqx_conf_schema:translations().
@@ -123,7 +125,7 @@ translation(Name) ->
 desc("log_audit_handler") ->
     ?DESC(emqx_conf_schema, "desc_audit_log_handler");
 desc(Name) ->
-    ee_delegate(desc, ?EE_SCHEMA_MODULES, Name).
+    ee_delegate(desc, ?EXTRA_SCHEMA_MODULES ++ ?EE_SCHEMA_MODULES, Name).
 
 validations() ->
     emqx_conf_schema:validations() ++ emqx_license_schema:validations().

+ 45 - 0
apps/emqx_enterprise/test/emqx_enterprise_schema_tests.erl

@@ -51,3 +51,48 @@ ensure_acl_conf() ->
         true -> ok;
         false -> file:write_file(File, <<"">>)
     end.
+
+injected_roots_test() ->
+    ExpectedRoots = lists:usort(ee_schema_roots()),
+    InjectedRoots = lists:usort([Root || {Root, _Sc} <- emqx_enterprise_schema:roots()]),
+    MissingRoots = ExpectedRoots -- InjectedRoots,
+    ?assertEqual([], MissingRoots, #{
+        expected_roots => ExpectedRoots,
+        injected_roots => InjectedRoots,
+        hint =>
+            <<
+                "maybe there's a missing schema module to be added to"
+                " `EE_SCHEMA_MODULES' or `EXTRA_SCHEMA_MODULES' in"
+                " `emqx_enterprise_schema'"
+            >>
+    }),
+    ok.
+
+ee_schema_roots() ->
+    lists:foldl(
+        fun(Filepath, Acc) ->
+            ["apps", App | _] = filename:split(Filepath),
+            Mod = module(Filepath),
+            case is_ee(App) andalso has_roots(Mod) of
+                true ->
+                    Roots = [Root || {Root, _Sc} <- Mod:roots()],
+                    Roots ++ Acc;
+                false ->
+                    Acc
+            end
+        end,
+        [],
+        filelib:wildcard("apps/*/src/**/*_schema.erl")
+    ).
+
+is_ee(App) ->
+    filelib:is_file(filename:join(["apps", App, "BSL.txt"])).
+
+module(Filepath) ->
+    ModStr = filename:basename(Filepath, ".erl"),
+    list_to_atom(ModStr).
+
+has_roots(Mod) ->
+    _ = Mod:module_info(),
+    erlang:function_exported(Mod, roots, 0) andalso
+        Mod:roots() =/= [].

+ 1 - 1
apps/emqx_oracle/src/emqx_oracle.app.src

@@ -1,6 +1,6 @@
 {application, emqx_oracle, [
     {description, "EMQX Enterprise Oracle Database Connector"},
-    {vsn, "0.2.3"},
+    {vsn, "0.2.4"},
     {registered, []},
     {applications, [
         kernel,

+ 1 - 1
apps/emqx_oracle/src/emqx_oracle_schema.erl

@@ -19,7 +19,7 @@
 namespace() -> oracle.
 
 roots() ->
-    [{config, #{type => hoconsc:ref(?REF_MODULE, config)}}].
+    [].
 
 fields(config) ->
     Fields =