use sea_orm::sea_query::{
ColumnDef, ConditionalStatement, Expr, ExprTrait, ForeignKey, ForeignKeyAction, Index,
IndexCreateStatement, Table, TableCreateStatement, TableDropStatement,
};
#[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()
}
#[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()
}
#[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(),
]
}
#[must_use]
pub fn drop_tables() -> [TableDropStatement; 2] {
[
Table::drop().table("taggables").if_exists().to_owned(),
Table::drop().table("tags").if_exists().to_owned(),
]
}