summaryrefslogtreecommitdiff
path: root/jstests/core/rename_collection_view.js
diff options
context:
space:
mode:
authorLucas de Castro Borges <lucas@gnuabordo.com.br>2025-02-14 14:26:38 -0300
committerLucas de Castro Borges <lucas@gnuabordo.com.br>2025-02-14 14:26:38 -0300
commit294bc6ecabf14c09c9bc8644704921dcf97cb44e (patch)
tree279b1e0bab53901a1647ac63c1c724f0f789a663 /jstests/core/rename_collection_view.js
parent70be7c27a251621187a1de533462ae2bb1e3bd39 (diff)
parent1e917fd798aa25b7066d4b414b51184f13d5a092 (diff)
Update upstream source from tag 'upstream/6.0.10'debian/6.0.10-1
Update to upstream version '6.0.10' with Debian dir 2d176fa254eee97b139f712fec5709641335a8c3
Diffstat (limited to 'jstests/core/rename_collection_view.js')
-rw-r--r--jstests/core/rename_collection_view.js92
1 files changed, 92 insertions, 0 deletions
diff --git a/jstests/core/rename_collection_view.js b/jstests/core/rename_collection_view.js
new file mode 100644
index 00000000000..c22599f5d3f
--- /dev/null
+++ b/jstests/core/rename_collection_view.js
@@ -0,0 +1,92 @@
+/**
+ * Basic test around rename collection involving views
+ */
+
+(function() {
+"use strict";
+
+const collNamePrefix = jsTestName() + "_collection";
+const viewNamePrefix = jsTestName() + "_view";
+let collCounter = 0;
+let viewCounter = 0;
+
+function getNewCollName() {
+ return collNamePrefix + collCounter++;
+}
+
+function getNewColl() {
+ let coll = db[getNewCollName()];
+ coll.drop();
+ assert.commandWorked(db[coll.getName()].insert({_id: 1}));
+ return coll;
+}
+
+/**
+ * Drops a view and then recreates against an underlying collection. Handle the case for those
+ * suites where the test runs multiple times against the same cluster/replicaset instance and view
+ * already exists
+ */
+function getNewView(collName) {
+ // new view with empty filter
+ const viewName = viewNamePrefix + viewCounter++;
+ db.runCommand({drop: viewName});
+ assert.commandWorked(db.createView(viewName, collName, []));
+ return db[viewName];
+}
+
+jsTest.log(
+ "Rename existing view should fail with CommandNotSupportedOnView indipendently on the target type (view or collection)");
+{
+ const srcColl = getNewColl();
+ const srcView = getNewView(srcColl.getName());
+ const dstColl = getNewColl();
+ const dstView = getNewView(dstColl.getName());
+
+ assert.commandFailedWithCode(
+ db.adminCommand({renameCollection: srcView.getFullName(), to: dstColl.getFullName()}),
+ ErrorCodes.CommandNotSupportedOnView,
+ "rename view to existing collection should fail with CommandNotSupportedOnView");
+
+ assert.commandFailedWithCode(
+ db.adminCommand(
+ {renameCollection: srcView.getFullName(), to: dstColl.getFullName(), dropTarget: true}),
+ ErrorCodes.CommandNotSupportedOnView,
+ "rename view to existing collection should fail with CommandNotSupportedOnView even if dropTarget is true");
+
+ assert.commandFailedWithCode(
+ db.adminCommand({renameCollection: srcView.getFullName(), to: dstView.getFullName()}),
+ ErrorCodes.CommandNotSupportedOnView,
+ "rename view to existing view should fail with CommandNotSupportedOnView");
+
+ assert.commandFailedWithCode(
+ db.adminCommand(
+ {renameCollection: srcView.getFullName(), to: dstView.getFullName(), dropTarget: true}),
+ ErrorCodes.CommandNotSupportedOnView,
+ "rename view to existing view should fail with CommandNotSupportedOnView even if dropTarget is true");
+}
+
+jsTest.log("Rename coll to existing view should fail with NamespaceExists");
+{
+ const srcColl = getNewColl();
+ const dstColl = getNewColl();
+
+ const dstView = getNewView(dstColl.getName());
+
+ assert.commandFailedWithCode(
+ db.adminCommand({renameCollection: srcColl.getFullName(), to: dstView.getFullName()}),
+ [
+ ErrorCodes.NamespaceExists,
+ ErrorCodes.CommandNotSupportedOnView, // TODO SERVER-78721 remove this error code
+ ],
+ "rename collection to existing view should fail with NamespaceExists");
+
+ assert.commandFailedWithCode(
+ db.adminCommand(
+ {renameCollection: srcColl.getFullName(), to: dstView.getFullName(), dropTarget: true}),
+ [
+ ErrorCodes.NamespaceExists,
+ ErrorCodes.CommandNotSupportedOnView, // TODO SERVER-78721 remove this error code
+ ],
+ "rename collection to existing view should fail with NamespaceExists even if dropTarget is true");
+}
+})();