2020-03-02 01:53:41 -05:00
|
|
|
// This file is part of Moonfire NVR, a security camera network video recorder.
|
2021-02-17 16:28:48 -05:00
|
|
|
// Copyright (C) 2020 The Moonfire NVR Authors; see AUTHORS and LICENSE.txt.
|
|
|
|
// SPDX-License-Identifier: GPL-v3.0-or-later WITH GPL-3.0-linking-exception.
|
2017-01-16 17:21:08 -05:00
|
|
|
|
2018-02-21 01:46:14 -05:00
|
|
|
use failure::Error;
|
2018-12-28 22:53:29 -05:00
|
|
|
use log::info;
|
2020-04-18 01:41:55 -04:00
|
|
|
use std::path::PathBuf;
|
2021-02-17 01:15:54 -05:00
|
|
|
use structopt::StructOpt;
|
2020-04-18 01:41:55 -04:00
|
|
|
|
|
|
|
#[derive(StructOpt)]
|
|
|
|
pub struct Args {
|
|
|
|
/// Directory holding the SQLite3 index database.
|
2021-02-17 01:15:54 -05:00
|
|
|
#[structopt(
|
|
|
|
long,
|
|
|
|
default_value = "/var/lib/moonfire-nvr/db",
|
|
|
|
value_name = "path",
|
|
|
|
parse(from_os_str)
|
|
|
|
)]
|
2020-04-18 01:41:55 -04:00
|
|
|
db_dir: PathBuf,
|
2017-01-16 17:21:08 -05:00
|
|
|
}
|
|
|
|
|
2021-02-11 13:45:56 -05:00
|
|
|
pub fn run(args: &Args) -> Result<i32, Error> {
|
2020-04-18 01:41:55 -04:00
|
|
|
let (_db_dir, mut conn) = super::open_conn(&args.db_dir, super::OpenMode::Create)?;
|
2017-01-16 17:21:08 -05:00
|
|
|
|
|
|
|
// Check if the database has already been initialized.
|
|
|
|
let cur_ver = db::get_schema_version(&conn)?;
|
|
|
|
if let Some(v) = cur_ver {
|
|
|
|
info!("Database is already initialized with schema version {}.", v);
|
2021-02-11 13:45:56 -05:00
|
|
|
return Ok(0);
|
2017-01-16 17:21:08 -05:00
|
|
|
}
|
|
|
|
|
2021-02-11 00:44:06 -05:00
|
|
|
// Use WAL mode (which is the most efficient way to preserve database integrity) with a large
|
|
|
|
// page size (so reading large recording_playback rows doesn't require as many seeks). Changing
|
|
|
|
// the page size requires doing a vacuum in non-WAL mode. This will be cheap on an empty
|
|
|
|
// database. https://www.sqlite.org/pragma.html#pragma_page_size
|
2021-02-17 01:15:54 -05:00
|
|
|
conn.execute_batch(
|
|
|
|
r#"
|
2021-02-11 00:44:06 -05:00
|
|
|
pragma journal_mode = delete;
|
2017-01-16 17:21:08 -05:00
|
|
|
pragma page_size = 16384;
|
2021-02-11 00:44:06 -05:00
|
|
|
vacuum;
|
|
|
|
pragma journal_mode = wal;
|
2021-02-17 01:15:54 -05:00
|
|
|
"#,
|
|
|
|
)?;
|
2018-03-23 16:31:23 -04:00
|
|
|
db::init(&mut conn)?;
|
2017-01-16 17:21:08 -05:00
|
|
|
info!("Database initialized.");
|
2021-02-11 13:45:56 -05:00
|
|
|
Ok(0)
|
2017-01-16 17:21:08 -05:00
|
|
|
}
|