| 304 | } |
| 305 | |
| 306 | void DatabaseAtomic::commitCreateTable(const ASTCreateQuery & query, const StoragePtr & table, |
| 307 | const String & table_metadata_tmp_path, const String & table_metadata_path, |
| 308 | ContextPtr query_context) |
| 309 | { |
| 310 | DetachedTables not_in_use; |
| 311 | auto table_data_path = getTableDataPath(query); |
| 312 | bool locked_uuid = false; |
| 313 | try |
| 314 | { |
| 315 | std::unique_lock lock{mutex}; |
| 316 | if (query.database != database_name) |
| 317 | throw Exception(ErrorCodes::UNKNOWN_DATABASE, "Database was renamed to `{}`, cannot create table in `{}`", |
| 318 | database_name, query.database); |
| 319 | /// Do some checks before renaming file from .tmp to .sql |
| 320 | not_in_use = cleanupDetachedTables(); |
| 321 | assertDetachedTableNotInUse(query.uuid); |
| 322 | /// We will get en exception if some table with the same UUID exists (even if it's detached table or table from another database) |
| 323 | DatabaseCatalog::instance().addUUIDMapping(query.uuid); |
| 324 | locked_uuid = true; |
| 325 | |
| 326 | auto txn = query_context->getZooKeeperMetadataTransaction(); |
| 327 | if (txn && !query_context->isInternalSubquery()) |
| 328 | txn->commit(); /// Commit point (a sort of) for Replicated database |
| 329 | |
| 330 | /// NOTE: replica will be lost if server crashes before the following renameNoReplace(...) |
| 331 | /// TODO better detection and recovery |
| 332 | |
| 333 | /// It throws if `table_metadata_path` already exists (it's possible if table was detached) |
| 334 | renameNoReplace(table_metadata_tmp_path, table_metadata_path); /// Commit point (a sort of) |
| 335 | attachTableUnlocked(query.table, table, lock); /// Should never throw |
| 336 | table_name_to_path.emplace(query.table, table_data_path); |
| 337 | } |
| 338 | catch (...) |
| 339 | { |
| 340 | fs::remove(table_metadata_tmp_path); |
| 341 | if (locked_uuid) |
| 342 | DatabaseCatalog::instance().removeUUIDMappingFinally(query.uuid); |
| 343 | throw; |
| 344 | } |
| 345 | if (table->storesDataOnDisk()) |
| 346 | tryCreateSymlink(query.table, table_data_path); |
| 347 | } |
| 348 | |
| 349 | void DatabaseAtomic::commitAlterTable(const StorageID & table_id, const String & table_metadata_tmp_path, const String & table_metadata_path, |
| 350 | const String & /*statement*/, ContextPtr query_context) |
nothing calls this directly
no test coverage detected