mirror of https://github.com/containers/podman.git
Fix SQLite DB schema migration code
It now can safely run on bare databases, before any tables are created. Signed-off-by: Matthew Heon <matthew.heon@pm.me>
This commit is contained in:
parent
f1bcd0d781
commit
94f905a503
|
@ -77,15 +77,16 @@ func NewSqliteState(runtime *Runtime) (_ State, defErr error) {
|
||||||
return nil, fmt.Errorf("setting full fsync mode in db: %w", err)
|
return nil, fmt.Errorf("setting full fsync mode in db: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Migrate schema (if necessary)
|
||||||
|
if err := state.migrateSchemaIfNecessary(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
// Set up tables
|
// Set up tables
|
||||||
if err := sqliteInitTables(state.conn); err != nil {
|
if err := sqliteInitTables(state.conn); err != nil {
|
||||||
return nil, fmt.Errorf("creating tables: %w", err)
|
return nil, fmt.Errorf("creating tables: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := state.migrateSchemaIfNecessary(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
state.valid = true
|
state.valid = true
|
||||||
state.runtime = runtime
|
state.runtime = runtime
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,20 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *SQLiteState) migrateSchemaIfNecessary() (defErr error) {
|
func (s *SQLiteState) migrateSchemaIfNecessary() (defErr error) {
|
||||||
|
// First, check if the DBConfig table exists
|
||||||
|
checkRow := s.conn.QueryRow("SELECT 1 FROM sqlite_master WHERE type='table' AND name='DBConfig';")
|
||||||
|
var check int
|
||||||
|
if err := checkRow.Scan(&check); err != nil {
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return fmt.Errorf("checking if DB config table exists: %w", err)
|
||||||
|
}
|
||||||
|
if check != 1 {
|
||||||
|
// Table does not exist, fresh database, no need to migrate.
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
row := s.conn.QueryRow("SELECT SchemaVersion FROM DBConfig;")
|
row := s.conn.QueryRow("SELECT SchemaVersion FROM DBConfig;")
|
||||||
var schemaVer int
|
var schemaVer int
|
||||||
if err := row.Scan(&schemaVer); err != nil {
|
if err := row.Scan(&schemaVer); err != nil {
|
||||||
|
@ -24,6 +38,7 @@ func (s *SQLiteState) migrateSchemaIfNecessary() (defErr error) {
|
||||||
// Schema was just created, so it has to be the latest.
|
// Schema was just created, so it has to be the latest.
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
return fmt.Errorf("scanning schema version from DB config: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the schema version 0 or less, it's invalid
|
// If the schema version 0 or less, it's invalid
|
||||||
|
|
Loading…
Reference in New Issue