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) 2018 The Moonfire NVR Authors; see AUTHORS and LICENSE.txt.
|
|
|
|
// SPDX-License-Identifier: GPL-v3.0-or-later WITH GPL-3.0-linking-exception.
|
2018-02-20 13:11:10 -05:00
|
|
|
|
|
|
|
/// Upgrades a version 2 schema to a version 3 schema.
|
2018-03-01 12:26:03 -05:00
|
|
|
/// Note that a version 2 schema is never actually used; so we know the upgrade from version 1 was
|
|
|
|
/// completed, and possibly an upgrade from 2 to 3 is half-finished.
|
2021-10-26 13:12:19 -04:00
|
|
|
use crate::db::{self, SqlUuid};
|
2018-12-28 13:21:49 -05:00
|
|
|
use crate::dir;
|
|
|
|
use crate::schema;
|
2021-02-17 01:15:54 -05:00
|
|
|
use failure::Error;
|
2020-03-19 23:46:25 -04:00
|
|
|
use rusqlite::params;
|
2019-07-12 00:59:01 -04:00
|
|
|
use std::os::unix::io::AsRawFd;
|
2018-03-01 12:26:03 -05:00
|
|
|
use std::sync::Arc;
|
2018-02-20 13:11:10 -05:00
|
|
|
|
2018-03-01 12:26:03 -05:00
|
|
|
/// Opens the sample file dir.
|
|
|
|
///
|
|
|
|
/// Makes a couple simplifying assumptions valid for version 2:
|
|
|
|
/// * there's only one dir.
|
|
|
|
/// * it has a last completed open.
|
|
|
|
fn open_sample_file_dir(tx: &rusqlite::Transaction) -> Result<Arc<dir::SampleFileDir>, Error> {
|
2021-10-26 13:12:19 -04:00
|
|
|
let (p, s_uuid, o_id, o_uuid, db_uuid): (String, SqlUuid, i32, SqlUuid, SqlUuid) = tx
|
|
|
|
.query_row(
|
2021-02-17 01:15:54 -05:00
|
|
|
r#"
|
|
|
|
select
|
|
|
|
s.path, s.uuid, s.last_complete_open_id, o.uuid, m.uuid
|
|
|
|
from
|
|
|
|
sample_file_dir s
|
|
|
|
join open o on (s.last_complete_open_id = o.id)
|
|
|
|
cross join meta m
|
|
|
|
"#,
|
|
|
|
params![],
|
|
|
|
|row| {
|
|
|
|
Ok((
|
|
|
|
row.get(0)?,
|
|
|
|
row.get(1)?,
|
|
|
|
row.get(2)?,
|
|
|
|
row.get(3)?,
|
|
|
|
row.get(4)?,
|
|
|
|
))
|
|
|
|
},
|
|
|
|
)?;
|
2018-03-01 12:26:03 -05:00
|
|
|
let mut meta = schema::DirMeta::default();
|
|
|
|
meta.db_uuid.extend_from_slice(&db_uuid.0.as_bytes()[..]);
|
|
|
|
meta.dir_uuid.extend_from_slice(&s_uuid.0.as_bytes()[..]);
|
|
|
|
{
|
2020-11-23 03:23:03 -05:00
|
|
|
let open = meta.last_complete_open.set_default();
|
2018-03-01 12:26:03 -05:00
|
|
|
open.id = o_id as u32;
|
|
|
|
open.uuid.extend_from_slice(&o_uuid.0.as_bytes()[..]);
|
|
|
|
}
|
|
|
|
dir::SampleFileDir::open(&p, &meta)
|
|
|
|
}
|
2018-02-20 13:11:10 -05:00
|
|
|
|
2018-03-01 12:26:03 -05:00
|
|
|
pub fn run(_args: &super::Args, tx: &rusqlite::Transaction) -> Result<(), Error> {
|
|
|
|
let d = open_sample_file_dir(&tx)?;
|
2021-02-17 01:15:54 -05:00
|
|
|
let mut stmt = tx.prepare(
|
|
|
|
r#"
|
2018-03-01 12:26:03 -05:00
|
|
|
select
|
|
|
|
composite_id,
|
|
|
|
sample_file_uuid
|
|
|
|
from
|
|
|
|
recording_playback
|
2021-02-17 01:15:54 -05:00
|
|
|
"#,
|
|
|
|
)?;
|
2020-03-19 23:46:25 -04:00
|
|
|
let mut rows = stmt.query(params![])?;
|
2019-05-31 19:19:04 -04:00
|
|
|
while let Some(row) = rows.next()? {
|
|
|
|
let id = db::CompositeId(row.get(0)?);
|
2021-10-26 13:12:19 -04:00
|
|
|
let sample_file_uuid: SqlUuid = row.get(1)?;
|
2019-07-22 01:40:01 -04:00
|
|
|
let from_path = super::UuidPath::from(sample_file_uuid.0);
|
2019-07-12 00:59:01 -04:00
|
|
|
let to_path = crate::dir::CompositeIdPath::from(id);
|
2021-02-17 01:15:54 -05:00
|
|
|
if let Err(e) = nix::fcntl::renameat(
|
|
|
|
Some(d.fd.as_raw_fd()),
|
|
|
|
&from_path,
|
|
|
|
Some(d.fd.as_raw_fd()),
|
|
|
|
&to_path,
|
|
|
|
) {
|
2021-07-09 18:01:15 -04:00
|
|
|
if e == nix::Error::ENOENT {
|
2021-02-17 01:15:54 -05:00
|
|
|
continue; // assume it was already moved.
|
2018-03-01 12:26:03 -05:00
|
|
|
}
|
2021-05-17 17:31:50 -04:00
|
|
|
return Err(e.into());
|
2018-03-01 12:26:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// These create statements match the schema.sql when version 3 was the latest.
|
2021-02-17 01:15:54 -05:00
|
|
|
tx.execute_batch(
|
|
|
|
r#"
|
2018-03-01 12:26:03 -05:00
|
|
|
alter table recording_playback rename to old_recording_playback;
|
|
|
|
create table recording_playback (
|
|
|
|
composite_id integer primary key references recording (composite_id),
|
|
|
|
video_index blob not null check (length(video_index) > 0)
|
|
|
|
);
|
|
|
|
insert into recording_playback
|
|
|
|
select
|
|
|
|
composite_id,
|
|
|
|
video_index
|
|
|
|
from
|
|
|
|
old_recording_playback;
|
|
|
|
drop table old_recording_playback;
|
2018-03-22 11:45:32 -04:00
|
|
|
drop table old_recording;
|
|
|
|
drop table old_camera;
|
|
|
|
drop table old_video_sample_entry;
|
2021-02-17 01:15:54 -05:00
|
|
|
"#,
|
|
|
|
)?;
|
2018-03-01 12:26:03 -05:00
|
|
|
Ok(())
|
2018-02-20 13:11:10 -05:00
|
|
|
}
|