From a2bf8542d9af28147d374cb007a5ecd6775db5d8 Mon Sep 17 00:00:00 2001 From: Evie Viau Date: Sat, 4 Feb 2023 01:19:02 -0800 Subject: [PATCH] Create post schema --- migration/src/lib.rs | 6 ++- .../src/m20230204_011034_create_posts.rs | 49 +++++++++++++++++++ src/entities/mod.rs | 1 + src/entities/post.rs | 21 ++++++++ src/entities/prelude.rs | 1 + 5 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 migration/src/m20230204_011034_create_posts.rs create mode 100644 src/entities/post.rs diff --git a/migration/src/lib.rs b/migration/src/lib.rs index 48e9254..75049b4 100644 --- a/migration/src/lib.rs +++ b/migration/src/lib.rs @@ -1,11 +1,15 @@ pub use sea_orm_migration::prelude::*; mod m20230204_001022_create_pages; +mod m20230204_011034_create_posts; pub struct Migrator; #[async_trait::async_trait] impl MigratorTrait for Migrator { fn migrations() -> Vec> { - vec![Box::new(m20230204_001022_create_pages::Migration)] + vec![ + Box::new(m20230204_001022_create_pages::Migration), + Box::new(m20230204_011034_create_posts::Migration), + ] } } diff --git a/migration/src/m20230204_011034_create_posts.rs b/migration/src/m20230204_011034_create_posts.rs new file mode 100644 index 0000000..9a3e419 --- /dev/null +++ b/migration/src/m20230204_011034_create_posts.rs @@ -0,0 +1,49 @@ +use sea_orm_migration::prelude::*; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .create_table( + Table::create() + .table(Post::Table) + .if_not_exists() + .col( + ColumnDef::new(Post::Id) + .string() + .not_null() + .primary_key(), + ) + .col(ColumnDef::new(Post::Draft).boolean().not_null()) + .col(ColumnDef::new(Post::Slug).string().not_null()) + .col(ColumnDef::new(Post::Title).string().not_null()) + .col(ColumnDef::new(Post::Summary).string()) + .col(ColumnDef::new(Post::Published).date_time().not_null()) + .col(ColumnDef::new(Post::Updated).date_time()) + .to_owned(), + ) + .await + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .drop_table(Table::drop().table(Post::Table).to_owned()) + .await + } +} + +/// Learn more at https://docs.rs/sea-query#iden +#[derive(Iden)] +enum Post { + Table, + Id, + Draft, + Slug, + Title, + Summary, + Published, + Updated, +} diff --git a/src/entities/mod.rs b/src/entities/mod.rs index df3b074..57cfd6f 100644 --- a/src/entities/mod.rs +++ b/src/entities/mod.rs @@ -3,3 +3,4 @@ pub mod prelude; pub mod page; +pub mod post; diff --git a/src/entities/post.rs b/src/entities/post.rs new file mode 100644 index 0000000..a8d20c8 --- /dev/null +++ b/src/entities/post.rs @@ -0,0 +1,21 @@ +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.10.7 + +use sea_orm::entity::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] +#[sea_orm(table_name = "post")] +pub struct Model { + #[sea_orm(primary_key, auto_increment = false)] + pub id: String, + pub draft: i8, + pub slug: String, + pub title: String, + pub summary: Option, + pub published: DateTime, + pub updated: Option, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation {} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/src/entities/prelude.rs b/src/entities/prelude.rs index f22f480..b2296eb 100644 --- a/src/entities/prelude.rs +++ b/src/entities/prelude.rs @@ -1,3 +1,4 @@ //! `SeaORM` Entity. Generated by sea-orm-codegen 0.10.7 pub use super::page::Entity as Page; +pub use super::post::Entity as Post;