sea-orm-taggable 0.1.0

Tags for any SeaORM entity: attach, detach, sync, tag types, ordering and tag filters
Documentation
//! The migration for the `tags` and `taggables` tables.

use sea_orm::sea_query::{
    ColumnDef, ConditionalStatement, Expr, ExprTrait, ForeignKey, ForeignKeyAction, Index,
    IndexCreateStatement, Table, TableCreateStatement, TableDropStatement,
};

/// `CREATE TABLE tags`.
///
/// Run [`tags_table`], [`taggables_table`] and [`indexes`] in your migration's `up`,
/// [`drop_tables`] in its `down`:
///
/// ```
/// use sea_orm::sea_query::PostgresQueryBuilder;
///
/// use sea_orm_taggable::schema;
///
/// // In a SeaORM migration: manager.create_table(schema::tags_table()).await?; ...
/// let sql: Vec<String> = schema::indexes()
///     .iter()
///     .map(|index| index.to_string(PostgresQueryBuilder))
///     .collect();
/// assert_eq!(sql[0], r#"CREATE UNIQUE INDEX "tags_type_slug_unique" ON "tags" ("type", "slug") WHERE "type" IS NOT NULL"#);
/// assert_eq!(sql[1], r#"CREATE UNIQUE INDEX "tags_slug_untyped_unique" ON "tags" ("slug") WHERE "type" IS NULL"#);
/// ```
#[must_use]
pub fn tags_table() -> TableCreateStatement {
    Table::create()
        .table("tags")
        .if_not_exists()
        .col(
            ColumnDef::new("id")
                .big_integer()
                .not_null()
                .auto_increment()
                .primary_key(),
        )
        .col(ColumnDef::new("name").string_len(255).not_null())
        .col(ColumnDef::new("slug").string_len(255).not_null())
        .col(ColumnDef::new("type").string_len(255).null())
        .col(ColumnDef::new("order_column").integer().not_null())
        .col(
            ColumnDef::new("created_at")
                .timestamp_with_time_zone()
                .not_null(),
        )
        .col(
            ColumnDef::new("updated_at")
                .timestamp_with_time_zone()
                .not_null(),
        )
        .to_owned()
}

/// `CREATE TABLE taggables`: deleting a tag removes its rows here.
#[must_use]
pub fn taggables_table() -> TableCreateStatement {
    Table::create()
        .table("taggables")
        .if_not_exists()
        .col(ColumnDef::new("tag_id").big_integer().not_null())
        .col(ColumnDef::new("taggable_type").string_len(255).not_null())
        .col(ColumnDef::new("taggable_id").big_integer().not_null())
        .col(ColumnDef::new("position").integer().not_null())
        .primary_key(
            Index::create()
                .col("tag_id")
                .col("taggable_type")
                .col("taggable_id"),
        )
        .foreign_key(
            ForeignKey::create()
                .name("taggables_tag_id_foreign")
                .from("taggables", "tag_id")
                .to("tags", "id")
                .on_delete(ForeignKeyAction::Cascade),
        )
        .to_owned()
}

/// The indexes: a tag's slug is unique within its type (and among plain tags), and
/// the tags of a row are found through `(taggable_type, taggable_id)`.
///
/// The unique indexes are partial (`WHERE`), which Postgres and SQLite support and
/// MySQL does not.
#[must_use]
pub fn indexes() -> Vec<IndexCreateStatement> {
    vec![
        Index::create()
            .name("tags_type_slug_unique")
            .table("tags")
            .col("type")
            .col("slug")
            .unique()
            .and_where(Expr::col("type").is_not_null())
            .to_owned(),
        Index::create()
            .name("tags_slug_untyped_unique")
            .table("tags")
            .col("slug")
            .unique()
            .and_where(Expr::col("type").is_null())
            .to_owned(),
        Index::create()
            .name("taggables_taggable_type_taggable_id_index")
            .table("taggables")
            .col("taggable_type")
            .col("taggable_id")
            .to_owned(),
    ]
}

/// `DROP TABLE taggables, tags`, for the migration's `down`.
#[must_use]
pub fn drop_tables() -> [TableDropStatement; 2] {
    [
        Table::drop().table("taggables").if_exists().to_owned(),
        Table::drop().table("tags").if_exists().to_owned(),
    ]
}