MCPcopy Create free account
hub / github.com/ClickHouse/ClickHouse / updateDatabaseName

Method updateDatabaseName

src/Interpreters/DatabaseCatalog.cpp:732–775  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

730}
731
732void DatabaseCatalog::updateDatabaseName(const String & old_name, const String & new_name, const Strings & tables_in_database)
733{
734 std::lock_guard lock{databases_mutex};
735 chassert(!databases.contains(new_name));
736 auto it = databases.find(old_name);
737 chassert(it != databases.end());
738 auto db = it->second;
739 databases.erase(it);
740 databases.emplace(new_name, db);
741
742 auto local_it = databases_without_remote.find(old_name);
743 if (local_it != databases_without_remote.end())
744 {
745 databases_without_remote.erase(local_it);
746 databases_without_remote.emplace(new_name, db);
747 }
748
749 for (const auto & table_name : tables_in_database)
750 {
751 auto removed_ref_deps = referential_dependencies.removeDependencies(StorageID{old_name, table_name}, /* remove_isolated_tables= */ true);
752 auto removed_loading_deps = loading_dependencies.removeDependencies(StorageID{old_name, table_name}, /* remove_isolated_tables= */ true);
753 referential_dependencies.addDependencies(StorageID{new_name, table_name}, removed_ref_deps);
754 loading_dependencies.addDependencies(StorageID{new_name, table_name}, removed_loading_deps);
755
756 /// `view_dependencies` is rewired in both directions: the table being renamed
757 /// may be a materialized view (incoming edges from its source) and/or a source
758 /// of materialized views (outgoing edges to its dependent MVs). Both sides must
759 /// be re-keyed under `new_name`, otherwise lookups by the new name fail and
760 /// inserts into the renamed source no longer reach its MVs.
761 auto tables_from = view_dependencies.getDependents(StorageID{old_name, table_name});
762 for (const auto & the_table_from : tables_from)
763 {
764 view_dependencies.removeDependency(the_table_from, StorageID{old_name, table_name}, /* remove_isolated_tables= */ true);
765 view_dependencies.addDependency(the_table_from, StorageID{new_name, table_name});
766 }
767
768 auto views_from = view_dependencies.getDependencies(StorageID{old_name, table_name});
769 for (const auto & view : views_from)
770 {
771 view_dependencies.removeDependency(StorageID{old_name, table_name}, view, /* remove_isolated_tables= */ true);
772 view_dependencies.addDependency(StorageID{new_name, table_name}, view);
773 }
774 }
775}
776
777void DatabaseCatalog::updateMetadataFile(const String & database_name, const ASTPtr & create_query)
778{

Callers 1

renameDatabaseMethod · 0.80

Calls 11

removeDependencyMethod · 0.80
containsMethod · 0.45
findMethod · 0.45
endMethod · 0.45
eraseMethod · 0.45
emplaceMethod · 0.45
removeDependenciesMethod · 0.45
addDependenciesMethod · 0.45
getDependentsMethod · 0.45
addDependencyMethod · 0.45
getDependenciesMethod · 0.45

Tested by

no test coverage detected