Skip to content

Commit 3c3b7c8

Browse files
committed
fix(sqlite): Update cache after removing bucket or manifests (#5967)
1 parent 18c4014 commit 3c3b7c8

4 files changed

Lines changed: 107 additions & 15 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
### Features
44

5-
- **scoop-search:** Use SQLite for caching apps to speed up local search ([#5851](https://github.com/ScoopInstaller/Scoop/issues/5851), [#5918](https://github.com/ScoopInstaller/Scoop/issues/5918), [#5946](https://github.com/ScoopInstaller/Scoop/issues/5946), [#5949](https://github.com/ScoopInstaller/Scoop/issues/5949), [#5955](https://github.com/ScoopInstaller/Scoop/issues/5955), [#5966](https://github.com/ScoopInstaller/Scoop/issues/5966))
5+
- **scoop-search:** Use SQLite for caching apps to speed up local search ([#5851](https://github.com/ScoopInstaller/Scoop/issues/5851), [#5918](https://github.com/ScoopInstaller/Scoop/issues/5918), [#5946](https://github.com/ScoopInstaller/Scoop/issues/5946), [#5949](https://github.com/ScoopInstaller/Scoop/issues/5949), [#5955](https://github.com/ScoopInstaller/Scoop/issues/5955), [#5966](https://github.com/ScoopInstaller/Scoop/issues/5966), [#5967](https://github.com/ScoopInstaller/Scoop/issues/5967))
66
- **core:** New cache filename format ([#5929](https://github.com/ScoopInstaller/Scoop/issues/5929), [#5944](https://github.com/ScoopInstaller/Scoop/issues/5944))
77

88
### Bug Fixes

lib/buckets.ps1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ function rm_bucket($name) {
172172
}
173173

174174
Remove-Item $dir -Recurse -Force -ErrorAction Stop
175+
if (get_config USE_SQLITE_CACHE) {
176+
info 'Updating cache...'
177+
Remove-ScoopDBItem -Bucket $name
178+
}
179+
success "The $name bucket was removed successfully."
175180
return 0
176181
}
177182

lib/database.ps1

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ function Open-ScoopDB {
7979
suggest TEXT,
8080
PRIMARY KEY (name, version, bucket)
8181
)"
82+
$tableCommand.CommandType = [System.Data.CommandType]::Text
8283
$tableCommand.ExecuteNonQuery() | Out-Null
8384
$tableCommand.Dispose()
8485
return $db
@@ -88,7 +89,7 @@ function Open-ScoopDB {
8889
.SYNOPSIS
8990
Set Scoop database item(s).
9091
.DESCRIPTION
91-
Insert or replace Scoop database item(s) into the database.
92+
Insert or replace item(s) into the Scoop SQLite database.
9293
.PARAMETER InputObject
9394
System.Object[]
9495
The database item(s) to insert or replace.
@@ -113,6 +114,7 @@ function Set-ScoopDBItem {
113114
$dbQuery = "INSERT OR REPLACE INTO app ($($colName -join ', ')) VALUES ($('@' + ($colName -join ', @')))"
114115
$dbCommand = $db.CreateCommand()
115116
$dbCommand.CommandText = $dbQuery
117+
$dbCommand.CommandType = [System.Data.CommandType]::Text
116118
}
117119
process {
118120
foreach ($item in $InputObject) {
@@ -161,7 +163,7 @@ function Set-ScoopDB {
161163
)
162164

163165
begin {
164-
$list = [System.Collections.Generic.List[PSCustomObject]]::new()
166+
$list = [System.Collections.Generic.List[psobject]]::new()
165167
$arch = Get-DefaultArchitecture
166168
}
167169
process {
@@ -220,11 +222,14 @@ function Set-ScoopDB {
220222
.SYNOPSIS
221223
Select Scoop database item(s).
222224
.DESCRIPTION
223-
Select Scoop database item(s) from the database.
225+
Select item(s) from the Scoop SQLite database.
224226
The pattern is matched against the name, binaries, and shortcuts columns for apps.
225227
.PARAMETER Pattern
226228
System.String
227229
The pattern to search for. If is an empty string, all items will be returned.
230+
.PARAMETER From
231+
System.String[]
232+
The fields to search from.
228233
.INPUTS
229234
System.String
230235
.OUTPUTS
@@ -239,6 +244,7 @@ function Select-ScoopDBItem {
239244
[string]
240245
$Pattern,
241246
[Parameter(Mandatory, Position = 1)]
247+
[ValidateNotNullOrEmpty()]
242248
[string[]]
243249
$From
244250
)
@@ -252,10 +258,10 @@ function Select-ScoopDBItem {
252258
$dbCommand = $db.CreateCommand()
253259
$dbCommand.CommandText = $dbQuery
254260
$dbCommand.CommandType = [System.Data.CommandType]::Text
261+
$dbAdapter.SelectCommand = $dbCommand
255262
}
256263
process {
257264
$dbCommand.Parameters.AddWithValue('@Pattern', $(if ($Pattern -eq '') { '%' } else { '%' + $Pattern + '%' })) | Out-Null
258-
$dbAdapter.SelectCommand = $dbCommand
259265
[void]$dbAdapter.Fill($result)
260266
}
261267
end {
@@ -269,7 +275,7 @@ function Select-ScoopDBItem {
269275
.SYNOPSIS
270276
Get Scoop database item.
271277
.DESCRIPTION
272-
Get Scoop database item from the database.
278+
Get item from the Scoop SQLite database.
273279
.PARAMETER Name
274280
System.String
275281
The name of the item to get.
@@ -312,12 +318,12 @@ function Get-ScoopDBItem {
312318
$dbCommand = $db.CreateCommand()
313319
$dbCommand.CommandText = $dbQuery
314320
$dbCommand.CommandType = [System.Data.CommandType]::Text
321+
$dbAdapter.SelectCommand = $dbCommand
315322
}
316323
process {
317324
$dbCommand.Parameters.AddWithValue('@Name', $Name) | Out-Null
318325
$dbCommand.Parameters.AddWithValue('@Bucket', $Bucket) | Out-Null
319326
$dbCommand.Parameters.AddWithValue('@Version', $Version) | Out-Null
320-
$dbAdapter.SelectCommand = $dbCommand
321327
[void]$dbAdapter.Fill($result)
322328
}
323329
end {
@@ -326,3 +332,60 @@ function Get-ScoopDBItem {
326332
return $result
327333
}
328334
}
335+
336+
<#
337+
.SYNOPSIS
338+
Remove Scoop database item(s).
339+
.DESCRIPTION
340+
Remove item(s) from the Scoop SQLite database.
341+
.PARAMETER Name
342+
System.String
343+
The name of the item to remove.
344+
.PARAMETER Bucket
345+
System.String
346+
The bucket of the item to remove.
347+
.INPUTS
348+
System.String
349+
.OUTPUTS
350+
None
351+
#>
352+
function Remove-ScoopDBItem {
353+
[CmdletBinding()]
354+
param (
355+
[Parameter(Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName)]
356+
[string]
357+
$Name,
358+
[Parameter(Mandatory, Position = 1, ValueFromPipelineByPropertyName)]
359+
[string]
360+
$Bucket
361+
)
362+
363+
begin {
364+
$db = Open-ScoopDB
365+
$dbTrans = $db.BeginTransaction()
366+
$dbQuery = 'DELETE FROM app WHERE bucket = @Bucket'
367+
$dbCommand = $db.CreateCommand()
368+
$dbCommand.CommandText = $dbQuery
369+
$dbCommand.CommandType = [System.Data.CommandType]::Text
370+
}
371+
process {
372+
$dbCommand.Parameters.AddWithValue('@Bucket', $Bucket) | Out-Null
373+
if ($Name) {
374+
$dbCommand.CommandText = $dbQuery + ' AND name = @Name'
375+
$dbCommand.Parameters.AddWithValue('@Name', $Name) | Out-Null
376+
}
377+
$dbCommand.ExecuteNonQuery() | Out-Null
378+
}
379+
end {
380+
try {
381+
$dbTrans.Commit()
382+
} catch {
383+
$dbTrans.Rollback()
384+
throw $_
385+
} finally {
386+
$dbCommand.Dispose()
387+
$dbTrans.Dispose()
388+
$db.Dispose()
389+
}
390+
}
391+
}

libexec/scoop-update.ps1

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ function Sync-Bucket {
181181

182182
$buckets | Where-Object { !$_.valid } | ForEach-Object { Write-Host "'$($_.name)' is not a git repository. Skipped." }
183183

184-
$updatedFiles = [System.Collections.ArrayList]::Synchronized([System.Collections.ArrayList]::new())
184+
$updatedFiles = [System.Collections.Generic.SynchronizedCollection[string]]::new()
185+
$removedFiles = [System.Collections.Generic.SynchronizedCollection[psobject]]::new()
185186
if ($PSVersionTable.PSVersion.Major -ge 7) {
186187
# Parallel parameter is available since PowerShell 7
187188
$buckets | Where-Object { $_.valid } | ForEach-Object -ThrottleLimit 5 -Parallel {
@@ -198,10 +199,21 @@ function Sync-Bucket {
198199
Invoke-GitLog -Path $bucketLoc -Name $name -CommitHash $previousCommit
199200
}
200201
if (get_config USE_SQLITE_CACHE) {
201-
Invoke-Git -Path $bucketLoc -ArgumentList @('diff', '--name-only', '--diff-filter=d', $previousCommit) | ForEach-Object {
202-
$filePath = Join-Path $bucketLoc $_
202+
Invoke-Git -Path $bucketLoc -ArgumentList @('diff', '--name-status', $previousCommit) | ForEach-Object {
203+
$status, $file = $_ -split '\s+', 2
204+
$filePath = Join-Path $bucketLoc $file
203205
if ($filePath -match "^$([regex]::Escape($innerBucketLoc)).*\.json$") {
204-
[void]($using:updatedFiles).Add($filePath)
206+
switch ($status) {
207+
{ $_ -in 'A', 'M', 'R' } {
208+
[void]($using:updatedFiles).Add($filePath)
209+
}
210+
'D' {
211+
[void]($using:removedFiles).Add([pscustomobject]@{
212+
Name = ([System.IO.FileInfo]$file).BaseName
213+
Bucket = $name
214+
})
215+
}
216+
}
205217
}
206218
}
207219
}
@@ -218,18 +230,30 @@ function Sync-Bucket {
218230
Invoke-GitLog -Path $bucketLoc -Name $name -CommitHash $previousCommit
219231
}
220232
if (get_config USE_SQLITE_CACHE) {
221-
Invoke-Git -Path $bucketLoc -ArgumentList @('diff', '--name-only', '--diff-filter=d', $previousCommit) | ForEach-Object {
222-
$filePath = Join-Path $bucketLoc $_
233+
Invoke-Git -Path $bucketLoc -ArgumentList @('diff', '--name-status', $previousCommit) | ForEach-Object {
234+
$status, $file = $_ -split '\s+', 2
235+
$filePath = Join-Path $bucketLoc $file
223236
if ($filePath -match "^$([regex]::Escape($innerBucketLoc)).*\.json$") {
224-
[void]($updatedFiles).Add($filePath)
237+
switch ($status) {
238+
{ $_ -in 'A', 'M', 'R' } {
239+
[void]($updatedFiles).Add($filePath)
240+
}
241+
'D' {
242+
[void]($removedFiles).Add([pscustomobject]@{
243+
Name = ([System.IO.FileInfo]$file).BaseName
244+
Bucket = $name
245+
})
246+
}
247+
}
225248
}
226249
}
227250
}
228251
}
229252
}
230-
if ((get_config USE_SQLITE_CACHE) -and ($updatedFiles.Count -gt 0)) {
253+
if ((get_config USE_SQLITE_CACHE) -and ($updatedFiles.Count -gt 0 -or $removedFiles.Count -gt 0)) {
231254
info 'Updating cache...'
232255
Set-ScoopDB -Path $updatedFiles
256+
$removedFiles | Remove-ScoopDBItem
233257
}
234258
}
235259

0 commit comments

Comments
 (0)