Skip to content

Commit d202ab9

Browse files
committed
#134 New task for deleting collections
1 parent ade3856 commit d202ab9

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/main/groovy/com/marklogic/gradle/MarkLogicPlugin.groovy

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.marklogic.gradle
22

33
import com.marklogic.appdeployer.command.databases.DeployOtherDatabasesCommand
44
import com.marklogic.appdeployer.command.forests.DeployCustomForestsCommand
5+
import com.marklogic.gradle.task.databases.DeleteCollectionTask
56
import com.marklogic.gradle.task.forests.DeployCustomForestsTask
67
import com.marklogic.gradle.task.qconsole.ExportWorkspacesTask
78
import com.marklogic.gradle.task.qconsole.ImportWorkspacesTask
@@ -171,6 +172,7 @@ class MarkLogicPlugin implements Plugin<Project> {
171172
project.task("mlClearModulesDatabase", type: ClearModulesDatabaseTask, group: dbGroup, dependsOn: "mlDeleteModuleTimestampsFile", description: "Deletes potentially all of the documents in the modules database; has a property for excluding documents from deletion")
172173
project.task("mlClearSchemasDatabase", type: ClearSchemasDatabaseTask, group: dbGroup, description: "Deletes all documents in the schemas database")
173174
project.task("mlClearTriggersDatabase", type: ClearTriggersDatabaseTask, group: dbGroup, description: "Deletes all documents in the triggers database")
175+
project.task("mlDeleteCollection", type: DeleteCollectionTask, group: dbGroup, description: "Delete the collection of documents in the content database; use -Pcollection=name to specify the collection name on the command line")
174176
project.task("mlDeployDatabases", type: DeployDatabasesTask, group: dbGroup, dependsOn: "mlPrepareRestApiDependencies", description: "Deploy each database, updating it if it exists, in the configuration directory")
175177
project.task("mlMergeContentDatabase", type: MergeContentDatabaseTask, group: dbGroup, description: "Merge the database named by mlAppConfig.contentDatabaseName")
176178
project.task("mlMergeDatabase", type: MergeDatabaseTask, group: dbGroup, description: "Merge the database named by the project property dbName; e.g. gradle mlMergeDatabase -PdbName=my-database")
@@ -248,7 +250,7 @@ class MarkLogicPlugin implements Plugin<Project> {
248250
project.task("mlDeployTasks", type: DeployTasksTask, group: taskGroup, description: "Deploy each scheduled task, updating it if it exists, in the configuration directory")
249251
project.task("mlUndeployTasks", type: UndeployTasksTask, group: taskGroup, description: "Undeploy (delete) each scheduled task in the configuration directory")
250252
project.task("mlWaitForTaskServer", type: WaitForTaskServerTask, group: taskGroup, description: "Wait for the task server to not have any requests in progress")
251-
253+
252254
String triggerGroup = "ml-gradle Trigger"
253255
project.task("mlDeployTriggers", type: DeployTriggersTask, group: triggerGroup, description: "Deploy each trigger, updating it if it exists, in the configuration directory")
254256

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.marklogic.gradle.task.databases
2+
3+
import com.marklogic.gradle.task.MarkLogicTask
4+
import org.gradle.api.tasks.TaskAction
5+
6+
class DeleteCollectionTask extends MarkLogicTask {
7+
8+
String collection
9+
boolean showEstimate = true
10+
11+
@TaskAction
12+
void deleteCollection() {
13+
String coll = collection
14+
if (project.hasProperty("collection")) {
15+
coll = project.property("collection")
16+
}
17+
if (coll == null) {
18+
println "Please specify a collection to delete; e.g. -Pcollection=changeme"
19+
return
20+
}
21+
22+
def client = newClient()
23+
try {
24+
if (showEstimate) {
25+
try {
26+
def estimate = client.newServerEval().xquery("xdmp:estimate(collection('" + coll + "'))").eval().next().getAs(String.class)
27+
println "Collection " + coll + " has " + estimate + " documents in it"
28+
} catch (Exception e) {
29+
// Don't let this bomb the task
30+
println "Unable to get estimate of collection size, cause: " + e.getMessage()
31+
}
32+
}
33+
println "Deleting collection " + coll + " in the content database; note that this may timeout if your collection has a sufficiently large enough number of documents"
34+
client.newServerEval().xquery("xdmp:collection-delete('" + coll + "')").eval()
35+
} finally {
36+
client.release()
37+
}
38+
println "Finished deleting collection " + coll
39+
}
40+
}

0 commit comments

Comments
 (0)