sqlite: addContainer: add named volume only once

There's a unique constraint in the table, so we shouldn't add the same
volume more than once to the same container.

[NO NEW TESTS NEEDED] as it fixes an existing one.

Signed-off-by: Valentin Rothberg <vrothberg@redhat.com>
This commit is contained in:
Valentin Rothberg
2023-02-28 13:21:06 +01:00
parent 86d12520e9
commit 2342d1a314

View File

@ -399,9 +399,13 @@ func (s *SQLiteState) addContainer(ctr *Container) (defErr error) {
return fmt.Errorf("adding container dependency %s to database: %w", dep, err)
}
}
volMap := make(map[string]bool)
for _, vol := range ctr.config.NamedVolumes {
if _, err := tx.Exec("INSERT INTO ContainerVolume VALUES (?, ?);", ctr.ID(), vol.Name); err != nil {
return fmt.Errorf("adding container volume %s to database: %w", vol.Name, err)
if _, ok := volMap[vol.Name]; !ok {
if _, err := tx.Exec("INSERT INTO ContainerVolume VALUES (?, ?);", ctr.ID(), vol.Name); err != nil {
return fmt.Errorf("adding container volume %s to database: %w", vol.Name, err)
}
volMap[vol.Name] = true
}
}