1.9 KiB · text History 7ce364f
//! Rich links staff attach to a post, shown as cards under it.
//!
//! The page's metadata is fetched once, in the background, and stored here,
//! so a card renders from our own database: a page view never waits on (or
//! tells) somebody else's server. The card picture is copied in too, as
//! bytes, like an avatar, rather than hotlinked.
//!
//! * `url` — normalised (http/https, bare domains as https). Unique per post.
//! * `status` — `pending` (not fetched yet; picked up again at startup),
//!   `ok`, or `failed` (the card shows the link and its host).
//! * `created_by_id` — the staff member who attached it.
use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

const UP: &str = r#"
CREATE TABLE post_links (
    id UUID PRIMARY KEY,
    post_id UUID NOT NULL REFERENCES posts(id) ON DELETE CASCADE ON UPDATE CASCADE,
    url VARCHAR(2048) NOT NULL,
    position INTEGER NOT NULL DEFAULT 0,
    status VARCHAR(16) NOT NULL DEFAULT 'pending',
    title VARCHAR(300) NULL,
    description TEXT NULL,
    site_name VARCHAR(200) NULL,
    image_data BYTEA NULL,
    image_content_type VARCHAR(32) NULL,
    fetched_at TIMESTAMPTZ NULL,
    created_by_id UUID NULL REFERENCES users(id) ON DELETE SET NULL,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    CONSTRAINT uq_post_links_post_url UNIQUE (post_id, url)
);
CREATE INDEX idx_post_links_pending ON post_links (created_at) WHERE status = 'pending';
"#;

const DOWN: &str = r#"
DROP TABLE IF EXISTS post_links;
"#;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager.get_connection().execute_unprepared(UP).await.map(|_| ())
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager.get_connection().execute_unprepared(DOWN).await.map(|_| ())
    }
}