Skip to content

Commit 611225b

Browse files
committed
Add database element removal methods
These methods allow for removal of libraries and releases from the database.
1 parent f51f98d commit 611225b

File tree

2 files changed

+262
-0
lines changed

2 files changed

+262
-0
lines changed

internal/libraries/db/db.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,30 @@ func (db *DB) AddLibrary(library *Library) error {
9696
return nil
9797
}
9898

99+
// RemoveLibrary removes a library and all its releases from the database.
100+
func (db *DB) RemoveLibrary(libraryName string) error {
101+
db.mutex.Lock()
102+
defer db.mutex.Unlock()
103+
return db.removeLibrary(libraryName)
104+
}
105+
106+
func (db *DB) removeLibrary(libraryName string) error {
107+
found := false
108+
for i := range db.Libraries {
109+
for i < len(db.Libraries) && db.Libraries[i].Name == libraryName {
110+
found = true
111+
db.Libraries = append(db.Libraries[:i], db.Libraries[i+1:]...)
112+
}
113+
}
114+
if !found {
115+
return errors.New("library not found")
116+
}
117+
118+
db.removeReleases(libraryName) // It's OK if no releases were found.
119+
120+
return nil
121+
}
122+
99123
// HasLibrary returns whether the database already contains the given library.
100124
func (db *DB) HasLibrary(libraryName string) bool {
101125
db.mutex.Lock()
@@ -149,6 +173,28 @@ func (db *DB) AddRelease(release *Release, repoURL string) error {
149173
return nil
150174
}
151175

176+
// RemoveReleaseByNameVersion removes the given library release from the database.
177+
func (db *DB) RemoveReleaseByNameVersion(libraryName string, libraryVersion string) error {
178+
db.mutex.Lock()
179+
defer db.mutex.Unlock()
180+
return db.removeReleaseByNameVersion(libraryName, libraryVersion)
181+
}
182+
183+
func (db *DB) removeReleaseByNameVersion(libraryName string, libraryVersion string) error {
184+
found := false
185+
for i, release := range db.Releases {
186+
if release.LibraryName == libraryName && release.Version.String() == libraryVersion {
187+
found = true
188+
db.Releases = append(db.Releases[:i], db.Releases[i+1:]...)
189+
}
190+
}
191+
if !found {
192+
return errors.New("release not found")
193+
}
194+
195+
return nil
196+
}
197+
152198
// HasReleaseByNameVersion returns whether the database contains a release for the given library and version number.
153199
func (db *DB) HasReleaseByNameVersion(libraryName string, libraryVersion string) bool {
154200
db.mutex.Lock()
@@ -276,6 +322,28 @@ func (db *DB) findReleasesOfLibrary(lib *Library) []*Release {
276322
return releases
277323
}
278324

325+
// RemoveReleases removes all releases of a library from the database.
326+
func (db *DB) RemoveReleases(libraryName string) error {
327+
db.mutex.Lock()
328+
defer db.mutex.Unlock()
329+
return db.removeReleases(libraryName)
330+
}
331+
332+
func (db *DB) removeReleases(libraryName string) error {
333+
found := false
334+
for i := range db.Releases {
335+
for i < len(db.Releases) && db.Releases[i].LibraryName == libraryName {
336+
found = true
337+
db.Releases = append(db.Releases[:i], db.Releases[i+1:]...)
338+
}
339+
}
340+
if !found {
341+
return errors.New("releases not found")
342+
}
343+
344+
return nil
345+
}
346+
279347
// Commit saves the database to disk.
280348
func (db *DB) Commit() error {
281349
return db.SaveToFile()

internal/libraries/db/db_test.go

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
// This file is part of libraries-repository-engine.
2+
//
3+
// Copyright 2021 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as published
7+
// by the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
//
18+
// You can be released from the requirements of the above licenses by purchasing
19+
// a commercial license. Buying such a license is mandatory if you want to
20+
// modify or otherwise use the software for commercial activities involving the
21+
// Arduino software without disclosing the source code of your own applications.
22+
// To purchase a commercial license, send an email to [email protected].
23+
24+
package db
25+
26+
import (
27+
"testing"
28+
29+
"github.com/stretchr/testify/assert"
30+
"github.com/stretchr/testify/require"
31+
)
32+
33+
func testerDB() *DB {
34+
tDB := DB{
35+
Libraries: []*Library{
36+
{
37+
Name: "FooLib",
38+
Repository: "https://github.com/Bar/FooLib.git",
39+
SupportLevel: "",
40+
},
41+
{
42+
Name: "BazLib",
43+
Repository: "https://github.com/Bar/BazLib.git",
44+
SupportLevel: "",
45+
},
46+
{
47+
Name: "QuxLib",
48+
Repository: "https://github.com/Zeb/QuxLib.git",
49+
SupportLevel: "",
50+
},
51+
},
52+
Releases: []*Release{
53+
{
54+
LibraryName: "FooLib",
55+
Version: Version{"1.0.0"},
56+
Author: "Barthor",
57+
Maintainer: "Bartainer",
58+
License: "MIT",
59+
Sentence: "asdf",
60+
Paragraph: "zxcv",
61+
Website: "https://example.com",
62+
Category: "Other",
63+
Architectures: []string{"avr"},
64+
Types: []string{"Contributed"},
65+
URL: "http://www.example.com/libraries/github.com/Bar/FooLib-1.0.0.zip",
66+
ArchiveFileName: "FooLib-1.0.0.zip",
67+
Size: 123,
68+
Checksum: "SHA-256:887f897cfb1818a53652aef39c2a4b8de3c69c805520b2953a562a787b422420",
69+
Includes: []string{"FooLib.h"},
70+
Dependencies: []*Dependency{
71+
{
72+
Name: "BazLib",
73+
Version: "2.0.0",
74+
},
75+
},
76+
Log: "Some log messages",
77+
},
78+
{
79+
LibraryName: "BazLib",
80+
Version: Version{"2.0.0"},
81+
Author: "Barthor",
82+
Maintainer: "Bartainer",
83+
License: "MIT",
84+
Sentence: "asdf",
85+
Paragraph: "zxcv",
86+
Website: "https://example.com",
87+
Category: "Other",
88+
Architectures: []string{"avr"},
89+
Types: []string{"Contributed"},
90+
URL: "http://www.example.com/libraries/github.com/Bar/BazLib-2.0.0.zip",
91+
ArchiveFileName: "BazLib-2.0.0.zip",
92+
Size: 123,
93+
Checksum: "SHA-256:887f897cfb1818a53652aef39c2a4b8de3c69c805520b2953a562a787b422420",
94+
Includes: []string{"BazLib.h"},
95+
Dependencies: []*Dependency{},
96+
Log: "Some log messages",
97+
},
98+
{
99+
LibraryName: "BazLib",
100+
Version: Version{"2.1.0"},
101+
Author: "Barthor",
102+
Maintainer: "Bartainer",
103+
License: "MIT",
104+
Sentence: "asdf",
105+
Paragraph: "zxcv",
106+
Website: "https://example.com",
107+
Category: "Other",
108+
Architectures: []string{"avr"},
109+
Types: []string{"Contributed"},
110+
URL: "http://www.example.com/libraries/github.com/Bar/BazLib-2.1.0.zip",
111+
ArchiveFileName: "BazLib-2.1.0.zip",
112+
Size: 123,
113+
Checksum: "SHA-256:887f897cfb1818a53652aef39c2a4b8de3c69c805520b2953a562a787b422420",
114+
Includes: []string{"BazLib.h"},
115+
Dependencies: []*Dependency{},
116+
Log: "Some log messages",
117+
},
118+
{
119+
LibraryName: "FooLib",
120+
Version: Version{"1.1.0"},
121+
Author: "Barthor",
122+
Maintainer: "Bartainer",
123+
License: "MIT",
124+
Sentence: "asdf",
125+
Paragraph: "zxcv",
126+
Website: "https://example.com",
127+
Category: "Other",
128+
Architectures: []string{"avr"},
129+
Types: []string{"Contributed"},
130+
URL: "http://www.example.com/libraries/github.com/Bar/FooLib-1.1.0.zip",
131+
ArchiveFileName: "FooLib-1.1.0.zip",
132+
Size: 123,
133+
Checksum: "SHA-256:887f897cfb1818a53652aef39c2a4b8de3c69c805520b2953a562a787b422420",
134+
Includes: []string{"FooLib.h"},
135+
Dependencies: []*Dependency{
136+
{
137+
Name: "BazLib",
138+
Version: "",
139+
},
140+
},
141+
Log: "Some log messages",
142+
},
143+
},
144+
libraryFile: "some-file.json",
145+
}
146+
147+
return &tDB
148+
}
149+
150+
func TestRemoveLibrary(t *testing.T) {
151+
testDB := testerDB()
152+
assert.True(t, testDB.HasLibrary("FooLib"))
153+
assert.True(t, testDB.HasReleaseByNameVersion("FooLib", "1.0.0"))
154+
err := testDB.RemoveLibrary("FooLib")
155+
require.NoError(t, err)
156+
assert.False(t, testDB.HasLibrary("FooLib"))
157+
assert.False(t, testDB.HasReleaseByNameVersion("FooLib", "1.0.0"))
158+
assert.False(t, testDB.HasReleaseByNameVersion("FooLib", "1.1.0"))
159+
160+
assert.True(t, testDB.HasLibrary("QuxLib"))
161+
err = testDB.RemoveLibrary("QuxLib")
162+
require.NoError(t, err)
163+
assert.False(t, testDB.HasLibrary("QuxLib"))
164+
165+
err = testDB.RemoveLibrary("nonexistent")
166+
assert.Error(t, err)
167+
}
168+
169+
func TestRemoveReleaseByNameVersion(t *testing.T) {
170+
testDB := testerDB()
171+
assert.True(t, testDB.HasReleaseByNameVersion("FooLib", "1.0.0"))
172+
assert.True(t, testDB.HasReleaseByNameVersion("FooLib", "1.1.0"))
173+
err := testDB.RemoveReleaseByNameVersion("FooLib", "1.0.0")
174+
require.NoError(t, err)
175+
assert.False(t, testDB.HasReleaseByNameVersion("FooLib", "1.0.0"))
176+
assert.True(t, testDB.HasReleaseByNameVersion("FooLib", "1.1.0"))
177+
178+
err = testDB.RemoveReleaseByNameVersion("nonexistent", "1.0.0")
179+
assert.Error(t, err)
180+
err = testDB.RemoveReleaseByNameVersion("FooLib", "99.99.99")
181+
assert.Error(t, err)
182+
}
183+
184+
func TestRemoveReleases(t *testing.T) {
185+
testDB := testerDB()
186+
assert.True(t, testDB.HasReleaseByNameVersion("FooLib", "1.0.0"))
187+
err := testDB.RemoveReleases("FooLib")
188+
require.NoError(t, err)
189+
assert.False(t, testDB.HasReleaseByNameVersion("FooLib", "1.0.0"))
190+
assert.False(t, testDB.HasReleaseByNameVersion("FooLib", "1.1.0"))
191+
192+
err = testDB.RemoveReleases("nonexistent")
193+
assert.Error(t, err)
194+
}

0 commit comments

Comments
 (0)