2019-07-05 00:22:45 -04:00
|
|
|
// This file is part of Moonfire NVR, a security camera network video recorder.
|
2021-02-17 16:28:48 -05:00
|
|
|
// Copyright (C) 2019 The Moonfire NVR Authors; see AUTHORS and LICENSE.txt.
|
|
|
|
// SPDX-License-Identifier: GPL-v3.0-or-later WITH GPL-3.0-linking-exception.
|
2019-06-06 19:18:13 -04:00
|
|
|
|
|
|
|
/// Upgrades a version 3 schema to a version 4 schema.
|
|
|
|
use failure::Error;
|
|
|
|
|
|
|
|
pub fn run(_args: &super::Args, tx: &rusqlite::Transaction) -> Result<(), Error> {
|
|
|
|
// These create statements match the schema.sql when version 4 was the latest.
|
2021-02-17 01:15:54 -05:00
|
|
|
tx.execute_batch(
|
|
|
|
r#"
|
2019-12-28 08:48:08 -05:00
|
|
|
alter table meta add column max_signal_changes integer check (max_signal_changes >= 0);
|
|
|
|
|
2019-06-06 19:18:13 -04:00
|
|
|
create table signal (
|
|
|
|
id integer primary key,
|
|
|
|
source_uuid blob not null check (length(source_uuid) = 16),
|
|
|
|
type_uuid blob not null check (length(type_uuid) = 16),
|
|
|
|
short_name not null,
|
|
|
|
unique (source_uuid, type_uuid)
|
|
|
|
);
|
|
|
|
|
|
|
|
create table signal_type_enum (
|
|
|
|
type_uuid blob not null check (length(type_uuid) = 16),
|
|
|
|
value integer not null check (value > 0 and value < 16),
|
|
|
|
name text not null,
|
|
|
|
motion int not null check (motion in (0, 1)) default 0,
|
|
|
|
color text
|
|
|
|
);
|
|
|
|
|
2019-06-20 19:03:59 -04:00
|
|
|
create table signal_change (
|
2019-06-14 00:55:15 -04:00
|
|
|
time_90k integer primary key,
|
2019-06-20 19:03:59 -04:00
|
|
|
changes blob not null
|
2019-06-06 19:18:13 -04:00
|
|
|
);
|
2019-06-19 18:17:50 -04:00
|
|
|
|
2019-06-20 19:03:59 -04:00
|
|
|
alter table user add column permissions blob not null default X'';
|
|
|
|
alter table user_session add column permissions blob not null default X'';
|
|
|
|
|
|
|
|
-- Set permissions to "view_video" on existing users and sessions to preserve their
|
|
|
|
-- behavior. Newly created users won't have prepopulated permissions like this.
|
|
|
|
update user set permissions = X'0801';
|
|
|
|
update user_session set permissions = X'0801';
|
2019-07-01 00:54:52 -04:00
|
|
|
|
2019-07-01 01:33:36 -04:00
|
|
|
alter table camera rename to old_camera;
|
|
|
|
create table camera (
|
|
|
|
id integer primary key,
|
|
|
|
uuid blob unique not null check (length(uuid) = 16),
|
|
|
|
short_name text not null,
|
|
|
|
description text,
|
|
|
|
onvif_host text,
|
|
|
|
username text,
|
|
|
|
password text
|
|
|
|
);
|
|
|
|
insert into camera
|
|
|
|
select
|
|
|
|
id,
|
|
|
|
uuid,
|
|
|
|
short_name,
|
|
|
|
description,
|
|
|
|
host,
|
|
|
|
username,
|
|
|
|
password
|
|
|
|
from
|
|
|
|
old_camera;
|
|
|
|
|
2021-10-26 17:00:18 -04:00
|
|
|
create table signal_camera (
|
|
|
|
signal_id integer references signal (id),
|
|
|
|
camera_id integer references camera (id),
|
|
|
|
type integer not null,
|
|
|
|
primary key (signal_id, camera_id)
|
|
|
|
) without rowid;
|
|
|
|
|
2019-07-01 00:54:52 -04:00
|
|
|
alter table stream rename to old_stream;
|
|
|
|
create table stream (
|
|
|
|
id integer primary key,
|
|
|
|
camera_id integer not null references camera (id),
|
|
|
|
sample_file_dir_id integer references sample_file_dir (id),
|
|
|
|
type text not null check (type in ('main', 'sub')),
|
|
|
|
record integer not null check (record in (1, 0)),
|
|
|
|
rtsp_url text not null,
|
|
|
|
retain_bytes integer not null check (retain_bytes >= 0),
|
|
|
|
flush_if_sec integer not null,
|
|
|
|
next_recording_id integer not null check (next_recording_id >= 0),
|
|
|
|
unique (camera_id, type)
|
|
|
|
);
|
|
|
|
insert into stream
|
|
|
|
select
|
|
|
|
s.id,
|
|
|
|
s.camera_id,
|
|
|
|
s.sample_file_dir_id,
|
|
|
|
s.type,
|
|
|
|
s.record,
|
2019-07-01 01:33:36 -04:00
|
|
|
'rtsp://' || c.onvif_host || s.rtsp_path as rtsp_url,
|
2019-07-01 00:54:52 -04:00
|
|
|
retain_bytes,
|
|
|
|
flush_if_sec,
|
|
|
|
next_recording_id
|
|
|
|
from
|
|
|
|
old_stream s join camera c on (s.camera_id = c.id);
|
|
|
|
|
2019-07-01 01:33:36 -04:00
|
|
|
alter table recording rename to old_recording;
|
|
|
|
create table recording (
|
|
|
|
composite_id integer primary key,
|
|
|
|
open_id integer not null,
|
|
|
|
stream_id integer not null references stream (id),
|
|
|
|
run_offset integer not null,
|
|
|
|
flags integer not null,
|
|
|
|
sample_file_bytes integer not null check (sample_file_bytes > 0),
|
|
|
|
start_time_90k integer not null check (start_time_90k > 0),
|
|
|
|
duration_90k integer not null
|
|
|
|
check (duration_90k >= 0 and duration_90k < 5*60*90000),
|
|
|
|
video_samples integer not null check (video_samples > 0),
|
|
|
|
video_sync_samples integer not null check (video_sync_samples > 0),
|
|
|
|
video_sample_entry_id integer references video_sample_entry (id),
|
|
|
|
check (composite_id >> 32 = stream_id)
|
2019-07-01 00:54:52 -04:00
|
|
|
);
|
2019-07-01 01:33:36 -04:00
|
|
|
insert into recording select
|
|
|
|
composite_id,
|
|
|
|
open_id,
|
|
|
|
stream_id,
|
|
|
|
run_offset,
|
|
|
|
flags,
|
|
|
|
sample_file_bytes,
|
|
|
|
start_time_90k,
|
|
|
|
duration_90k,
|
|
|
|
video_samples,
|
|
|
|
video_sync_samples,
|
|
|
|
video_sample_entry_id
|
|
|
|
from old_recording;
|
|
|
|
drop index recording_cover;
|
|
|
|
create index recording_cover on recording (
|
|
|
|
stream_id,
|
|
|
|
start_time_90k,
|
|
|
|
open_id,
|
|
|
|
duration_90k,
|
|
|
|
video_samples,
|
|
|
|
video_sync_samples,
|
|
|
|
video_sample_entry_id,
|
|
|
|
sample_file_bytes,
|
|
|
|
run_offset,
|
|
|
|
flags
|
|
|
|
);
|
|
|
|
|
|
|
|
alter table recording_integrity rename to old_recording_integrity;
|
|
|
|
create table recording_integrity (
|
|
|
|
composite_id integer primary key references recording (composite_id),
|
|
|
|
local_time_delta_90k integer,
|
|
|
|
local_time_since_open_90k integer,
|
|
|
|
wall_time_delta_90k integer,
|
|
|
|
sample_file_sha1 blob check (length(sample_file_sha1) <= 20)
|
|
|
|
);
|
|
|
|
insert into recording_integrity select * from old_recording_integrity;
|
|
|
|
|
|
|
|
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 * from old_recording_playback;
|
|
|
|
|
|
|
|
drop table old_recording_playback;
|
|
|
|
drop table old_recording_integrity;
|
|
|
|
drop table old_recording;
|
|
|
|
drop table old_stream;
|
2019-07-01 00:54:52 -04:00
|
|
|
drop table old_camera;
|
2019-07-11 11:07:40 -04:00
|
|
|
|
|
|
|
-- This was supposed to be present in version 2, but the upgrade procedure used to miss it.
|
|
|
|
-- Catch up so we know a version 4 database is right.
|
|
|
|
create index if not exists user_session_uid on user_session (user_id);
|
2021-02-17 01:15:54 -05:00
|
|
|
"#,
|
|
|
|
)?;
|
2019-06-06 19:18:13 -04:00
|
|
|
Ok(())
|
|
|
|
}
|