diff --git a/migrations/20221205020531_create_accounts.sql b/migrations/20221205020531_create_accounts.sql index 8f76a7f..0bbe332 100644 --- a/migrations/20221205020531_create_accounts.sql +++ b/migrations/20221205020531_create_accounts.sql @@ -1,5 +1,6 @@ CREATE TABLE accounts ( id BIGSERIAL PRIMARY KEY, + iri VARCHAR, name VARCHAR NOT NULL, domain VARCHAR NOT NULL, display_name VARCHAR DEFAULT NULL, @@ -7,4 +8,5 @@ CREATE TABLE accounts ( updated_at TIMESTAMP NOT NULL DEFAULT now() ); +CREATE UNIQUE INDEX index_accounts_on_iri ON accounts (iri); CREATE UNIQUE INDEX index_accounts_on_name_and_domain ON accounts (name, domain); diff --git a/migrations/20221205235226_create_notes.sql b/migrations/20221205235226_create_notes.sql index db11fba..2bca521 100644 --- a/migrations/20221205235226_create_notes.sql +++ b/migrations/20221205235226_create_notes.sql @@ -1,7 +1,7 @@ CREATE TABLE notes ( id BIGSERIAL PRIMARY KEY, account_id BIGINT REFERENCES accounts (id), - uri VARCHAR, + iri VARCHAR, content TEXT NOT NULL, source TEXT DEFAULT NULL, summary TEXT DEFAULT NULL, @@ -11,5 +11,5 @@ CREATE TABLE notes ( ); CREATE INDEX index_notes_on_account_id ON notes (account_id); -CREATE UNIQUE INDEX index_notes_on_uri ON notes (uri); +CREATE UNIQUE INDEX index_notes_on_iri ON notes (iri); CREATE INDEX index_notes_on_created_at ON notes (created_at); diff --git a/migrations/20221207230140_create_follows.sql b/migrations/20221207230140_create_follows.sql index 436b3f9..c23bbc7 100644 --- a/migrations/20221207230140_create_follows.sql +++ b/migrations/20221207230140_create_follows.sql @@ -1,9 +1,10 @@ CREATE TABLE follows ( id BIGSERIAL PRIMARY KEY, + iri VARCHAR, follower_id BIGINT REFERENCES accounts (id) ON DELETE CASCADE, followee_id BIGINT REFERENCES accounts (id) ON DELETE CASCADE, - created_at TIMESTAMP DEFAULT now() + created_at TIMESTAMP NOT NULL DEFAULT now() ); -CREATE INDEX index_follows_on_follower_id ON follows (follower_id); -CREATE INDEX index_follows_on_followee_id ON follows (followee_id); +CREATE UNIQUE INDEX index_follows_on_iri ON follows(iri); +CREATE UNIQUE INDEX index_follows_on_follower_id_and_followee_id ON follows (follower_id, followee_id); diff --git a/migrations/20221211175753_create_likes.sql b/migrations/20221211175753_create_likes.sql index 8e89e0d..03b31e0 100644 --- a/migrations/20221211175753_create_likes.sql +++ b/migrations/20221211175753_create_likes.sql @@ -1,10 +1,10 @@ CREATE TABLE likes ( id BIGSERIAL PRIMARY KEY, + iri VARCHAR, note_id BIGINT REFERENCES notes (id) ON DELETE CASCADE, account_id BIGINT REFERENCES accounts (id) ON DELETE CASCADE, created_at TIMESTAMP NOT NULL DEFAULT now() ); -CREATE INDEX index_likes_on_note_id ON likes (note_id); -CREATE INDEX index_likes_on_account_id ON likes (account_id); +CREATE UNIQUE INDEX index_likes_on_iri ON likes (iri); CREATE UNIQUE INDEX index_likes_on_note_id_and_account_id ON likes (note_id, account_id); diff --git a/src/data/account/pg.rs b/src/data/account/pg.rs index fdccad0..ff9821a 100644 --- a/src/data/account/pg.rs +++ b/src/data/account/pg.rs @@ -30,10 +30,11 @@ impl PgAccountDataSource { { let new = new.into(); let account: Account = sqlx::query_as( - "INSERT INTO accounts (name, domain, display_name) - VALUES ($1, $2, $3) + "INSERT INTO accounts (iri, name, domain, display_name) + VALUES ($1, $2, $3, $4) RETURNING *", ) + .bind(new.iri) .bind(new.name) .bind(new.domain) .bind(new.display_name) diff --git a/src/data/follow/pg.rs b/src/data/follow/pg.rs index 18ad85c..2878188 100644 --- a/src/data/follow/pg.rs +++ b/src/data/follow/pg.rs @@ -29,8 +29,11 @@ impl PgFollowDataSource { { let new = new.into(); let follow: Follow = sqlx::query_as( - "INSERT INTO follows (follower_id, followee_id) VALUES ($1, $2) RETURNING *", + "INSERT INTO follows (iri, follower_id, followee_id) + VALUES ($1, $2, $3) + RETURNING *", ) + .bind(new.iri) .bind(new.follower_id) .bind(new.followee_id) .fetch_one(&self.pool) diff --git a/src/data/like/pg.rs b/src/data/like/pg.rs index 488f9ef..d679944 100644 --- a/src/data/like/pg.rs +++ b/src/data/like/pg.rs @@ -30,12 +30,16 @@ impl PgLikeDataSource { E: Into, { let new = new.try_into().map_err(|e| e.into())?.inner(); - let like: Like = - sqlx::query_as("INSERT INTO likes (note_id, account_id) VALUES ($1, $2) RETURNING *") - .bind(new.note_id) - .bind(new.account_id) - .fetch_one(&self.pool) - .await?; + let like: Like = sqlx::query_as( + "INSERT INTO likes (iri, note_id, account_id) + VALUES ($1, $2, $3) + RETURNING *", + ) + .bind(new.iri) + .bind(new.note_id) + .bind(new.account_id) + .fetch_one(&self.pool) + .await?; Ok(like) } diff --git a/src/data/note/pg.rs b/src/data/note/pg.rs index b9a87b2..1f35011 100644 --- a/src/data/note/pg.rs +++ b/src/data/note/pg.rs @@ -51,7 +51,7 @@ impl PgNoteDataSource { let note: Note = sqlx::query_as( "INSERT INTO notes ( account_id, - uri, + iri, content, source, summary, @@ -60,7 +60,7 @@ impl PgNoteDataSource { RETURNING *", ) .bind(new.account_id) - .bind(new.uri) + .bind(new.iri) .bind(new.content) .bind(new.source) .bind(new.summary) diff --git a/src/model/account.rs b/src/model/account.rs index 5dcfe38..3ef5f4a 100644 --- a/src/model/account.rs +++ b/src/model/account.rs @@ -8,6 +8,7 @@ use crate::util::validate::{ResultBuilder, Validate}; #[derive(Deserialize, Serialize, FromRow)] pub struct Account { pub id: Id, + pub iri: Option, pub name: String, pub domain: String, pub display_name: Option, @@ -16,6 +17,7 @@ pub struct Account { } pub struct NewAccount { + pub iri: Option, pub name: String, pub domain: String, pub display_name: Option, diff --git a/src/model/follow.rs b/src/model/follow.rs index 5bb2543..1b3024c 100644 --- a/src/model/follow.rs +++ b/src/model/follow.rs @@ -8,12 +8,14 @@ use crate::util::validate::{ResultBuilder, Validate}; #[derive(Deserialize, Serialize, FromRow)] pub struct Follow { pub id: Id, + pub iri: Option, pub follower_id: Id, pub followee_id: Id, pub created_at: NaiveDateTime, } pub struct NewFollow { + pub iri: Option, pub follower_id: Id, pub followee_id: Id, } diff --git a/src/model/like.rs b/src/model/like.rs index 46c381c..192ddac 100644 --- a/src/model/like.rs +++ b/src/model/like.rs @@ -8,12 +8,14 @@ use crate::util::validate::{ResultBuilder, Validate}; #[derive(Deserialize, Serialize, FromRow)] pub struct Like { pub id: Id, + pub iri: Option, pub note_id: Id, pub account_id: Id, pub created_at: NaiveDateTime, } pub struct NewLike { + pub iri: Option, pub note_id: Id, pub account_id: Id, }