Skip to content

Commit

Permalink
document and check minimum SQLite version
Browse files Browse the repository at this point in the history
Fixes #147
  • Loading branch information
scottlamb committed Aug 19, 2021
1 parent dd4a901 commit 4e77a26
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Each release is tagged in Git and on the Docker repository
* fix [#146](https://github.com/scottlamb/moonfire-nvr/issues/146): "init
segment fetch error" when browsers have cached data from `v0.6.4` and
before.
* fix [#147](https://github.com/scottlamb/moonfire-nvr/issues/147): confusing
`nvr init` failures when using very old versions of SQLite.

## `v0.6.5` (2021-08-13)

Expand Down
4 changes: 3 additions & 1 deletion guide/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,9 @@ To build the server, you will need the following C libraries installed:
timeouts for RTSP. For reliable reconnections on error, it's strongly
recommended to use ffmpeg library versions >= 55.1.101.

* [SQLite3](https://www.sqlite.org/).
* [SQLite3](https://www.sqlite.org/), at least version 3.14.0.
(You can skip this if you compile with `--features=bundled` and
don't mind the `moonfire-nvr sql` command not working.)
* [`ncursesw`](https://www.gnu.org/software/ncurses/), the UTF-8 version of
the `ncurses` library.
Expand Down
19 changes: 17 additions & 2 deletions server/db/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use crate::schema;
use crate::signal;
use base::clock::{self, Clocks};
use base::strutil::encode_size;
use failure::{bail, format_err, Error};
use failure::{bail, format_err, Error, ResultExt};
use fnv::{FnvHashMap, FnvHashSet};
use hashlink::LinkedHashMap;
use itertools::Itertools;
Expand Down Expand Up @@ -2117,13 +2117,27 @@ pub(crate) fn set_integrity_pragmas(conn: &mut rusqlite::Connection) -> Result<(
Ok(())
}

pub(crate) fn check_sqlite_version() -> Result<(), Error> {
// SQLite version 3.14.0 introduced the "without rowid" syntax used in the schema.
// https://www.sqlite.org/vtab.html#worid
if rusqlite::version_number() < 3014000 {
bail!(
"SQLite version {} is too old; need at least 3.14.0",
rusqlite::version()
);
}
Ok(())
}

/// Initializes a database.
/// Note this doesn't set journal options, so that it can be used on in-memory databases for
/// test code.
pub fn init(conn: &mut rusqlite::Connection) -> Result<(), Error> {
check_sqlite_version()?;
set_integrity_pragmas(conn)?;
let tx = conn.transaction()?;
tx.execute_batch(include_str!("schema.sql"))?;
tx.execute_batch(include_str!("schema.sql"))
.context("unable to create database schema")?;
{
let uuid = ::uuid::Uuid::new_v4();
let uuid_bytes = &uuid.as_bytes()[..];
Expand Down Expand Up @@ -2221,6 +2235,7 @@ impl<C: Clocks + Clone> Database<C> {
mut conn: rusqlite::Connection,
read_write: bool,
) -> Result<Database<C>, Error> {
check_sqlite_version()?;
set_integrity_pragmas(&mut conn)?;
check_schema_version(&conn)?;

Expand Down
1 change: 1 addition & 0 deletions server/db/upgrade/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ fn upgrade(args: &Args, target_ver: i32, conn: &mut rusqlite::Connection) -> Res
}

pub fn run(args: &Args, conn: &mut rusqlite::Connection) -> Result<(), Error> {
db::check_sqlite_version()?;
db::set_integrity_pragmas(conn)?;
set_journal_mode(&conn, args.preset_journal)?;
upgrade(args, db::EXPECTED_VERSION, conn)?;
Expand Down

0 comments on commit 4e77a26

Please sign in to comment.