فهرست منبع

feat: add `table_set_name` callback to `emqx_db_backup` behaviour

This allows us to name sets of mnesia tables to be exported.
Thales Macedo Garitezi 1 سال پیش
والد
کامیت
9ba9a3a196

+ 8 - 0
apps/emqx/src/bhvrs/emqx_db_backup.erl

@@ -16,12 +16,16 @@
 
 -module(emqx_db_backup).
 
+-export([table_set_name/1]).
+
 -type traverse_break_reason() :: over | migrate.
 
 -type opts() :: #{print_fun => fun((io:format(), [term()]) -> ok)}.
 
 -callback backup_tables() -> [mria:table()].
 
+-callback table_set_name() -> binary().
+
 %% validate the backup
 %% return `ok` to traverse the next item
 %% return `{ok, over}` to finish the traverse
@@ -39,3 +43,7 @@
 -optional_callbacks([validate_mnesia_backup/1, migrate_mnesia_backup/1, on_backup_table_imported/2]).
 
 -export_type([traverse_break_reason/0]).
+
+-spec table_set_name(module()) -> binary().
+table_set_name(Mod) ->
+    Mod:table_set_name().

+ 3 - 1
apps/emqx/src/emqx_banned.erl

@@ -57,7 +57,7 @@
     code_change/3
 ]).
 
--export([backup_tables/0]).
+-export([backup_tables/0, table_set_name/0]).
 
 %% Internal exports (RPC)
 -export([
@@ -96,6 +96,8 @@ create_tables() ->
 %%--------------------------------------------------------------------
 backup_tables() -> tables().
 
+table_set_name() -> <<"banned">>.
+
 -spec tables() -> [atom()].
 tables() -> [?BANNED_RULE_TAB, ?BANNED_INDIVIDUAL_TAB].
 

+ 3 - 1
apps/emqx_auth_mnesia/src/emqx_authn_mnesia.erl

@@ -57,7 +57,7 @@
 
 -export([init_tables/0]).
 
--export([backup_tables/0]).
+-export([backup_tables/0, table_set_name/0]).
 
 -type user_group() :: binary().
 -type user_id() :: binary().
@@ -104,6 +104,8 @@ init_tables() ->
 
 backup_tables() -> [?TAB].
 
+table_set_name() -> <<"builtin_authn">>.
+
 %%------------------------------------------------------------------------------
 %% APIs
 %%------------------------------------------------------------------------------

+ 3 - 1
apps/emqx_auth_mnesia/src/emqx_authn_scram_mnesia.erl

@@ -46,7 +46,7 @@
     group_match_spec/1
 ]).
 
--export([backup_tables/0]).
+-export([backup_tables/0, table_set_name/0]).
 
 %% Internal exports (RPC)
 -export([
@@ -104,6 +104,8 @@ init_tables() ->
 
 backup_tables() -> [?TAB].
 
+table_set_name() -> <<"builtin_authn_scram">>.
+
 %%------------------------------------------------------------------------------
 %% APIs
 %%------------------------------------------------------------------------------

+ 3 - 1
apps/emqx_auth_mnesia/src/emqx_authz_mnesia.erl

@@ -68,7 +68,7 @@
     record_count/0
 ]).
 
--export([backup_tables/0]).
+-export([backup_tables/0, table_set_name/0]).
 
 -ifdef(TEST).
 -compile(export_all).
