diff --git a/migrations/2024-07-19-133751_folders/down.sql b/migrations/2024-07-19-133751_folders/down.sql deleted file mode 100644 index 612f404..0000000 --- a/migrations/2024-07-19-133751_folders/down.sql +++ /dev/null @@ -1,2 +0,0 @@ -DROP TABLE bookmarks_folders; -DROP TABLE folders; diff --git a/migrations/2024-07-20-161654_folders/down.sql b/migrations/2024-07-20-161654_folders/down.sql new file mode 100644 index 0000000..49a3a12 --- /dev/null +++ b/migrations/2024-07-20-161654_folders/down.sql @@ -0,0 +1,2 @@ +ALTER TABLE bookmarks DROP COLUMN folder_id; +DROP TABLE folders; diff --git a/migrations/2024-07-19-133751_folders/up.sql b/migrations/2024-07-20-161654_folders/up.sql similarity index 57% rename from migrations/2024-07-19-133751_folders/up.sql rename to migrations/2024-07-20-161654_folders/up.sql index da03f89..49de77a 100644 --- a/migrations/2024-07-19-133751_folders/up.sql +++ b/migrations/2024-07-20-161654_folders/up.sql @@ -5,8 +5,5 @@ CREATE TABLE folders ( created_at timestamp(6) with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP, UNIQUE ("path") ); -CREATE TABLE bookmarks_folders( - bookmark_id integer REFERENCES bookmarks(id) ON DELETE CASCADE, - folder_id integer REFERENCES folders(id), - PRIMARY KEY (bookmark_id, folder_id) -); +ALTER TABLE bookmarks +ADD COLUMN folder_id integer REFERENCES folders(id); diff --git a/src/db/bookmark.rs b/src/db/bookmark.rs index e33a2ad..3fa0352 100644 --- a/src/db/bookmark.rs +++ b/src/db/bookmark.rs @@ -29,6 +29,7 @@ pub struct Bookmark { pub deleted_at: Option, #[serde(with = "time::serde::rfc3339")] pub updated_at: time::OffsetDateTime, + pub folder_id: Option, } #[derive(Insertable, AsChangeset, Deserialize, Serialize, Debug, Clone)] diff --git a/src/db/folder.rs b/src/db/folder.rs index 7fc8461..3bd34e5 100644 --- a/src/db/folder.rs +++ b/src/db/folder.rs @@ -2,8 +2,7 @@ use diesel::prelude::*; use diesel_async::{AsyncPgConnection as Connection, RunQueryDsl}; use rocket::serde::{Deserialize, Serialize}; -use super::bookmark::Bookmark; -use super::schema::{bookmarks_folders, folders}; +use super::schema::folders; use crate::utils::DatabaseError; #[derive(Queryable, Selectable, Identifiable, Debug, Deserialize, Serialize)] @@ -18,16 +17,6 @@ pub struct Folder { pub updated_at: time::OffsetDateTime, } -#[derive(Insertable, Identifiable, Selectable, Queryable, Associations, Debug)] -#[diesel(belongs_to(Bookmark))] -#[diesel(belongs_to(Folder))] -#[diesel(table_name = bookmarks_folders)] -#[diesel(primary_key(bookmark_id, folder_id))] -pub struct BookmarkFolder { - pub bookmark_id: i32, - pub folder_id: i32, -} - #[derive(Insertable, Debug, Clone)] #[diesel(table_name = folders)] pub struct NewFolder<'a> { diff --git a/src/db/schema.rs b/src/db/schema.rs index 05258a8..0a0b945 100644 --- a/src/db/schema.rs +++ b/src/db/schema.rs @@ -8,13 +8,7 @@ diesel::table! { created_at -> Timestamptz, deleted_at -> Nullable, updated_at -> Timestamptz, - } -} - -diesel::table! { - bookmarks_folders (bookmark_id, folder_id) { - bookmark_id -> Int4, - folder_id -> Int4, + folder_id -> Nullable, } } @@ -43,15 +37,8 @@ diesel::table! { } } -diesel::joinable!(bookmarks_folders -> bookmarks (bookmark_id)); -diesel::joinable!(bookmarks_folders -> folders (folder_id)); +diesel::joinable!(bookmarks -> folders (folder_id)); diesel::joinable!(bookmarks_tags -> bookmarks (bookmark_id)); diesel::joinable!(bookmarks_tags -> tags (tag_id)); -diesel::allow_tables_to_appear_in_same_query!( - bookmarks, - bookmarks_folders, - bookmarks_tags, - folders, - tags, -); +diesel::allow_tables_to_appear_in_same_query!(bookmarks, bookmarks_tags, folders, tags,);