comparison bin/cleandb.sh @ 8:bf99236cc5cd default tip

try to recover from upgrade fail
author Charlie Root
date Sat, 29 Dec 2018 07:07:34 -0500
parents 4681f974d28b
children
comparison
equal deleted inserted replaced
7:65fd7d441cf1 8:bf99236cc5cd
3 /* 3 /*
4 +-----------------------------------------------------------------------+ 4 +-----------------------------------------------------------------------+
5 | bin/cleandb.sh | 5 | bin/cleandb.sh |
6 | | 6 | |
7 | This file is part of the Roundcube Webmail client | 7 | This file is part of the Roundcube Webmail client |
8 | Copyright (C) 2010, The Roundcube Dev Team | 8 | Copyright (C) 2010-2015, The Roundcube Dev Team |
9 | | 9 | |
10 | Licensed under the GNU General Public License version 3 or | 10 | Licensed under the GNU General Public License version 3 or |
11 | any later version with exceptions for skins & plugins. | 11 | any later version with exceptions for skins & plugins. |
12 | See the README file for a full license statement. | 12 | See the README file for a full license statement. |
13 | | 13 | |
21 21
22 define('INSTALL_PATH', realpath(__DIR__ . '/..') . '/' ); 22 define('INSTALL_PATH', realpath(__DIR__ . '/..') . '/' );
23 23
24 require INSTALL_PATH.'program/include/clisetup.php'; 24 require INSTALL_PATH.'program/include/clisetup.php';
25 25
26 // mapping for table name => primary key
27 $primary_keys = array(
28 'contacts' => "contact_id",
29 'contactgroups' => "contactgroup_id",
30 );
31
32 // connect to DB
33 $RCMAIL = rcube::get_instance();
34 $db = $RCMAIL->get_dbh();
35 $db->db_connect('w');
36
37 if (!$db->is_connected() || $db->is_error()) {
38 rcube::raise_error("No DB connection", false, true);
39 }
40
41 if (!empty($_SERVER['argv'][1])) 26 if (!empty($_SERVER['argv'][1]))
42 $days = intval($_SERVER['argv'][1]); 27 $days = intval($_SERVER['argv'][1]);
43 else 28 else
44 $days = 7; 29 $days = 7;
45 30
46 // remove all deleted records older than two days 31 rcmail_utils::db_clean($days);
47 $threshold = date('Y-m-d 00:00:00', time() - $days * 86400);
48
49 foreach (array('contacts','contactgroups','identities') as $table) {
50
51 $sqltable = $db->table_name($table, true);
52
53 // also delete linked records
54 // could be skipped for databases which respect foreign key constraints
55 if ($db->db_provider == 'sqlite'
56 && ($table == 'contacts' || $table == 'contactgroups')
57 ) {
58 $pk = $primary_keys[$table];
59 $memberstable = $db->table_name('contactgroupmembers');
60
61 $db->query(
62 "DELETE FROM " . $db->quote_identifier($memberstable).
63 " WHERE `$pk` IN (".
64 "SELECT `$pk` FROM $sqltable".
65 " WHERE `del` = 1 AND `changed` < ?".
66 ")",
67 $threshold);
68
69 echo $db->affected_rows() . " records deleted from '$memberstable'\n";
70 }
71
72 // delete outdated records
73 $db->query("DELETE FROM $sqltable WHERE `del` = 1 AND `changed` < ?", $threshold);
74
75 echo $db->affected_rows() . " records deleted from '$table'\n";
76 }
77 32
78 ?> 33 ?>