@@ -131,6 +131,8 @@ authorize(
 
 backup_tables() -> [?ACL_TABLE].
 
+table_set_name() -> <<"builtin_authz">>.
+
 %%--------------------------------------------------------------------
 %% Management API
 %%--------------------------------------------------------------------

+ 3 - 1
apps/emqx_dashboard/src/emqx_dashboard_admin.erl

@@ -55,7 +55,7 @@
 
 -export([role/1]).
 
--export([backup_tables/0]).
+-export([backup_tables/0, table_set_name/0]).
 
 -if(?EMQX_RELEASE_EDITION == ee).
 -export([add_sso_user/4, lookup_user/2]).
@@ -91,6 +91,8 @@ create_tables() ->
 
 backup_tables() -> [?ADMIN].
 
+table_set_name() -> <<"dashboard_users">>.
+
 %%--------------------------------------------------------------------
 %% bootstrap API
 %%--------------------------------------------------------------------

+ 3 - 1
apps/emqx_management/src/emqx_mgmt_auth.erl

@@ -39,7 +39,7 @@
 -export([authorize/4]).
 -export([post_config_update/5]).
 
--export([backup_tables/0, validate_mnesia_backup/1]).
+-export([backup_tables/0, table_set_name/0, validate_mnesia_backup/1]).
 
 %% Internal exports (RPC)
 -export([
@@ -88,6 +88,8 @@ create_tables() ->
 
 backup_tables() -> [?APP].
 
+table_set_name() -> <<"api_keys">>.
+
 validate_mnesia_backup({schema, _Tab, CreateList} = Schema) ->
     case emqx_mgmt_data_backup:default_validate_mnesia_backup(Schema) of
         ok ->

+ 27 - 0
apps/emqx_management/src/emqx_mgmt_data_backup.erl

@@ -1034,3 +1034,30 @@ cloud_export_mnesia_table_filter(TableName) ->
             emqx_banned_rules
         ]
     ).
+
+-ifdef(TEST).
+-include_lib("eunit/include/eunit.hrl").
+
+%% Different implementations of `emqx_db_backup' behaviour should have distinct names.
+ensure_no_table_set_name_clash_test() ->
+    UmbrellaApps = emqx_machine_boot:reboot_apps(),
+    lists:foreach(fun(App) -> application:load(App) end, UmbrellaApps),
+    Mods = modules_with_mnesia_tabs_to_backup(),
+    Names = lists:sort(
+        lists:map(
+            fun(Mod) ->
+                try
+                    emqx_db_backup:table_set_name(Mod)
+                catch
+                    error:undef ->
+                        ct:fail("module ~s is missing an implementation of `table_set_name'", [Mod])
+                end
+            end,
+            Mods
+        )
+    ),
+    UniqueNames = lists:usort(Names),
+    Duplicates = Names -- UniqueNames,
+    ?assertEqual([], Duplicates),
+    ok.
+-endif.

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

@@ -2,7 +2,7 @@
 {application, emqx_psk, [
     {description, "EMQX PSK"},
     % strict semver, bump manually!
-    {vsn, "5.0.7"},
+    {vsn, "5.0.8"},
     {modules, []},
     {registered, [emqx_psk_sup]},
     {applications, [kernel, stdlib]},

+ 4 - 1
apps/emqx_psk/src/emqx_psk.erl

@@ -55,7 +55,8 @@
 %% Data backup
 -export([
     import_config/1,
-    backup_tables/0
+    backup_tables/0,
+    table_set_name/0
 ]).
 
 -record(psk_entry, {
@@ -96,6 +97,8 @@ create_tables() ->
 
 backup_tables() -> [?TAB].
 
+table_set_name() -> <<"psk">>.
+
 %%------------------------------------------------------------------------------
 %% APIs
 %%------------------------------------------------------------------------------

+ 3 - 0
apps/emqx_retainer/src/emqx_retainer_mnesia.erl

@@ -60,6 +60,7 @@
 
 -export([
     backup_tables/0,
+    table_set_name/0,
     on_backup_table_imported/2
 ]).
 
@@ -90,6 +91,8 @@ topics() ->
 backup_tables() ->
     [?TAB_MESSAGE || is_enabled()].
 
+table_set_name() -> <<"builtin_retainer">>.
+
 on_backup_table_imported(?TAB_MESSAGE, Opts) ->
     case is_enabled() of
         true ->