diff --git a/migrations/2018-09-05-174106_create_tags/down.sql b/migrations/2018-09-05-174106_create_tags/down.sql new file mode 100644 index 0000000..43c79a4 --- /dev/null +++ b/migrations/2018-09-05-174106_create_tags/down.sql @@ -0,0 +1,2 @@ +-- This file should undo anything in `up.sql` +DROP TABLE tags; diff --git a/migrations/2018-09-05-174106_create_tags/up.sql b/migrations/2018-09-05-174106_create_tags/up.sql new file mode 100644 index 0000000..9ef3285 --- /dev/null +++ b/migrations/2018-09-05-174106_create_tags/up.sql @@ -0,0 +1,7 @@ +-- Your SQL goes here +CREATE TABLE tags ( + id SERIAL PRIMARY KEY, + tag TEXT NOT NULL DEFAULT '', + is_hastag BOOLEAN NOT NULL DEFAULT 'f', + post_id INTEGER REFERENCES posts(id) ON DELETE CASCADE NOT NULL +) diff --git a/plume-models/src/lib.rs b/plume-models/src/lib.rs index f2c80c6..acfc2b1 100644 --- a/plume-models/src/lib.rs +++ b/plume-models/src/lib.rs @@ -119,4 +119,5 @@ pub mod posts; pub mod reshares; pub mod safe_string; pub mod schema; +pub mod tags; pub mod users; diff --git a/plume-models/src/schema.rs b/plume-models/src/schema.rs index 7214871..1a9cd29 100644 --- a/plume-models/src/schema.rs +++ b/plume-models/src/schema.rs @@ -139,6 +139,15 @@ table! { } } +table! { + tags (id) { + id -> Int4, + tag -> Text, + is_hastag -> Bool, + post_id -> Int4, + } +} + table! { users (id) { id -> Int4, @@ -178,6 +187,7 @@ joinable!(post_authors -> users (author_id)); joinable!(posts -> blogs (blog_id)); joinable!(reshares -> posts (post_id)); joinable!(reshares -> users (user_id)); +joinable!(tags -> posts (post_id)); joinable!(users -> instances (instance_id)); allow_tables_to_appear_in_same_query!( @@ -193,5 +203,6 @@ allow_tables_to_appear_in_same_query!( post_authors, posts, reshares, + tags, users, ); diff --git a/plume-models/src/tags.rs b/plume-models/src/tags.rs new file mode 100644 index 0000000..359f32d --- /dev/null +++ b/plume-models/src/tags.rs @@ -0,0 +1,23 @@ +use diesel::{self, PgConnection, ExpressionMethods, RunQueryDsl, QueryDsl}; +use schema::tags; + +#[derive(Queryable)] +pub struct Tag { + pub id: i32, + pub tag: String, + pub is_hastag: bool, + pub post_id: i32 +} + +#[derive(Insertable)] +#[table_name = "tags"] +pub struct NewTag { + pub tag: String, + pub is_hastag: bool, + pub post_id: i32 +} + +impl Tag { + insert!(tags, NewTag); + get!(tags); +}