mirror of
https://github.com/scottlamb/moonfire-nvr.git
synced 2025-04-13 07:50:24 -04:00
compare schema version on "moonfire-nvr check"
This commit is contained in:
parent
51f43e9124
commit
cdd878f452
@ -50,6 +50,14 @@ pub struct Options {
|
|||||||
pub fn run(conn: &rusqlite::Connection, opts: &Options) -> Result<i32, Error> {
|
pub fn run(conn: &rusqlite::Connection, opts: &Options) -> Result<i32, Error> {
|
||||||
let mut printed_error = false;
|
let mut printed_error = false;
|
||||||
|
|
||||||
|
// Compare stated schema version.
|
||||||
|
if let Err(e) = db::check_schema_version(conn) {
|
||||||
|
error!("Schema version is not as expected:\n{}", e);
|
||||||
|
printed_error = true;
|
||||||
|
} else {
|
||||||
|
info!("Schema at expected version {}.", db::EXPECTED_VERSION);
|
||||||
|
}
|
||||||
|
|
||||||
// Compare schemas.
|
// Compare schemas.
|
||||||
{
|
{
|
||||||
let mut expected = rusqlite::Connection::open_in_memory()?;
|
let mut expected = rusqlite::Connection::open_in_memory()?;
|
||||||
@ -57,13 +65,16 @@ pub fn run(conn: &rusqlite::Connection, opts: &Options) -> Result<i32, Error> {
|
|||||||
if let Some(diffs) = compare::get_diffs("actual", conn, "expected", &expected)? {
|
if let Some(diffs) = compare::get_diffs("actual", conn, "expected", &expected)? {
|
||||||
error!("Schema is not as expected:\n{}", &diffs);
|
error!("Schema is not as expected:\n{}", &diffs);
|
||||||
printed_error = true;
|
printed_error = true;
|
||||||
warn!("The following analysis may be incorrect or encounter errors due to schema differences.");
|
|
||||||
} else {
|
} else {
|
||||||
println!("Schema is as expected.");
|
println!("Schema is as expected.");
|
||||||
}
|
}
|
||||||
info!("Done comparing schemas.");
|
info!("Done comparing schemas.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if printed_error {
|
||||||
|
warn!("The following analysis may be incorrect or encounter errors due to schema differences.");
|
||||||
|
}
|
||||||
|
|
||||||
let db_uuid = raw::get_db_uuid(&conn)?;
|
let db_uuid = raw::get_db_uuid(&conn)?;
|
||||||
|
|
||||||
// Scan directories.
|
// Scan directories.
|
||||||
|
@ -2025,6 +2025,28 @@ pub fn get_schema_version(conn: &rusqlite::Connection) -> Result<Option<i32>, Er
|
|||||||
|row| row.get(0))?))
|
|row| row.get(0))?))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Checks that the schema version in the given database is as expected.
|
||||||
|
pub(crate) fn check_schema_version(conn: &rusqlite::Connection) -> Result<(), Error> {
|
||||||
|
let ver = get_schema_version(conn)?.ok_or_else(|| format_err!(
|
||||||
|
"no such table: version. \
|
||||||
|
\
|
||||||
|
If you are starting from an \
|
||||||
|
empty database, see README.md to complete the \
|
||||||
|
installation. If you are starting from a database \
|
||||||
|
that predates schema versioning, see guide/schema.md."))?;
|
||||||
|
if ver < EXPECTED_VERSION {
|
||||||
|
bail!("Database schema version {} is too old (expected {}); \
|
||||||
|
see upgrade instructions in guide/upgrade.md.",
|
||||||
|
ver, EXPECTED_VERSION);
|
||||||
|
} else if ver > EXPECTED_VERSION {
|
||||||
|
bail!("Database schema version {} is too new (expected {}); \
|
||||||
|
must use a newer binary to match.", ver,
|
||||||
|
EXPECTED_VERSION);
|
||||||
|
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
/// The recording database. Abstracts away SQLite queries. Also maintains in-memory state
|
/// The recording database. Abstracts away SQLite queries. Also maintains in-memory state
|
||||||
/// (loaded on startup, and updated on successful commit) to avoid expensive scans over the
|
/// (loaded on startup, and updated on successful commit) to avoid expensive scans over the
|
||||||
/// recording table on common queries.
|
/// recording table on common queries.
|
||||||
@ -2060,25 +2082,7 @@ impl<C: Clocks + Clone> Database<C> {
|
|||||||
pub fn new(clocks: C, mut conn: rusqlite::Connection,
|
pub fn new(clocks: C, mut conn: rusqlite::Connection,
|
||||||
read_write: bool) -> Result<Database<C>, Error> {
|
read_write: bool) -> Result<Database<C>, Error> {
|
||||||
set_integrity_pragmas(&mut conn)?;
|
set_integrity_pragmas(&mut conn)?;
|
||||||
{
|
check_schema_version(&conn)?;
|
||||||
let ver = get_schema_version(&conn)?.ok_or_else(|| format_err!(
|
|
||||||
"no such table: version. \
|
|
||||||
\
|
|
||||||
If you are starting from an \
|
|
||||||
empty database, see README.md to complete the \
|
|
||||||
installation. If you are starting from a database \
|
|
||||||
that predates schema versioning, see guide/schema.md."))?;
|
|
||||||
if ver < EXPECTED_VERSION {
|
|
||||||
bail!("Database schema version {} is too old (expected {}); \
|
|
||||||
see upgrade instructions in guide/upgrade.md.",
|
|
||||||
ver, EXPECTED_VERSION);
|
|
||||||
} else if ver > EXPECTED_VERSION {
|
|
||||||
bail!("Database schema version {} is too new (expected {}); \
|
|
||||||
must use a newer binary to match.", ver,
|
|
||||||
EXPECTED_VERSION);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Note: the meta check comes after the version check to improve the error message when
|
// Note: the meta check comes after the version check to improve the error message when
|
||||||
// trying to open a version 0 or version 1 database (which lacked the meta table).
|
// trying to open a version 0 or version 1 database (which lacked the meta table).
|
||||||
|
Loading…
x
Reference in New Issue
Block a user