[Vtigercrm-developers] deleting Documents file attachments

Adam Heinz amh at metricwise.net
Mon Nov 5 07:50:20 PST 2012


I've done a bit more research and it looks like what is happening is
that the crm entity of setype Documents is being deleted, but there is
a second entity of setype Documents Attachment that is persisting.  It
was simple enough to detect orphans with some SQL [1] and also a small
script I wrote to scan the disk [2].  What's really odd here is that
there is does not appear to be any sort of Attachments module, even
though there is an setype.

Anyway, I'm going to look into writing some sort of afterdelete
trigger on Documents to delete the associated attachment (I will check
for other references first).

[1]
select count(1) from vtiger_attachments where attachmentsid not in
(select attachmentsid from vtiger_seattachmentsrel);
[2]
#!/usr/bin/php
<?php
require_once('config.php');
require_once('adodb/adodb-exceptions.inc.php');
require_once('include/database/PearDatabase.php');
require_once('modules/Users/Users.php');

global $adb, $current_user;
$current_user = Users::getActiveAdminUser();

$attachments = $adb->database->GetAssoc("SELECT CONCAT(path,
attachmentsid, '_', name), attachmentsid FROM vtiger_attachments");
traverse("storage");

foreach ($attachments as $filepath => $crmid) {
	error_log("file $filepath not found on disk");
}

function traverse($dirname) {
	global $attachments;
	$dir = opendir($dirname);
	while ($filename = readdir($dir)) {
		if ('.' == $filename[0]) {
			continue;
		}
		$filepath = "$dirname/$filename";
		if (is_dir($filepath)) {
			traverse($filepath);
		} else if ($attachments[$filepath]) {
			unset($attachments[$filepath]);
		} else {
			error_log("file $filepath not found in database");
		}
	}
}


More information about the vtigercrm-developers mailing list