30
30
use OCA \Files_External \Lib \InsufficientDataForMeaningfulAnswerException ;
31
31
use OCA \Files_External \Lib \StorageConfig ;
32
32
use OCA \Files_External \Service \GlobalStoragesService ;
33
+ use OCP \DB \QueryBuilder \IQueryBuilder ;
33
34
use OCP \Files \Notify \IChange ;
34
35
use OCP \Files \Notify \INotifyHandler ;
35
36
use OCP \Files \Notify \IRenameChange ;
@@ -48,8 +49,6 @@ class Notify extends Base {
48
49
private $ globalService ;
49
50
/** @var IDBConnection */
50
51
private $ connection ;
51
- /** @var \OCP\DB\QueryBuilder\IQueryBuilder */
52
- private $ updateQuery ;
53
52
/** @var ILogger */
54
53
private $ logger ;
55
54
@@ -58,7 +57,6 @@ function __construct(GlobalStoragesService $globalService, IDBConnection $connec
58
57
$ this ->globalService = $ globalService ;
59
58
$ this ->connection = $ connection ;
60
59
$ this ->logger = $ logger ;
61
- $ this ->updateQuery = $ this ->getUpdateQuery ($ this ->connection );
62
60
}
63
61
64
62
protected function configure () {
@@ -162,13 +160,22 @@ private function markParentAsOutdated($mountId, $path, OutputInterface $output)
162
160
}
163
161
164
162
try {
165
- $ this ->updateQuery -> execute ([ $ parent , $ mountId] );
163
+ $ storageIds = $ this ->getStorageIds ( $ mountId );
166
164
} catch (DriverException $ ex ) {
167
- $ this ->logger ->logException ($ ex , ['app ' => ' files_external ' , ' message ' => 'Error while trying to mark folder as outdated ' , 'level ' => ILogger::WARN ]);
165
+ $ this ->logger ->logException ($ ex , ['message ' => 'Error while trying to find correct storage ids. ' , 'level ' => ILogger::WARN ]);
168
166
$ this ->connection = $ this ->reconnectToDatabase ($ this ->connection , $ output );
169
167
$ output ->writeln ('<info>Needed to reconnect to the database</info> ' );
170
- $ this ->updateQuery = $ this ->getUpdateQuery ($ this ->connection );
171
- $ this ->updateQuery ->execute ([$ parent , $ mountId ]);
168
+ $ storageIds = $ this ->getStorageIds ($ mountId );
169
+ }
170
+ if (count ($ storageIds ) === 0 ) {
171
+ throw new StorageNotAvailableException ('No storages found by mount ID ' . $ mountId );
172
+ }
173
+ $ storageIds = array_map ('intval ' , $ storageIds );
174
+
175
+ $ result = $ this ->updateParent ($ storageIds , $ parent );
176
+ if ($ result === 0 ) {
177
+ //TODO: Find existing parent further up the tree in the database and register that folder instead.
178
+ $ this ->logger ->info ('Failed updating parent for " ' . $ path . '" while trying to register change. It may not exist in the filecache. ' );
172
179
}
173
180
}
174
181
@@ -199,15 +206,33 @@ private function logUpdate(IChange $change, OutputInterface $output) {
199
206
}
200
207
201
208
/**
202
- * @return \Doctrine\DBAL\Statement
209
+ * @param int $mountId
210
+ * @return array
211
+ */
212
+ private function getStorageIds ($ mountId ) {
213
+ $ qb = $ this ->connection ->getQueryBuilder ();
214
+ return $ qb
215
+ ->select ('storage_id ' )
216
+ ->from ('mounts ' )
217
+ ->where ($ qb ->expr ()->eq ('mount_id ' , $ qb ->createNamedParameter ($ mountId , IQueryBuilder::PARAM_INT )))
218
+ ->execute ()
219
+ ->fetchAll (\PDO ::FETCH_COLUMN );
220
+ }
221
+
222
+ /**
223
+ * @param array $storageIds
224
+ * @param string $parent
225
+ * @return int
203
226
*/
204
- private function getUpdateQuery (IDBConnection $ connection ) {
205
- // the query builder doesn't really like subqueries with parameters
206
- return $ connection ->prepare (
207
- 'UPDATE *PREFIX*filecache SET size = -1
208
- WHERE `path` = ?
209
- AND `storage` IN (SELECT storage_id FROM *PREFIX*mounts WHERE mount_id = ?) '
210
- );
227
+ private function updateParent ($ storageIds , $ parent ) {
228
+ $ pathHash = md5 (trim (\OC_Util::normalizeUnicode ($ parent ), '/ ' ));
229
+ $ qb = $ this ->connection ->getQueryBuilder ();
230
+ return $ qb
231
+ ->update ('filecache ' )
232
+ ->set ('size ' , $ qb ->createNamedParameter (-1 , IQueryBuilder::PARAM_INT ))
233
+ ->where ($ qb ->expr ()->in ('storage ' , $ qb ->createNamedParameter ($ storageIds , IQueryBuilder::PARAM_INT_ARRAY , ':storage_ids ' )))
234
+ ->andWhere ($ qb ->expr ()->eq ('path_hash ' , $ qb ->createNamedParameter ($ pathHash , IQueryBuilder::PARAM_STR )))
235
+ ->execute ();
211
236
}
212
237
213
238
/**
@@ -217,14 +242,14 @@ private function reconnectToDatabase(IDBConnection $connection, OutputInterface
217
242
try {
218
243
$ connection ->close ();
219
244
} catch (\Exception $ ex ) {
220
- $ this ->logger ->logException ($ ex , ['app ' => ' files_external ' , ' message ' => 'Error while disconnecting from DB ' , 'level ' => ILogger::WARN ]);
245
+ $ this ->logger ->logException ($ ex , ['message ' => 'Error while disconnecting from DB ' , 'level ' => ILogger::WARN ]);
221
246
$ output ->writeln ("<info>Error while disconnecting from database: {$ ex ->getMessage ()}</info> " );
222
247
}
223
248
while (!$ connection ->isConnected ()) {
224
249
try {
225
250
$ connection ->connect ();
226
251
} catch (\Exception $ ex ) {
227
- $ this ->logger ->logException ($ ex , ['app ' => ' files_external ' , ' message ' => 'Error while re-connecting to database ' , 'level ' => ILogger::WARN ]);
252
+ $ this ->logger ->logException ($ ex , ['message ' => 'Error while re-connecting to database ' , 'level ' => ILogger::WARN ]);
228
253
$ output ->writeln ("<info>Error while re-connecting to database: {$ ex ->getMessage ()}</info> " );
229
254
sleep (60 );
230
255
}
0 commit comments