11#pragma once
22
3+ #include < Common/Logger.h>
34#include < Core/UUID.h>
45#include < unordered_map>
5- #include < unordered_set>
66
77
88namespace DB
@@ -12,6 +12,7 @@ enum class AccessEntityType : uint8_t;
1212struct IAccessEntity ;
1313using AccessEntityPtr = std::shared_ptr<const IAccessEntity>;
1414class AccessRightsElements ;
15+ class IAccessStorage ;
1516class IBackup ;
1617using BackupPtr = std::shared_ptr<const IBackup>;
1718class IBackupEntry ;
@@ -20,37 +21,118 @@ struct RestoreSettings;
2021enum class RestoreAccessCreationMode : uint8_t ;
2122
2223
23- // / Makes a backup of access entities of a specified type .
24- std::pair<String, BackupEntryPtr> makeBackupEntryForAccess (
25- const std::vector<std::pair< UUID, AccessEntityPtr>> & access_entities ,
26- const String & data_path_in_backup ,
27- size_t counter ,
28- const AccessControl & access_control );
24+ // / Makes a backup entry for of a set of access entities .
25+ std::pair<String, BackupEntryPtr> makeBackupEntryForAccessEntities (
26+ const std::vector<UUID> & entities_ids ,
27+ const std::unordered_map<UUID, AccessEntityPtr> & all_entities ,
28+ bool write_dependents ,
29+ const String & data_path_in_backup );
2930
31+ struct AccessEntitiesToRestore
32+ {
33+ // / Access entities loaded from backup with new randomly generated UUIDs.
34+ std::vector<std::pair<UUID /* new_id */ , AccessEntityPtr /* new_entity */ >> new_entities;
35+
36+ // / Dependents are access entities which exist already and they should be updated after restoring.
37+ // / For example, if there were a role granted to a user: `CREATE USER user1; CREATE ROLE role1; GRANT role1 TO user1`,
38+ // / and we're restoring only role `role1` because user `user1` already exists,
39+ // / then user `user1` should be modified after restoring role `role1` to add this grant `GRANT role1 TO user1`.
40+ struct Dependent
41+ {
42+ // / UUID of an existing access entities.
43+ UUID existing_id;
44+
45+ // / Source access entity from backup to copy dependencies from.
46+ AccessEntityPtr source;
47+ };
48+ using Dependents = std::vector<Dependent>;
49+ Dependents dependents;
50+ };
3051
3152// / Restores access entities from a backup.
53+ void restoreAccessEntitiesFromBackup (
54+ IAccessStorage & access_storage,
55+ const AccessEntitiesToRestore & entities_to_restore,
56+ const RestoreSettings & restore_settings);
57+
58+
59+ // / Loads access entities from a backup and prepares them for insertion into an access storage.
3260class AccessRestorerFromBackup
3361{
3462public:
3563 AccessRestorerFromBackup (const BackupPtr & backup_, const RestoreSettings & restore_settings_);
3664 ~AccessRestorerFromBackup ();
3765
3866 // / Adds a data path to loads access entities from.
39- void addDataPath (const String & data_path);
67+ void addDataPath (const String & data_path_in_backup);
68+
69+ // / Loads access entities from the backup.
70+ void loadFromBackup ();
4071
4172 // / Checks that the current user can do restoring.
73+ // / Function loadFromBackup() must be called before that.
4274 AccessRightsElements getRequiredAccess () const ;
4375
44- // / Inserts all access entities loaded from all the paths added by addDataPath().
45- std::vector<std::pair<UUID, AccessEntityPtr>> getAccessEntities (const AccessControl & access_control) const ;
76+ // / Generates random IDs for access entities we're restoring to insert them into an access storage;
77+ // / and finds IDs of existing access entities which are used as dependencies.
78+ void generateRandomIDsAndResolveDependencies (const AccessControl & access_control);
79+
80+ // / Returns access entities loaded from backup and prepared for insertion into an access storage.
81+ // / Both functions loadFromBackup() and generateRandomIDsAndResolveDependencies() must be called before that.
82+ AccessEntitiesToRestore getEntitiesToRestore (const String & data_path_in_backup) const ;
4683
4784private:
48- BackupPtr backup;
49- RestoreAccessCreationMode creation_mode;
50- bool allow_unresolved_dependencies = false ;
51- std::vector<std::pair<UUID, AccessEntityPtr>> entities;
52- std::unordered_map<UUID, std::pair<String, AccessEntityType>> dependencies;
53- std::unordered_set<String> data_paths;
85+ const BackupPtr backup;
86+ const RestoreAccessCreationMode creation_mode;
87+ const bool skip_unresolved_dependencies;
88+ const bool update_dependents;
89+ const LoggerPtr log;
90+
91+ // / Whether loadFromBackup() finished.
92+ bool loaded = false ;
93+
94+ // / Whether generateRandomIDsAndResolveDependencies() finished.
95+ bool ids_assigned = false ;
96+
97+ Strings data_paths_in_backup;
98+ String data_path_with_entities_to_restore;
99+
100+ // / Information about an access entity loaded from the backup.
101+ struct EntityInfo
102+ {
103+ UUID id;
104+ String name;
105+ AccessEntityType type;
106+
107+ AccessEntityPtr entity = nullptr ; // / Can be nullptr if `restore=false`.
108+
109+ // / Index in `data_paths_in_backup`.
110+ size_t data_path_index = 0 ;
111+
112+ // / Whether we're going to restore this entity.
113+ // / For example,
114+ // / in case of `RESTORE TABLE system.roles` this flag is true for all the roles loaded from the backup, and
115+ // / in case of `RESTORE ALL` this flag is always true.
116+ bool restore = false ;
117+
118+ // / Whether this entity info was added as a dependency of another entity which we're going to restore.
119+ // / For example, if we're going to restore the following user: `CREATE USER user1 DEFAULT ROLE role1, role2 SETTINGS PROFILE profile1, profile2`
120+ // / then `restore=true` for `user1` and `is_dependency=true` for `role1`, `role2`, `profile1`, `profile2`.
121+ // / Flags `restore` and `is_dependency` both can be set at the same time.
122+ bool is_dependency = false ;
123+
124+ // / Whether this entity info is a dependent of another entity which we're going to restore.
125+ // / For example, if we're going to restore role `role1` and there is also the following user stored in the backup:
126+ // / `CREATE USER user1 DEFAULT ROLE role1`, then `is_dependent=true` for `user1`.
127+ // / This flags is set by generateRandomIDsAndResolveDependencies().
128+ bool is_dependent = false ;
129+
130+ // / New UUID for this entity - either randomly generated or copied from an existing entity.
131+ // / This UUID is assigned by generateRandomIDsAndResolveDependencies().
132+ std::optional<UUID> new_id = std::nullopt ;
133+ };
134+
135+ std::unordered_map<UUID, EntityInfo> entity_infos;
54136};
55137
56138}
0 commit comments