Migrate from database storage
Last modified by Thomas Mortagne on 2026/06/02 17:54
| A set of scripts to migrate some data from their old database location to the filesystem |
| Type | Snippet |
| Category | Other |
| Developed by | |
| Rating | |
| License | GNU Lesser General Public License 2.1 |
Table of contents
Description
Deleted attachments
The following script migrates asynchronously the content of the deleted attachments (the first 10000 of each wiki) from the database to the currently configured storage.
{{async}}
{{groovy}}
// Remember the current wiki
currentWiki = xcontext.context.getWikiId();
try {
for (wiki in services.wiki.getAllIds()) {
println "== ${wiki}"
println ''
xcontext.context.setWikiId(wiki)
store = services.component.getInstance(com.xpn.xwiki.store.AttachmentRecycleBinStore.class, 'hibernate')
ids = services.query.hql("select id from DeletedAttachment WHERE contentStore is null OR contentStore='hibernate' order by id asc").setLimit(10000).execute()
if (ids.isEmpty()) {
println '{{success}}No more deleted attachments to migrate.{{/success}}'
} else {
for (id in ids) {
try {
// Get the deleted document from the store
deletedAttachment = store.getDeletedAttachment(id, xcontext.context, true)
// Get the attachment from the deleted attachment
attachment = deletedAttachment.restoreAttachment()
// Associated the attachment with a XWikiDocument instance
attachmentDocument = new com.xpn.xwiki.doc.XWikiDocument(services.model.resolveDocument(deletedAttachment.docName))
attachmentDocument.setAttachment(attachment)
attachment.setDoc(attachmentDocument)
// Re-store the deleted attachment with the configured content store
store.saveToRecycleBin(attachment, deletedAttachment.deleter, deletedAttachment.date, xcontext.context, true)
// Remove the old deleted attachment
store.deleteFromRecycleBin(id, xcontext.context, true)
println "* {{success}}##${deletedAttachment}##{{/success}}"
} catch (Exception e) {
println "* {{error}}Failed to migrate deleted attachment with id ${id}:";
println org.apache.commons.lang3.exception.ExceptionUtils.getStackTrace(e);
println "{{/error}}";
}
}
}
}
} finally {
xcontext.context.setWikiId(currentWiki)
}
{{/groovy}}
{{/async}}