Skip to content

Commit

Permalink
Correct relationship between folders and bookmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
linw1995 committed Aug 2, 2024
1 parent 7a63398 commit c641453
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 35 deletions.
2 changes: 0 additions & 2 deletions migrations/2024-07-19-133751_folders/down.sql

This file was deleted.

2 changes: 2 additions & 0 deletions migrations/2024-07-20-161654_folders/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE bookmarks DROP COLUMN folder_id;
DROP TABLE folders;
Original file line number Diff line number Diff line change
Expand Up @@ -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);
1 change: 1 addition & 0 deletions src/db/bookmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub struct Bookmark {
pub deleted_at: Option<time::OffsetDateTime>,
#[serde(with = "time::serde::rfc3339")]
pub updated_at: time::OffsetDateTime,
pub folder_id: Option<i32>,
}

#[derive(Insertable, AsChangeset, Deserialize, Serialize, Debug, Clone)]
Expand Down
13 changes: 1 addition & 12 deletions src/db/folder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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> {
Expand Down
19 changes: 3 additions & 16 deletions src/db/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@ diesel::table! {
created_at -> Timestamptz,
deleted_at -> Nullable<Timestamptz>,
updated_at -> Timestamptz,
}
}

diesel::table! {
bookmarks_folders (bookmark_id, folder_id) {
bookmark_id -> Int4,
folder_id -> Int4,
folder_id -> Nullable<Int4>,
}
}

Expand Down Expand Up @@ -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,);

0 comments on commit c641453

Please sign in to comment.