Skip to content

Commit 9dd348a

Browse files
committed
fix duplicates and fix nuke
1 parent 47d70ee commit 9dd348a

File tree

4 files changed

+46
-10
lines changed

4 files changed

+46
-10
lines changed

src-tauri/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ fn toggle_source(value: bool, source_id: i64) -> Result<(), String> {
287287

288288
#[tauri::command(async)]
289289
fn delete_database() -> Result<(), String> {
290-
sql::delete_database().map_err(map_err_frontend)
290+
utils::create_nuke_request().map_err(map_err_frontend)
291291
}
292292

293293
#[tauri::command(async)]

src-tauri/src/main.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
22
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
3-
use anyhow::Result;
3+
use anyhow::{Context, Result};
4+
use open_tv_lib::log;
45
fn main() -> Result<()> {
6+
_ = open_tv_lib::utils::check_nuke()
7+
.with_context(|| "Failed to delete db after nuke request")
8+
.inspect_err(|e| log::log(format!("{:?}", e)));
59
open_tv_lib::sql::create_or_initialize_db()?;
610
open_tv_lib::run();
711
Ok(())

src-tauri/src/sql.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use rusqlite::{OptionalExtension, Row, Transaction, params, params_from_iter};
1919
use rusqlite_migration::{M, Migrations};
2020

2121
const PAGE_SIZE: u8 = 36;
22+
pub const DB_NAME: &str = "db.sqlite";
2223
static CONN: LazyLock<Pool<SqliteConnectionManager>> = LazyLock::new(|| create_connection_pool());
2324

2425
pub fn get_conn() -> Result<PooledConnection<SqliteConnectionManager>> {
@@ -38,7 +39,7 @@ fn get_and_create_sqlite_db_path() -> String {
3839
if !path.exists() {
3940
std::fs::create_dir_all(&path).unwrap();
4041
}
41-
path.push("db.sqlite");
42+
path.push(DB_NAME);
4243
return path.to_string_lossy().to_string();
4344
}
4445

@@ -117,11 +118,6 @@ fn structure_exists() -> Result<bool> {
117118
Ok(table_exists)
118119
}
119120

120-
pub fn delete_database() -> Result<()> {
121-
std::fs::remove_file(get_and_create_sqlite_db_path())?;
122-
std::process::exit(0);
123-
}
124-
125121
pub fn create_or_initialize_db() -> Result<()> {
126122
if !structure_exists()? {
127123
create_structure()?;
@@ -213,6 +209,12 @@ fn apply_migrations() -> Result<()> {
213209
CREATE UNIQUE INDEX unique_seasons ON seasons(season_number, series_id, source_id);
214210
"#,
215211
),
212+
M::up(
213+
r#"
214+
DROP INDEX IF EXISTS channels_unique;
215+
CREATE UNIQUE INDEX channels_unique ON channels(name, source_id, url, series_id, season_id);
216+
"#,
217+
),
216218
]);
217219
migrations.to_latest(&mut sql)?;
218220
Ok(())
@@ -280,7 +282,7 @@ pub fn insert_channel(tx: &Transaction, channel: Channel) -> Result<()> {
280282
r#"
281283
INSERT INTO channels (name, group_id, image, url, source_id, media_type, series_id, favorite, stream_id, tv_archive, season_id, episode_num)
282284
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
283-
ON CONFLICT (name, source_id)
285+
ON CONFLICT (name, source_id, url, series_id, season_id)
284286
DO UPDATE SET
285287
url = excluded.url,
286288
media_type = excluded.media_type,

src-tauri/src/utils.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ use crate::{
88
};
99
use anyhow::{Context, Result, anyhow, bail};
1010
use chrono::{DateTime, Local, Utc};
11+
use directories::ProjectDirs;
1112
use regex::Regex;
1213
use reqwest::Client;
1314
use serde::Serialize;
1415
use std::{
1516
env::{consts::OS, current_exe},
16-
path::Path,
17+
fs::File,
18+
path::{Path, PathBuf},
1719
sync::{
1820
Arc, LazyLock,
1921
atomic::{AtomicBool, Ordering::Relaxed},
@@ -192,6 +194,34 @@ pub fn is_container() -> bool {
192194
std::env::var("container").is_ok()
193195
}
194196

197+
pub fn create_nuke_request() -> Result<()> {
198+
let path = get_nuke_path()?;
199+
File::create(path)?;
200+
std::process::exit(0);
201+
}
202+
203+
fn get_nuke_path() -> Result<PathBuf> {
204+
let path = ProjectDirs::from("dev", "fredol", "open-tv").context("project dir not found")?;
205+
let path = path.cache_dir();
206+
let path = path.join("nuke.txt");
207+
Ok(path)
208+
}
209+
210+
pub fn check_nuke() -> Result<()> {
211+
let path = get_nuke_path()?;
212+
if !path.exists() {
213+
return Ok(());
214+
}
215+
std::fs::remove_file(path)?;
216+
let path = ProjectDirs::from("dev", "fredol", "open-tv").context("project dir not found")?;
217+
let path = path.data_dir();
218+
let path = path.join(sql::DB_NAME);
219+
if path.exists() {
220+
std::fs::remove_file(path)?;
221+
}
222+
Ok(())
223+
}
224+
195225
#[cfg(test)]
196226
mod test_utils {
197227
use super::sanitize;

0 commit comments

Comments
 (0)