| 775 | } |
| 776 | |
| 777 | void DatabaseCatalog::updateMetadataFile(const String & database_name, const ASTPtr & create_query) |
| 778 | { |
| 779 | std::lock_guard lock{databases_mutex}; |
| 780 | if (!create_query) |
| 781 | throw Exception(ErrorCodes::THERE_IS_NO_QUERY, "Unable to show the create query of database {}", backQuoteIfNeed(database_name)); |
| 782 | |
| 783 | auto * ast_create_query = create_query->as<ASTCreateQuery>(); |
| 784 | ast_create_query->attach = true; |
| 785 | |
| 786 | WriteBufferFromOwnString statement_buf; |
| 787 | IAST::FormatSettings format_settings(/*one_line=*/false); |
| 788 | ast_create_query->format(statement_buf, format_settings); |
| 789 | writeChar('\n', statement_buf); |
| 790 | String statement = statement_buf.str(); |
| 791 | |
| 792 | auto metadata_file_path = getMetadataFilePath(database_name); |
| 793 | auto metadata_tmp_file_path = getMetadataTmpFilePath(database_name); |
| 794 | auto default_db_disk = getContext()->getDatabaseDisk(); |
| 795 | |
| 796 | writeMetadataFile( |
| 797 | default_db_disk, |
| 798 | /*file_path=*/metadata_tmp_file_path, |
| 799 | /*content=*/statement, |
| 800 | getContext()->getSettingsRef()[Setting::fsync_metadata]); |
| 801 | |
| 802 | try |
| 803 | { |
| 804 | /// rename atomically replaces the old file with the new one. |
| 805 | default_db_disk->replaceFile(metadata_tmp_file_path, metadata_file_path); |
| 806 | } |
| 807 | catch (...) |
| 808 | { |
| 809 | default_db_disk->removeFileIfExists(metadata_tmp_file_path); |
| 810 | throw; |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | DatabasePtr DatabaseCatalog::getDatabase(std::string_view database_name) const |
| 815 | { |
no test coverage detected