| 393 | } |
| 394 | |
| 395 | void DatabaseAtomic::commitCreateTable(const ASTCreateQuery & query, const StoragePtr & table, |
| 396 | const String & table_metadata_tmp_path, const String & table_metadata_path, |
| 397 | ContextPtr query_context) |
| 398 | { |
| 399 | auto db_disk = getDisk(); |
| 400 | |
| 401 | createDirectories(); |
| 402 | DetachedTables not_in_use; |
| 403 | auto table_data_path = getTableDataPath(query); |
| 404 | try |
| 405 | { |
| 406 | std::lock_guard lock{mutex}; |
| 407 | if (query.getDatabase() != database_name) |
| 408 | throw Exception(ErrorCodes::UNKNOWN_DATABASE, "Database was renamed to `{}`, cannot create table in `{}`", |
| 409 | database_name, query.getDatabase()); |
| 410 | /// Do some checks before renaming file from .tmp to .sql |
| 411 | not_in_use = cleanupDetachedTables(); |
| 412 | assertDetachedTableNotInUse(query.uuid); |
| 413 | chassert(DatabaseCatalog::instance().hasUUIDMapping(query.uuid)); |
| 414 | |
| 415 | auto txn = query_context->getZooKeeperMetadataTransaction(); |
| 416 | if (txn && !query_context->isInternalSubquery()) |
| 417 | txn->commit(); /// Commit point (a sort of) for Replicated database |
| 418 | |
| 419 | /// NOTE: replica will be lost if server crashes before the following renameNoReplace(...) |
| 420 | /// TODO better detection and recovery |
| 421 | |
| 422 | /// It throws if `table_metadata_path` already exists (it's possible if table was detached) |
| 423 | db_disk->moveFile(table_metadata_tmp_path, table_metadata_path); /// Commit point (a sort of) |
| 424 | attachTableUnlocked(query.getTable(), table); /// Should never throw |
| 425 | table_name_to_path.emplace(query.getTable(), table_data_path); |
| 426 | } |
| 427 | catch (...) |
| 428 | { |
| 429 | db_disk->removeFileIfExists(table_metadata_tmp_path); |
| 430 | throw; |
| 431 | } |
| 432 | if (table->storesDataOnDisk()) |
| 433 | tryCreateSymlink(table); |
| 434 | } |
| 435 | |
| 436 | void DatabaseAtomic::commitAlterTable(const StorageID & table_id, const String & table_metadata_tmp_path, const String & table_metadata_path, |
| 437 | const String & /*statement*/, ContextPtr query_context) |
nothing calls this directly
no test coverage detected