| 1382 | |
| 1383 | |
| 1384 | static void |
| 1385 | ShardCollectionCore(ShardCollectionArgs *args) |
| 1386 | { |
| 1387 | Datum databaseDatum = CStringGetTextDatum(args->databaseName); |
| 1388 | Datum collectionDatum = CStringGetTextDatum(args->collectionName); |
| 1389 | |
| 1390 | /* Allow for checking if reference metadata tables are correctly handled */ |
| 1391 | EnsureMetadataTableReplicated("collections"); |
| 1392 | |
| 1393 | MongoCollection *collection = GetMongoCollectionByNameDatum( |
| 1394 | databaseDatum, collectionDatum, AccessShareLock); |
| 1395 | |
| 1396 | if (collection == NULL) |
| 1397 | { |
| 1398 | if (args->shardingMode != ShardCollectionMode_Shard) |
| 1399 | { |
| 1400 | ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_NAMESPACENOTSHARDED), |
| 1401 | errmsg("Collection %s.%s is not sharded", |
| 1402 | args->databaseName, args->collectionName), |
| 1403 | errdetail_log( |
| 1404 | "Unable to perform re/unshard operation on a collection that is missing: %s.%s", |
| 1405 | args->databaseName, args->collectionName))); |
| 1406 | } |
| 1407 | |
| 1408 | CreateCollection(databaseDatum, collectionDatum); |
| 1409 | collection = GetMongoCollectionByNameDatum( |
| 1410 | databaseDatum, collectionDatum, AccessShareLock); |
| 1411 | |
| 1412 | if (collection == NULL) |
| 1413 | { |
| 1414 | ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_INTERNALERROR), |
| 1415 | errmsg("Failed to create collection %s.%s for sharding", |
| 1416 | args->databaseName, args->collectionName), |
| 1417 | errdetail_log( |
| 1418 | "Collection was not found after implicit create for sharding for %s.%s", |
| 1419 | args->databaseName, args->collectionName))); |
| 1420 | } |
| 1421 | } |
| 1422 | |
| 1423 | if (collection->shardKey == NULL && |
| 1424 | args->shardingMode != ShardCollectionMode_Shard) |
| 1425 | { |
| 1426 | ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_NAMESPACENOTSHARDED), |
| 1427 | errmsg("Collection %s.%s is not sharded", |
| 1428 | args->databaseName, args->collectionName), |
| 1429 | errdetail_log( |
| 1430 | "Can not re/unshard collection that is not sharded: %s.%s", |
| 1431 | args->databaseName, args->collectionName))); |
| 1432 | } |
| 1433 | |
| 1434 | if (!args->forceRedistribution && |
| 1435 | collection->shardKey != NULL && PgbsonEquals(collection->shardKey, |
| 1436 | args->shardKeyDefinition)) |
| 1437 | { |
| 1438 | ereport(NOTICE, (errmsg( |
| 1439 | "Skipping Sharding for collection %s.%s as the same options were passed in.", |
| 1440 | args->databaseName, args->collectionName))); |
| 1441 | return; |
no test coverage detected