48 lines
1.0 KiB
Go
48 lines
1.0 KiB
Go
package migrate
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
)
|
|
|
|
// ValidateOwnershipSchema ensures the runtime DB matches the ownership workflow schema.
|
|
func ValidateOwnershipSchema(db *sql.DB) error {
|
|
required := []struct {
|
|
table string
|
|
column string
|
|
}{
|
|
{table: "machines", column: "vendor_serial"},
|
|
{table: "parts", column: "vendor_serial"},
|
|
{table: "machine_firmware_states", column: "machine_id"},
|
|
}
|
|
|
|
for _, item := range required {
|
|
ok, err := columnExists(db, item.table, item.column)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !ok {
|
|
return fmt.Errorf("database schema is outdated: missing %s.%s; run `make db-reset`", item.table, item.column)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func columnExists(db *sql.DB, table, column string) (bool, error) {
|
|
var count int
|
|
err := db.QueryRow(
|
|
`SELECT COUNT(*)
|
|
FROM information_schema.columns
|
|
WHERE table_schema = DATABASE()
|
|
AND table_name = ?
|
|
AND column_name = ?`,
|
|
table,
|
|
column,
|
|
).Scan(&count)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return count > 0, nil
|
|
}
|