Merge branch 'non-null-ids' into 'main'
feat: make DB IDs not null See merge request kernald/reddit-magnet!27
This commit is contained in:
commit
baca332353
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();
|
||||
|
||||
for magnet in unprocessed_magnets {
|
||||
if let Some(id) = magnet.id {
|
||||
match self
|
||||
.client
|
||||
.submit_magnet(
|
||||
&magnet.link,
|
||||
&magnet.published_at.and_utc(),
|
||||
&magnet.imdb_id,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(_) => {
|
||||
debug!(
|
||||
"Successfully submitted magnet link to {}: {}",
|
||||
Self::name(),
|
||||
magnet.title
|
||||
);
|
||||
debug!("Magnet link: {}", magnet.link);
|
||||
db.mark_magnet_processed_for_table::<BitmagnetProcessedTable>(id)?;
|
||||
processed_magnets.push(magnet);
|
||||
}
|
||||
Err(e) => {
|
||||
warn!("Failed to submit magnet link to {}: {}", Self::name(), e);
|
||||
failed_magnets.push(magnet);
|
||||
}
|
||||
match self
|
||||
.client
|
||||
.submit_magnet(
|
||||
&magnet.link,
|
||||
&magnet.published_at.and_utc(),
|
||||
&magnet.imdb_id,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(_) => {
|
||||
debug!(
|
||||
"Successfully submitted magnet link to {}: {}",
|
||||
Self::name(),
|
||||
magnet.title
|
||||
);
|
||||
debug!("Magnet link: {}", magnet.link);
|
||||
db.mark_magnet_processed_for_table::<BitmagnetProcessedTable>(magnet.id)?;
|
||||
processed_magnets.push(magnet);
|
||||
}
|
||||
Err(e) => {
|
||||
warn!("Failed to submit magnet link to {}: {}", Self::name(), e);
|
||||
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();
|
||||
|
||||
for magnet in unprocessed_magnets {
|
||||
if let Some(id) = magnet.id {
|
||||
match self.client.submit_magnet(&magnet.link).await {
|
||||
Ok(_) => {
|
||||
debug!(
|
||||
"Successfully submitted magnet link to {}: {}",
|
||||
Self::name(),
|
||||
magnet.title
|
||||
);
|
||||
debug!("Magnet link: {}", magnet.link);
|
||||
db.mark_magnet_processed_for_table::<TransmissionProcessedTable>(id)?;
|
||||
processed_magnets.push(magnet);
|
||||
}
|
||||
Err(e) => {
|
||||
warn!("Failed to submit magnet link to {}: {}", Self::name(), e);
|
||||
failed_magnets.push(magnet);
|
||||
}
|
||||
match self.client.submit_magnet(&magnet.link).await {
|
||||
Ok(_) => {
|
||||
debug!(
|
||||
"Successfully submitted magnet link to {}: {}",
|
||||
Self::name(),
|
||||
magnet.title
|
||||
);
|
||||
debug!("Magnet link: {}", magnet.link);
|
||||
db.mark_magnet_processed_for_table::<TransmissionProcessedTable>(magnet.id)?;
|
||||
processed_magnets.push(magnet);
|
||||
}
|
||||
Err(e) => {
|
||||
warn!("Failed to submit magnet link to {}: {}", Self::name(), e);
|
||||
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(check_for_backend(diesel::sqlite::Sqlite))]
|
||||
pub struct Magnet {
|
||||
pub id: Option<i32>,
|
||||
pub id: i32,
|
||||
pub title: String,
|
||||
pub submitter: String,
|
||||
pub subreddit: String,
|
||||
|
|
@ -33,7 +33,7 @@ pub struct NewMagnet<'a> {
|
|||
#[diesel(table_name = bitmagnet_processed)]
|
||||
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
|
||||
pub struct BitmagnetProcessed {
|
||||
pub id: Option<i32>,
|
||||
pub id: i32,
|
||||
pub magnet_id: i32,
|
||||
pub processed_at: NaiveDateTime,
|
||||
}
|
||||
|
|
@ -51,7 +51,7 @@ pub struct NewBitmagnetProcessed<'a> {
|
|||
#[diesel(table_name = transmission_processed)]
|
||||
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
|
||||
pub struct TransmissionProcessed {
|
||||
pub id: Option<i32>,
|
||||
pub id: i32,
|
||||
pub magnet_id: i32,
|
||||
pub processed_at: NaiveDateTime,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -67,7 +67,7 @@ mod tests {
|
|||
|
||||
fn create_fake_magnet() -> Magnet {
|
||||
Magnet {
|
||||
id: None,
|
||||
id: 1,
|
||||
title: "foo".to_string(),
|
||||
submitter: "bar".to_string(),
|
||||
subreddit: "baz".to_string(),
|
||||
|
|
|
|||
|
|
@ -151,7 +151,7 @@ mod tests {
|
|||
|
||||
fn create_test_magnet(title: &str) -> Magnet {
|
||||
Magnet {
|
||||
id: Some(1),
|
||||
id: 1,
|
||||
title: title.to_string(),
|
||||
submitter: "test_user".to_string(),
|
||||
subreddit: "test_subreddit".to_string(),
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
diesel::table! {
|
||||
bitmagnet_processed (id) {
|
||||
id -> Nullable<Integer>,
|
||||
id -> Integer,
|
||||
magnet_id -> Integer,
|
||||
processed_at -> Timestamp,
|
||||
}
|
||||
|
|
@ -10,7 +10,7 @@ diesel::table! {
|
|||
|
||||
diesel::table! {
|
||||
magnets (id) {
|
||||
id -> Nullable<Integer>,
|
||||
id -> Integer,
|
||||
title -> Text,
|
||||
submitter -> Text,
|
||||
subreddit -> Text,
|
||||
|
|
@ -22,7 +22,7 @@ diesel::table! {
|
|||
|
||||
diesel::table! {
|
||||
transmission_processed (id) {
|
||||
id -> Nullable<Integer>,
|
||||
id -> Integer,
|
||||
magnet_id -> Integer,
|
||||
processed_at -> Timestamp,
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue