mirror of
https://github.com/containers/podman.git
synced 2025-05-21 09:05:56 +08:00
Improve handling of existing container names in SQLite
Return more sensible errors than SQLite's embedded constraint failure ones. Should fix a number of integration tests. Signed-off-by: Matt Heon <mheon@redhat.com>
This commit is contained in:
@ -1482,7 +1482,7 @@ func (s *SQLiteState) AddPod(pod *Pod) (defErr error) {
|
|||||||
return fmt.Errorf("checking if pod name %s exists in database: %w", pod.Name(), err)
|
return fmt.Errorf("checking if pod name %s exists in database: %w", pod.Name(), err)
|
||||||
}
|
}
|
||||||
} else if check != 0 {
|
} else if check != 0 {
|
||||||
return fmt.Errorf("name \"%s\" is in use: %w", pod.Name(), define.ErrPodExists)
|
return fmt.Errorf("name %q is in use: %w", pod.Name(), define.ErrPodExists)
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := tx.Exec("INSERT INTO IDNamespace VALUES (?);", pod.ID()); err != nil {
|
if _, err := tx.Exec("INSERT INTO IDNamespace VALUES (?);", pod.ID()); err != nil {
|
||||||
@ -1816,6 +1816,17 @@ func (s *SQLiteState) AddVolume(volume *Volume) (defErr error) {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
// TODO: There has to be a better way of doing this
|
||||||
|
var check int
|
||||||
|
row := tx.QueryRow("SELECT 1 FROM VolumeConfig WHERE Name=?;", volume.Name())
|
||||||
|
if err := row.Scan(&check); err != nil {
|
||||||
|
if !errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return fmt.Errorf("checking if volume name %s exists in database: %w", volume.Name(), err)
|
||||||
|
}
|
||||||
|
} else if check != 0 {
|
||||||
|
return fmt.Errorf("name %q is in use: %w", volume.Name(), define.ErrVolumeExists)
|
||||||
|
}
|
||||||
|
|
||||||
if _, err := tx.Exec("INSERT INTO VolumeConfig VALUES (?, ?, ?);", volume.Name(), storageID, cfgJSON); err != nil {
|
if _, err := tx.Exec("INSERT INTO VolumeConfig VALUES (?, ?, ?);", volume.Name(), storageID, cfgJSON); err != nil {
|
||||||
return fmt.Errorf("adding volume %s config to database: %w", volume.Name(), err)
|
return fmt.Errorf("adding volume %s config to database: %w", volume.Name(), err)
|
||||||
}
|
}
|
||||||
|
@ -374,6 +374,17 @@ func (s *SQLiteState) addContainer(ctr *Container) (defErr error) {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
// TODO: There has to be a better way of doing this
|
||||||
|
var check int
|
||||||
|
row := tx.QueryRow("SELECT 1 FROM ContainerConfig WHERE Name=?;", ctr.Name())
|
||||||
|
if err := row.Scan(&check); err != nil {
|
||||||
|
if !errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return fmt.Errorf("checking if container name %s exists in database: %w", ctr.Name(), err)
|
||||||
|
}
|
||||||
|
} else if check != 0 {
|
||||||
|
return fmt.Errorf("name %q is in use: %w", ctr.Name(), define.ErrCtrExists)
|
||||||
|
}
|
||||||
|
|
||||||
if _, err := tx.Exec("INSERT INTO IDNamespace VALUES (?);", ctr.ID()); err != nil {
|
if _, err := tx.Exec("INSERT INTO IDNamespace VALUES (?);", ctr.ID()); err != nil {
|
||||||
return fmt.Errorf("adding container id to database: %w", err)
|
return fmt.Errorf("adding container id to database: %w", err)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user