Fix SyncDir always consider root dirs as changed

This commit is contained in:
Ming Ming 2022-10-30 11:59:40 +08:00
parent 62716f3f72
commit 9759f172db
2 changed files with 23 additions and 10 deletions

View file

@ -593,6 +593,9 @@ enum FilesQueryMode {
expression, expression,
} }
typedef FilesQueryRelativePathBuilder = Expression<bool?> Function(
GeneratedColumn<String?> relativePath);
/// Build a Files table query /// Build a Files table query
/// ///
/// If you call more than one by* methods, the condition will be added up /// If you call more than one by* methods, the condition will be added up
@ -646,8 +649,12 @@ class FilesQueryBuilder {
_byRelativePath = path; _byRelativePath = path;
} }
void byOrRelativePath(String path) {
_byOrRelativePathBuilder((relativePath) => relativePath.equals(path));
}
void byOrRelativePathPattern(String pattern) { void byOrRelativePathPattern(String pattern) {
(_byOrRelativePathPatterns ??= []).add(pattern); _byOrRelativePathBuilder((relativePath) => relativePath.like(pattern));
} }
void byMimePattern(String pattern) { void byMimePattern(String pattern) {
@ -723,13 +730,13 @@ class FilesQueryBuilder {
if (_byRelativePath != null) { if (_byRelativePath != null) {
query.where(db.accountFiles.relativePath.equals(_byRelativePath)); query.where(db.accountFiles.relativePath.equals(_byRelativePath));
} }
if (_byOrRelativePathPatterns?.isNotEmpty == true) { if (_byOrRelativePathBuilders?.isNotEmpty == true) {
final expression = _byOrRelativePathPatterns! final expression = _byOrRelativePathBuilders!
.sublist(1) .sublist(1)
.fold<Expression<bool?>>( .fold<Expression<bool?>>(
db.accountFiles.relativePath.like(_byOrRelativePathPatterns![0]), _byOrRelativePathBuilders![0](db.accountFiles.relativePath),
(previousValue, element) => (previousValue, builder) =>
previousValue | db.accountFiles.relativePath.like(element)); previousValue | builder(db.accountFiles.relativePath));
query.where(expression); query.where(expression);
} }
if (_byMimePatterns?.isNotEmpty == true) { if (_byMimePatterns?.isNotEmpty == true) {
@ -770,6 +777,10 @@ class FilesQueryBuilder {
return query; return query;
} }
void _byOrRelativePathBuilder(FilesQueryRelativePathBuilder builder) {
(_byOrRelativePathBuilders ??= []).add(builder);
}
final SqliteDb db; final SqliteDb db;
FilesQueryMode _queryMode = FilesQueryMode.file; FilesQueryMode _queryMode = FilesQueryMode.file;
@ -783,7 +794,7 @@ class FilesQueryBuilder {
int? _byFileId; int? _byFileId;
Iterable<int>? _byFileIds; Iterable<int>? _byFileIds;
String? _byRelativePath; String? _byRelativePath;
List<String>? _byOrRelativePathPatterns; List<FilesQueryRelativePathBuilder>? _byOrRelativePathBuilders;
List<String>? _byMimePatterns; List<String>? _byMimePatterns;
bool? _byFavorite; bool? _byFavorite;
int? _byDirRowId; int? _byDirRowId;

View file

@ -27,7 +27,7 @@ class SyncDir {
String dirPath, { String dirPath, {
bool isRecursive = true, bool isRecursive = true,
}) async { }) async {
final dirCache = await _queryAllSubDirEtags(account, dirPath); final dirCache = await _queryAllDirEtags(account, dirPath);
final remoteRoot = final remoteRoot =
await LsSingleFile(_c.withRemoteFileRepo())(account, dirPath); await LsSingleFile(_c.withRemoteFileRepo())(account, dirPath);
return await _syncDir(account, remoteRoot, dirCache, return await _syncDir(account, remoteRoot, dirCache,
@ -81,7 +81,7 @@ class SyncDir {
return Tuple2(true, touchResult); return Tuple2(true, touchResult);
} }
Future<Map<int, String>> _queryAllSubDirEtags( Future<Map<int, String>> _queryAllDirEtags(
Account account, String dirPath) async { Account account, String dirPath) async {
final dir = File(path: dirPath); final dir = File(path: dirPath);
return await _c.sqliteDb.use((db) async { return await _c.sqliteDb.use((db) async {
@ -91,7 +91,9 @@ class SyncDir {
expressions: [db.files.fileId, db.files.etag]) expressions: [db.files.fileId, db.files.etag])
..setAppAccount(account); ..setAppAccount(account);
if (dir.strippedPathWithEmpty.isNotEmpty) { if (dir.strippedPathWithEmpty.isNotEmpty) {
q.byOrRelativePathPattern("${dir.strippedPathWithEmpty}/%"); q
..byOrRelativePath(dir.strippedPathWithEmpty)
..byOrRelativePathPattern("${dir.strippedPathWithEmpty}/%");
} }
return q.build(); return q.build();
}); });