feat: make DB IDs not null
This commit is contained in:
parent
ce876955b7
commit
7965998f43
8 changed files with 175 additions and 52 deletions
65
migrations/2025-05-04-055045_not_null_ids/down.sql
Normal file
65
migrations/2025-05-04-055045_not_null_ids/down.sql
Normal file
|
|
@ -0,0 +1,65 @@
|
||||||
|
--- magnets
|
||||||
|
|
||||||
|
ALTER TABLE magnets
|
||||||
|
RENAME TO magnets_new;
|
||||||
|
|
||||||
|
CREATE TABLE magnets
|
||||||
|
(
|
||||||
|
id INTEGER PRIMARY KEY,
|
||||||
|
title VARCHAR NOT NULL,
|
||||||
|
submitter VARCHAR NOT NULL,
|
||||||
|
subreddit VARCHAR NOT NULL,
|
||||||
|
link VARCHAR NOT NULL,
|
||||||
|
published_at DATETIME NOT NULL,
|
||||||
|
imdb_id VARCHAR
|
||||||
|
);
|
||||||
|
|
||||||
|
INSERT INTO magnets (id, title, submitter, subreddit, link, published_at, imdb_id)
|
||||||
|
SELECT id, title, submitter, subreddit, link, published_at, imdb_id
|
||||||
|
FROM magnets_new;
|
||||||
|
|
||||||
|
DROP TABLE magnets_new;
|
||||||
|
|
||||||
|
CREATE UNIQUE INDEX magnets_unique_link ON magnets (link);
|
||||||
|
|
||||||
|
--- bitmagnet_processed
|
||||||
|
|
||||||
|
ALTER TABLE bitmagnet_processed
|
||||||
|
RENAME TO bitmagnet_processed_new;
|
||||||
|
|
||||||
|
CREATE TABLE bitmagnet_processed
|
||||||
|
(
|
||||||
|
id INTEGER PRIMARY KEY,
|
||||||
|
magnet_id INTEGER NOT NULL,
|
||||||
|
processed_at DATETIME NOT NULL,
|
||||||
|
FOREIGN KEY (magnet_id) REFERENCES magnets (id)
|
||||||
|
);
|
||||||
|
|
||||||
|
INSERT INTO bitmagnet_processed (id, magnet_id, processed_at)
|
||||||
|
SELECT id, magnet_id, processed_at
|
||||||
|
FROM bitmagnet_processed_new;
|
||||||
|
|
||||||
|
DROP TABLE bitmagnet_processed_new;
|
||||||
|
|
||||||
|
CREATE INDEX bitmagnet_processed_magnet_id ON bitmagnet_processed (magnet_id);
|
||||||
|
|
||||||
|
--- transmission_processed
|
||||||
|
|
||||||
|
ALTER TABLE transmission_processed
|
||||||
|
RENAME TO transmission_processed_new;
|
||||||
|
|
||||||
|
CREATE TABLE transmission_processed
|
||||||
|
(
|
||||||
|
id INTEGER PRIMARY KEY,
|
||||||
|
magnet_id INTEGER NOT NULL,
|
||||||
|
processed_at DATETIME NOT NULL,
|
||||||
|
FOREIGN KEY (magnet_id) REFERENCES magnets (id)
|
||||||
|
);
|
||||||
|
|
||||||
|
INSERT INTO transmission_processed (id, magnet_id, processed_at)
|
||||||
|
SELECT id, magnet_id, processed_at
|
||||||
|
FROM transmission_processed_new;
|
||||||
|
|
||||||
|
DROP TABLE transmission_processed_new;
|
||||||
|
|
||||||
|
CREATE INDEX transmission_processed_magnet_id ON transmission_processed (magnet_id);
|
||||||
66
migrations/2025-05-04-055045_not_null_ids/up.sql
Normal file
66
migrations/2025-05-04-055045_not_null_ids/up.sql
Normal file
|
|
@ -0,0 +1,66 @@
|
||||||
|
--- magnets
|
||||||
|
|
||||||
|
ALTER TABLE magnets
|
||||||
|
RENAME TO magnets_old;
|
||||||
|
|
||||||
|
CREATE TABLE magnets
|
||||||
|
(
|
||||||
|
id INTEGER PRIMARY KEY NOT NULL,
|
||||||
|
title VARCHAR NOT NULL,
|
||||||
|
submitter VARCHAR NOT NULL,
|
||||||
|
subreddit VARCHAR NOT NULL,
|
||||||
|
link VARCHAR NOT NULL,
|
||||||
|
published_at DATETIME NOT NULL,
|
||||||
|
imdb_id VARCHAR
|
||||||
|
);
|
||||||
|
|
||||||
|
INSERT INTO magnets (id, title, submitter, subreddit, link, published_at, imdb_id)
|
||||||
|
SELECT id, title, submitter, subreddit, link, published_at, imdb_id
|
||||||
|
FROM magnets_old;
|
||||||
|
|
||||||
|
DROP TABLE magnets_old;
|
||||||
|
|
||||||
|
CREATE UNIQUE INDEX magnets_unique_link
|
||||||
|
ON magnets (link);
|
||||||
|
|
||||||
|
--- bitmagnet_processed
|
||||||
|
|
||||||
|
ALTER TABLE bitmagnet_processed
|
||||||
|
RENAME TO bitmagnet_processed_old;
|
||||||
|
|
||||||
|
CREATE TABLE bitmagnet_processed
|
||||||
|
(
|
||||||
|
id INTEGER PRIMARY KEY NOT NULL,
|
||||||
|
magnet_id INTEGER NOT NULL,
|
||||||
|
processed_at DATETIME NOT NULL,
|
||||||
|
FOREIGN KEY (magnet_id) REFERENCES magnets (id)
|
||||||
|
);
|
||||||
|
|
||||||
|
INSERT INTO bitmagnet_processed (id, magnet_id, processed_at)
|
||||||
|
SELECT id, magnet_id, processed_at
|
||||||
|
FROM bitmagnet_processed_old;
|
||||||
|
|
||||||
|
DROP TABLE bitmagnet_processed_old;
|
||||||
|
|
||||||
|
CREATE INDEX bitmagnet_processed_magnet_id ON bitmagnet_processed (magnet_id);
|
||||||
|
|
||||||
|
--- transmission_processed
|
||||||
|
|
||||||
|
ALTER TABLE transmission_processed
|
||||||
|
RENAME TO transmission_processed_old;
|
||||||
|
|
||||||
|
CREATE TABLE transmission_processed
|
||||||
|
(
|
||||||
|
id INTEGER PRIMARY KEY NOT NULL,
|
||||||
|
magnet_id INTEGER NOT NULL,
|
||||||
|
processed_at DATETIME NOT NULL,
|
||||||
|
FOREIGN KEY (magnet_id) REFERENCES magnets (id)
|
||||||
|
);
|
||||||
|
|
||||||
|
INSERT INTO transmission_processed (id, magnet_id, processed_at)
|
||||||
|
SELECT id, magnet_id, processed_at
|
||||||
|
FROM transmission_processed_old;
|
||||||
|
|
||||||
|
DROP TABLE transmission_processed_old;
|
||||||
|
|
||||||
|
CREATE INDEX transmission_processed_magnet_id ON transmission_processed (magnet_id);
|
||||||
|
|
@ -37,33 +37,29 @@ impl Action for BitmagnetAction {
|
||||||
let mut failed_magnets = Vec::new();
|
let mut failed_magnets = Vec::new();
|
||||||
|
|
||||||
for magnet in unprocessed_magnets {
|
for magnet in unprocessed_magnets {
|
||||||
if let Some(id) = magnet.id {
|
match self
|
||||||
match self
|
.client
|
||||||
.client
|
.submit_magnet(
|
||||||
.submit_magnet(
|
&magnet.link,
|
||||||
&magnet.link,
|
&magnet.published_at.and_utc(),
|
||||||
&magnet.published_at.and_utc(),
|
&magnet.imdb_id,
|
||||||
&magnet.imdb_id,
|
)
|
||||||
)
|
.await
|
||||||
.await
|
{
|
||||||
{
|
Ok(_) => {
|
||||||
Ok(_) => {
|
debug!(
|
||||||
debug!(
|
"Successfully submitted magnet link to {}: {}",
|
||||||
"Successfully submitted magnet link to {}: {}",
|
Self::name(),
|
||||||
Self::name(),
|
magnet.title
|
||||||
magnet.title
|
);
|
||||||
);
|
debug!("Magnet link: {}", magnet.link);
|
||||||
debug!("Magnet link: {}", magnet.link);
|
db.mark_magnet_processed_for_table::<BitmagnetProcessedTable>(magnet.id)?;
|
||||||
db.mark_magnet_processed_for_table::<BitmagnetProcessedTable>(id)?;
|
processed_magnets.push(magnet);
|
||||||
processed_magnets.push(magnet);
|
}
|
||||||
}
|
Err(e) => {
|
||||||
Err(e) => {
|
warn!("Failed to submit magnet link to {}: {}", Self::name(), e);
|
||||||
warn!("Failed to submit magnet link to {}: {}", Self::name(), e);
|
failed_magnets.push(magnet);
|
||||||
failed_magnets.push(magnet);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
warn!("Skipping magnet with null ID: {}", magnet.link);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -37,25 +37,21 @@ impl Action for TransmissionAction {
|
||||||
let mut failed_magnets = Vec::new();
|
let mut failed_magnets = Vec::new();
|
||||||
|
|
||||||
for magnet in unprocessed_magnets {
|
for magnet in unprocessed_magnets {
|
||||||
if let Some(id) = magnet.id {
|
match self.client.submit_magnet(&magnet.link).await {
|
||||||
match self.client.submit_magnet(&magnet.link).await {
|
Ok(_) => {
|
||||||
Ok(_) => {
|
debug!(
|
||||||
debug!(
|
"Successfully submitted magnet link to {}: {}",
|
||||||
"Successfully submitted magnet link to {}: {}",
|
Self::name(),
|
||||||
Self::name(),
|
magnet.title
|
||||||
magnet.title
|
);
|
||||||
);
|
debug!("Magnet link: {}", magnet.link);
|
||||||
debug!("Magnet link: {}", magnet.link);
|
db.mark_magnet_processed_for_table::<TransmissionProcessedTable>(magnet.id)?;
|
||||||
db.mark_magnet_processed_for_table::<TransmissionProcessedTable>(id)?;
|
processed_magnets.push(magnet);
|
||||||
processed_magnets.push(magnet);
|
}
|
||||||
}
|
Err(e) => {
|
||||||
Err(e) => {
|
warn!("Failed to submit magnet link to {}: {}", Self::name(), e);
|
||||||
warn!("Failed to submit magnet link to {}: {}", Self::name(), e);
|
failed_magnets.push(magnet);
|
||||||
failed_magnets.push(magnet);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
warn!("Skipping magnet with null ID: {}", magnet.link);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ use diesel::prelude::*;
|
||||||
#[diesel(table_name = magnets)]
|
#[diesel(table_name = magnets)]
|
||||||
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
|
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
|
||||||
pub struct Magnet {
|
pub struct Magnet {
|
||||||
pub id: Option<i32>,
|
pub id: i32,
|
||||||
pub title: String,
|
pub title: String,
|
||||||
pub submitter: String,
|
pub submitter: String,
|
||||||
pub subreddit: String,
|
pub subreddit: String,
|
||||||
|
|
@ -33,7 +33,7 @@ pub struct NewMagnet<'a> {
|
||||||
#[diesel(table_name = bitmagnet_processed)]
|
#[diesel(table_name = bitmagnet_processed)]
|
||||||
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
|
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
|
||||||
pub struct BitmagnetProcessed {
|
pub struct BitmagnetProcessed {
|
||||||
pub id: Option<i32>,
|
pub id: i32,
|
||||||
pub magnet_id: i32,
|
pub magnet_id: i32,
|
||||||
pub processed_at: NaiveDateTime,
|
pub processed_at: NaiveDateTime,
|
||||||
}
|
}
|
||||||
|
|
@ -51,7 +51,7 @@ pub struct NewBitmagnetProcessed<'a> {
|
||||||
#[diesel(table_name = transmission_processed)]
|
#[diesel(table_name = transmission_processed)]
|
||||||
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
|
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
|
||||||
pub struct TransmissionProcessed {
|
pub struct TransmissionProcessed {
|
||||||
pub id: Option<i32>,
|
pub id: i32,
|
||||||
pub magnet_id: i32,
|
pub magnet_id: i32,
|
||||||
pub processed_at: NaiveDateTime,
|
pub processed_at: NaiveDateTime,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -67,7 +67,7 @@ mod tests {
|
||||||
|
|
||||||
fn create_fake_magnet() -> Magnet {
|
fn create_fake_magnet() -> Magnet {
|
||||||
Magnet {
|
Magnet {
|
||||||
id: None,
|
id: 1,
|
||||||
title: "foo".to_string(),
|
title: "foo".to_string(),
|
||||||
submitter: "bar".to_string(),
|
submitter: "bar".to_string(),
|
||||||
subreddit: "baz".to_string(),
|
subreddit: "baz".to_string(),
|
||||||
|
|
|
||||||
|
|
@ -151,7 +151,7 @@ mod tests {
|
||||||
|
|
||||||
fn create_test_magnet(title: &str) -> Magnet {
|
fn create_test_magnet(title: &str) -> Magnet {
|
||||||
Magnet {
|
Magnet {
|
||||||
id: Some(1),
|
id: 1,
|
||||||
title: title.to_string(),
|
title: title.to_string(),
|
||||||
submitter: "test_user".to_string(),
|
submitter: "test_user".to_string(),
|
||||||
subreddit: "test_subreddit".to_string(),
|
subreddit: "test_subreddit".to_string(),
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
diesel::table! {
|
diesel::table! {
|
||||||
bitmagnet_processed (id) {
|
bitmagnet_processed (id) {
|
||||||
id -> Nullable<Integer>,
|
id -> Integer,
|
||||||
magnet_id -> Integer,
|
magnet_id -> Integer,
|
||||||
processed_at -> Timestamp,
|
processed_at -> Timestamp,
|
||||||
}
|
}
|
||||||
|
|
@ -10,7 +10,7 @@ diesel::table! {
|
||||||
|
|
||||||
diesel::table! {
|
diesel::table! {
|
||||||
magnets (id) {
|
magnets (id) {
|
||||||
id -> Nullable<Integer>,
|
id -> Integer,
|
||||||
title -> Text,
|
title -> Text,
|
||||||
submitter -> Text,
|
submitter -> Text,
|
||||||
subreddit -> Text,
|
subreddit -> Text,
|
||||||
|
|
@ -22,7 +22,7 @@ diesel::table! {
|
||||||
|
|
||||||
diesel::table! {
|
diesel::table! {
|
||||||
transmission_processed (id) {
|
transmission_processed (id) {
|
||||||
id -> Nullable<Integer>,
|
id -> Integer,
|
||||||
magnet_id -> Integer,
|
magnet_id -> Integer,
|
||||||
processed_at -> Timestamp,
|
processed_at -> Timestamp,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue