generated from CubeCraft-Creations/Tracehound
CUB-228: add battery_calibration_offset to cameras table
- Add column to 001_create_tables.sql for fresh databases - Add migration 002 for existing databases (idempotent via pragma_table_info check) - Implement runIncrementalMigrations in db.go - Add BatteryCalibrationOffset to Camera model - Update all camera SELECT queries (cameras List, detail, MQTT subscriber getCamera, register)
This commit is contained in:
@@ -14,6 +14,9 @@ import (
|
||||
//go:embed migrations/001_create_tables.sql
|
||||
var migration001 string
|
||||
|
||||
//go:embed migrations/002_add_camera_calibration.sql
|
||||
var migration002 string
|
||||
|
||||
// DB wraps the sql.DB with connection-level settings.
|
||||
type DB struct {
|
||||
*sql.DB
|
||||
@@ -62,6 +65,12 @@ func Open(path string) (*DB, error) {
|
||||
return nil, err
|
||||
}
|
||||
log.Println("Migrations complete")
|
||||
} else {
|
||||
// Run incremental migrations on existing databases
|
||||
if err := runIncrementalMigrations(db); err != nil {
|
||||
db.Close()
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return &DB{db}, nil
|
||||
@@ -83,6 +92,25 @@ func migrate(db *sql.DB, sql string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// runIncrementalMigrations applies migrations that haven't been run yet on
|
||||
// an existing database (one where the 001 schema already exists).
|
||||
func runIncrementalMigrations(db *sql.DB) error {
|
||||
// Migration 002: add battery_calibration_offset if it doesn't exist
|
||||
var colCount int
|
||||
err := db.QueryRow(`SELECT COUNT(*) FROM pragma_table_info('cameras') WHERE name = 'battery_calibration_offset'`).Scan(&colCount)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if colCount == 0 {
|
||||
log.Println("Running migration 002: add battery_calibration_offset")
|
||||
if err := migrate(db, migration002); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// splitSQL splits a SQL string on semicolons, respecting quoted strings.
|
||||
func splitSQL(sql string) []string {
|
||||
var stmts []string
|
||||
|
||||
Reference in New Issue
Block a user