-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathdb_embedded_migrations.h
More file actions
917 lines (832 loc) · 34.5 KB
/
db_embedded_migrations.h
File metadata and controls
917 lines (832 loc) · 34.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
/**
* @file db_embedded_migrations.h
* @brief Auto-generated embedded SQL migrations.
* DO NOT EDIT - generated by scripts/generate_embedded_migrations.py
*/
#ifndef DB_EMBEDDED_MIGRATIONS_H
#define DB_EMBEDDED_MIGRATIONS_H
#include "database/sqlite_migrate.h"
static const char migration_0001_up[] =
"-- Streams configuration table\n"
"CREATE TABLE IF NOT EXISTS streams (\n"
" id INTEGER PRIMARY KEY AUTOINCREMENT,\n"
" name TEXT NOT NULL UNIQUE,\n"
" url TEXT NOT NULL,\n"
" enabled INTEGER DEFAULT 1,\n"
" streaming_enabled INTEGER DEFAULT 1,\n"
" width INTEGER DEFAULT 1280,\n"
" height INTEGER DEFAULT 720,\n"
" fps INTEGER DEFAULT 30,\n"
" codec TEXT DEFAULT 'h264',\n"
" priority INTEGER DEFAULT 5,\n"
" record INTEGER DEFAULT 1,\n"
" segment_duration INTEGER DEFAULT 900\n"
");\n"
"\n"
"-- Recordings table\n"
"CREATE TABLE IF NOT EXISTS recordings (\n"
" id INTEGER PRIMARY KEY AUTOINCREMENT,\n"
" stream_name TEXT NOT NULL,\n"
" file_path TEXT NOT NULL,\n"
" start_time INTEGER NOT NULL,\n"
" end_time INTEGER,\n"
" size_bytes INTEGER DEFAULT 0,\n"
" width INTEGER,\n"
" height INTEGER,\n"
" fps INTEGER,\n"
" codec TEXT,\n"
" is_complete INTEGER DEFAULT 0,\n"
" created_at INTEGER DEFAULT (strftime('%s', 'now'))\n"
");\n"
"\n"
"-- Events table\n"
"CREATE TABLE IF NOT EXISTS events (\n"
" id INTEGER PRIMARY KEY AUTOINCREMENT,\n"
" type INTEGER NOT NULL,\n"
" timestamp INTEGER NOT NULL,\n"
" stream_name TEXT,\n"
" description TEXT NOT NULL,\n"
" details TEXT\n"
");\n"
"\n"
"-- Create index for efficient queries\n"
"CREATE INDEX IF NOT EXISTS idx_recordings_stream_name ON recordings(stream_name);\n"
"CREATE INDEX IF NOT EXISTS idx_recordings_start_time ON recordings(start_time);";
static const char migration_0001_down[] =
"DROP TABLE IF EXISTS events;\n"
"DROP INDEX IF EXISTS idx_recordings_start_time;\n"
"DROP INDEX IF EXISTS idx_recordings_stream_name;\n"
"DROP TABLE IF EXISTS recordings;\n"
"DROP TABLE IF EXISTS streams;";
static const char migration_0002_up[] =
"ALTER TABLE streams ADD COLUMN detection_based_recording INTEGER DEFAULT 0;\n"
"ALTER TABLE streams ADD COLUMN detection_model TEXT DEFAULT '';\n"
"ALTER TABLE streams ADD COLUMN detection_threshold REAL DEFAULT 0.5;\n"
"ALTER TABLE streams ADD COLUMN detection_interval INTEGER DEFAULT 10;\n"
"ALTER TABLE streams ADD COLUMN pre_detection_buffer INTEGER DEFAULT 0;\n"
"ALTER TABLE streams ADD COLUMN post_detection_buffer INTEGER DEFAULT 3;";
static const char migration_0002_down[] =
"-- SQLite doesn't support DROP COLUMN before 3.35.0\n"
"-- For older versions, we'd need to recreate the table\n"
"-- This is a no-op for safety on older SQLite versions\n"
"SELECT 1;";
static const char migration_0003_up[] =
"ALTER TABLE streams ADD COLUMN protocol INTEGER DEFAULT 0;\n"
"ALTER TABLE streams ADD COLUMN is_onvif INTEGER DEFAULT 0;";
static const char migration_0003_down[] =
"SELECT 1;";
static const char migration_0004_up[] =
"ALTER TABLE streams ADD COLUMN record_audio INTEGER DEFAULT 1;";
static const char migration_0004_down[] =
"SELECT 1;";
static const char migration_0005_up[] =
"CREATE TABLE IF NOT EXISTS detections (\n"
" id INTEGER PRIMARY KEY AUTOINCREMENT,\n"
" stream_name TEXT NOT NULL,\n"
" timestamp INTEGER NOT NULL,\n"
" label TEXT NOT NULL,\n"
" confidence REAL NOT NULL,\n"
" x REAL,\n"
" y REAL,\n"
" width REAL,\n"
" height REAL,\n"
" recording_id INTEGER,\n"
" created_at INTEGER DEFAULT (strftime('%s', 'now')),\n"
" FOREIGN KEY (recording_id) REFERENCES recordings(id)\n"
");\n"
"\n"
"CREATE INDEX IF NOT EXISTS idx_detections_stream ON detections(stream_name);\n"
"CREATE INDEX IF NOT EXISTS idx_detections_timestamp ON detections(timestamp);\n"
"CREATE INDEX IF NOT EXISTS idx_detections_label ON detections(label);";
static const char migration_0005_down[] =
"DROP INDEX IF EXISTS idx_detections_label;\n"
"DROP INDEX IF EXISTS idx_detections_timestamp;\n"
"DROP INDEX IF EXISTS idx_detections_stream;\n"
"DROP TABLE IF EXISTS detections;";
static const char migration_0006_up[] =
"CREATE TABLE IF NOT EXISTS users (\n"
" id INTEGER PRIMARY KEY AUTOINCREMENT,\n"
" username TEXT NOT NULL UNIQUE,\n"
" password_hash TEXT NOT NULL,\n"
" salt TEXT NOT NULL,\n"
" role TEXT NOT NULL,\n"
" email TEXT,\n"
" created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now')),\n"
" updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now')),\n"
" last_login INTEGER,\n"
" is_active INTEGER DEFAULT 1,\n"
" api_key TEXT\n"
");\n"
"\n"
"-- Create indexes for faster lookups\n"
"CREATE UNIQUE INDEX IF NOT EXISTS idx_users_username ON users (username);\n"
"CREATE INDEX IF NOT EXISTS idx_users_api_key ON users (api_key);\n"
"\n"
"-- Create sessions table for token-based authentication\n"
"CREATE TABLE IF NOT EXISTS sessions (\n"
" id INTEGER PRIMARY KEY AUTOINCREMENT,\n"
" user_id INTEGER NOT NULL,\n"
" token TEXT NOT NULL UNIQUE,\n"
" created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now')),\n"
" expires_at INTEGER NOT NULL,\n"
" FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE\n"
");\n"
"\n"
"CREATE INDEX IF NOT EXISTS idx_sessions_token ON sessions (token);\n"
"CREATE INDEX IF NOT EXISTS idx_sessions_user_id ON sessions (user_id);";
static const char migration_0006_down[] =
"DROP TABLE IF EXISTS sessions;\n"
"DROP TABLE IF EXISTS users;";
static const char migration_0007_up[] =
"CREATE TABLE IF NOT EXISTS motion_settings (\n"
" id INTEGER PRIMARY KEY AUTOINCREMENT,\n"
" stream_name TEXT NOT NULL UNIQUE,\n"
" enabled INTEGER DEFAULT 0,\n"
" sensitivity REAL DEFAULT 0.5,\n"
" threshold REAL DEFAULT 0.01,\n"
" cooldown_seconds INTEGER DEFAULT 5,\n"
" min_motion_frames INTEGER DEFAULT 3,\n"
" created_at INTEGER DEFAULT (strftime('%s', 'now')),\n"
" updated_at INTEGER DEFAULT (strftime('%s', 'now'))\n"
");";
static const char migration_0007_down[] =
"DROP TABLE IF EXISTS motion_settings;";
static const char migration_0008_up[] =
"CREATE TABLE IF NOT EXISTS detection_zones (\n"
" id TEXT PRIMARY KEY,\n"
" stream_name TEXT NOT NULL,\n"
" name TEXT NOT NULL,\n"
" enabled INTEGER DEFAULT 1,\n"
" color TEXT DEFAULT '#3b82f6',\n"
" polygon TEXT NOT NULL,\n"
" filter_classes TEXT DEFAULT '',\n"
" min_confidence REAL DEFAULT 0.0,\n"
" created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now')),\n"
" updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now')),\n"
" FOREIGN KEY (stream_name) REFERENCES streams(name) ON DELETE CASCADE\n"
");\n"
"\n"
"CREATE INDEX IF NOT EXISTS idx_zones_stream ON detection_zones(stream_name);";
static const char migration_0008_down[] =
"DROP INDEX IF EXISTS idx_zones_stream;\n"
"DROP TABLE IF EXISTS detection_zones;";
static const char migration_0009_up[] =
"ALTER TABLE detections ADD COLUMN track_id INTEGER DEFAULT -1;\n"
"ALTER TABLE detections ADD COLUMN zone_id TEXT DEFAULT '';";
static const char migration_0009_down[] =
"SELECT 1;";
static const char migration_0010_up[] =
"ALTER TABLE recordings ADD COLUMN trigger_type TEXT DEFAULT 'scheduled';";
static const char migration_0010_down[] =
"SELECT 1;";
static const char migration_0011_up[] =
"ALTER TABLE streams ADD COLUMN detection_api_url TEXT DEFAULT '';";
static const char migration_0011_down[] =
"SELECT 1;";
static const char migration_0012_up[] =
"ALTER TABLE streams ADD COLUMN backchannel_enabled INTEGER DEFAULT 0;";
static const char migration_0012_down[] =
"SELECT 1;";
static const char migration_0013_up[] =
"-- Per-stream retention settings\n"
"ALTER TABLE streams ADD COLUMN retention_days INTEGER DEFAULT 30;\n"
"ALTER TABLE streams ADD COLUMN detection_retention_days INTEGER DEFAULT 90;\n"
"ALTER TABLE streams ADD COLUMN max_storage_mb INTEGER DEFAULT 0;\n"
"\n"
"-- Recording protection\n"
"ALTER TABLE recordings ADD COLUMN protected INTEGER DEFAULT 0;\n"
"ALTER TABLE recordings ADD COLUMN retention_override_days INTEGER DEFAULT NULL;\n"
"\n"
"-- Indexes for efficient retention queries\n"
"CREATE INDEX IF NOT EXISTS idx_recordings_protected ON recordings(protected);\n"
"CREATE INDEX IF NOT EXISTS idx_recordings_trigger_type ON recordings(trigger_type);";
static const char migration_0013_down[] =
"DROP INDEX IF EXISTS idx_recordings_trigger_type;\n"
"DROP INDEX IF EXISTS idx_recordings_protected;\n"
"SELECT 1;";
static const char migration_0014_up[] =
"ALTER TABLE streams ADD COLUMN ptz_enabled INTEGER DEFAULT 0;\n"
"ALTER TABLE streams ADD COLUMN ptz_max_x INTEGER DEFAULT 0;\n"
"ALTER TABLE streams ADD COLUMN ptz_max_y INTEGER DEFAULT 0;\n"
"ALTER TABLE streams ADD COLUMN ptz_max_z INTEGER DEFAULT 0;\n"
"ALTER TABLE streams ADD COLUMN ptz_has_home INTEGER DEFAULT 0;";
static const char migration_0014_down[] =
"SELECT 1;";
static const char migration_0015_up[] =
"-- Buffer strategy: 'auto', 'none', 'go2rtc', 'hls_segment', 'memory_packet', 'mmap_hybrid'\n"
"ALTER TABLE streams ADD COLUMN buffer_strategy TEXT DEFAULT 'auto';";
static const char migration_0015_down[] =
"SELECT 1;";
static const char migration_0016_up[] =
"ALTER TABLE streams ADD COLUMN onvif_username TEXT DEFAULT '';\n"
"ALTER TABLE streams ADD COLUMN onvif_password TEXT DEFAULT '';\n"
"ALTER TABLE streams ADD COLUMN onvif_profile TEXT DEFAULT '';";
static const char migration_0016_down[] =
"SELECT 1;";
static const char migration_0017_up[] =
"ALTER TABLE detections ADD COLUMN recording_id INTEGER REFERENCES recordings(id);";
static const char migration_0017_down[] =
"SELECT 1;";
static const char migration_0018_up[] =
"-- Add ip_address column for tracking client IP\n"
"ALTER TABLE sessions ADD COLUMN ip_address TEXT;\n"
"\n"
"-- Add user_agent column for tracking client user agent\n"
"ALTER TABLE sessions ADD COLUMN user_agent TEXT;";
static const char migration_0018_down[] =
"-- SQLite doesn't support DROP COLUMN directly, so we need to recreate the table\n"
"-- For simplicity in the down migration, we'll just leave the columns\n"
"-- (they're nullable, so this won't break anything)";
static const char migration_0019_up[] =
"-- Add password_change_locked column to users table\n"
"-- This allows admins to lock password changes for specific users (e.g., demo accounts)\n"
"ALTER TABLE users ADD COLUMN password_change_locked INTEGER DEFAULT 0;";
static const char migration_0019_down[] =
"-- SQLite doesn't support DROP COLUMN directly, so we need to recreate the table\n"
"-- For simplicity in the down migration, we'll just leave the column\n"
"-- (it's nullable with a default, so this won't break anything)";
static const char migration_0020_up[] =
"-- Motion recording configuration table (per-stream settings)\n"
"CREATE TABLE IF NOT EXISTS motion_recording_config (\n"
" id INTEGER PRIMARY KEY AUTOINCREMENT,\n"
" stream_name TEXT NOT NULL UNIQUE,\n"
" enabled INTEGER DEFAULT 1,\n"
" pre_buffer_seconds INTEGER DEFAULT 5,\n"
" post_buffer_seconds INTEGER DEFAULT 5,\n"
" max_file_duration INTEGER DEFAULT 300,\n"
" codec TEXT DEFAULT 'h264',\n"
" quality TEXT DEFAULT 'medium',\n"
" retention_days INTEGER DEFAULT 7,\n"
" max_storage_mb INTEGER DEFAULT 0,\n"
" created_at INTEGER NOT NULL,\n"
" updated_at INTEGER NOT NULL,\n"
" FOREIGN KEY (stream_name) REFERENCES streams(name) ON DELETE CASCADE\n"
");\n"
"\n"
"-- Index for faster lookups\n"
"CREATE INDEX IF NOT EXISTS idx_motion_config_stream ON motion_recording_config(stream_name);\n"
"\n"
"-- Motion recordings table (separate from regular recordings)\n"
"CREATE TABLE IF NOT EXISTS motion_recordings (\n"
" id INTEGER PRIMARY KEY AUTOINCREMENT,\n"
" stream_name TEXT NOT NULL,\n"
" file_path TEXT NOT NULL UNIQUE,\n"
" start_time INTEGER NOT NULL,\n"
" end_time INTEGER,\n"
" size_bytes INTEGER DEFAULT 0,\n"
" width INTEGER,\n"
" height INTEGER,\n"
" fps INTEGER,\n"
" codec TEXT,\n"
" is_complete INTEGER DEFAULT 0,\n"
" created_at INTEGER NOT NULL,\n"
" FOREIGN KEY (stream_name) REFERENCES streams(name) ON DELETE CASCADE\n"
");\n"
"\n"
"-- Indexes for faster queries\n"
"CREATE INDEX IF NOT EXISTS idx_motion_recordings_stream ON motion_recordings(stream_name);\n"
"CREATE INDEX IF NOT EXISTS idx_motion_recordings_time ON motion_recordings(start_time);\n"
"CREATE INDEX IF NOT EXISTS idx_motion_recordings_complete ON motion_recordings(is_complete);";
static const char migration_0020_down[] =
"DROP INDEX IF EXISTS idx_motion_recordings_complete;\n"
"DROP INDEX IF EXISTS idx_motion_recordings_time;\n"
"DROP INDEX IF EXISTS idx_motion_recordings_stream;\n"
"DROP TABLE IF EXISTS motion_recordings;\n"
"DROP INDEX IF EXISTS idx_motion_config_stream;\n"
"DROP TABLE IF EXISTS motion_recording_config;";
static const char migration_0021_up[] =
"ALTER TABLE users ADD COLUMN totp_secret TEXT;\n"
"ALTER TABLE users ADD COLUMN totp_enabled INTEGER DEFAULT 0;";
static const char migration_0021_down[] =
"-- SQLite doesn't support DROP COLUMN in older versions\n"
"-- The columns are nullable with safe defaults, so they can remain";
static const char migration_0022_up[] =
"DROP TABLE IF EXISTS zones;\n"
"\n"
"CREATE TABLE IF NOT EXISTS detection_zones (\n"
" id TEXT PRIMARY KEY,\n"
" stream_name TEXT NOT NULL,\n"
" name TEXT NOT NULL,\n"
" enabled INTEGER DEFAULT 1,\n"
" color TEXT DEFAULT '#3b82f6',\n"
" polygon TEXT NOT NULL,\n"
" filter_classes TEXT DEFAULT '',\n"
" min_confidence REAL DEFAULT 0.0,\n"
" created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now')),\n"
" updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now')),\n"
" FOREIGN KEY (stream_name) REFERENCES streams(name) ON DELETE CASCADE\n"
");\n"
"\n"
"CREATE INDEX IF NOT EXISTS idx_detection_zones_stream ON detection_zones(stream_name);";
static const char migration_0022_down[] =
"DROP INDEX IF EXISTS idx_detection_zones_stream;\n"
"DROP TABLE IF EXISTS detection_zones;";
static const char migration_0023_up[] =
"ALTER TABLE streams ADD COLUMN detection_object_filter TEXT DEFAULT 'none';\n"
"ALTER TABLE streams ADD COLUMN detection_object_filter_list TEXT DEFAULT '';";
static const char migration_0023_down[] =
"-- SQLite doesn't support DROP COLUMN in older versions\n"
"-- These columns will just be ignored if not used";
static const char migration_0024_up[] =
"-- Tiered retention columns on recordings\n"
"ALTER TABLE recordings ADD COLUMN retention_tier INTEGER DEFAULT 2;\n"
"ALTER TABLE recordings ADD COLUMN disk_pressure_eligible INTEGER DEFAULT 1;\n"
"\n"
"-- Tier multipliers and storage priority on streams\n"
"ALTER TABLE streams ADD COLUMN tier_critical_multiplier REAL DEFAULT 3.0;\n"
"ALTER TABLE streams ADD COLUMN tier_important_multiplier REAL DEFAULT 2.0;\n"
"ALTER TABLE streams ADD COLUMN tier_ephemeral_multiplier REAL DEFAULT 0.25;\n"
"ALTER TABLE streams ADD COLUMN storage_priority INTEGER DEFAULT 5;\n"
"\n"
"-- Daily storage statistics table for analytics\n"
"CREATE TABLE IF NOT EXISTS storage_daily_stats (\n"
" id INTEGER PRIMARY KEY AUTOINCREMENT,\n"
" date TEXT NOT NULL,\n"
" stream_name TEXT NOT NULL,\n"
" tier INTEGER NOT NULL DEFAULT 2,\n"
" recording_count INTEGER NOT NULL DEFAULT 0,\n"
" total_bytes INTEGER NOT NULL DEFAULT 0,\n"
" deleted_count INTEGER NOT NULL DEFAULT 0,\n"
" deleted_bytes INTEGER NOT NULL DEFAULT 0,\n"
" created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n"
");\n"
"\n"
"-- Indexes for efficient queries on daily stats\n"
"CREATE INDEX IF NOT EXISTS idx_storage_daily_stats_stream_date ON storage_daily_stats(stream_name, date);\n"
"CREATE INDEX IF NOT EXISTS idx_storage_daily_stats_date_tier ON storage_daily_stats(date, tier);\n"
"\n"
"-- Index for tier-based retention queries\n"
"CREATE INDEX IF NOT EXISTS idx_recordings_retention_tier ON recordings(retention_tier);\n"
"CREATE INDEX IF NOT EXISTS idx_recordings_disk_pressure ON recordings(disk_pressure_eligible);";
static const char migration_0024_down[] =
"DROP INDEX IF EXISTS idx_recordings_disk_pressure;\n"
"DROP INDEX IF EXISTS idx_recordings_retention_tier;\n"
"DROP INDEX IF EXISTS idx_storage_daily_stats_date_tier;\n"
"DROP INDEX IF EXISTS idx_storage_daily_stats_stream_date;\n"
"DROP TABLE IF EXISTS storage_daily_stats;\n"
"SELECT 1;";
static const char migration_0025_up[] =
"-- Detection performance indexes (new)\n"
"CREATE INDEX IF NOT EXISTS idx_detections_recording_id ON detections(recording_id);\n"
"\n"
"-- Indexes previously only in db_core.c (never migrated)\n"
"CREATE INDEX IF NOT EXISTS idx_events_timestamp ON events(timestamp);\n"
"CREATE INDEX IF NOT EXISTS idx_events_type ON events(type);\n"
"CREATE INDEX IF NOT EXISTS idx_events_stream ON events(stream_name);\n"
"CREATE INDEX IF NOT EXISTS idx_recordings_end_time ON recordings(end_time);\n"
"CREATE INDEX IF NOT EXISTS idx_recordings_complete_stream_start ON recordings(is_complete, stream_name, start_time);\n"
"CREATE INDEX IF NOT EXISTS idx_streams_name ON streams(name);\n"
"CREATE INDEX IF NOT EXISTS idx_detections_stream_timestamp ON detections(stream_name, timestamp);";
static const char migration_0025_down[] =
"DROP INDEX IF EXISTS idx_detections_stream_timestamp;\n"
"DROP INDEX IF EXISTS idx_streams_name;\n"
"DROP INDEX IF EXISTS idx_recordings_complete_stream_start;\n"
"DROP INDEX IF EXISTS idx_recordings_end_time;\n"
"DROP INDEX IF EXISTS idx_events_stream;\n"
"DROP INDEX IF EXISTS idx_events_type;\n"
"DROP INDEX IF EXISTS idx_events_timestamp;\n"
"DROP INDEX IF EXISTS idx_detections_recording_id;";
static const char migration_0026_up[] =
"ALTER TABLE streams ADD COLUMN record_on_schedule INTEGER NOT NULL DEFAULT 0;\n"
"ALTER TABLE streams ADD COLUMN recording_schedule TEXT DEFAULT NULL;";
static const char migration_0026_down[] =
"SELECT 1;";
static const char migration_0027_up[] =
"ALTER TABLE streams ADD COLUMN group_name TEXT NOT NULL DEFAULT '';";
static const char migration_0027_down[] =
"SELECT 1;";
static const char migration_0028_up[] =
"ALTER TABLE streams ADD COLUMN tags TEXT NOT NULL DEFAULT '';"
"UPDATE streams SET tags = group_name WHERE (tags = '' OR tags IS NULL) AND group_name IS NOT NULL AND group_name != '';"
"ALTER TABLE streams DROP COLUMN group_name;";
static const char migration_0028_down[] =
"ALTER TABLE streams ADD COLUMN group_name TEXT NOT NULL DEFAULT '';"
"UPDATE streams SET group_name = CASE WHEN instr(tags, ',') > 0 THEN substr(tags, 1, instr(tags, ',') - 1) ELSE tags END WHERE tags != '';"
"ALTER TABLE streams DROP COLUMN tags;";
static const char migration_0029_up[] =
"ALTER TABLE users ADD COLUMN allowed_tags TEXT DEFAULT NULL;";
static const char migration_0029_down[] =
"SELECT 1;";
static const char migration_0030_up[] =
"CREATE TABLE IF NOT EXISTS system_settings (\n"
" key TEXT PRIMARY KEY,\n"
" value TEXT NOT NULL DEFAULT '',\n"
" updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now'))\n"
");\n"
"INSERT OR IGNORE INTO system_settings (key, value) VALUES ('setup_complete', '0');";
static const char migration_0030_down[] =
"DROP TABLE IF EXISTS system_settings;";
static const char migration_0031_up[] =
"ALTER TABLE streams ADD COLUMN onvif_port INTEGER DEFAULT 0;";
static const char migration_0031_down[] =
"SELECT 1;";
static const char migration_0032_up[] =
"CREATE TABLE IF NOT EXISTS recording_tags (\n"
" id INTEGER PRIMARY KEY AUTOINCREMENT,\n"
" recording_id INTEGER NOT NULL,\n"
" tag TEXT NOT NULL,\n"
" created_at INTEGER DEFAULT (strftime('%s', 'now')),\n"
" FOREIGN KEY (recording_id) REFERENCES recordings(id) ON DELETE CASCADE\n"
");\n"
"\n"
"CREATE UNIQUE INDEX IF NOT EXISTS idx_recording_tags_unique ON recording_tags(recording_id, tag);\n"
"CREATE INDEX IF NOT EXISTS idx_recording_tags_tag ON recording_tags(tag);\n"
"CREATE INDEX IF NOT EXISTS idx_recording_tags_recording ON recording_tags(recording_id);";
static const char migration_0032_down[] =
"DROP INDEX IF EXISTS idx_recording_tags_recording;\n"
"DROP INDEX IF EXISTS idx_recording_tags_tag;\n"
"DROP INDEX IF EXISTS idx_recording_tags_unique;\n"
"DROP TABLE IF EXISTS recording_tags;";
static const char migration_0033_up[] =
"DELETE FROM streams WHERE TRIM(name) = '' OR name IS NULL;";
static const char migration_0033_down[] =
"SELECT 1;";
static const char migration_0034_up[] =
"UPDATE streams\n"
" SET url = substr(url, 1, instr(url, '://') + 2) ||\n"
" substr(substr(url, instr(url, '://') + 3),\n"
" instr(substr(url, instr(url, '://') + 3), '@') + 1),\n"
" onvif_username = CASE\n"
" WHEN COALESCE(onvif_username, '') = '' THEN\n"
" CASE\n"
" WHEN instr(substr(substr(url, instr(url, '://') + 3),\n"
" 1,\n"
" instr(substr(url, instr(url, '://') + 3), '@') - 1),\n"
" ':') > 0 THEN\n"
" substr(substr(substr(url, instr(url, '://') + 3),\n"
" 1,\n"
" instr(substr(url, instr(url, '://') + 3), '@') - 1),\n"
" 1,\n"
" instr(substr(substr(url, instr(url, '://') + 3),\n"
" 1,\n"
" instr(substr(url, instr(url, '://') + 3), '@') - 1),\n"
" ':') - 1)\n"
" ELSE\n"
" substr(substr(url, instr(url, '://') + 3),\n"
" 1,\n"
" instr(substr(url, instr(url, '://') + 3), '@') - 1)\n"
" END\n"
" ELSE onvif_username\n"
" END,\n"
" onvif_password = CASE\n"
" WHEN COALESCE(onvif_password, '') = '' THEN\n"
" CASE\n"
" WHEN instr(substr(substr(url, instr(url, '://') + 3),\n"
" 1,\n"
" instr(substr(url, instr(url, '://') + 3), '@') - 1),\n"
" ':') > 0 THEN\n"
" substr(substr(substr(url, instr(url, '://') + 3),\n"
" 1,\n"
" instr(substr(url, instr(url, '://') + 3), '@') - 1),\n"
" instr(substr(substr(url, instr(url, '://') + 3),\n"
" 1,\n"
" instr(substr(url, instr(url, '://') + 3), '@') - 1),\n"
" ':') + 1)\n"
" ELSE ''\n"
" END\n"
" ELSE onvif_password\n"
" END\n"
" WHERE instr(url, '://') > 0\n"
" AND instr(substr(url, instr(url, '://') + 3), '@') > 0\n"
" AND (\n"
" is_onvif = 1 OR\n"
" COALESCE(onvif_profile, '') != '' OR\n"
" COALESCE(onvif_username, '') != '' OR\n"
" COALESCE(onvif_password, '') != '' OR\n"
" COALESCE(onvif_port, 0) > 0\n"
" );";
static const char migration_0034_down[] =
"SELECT 1;";
static const char migration_0035_up[] =
"ALTER TABLE sessions ADD COLUMN last_activity_at INTEGER;\n"
"UPDATE sessions\n"
" SET last_activity_at = COALESCE(last_activity_at, created_at, strftime('%s', 'now'));\n"
"ALTER TABLE sessions ADD COLUMN idle_expires_at INTEGER;\n"
"UPDATE sessions\n"
" SET idle_expires_at = COALESCE(idle_expires_at, expires_at, strftime('%s', 'now'));\n"
"CREATE INDEX IF NOT EXISTS idx_sessions_user_last_activity\n"
" ON sessions(user_id, last_activity_at DESC);\n"
"CREATE TABLE IF NOT EXISTS trusted_devices (\n"
" id INTEGER PRIMARY KEY AUTOINCREMENT,\n"
" user_id INTEGER NOT NULL,\n"
" token TEXT NOT NULL UNIQUE,\n"
" ip_address TEXT,\n"
" user_agent TEXT,\n"
" created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now')),\n"
" last_used_at INTEGER,\n"
" expires_at INTEGER NOT NULL,\n"
" FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE\n"
");\n"
"CREATE INDEX IF NOT EXISTS idx_trusted_devices_user_id ON trusted_devices(user_id);\n"
"CREATE INDEX IF NOT EXISTS idx_trusted_devices_token ON trusted_devices(token);\n"
"CREATE INDEX IF NOT EXISTS idx_trusted_devices_expires_at ON trusted_devices(expires_at);";
static const char migration_0035_down[] =
"DROP INDEX IF EXISTS idx_trusted_devices_expires_at;\n"
"DROP INDEX IF EXISTS idx_trusted_devices_token;\n"
"DROP INDEX IF EXISTS idx_trusted_devices_user_id;\n"
"DROP TABLE IF EXISTS trusted_devices;\n"
"DROP INDEX IF EXISTS idx_sessions_user_last_activity;";
static const char migration_0036_up[] =
"ALTER TABLE streams ADD COLUMN admin_url TEXT DEFAULT '';";
static const char migration_0036_down[] =
"SELECT 1;";
static const char migration_0037_up[] =
"ALTER TABLE users ADD COLUMN allowed_login_cidrs TEXT DEFAULT NULL;";
static const char migration_0037_down[] =
"SELECT 1;";
static const char migration_0038_up[] =
"ALTER TABLE streams ADD COLUMN privacy_mode INTEGER DEFAULT 0;";
static const char migration_0038_down[] =
"SELECT 1;";
static const char migration_0039_up[] =
"ALTER TABLE streams ADD COLUMN motion_trigger_source TEXT DEFAULT '';";
static const char migration_0039_down[] =
"SELECT 1;";
static const char migration_0040_up[] =
"ALTER TABLE streams ADD COLUMN go2rtc_source_override TEXT DEFAULT '';";
static const char migration_0040_down[] =
"SELECT 1;";
static const migration_t embedded_migrations_data[] = {
{
.version = "0001",
.description = "initial_schema",
.sql_up = migration_0001_up,
.sql_down = migration_0001_down,
.is_embedded = true
},
{
.version = "0002",
.description = "add_detection_columns",
.sql_up = migration_0002_up,
.sql_down = migration_0002_down,
.is_embedded = true
},
{
.version = "0003",
.description = "add_protocol_onvif",
.sql_up = migration_0003_up,
.sql_down = migration_0003_down,
.is_embedded = true
},
{
.version = "0004",
.description = "add_record_audio",
.sql_up = migration_0004_up,
.sql_down = migration_0004_down,
.is_embedded = true
},
{
.version = "0005",
.description = "add_detections_table",
.sql_up = migration_0005_up,
.sql_down = migration_0005_down,
.is_embedded = true
},
{
.version = "0006",
.description = "add_users_table",
.sql_up = migration_0006_up,
.sql_down = migration_0006_down,
.is_embedded = true
},
{
.version = "0007",
.description = "add_motion_settings",
.sql_up = migration_0007_up,
.sql_down = migration_0007_down,
.is_embedded = true
},
{
.version = "0008",
.description = "add_zones_table",
.sql_up = migration_0008_up,
.sql_down = migration_0008_down,
.is_embedded = true
},
{
.version = "0009",
.description = "add_detection_tracking",
.sql_up = migration_0009_up,
.sql_down = migration_0009_down,
.is_embedded = true
},
{
.version = "0010",
.description = "add_trigger_type",
.sql_up = migration_0010_up,
.sql_down = migration_0010_down,
.is_embedded = true
},
{
.version = "0011",
.description = "add_detection_api_url",
.sql_up = migration_0011_up,
.sql_down = migration_0011_down,
.is_embedded = true
},
{
.version = "0012",
.description = "add_backchannel",
.sql_up = migration_0012_up,
.sql_down = migration_0012_down,
.is_embedded = true
},
{
.version = "0013",
.description = "add_retention_policy",
.sql_up = migration_0013_up,
.sql_down = migration_0013_down,
.is_embedded = true
},
{
.version = "0014",
.description = "add_ptz_support",
.sql_up = migration_0014_up,
.sql_down = migration_0014_down,
.is_embedded = true
},
{
.version = "0015",
.description = "add_buffer_strategy",
.sql_up = migration_0015_up,
.sql_down = migration_0015_down,
.is_embedded = true
},
{
.version = "0016",
.description = "add_onvif_credentials",
.sql_up = migration_0016_up,
.sql_down = migration_0016_down,
.is_embedded = true
},
{
.version = "0017",
.description = "add_recording_id_to_detections",
.sql_up = migration_0017_up,
.sql_down = migration_0017_down,
.is_embedded = true
},
{
.version = "0018",
.description = "add_session_tracking",
.sql_up = migration_0018_up,
.sql_down = migration_0018_down,
.is_embedded = true
},
{
.version = "0019",
.description = "add_password_change_locked",
.sql_up = migration_0019_up,
.sql_down = migration_0019_down,
.is_embedded = true
},
{
.version = "0020",
.description = "add_motion_recording_config",
.sql_up = migration_0020_up,
.sql_down = migration_0020_down,
.is_embedded = true
},
{
.version = "0021",
.description = "add_totp_mfa",
.sql_up = migration_0021_up,
.sql_down = migration_0021_down,
.is_embedded = true
},
{
.version = "0022",
.description = "fix_detection_zones_table",
.sql_up = migration_0022_up,
.sql_down = migration_0022_down,
.is_embedded = true
},
{
.version = "0023",
.description = "add_detection_object_filters",
.sql_up = migration_0023_up,
.sql_down = migration_0023_down,
.is_embedded = true
},
{
.version = "0024",
.description = "enhanced_storage_management",
.sql_up = migration_0024_up,
.sql_down = migration_0024_down,
.is_embedded = true
},
{
.version = "0025",
.description = "add_detection_performance_indexes",
.sql_up = migration_0025_up,
.sql_down = migration_0025_down,
.is_embedded = true
},
{
.version = "0026",
.description = "add_recording_schedule",
.sql_up = migration_0026_up,
.sql_down = migration_0026_down,
.is_embedded = true
},
{
.version = "0027",
.description = "add_stream_group",
.sql_up = migration_0027_up,
.sql_down = migration_0027_down,
.is_embedded = true
},
{
.version = "0028",
.description = "add_stream_tags",
.sql_up = migration_0028_up,
.sql_down = migration_0028_down,
.is_embedded = true
},
{
.version = "0029",
.description = "add_user_allowed_tags",
.sql_up = migration_0029_up,
.sql_down = migration_0029_down,
.is_embedded = true
},
{
.version = "0030",
.description = "add_system_settings",
.sql_up = migration_0030_up,
.sql_down = migration_0030_down,
.is_embedded = true
},
{
.version = "0031",
.description = "add_onvif_port",
.sql_up = migration_0031_up,
.sql_down = migration_0031_down,
.is_embedded = true
},
{
.version = "0032",
.description = "add_recording_tags",
.sql_up = migration_0032_up,
.sql_down = migration_0032_down,
.is_embedded = true
},
{
.version = "0033",
.description = "cleanup_blank_stream_names",
.sql_up = migration_0033_up,
.sql_down = migration_0033_down,
.is_embedded = true
},
{
.version = "0034",
.description = "repair_onvif_embedded_credentials",
.sql_up = migration_0034_up,
.sql_down = migration_0034_down,
.is_embedded = true
},
{
.version = "0035",
.description = "modernize_auth_sessions",
.sql_up = migration_0035_up,
.sql_down = migration_0035_down,
.is_embedded = true
},
{
.version = "0036",
.description = "add_stream_admin_url",
.sql_up = migration_0036_up,
.sql_down = migration_0036_down,
.is_embedded = true
},
{
.version = "0037",
.description = "add_user_allowed_login_cidrs",
.sql_up = migration_0037_up,
.sql_down = migration_0037_down,
.is_embedded = true
},
{
.version = "0038",
.description = "add_privacy_mode",
.sql_up = migration_0038_up,
.sql_down = migration_0038_down,
.is_embedded = true
},
{
.version = "0039",
.description = "add_motion_trigger_source",
.sql_up = migration_0039_up,
.sql_down = migration_0039_down,
.is_embedded = true
},
{
.version = "0040",
.description = "add_go2rtc_source_override",
.sql_up = migration_0040_up,
.sql_down = migration_0040_down,
.is_embedded = true
},
};
#define EMBEDDED_MIGRATIONS_COUNT 40
#endif /* DB_EMBEDDED_MIGRATIONS_H */