-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat[ai]: Add crud operations for a conversations table
Signed-off-by: Hiram Chirino <[email protected]>
- Loading branch information
Showing
6 changed files
with
274 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use sea_orm::entity::prelude::*; | ||
use time::OffsetDateTime; | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)] | ||
#[sea_orm(table_name = "conversation")] | ||
pub struct Model { | ||
#[sea_orm(primary_key)] | ||
pub id: Uuid, | ||
pub user_id: String, | ||
pub state: serde_json::Value, | ||
pub seq: i32, | ||
pub summary: String, | ||
pub updated_at: OffsetDateTime, | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] | ||
pub enum Relation {} | ||
|
||
impl ActiveModelBehavior for ActiveModel {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use crate::UuidV4; | ||
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(Conversation::Table) | ||
.col( | ||
ColumnDef::new(Conversation::Id) | ||
.uuid() | ||
.not_null() | ||
.default(Func::cust(UuidV4)) | ||
.primary_key(), | ||
) | ||
.col(ColumnDef::new(Conversation::UserId).string().not_null()) | ||
.col(ColumnDef::new(Conversation::State).json_binary().not_null()) | ||
.col(ColumnDef::new(Conversation::Seq).integer().not_null()) | ||
.col(ColumnDef::new(Conversation::Summary).string().not_null()) | ||
.col( | ||
ColumnDef::new(Conversation::UpdatedAt) | ||
.timestamp_with_time_zone() | ||
.not_null(), | ||
) | ||
.to_owned(), | ||
) | ||
.await?; | ||
|
||
// this index should speed up lookup up the most recent conversations for a user | ||
manager | ||
.create_index( | ||
Index::create() | ||
.table(Conversation::Table) | ||
.name(Conversation::ConverstationUserIdUpdatedAtIdx.to_string()) | ||
.col(Conversation::UserId) | ||
.col(Conversation::UpdatedAt) | ||
.to_owned(), | ||
) | ||
.await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
manager | ||
.drop_index( | ||
Index::drop() | ||
.if_exists() | ||
.table(Conversation::Table) | ||
.name(Conversation::ConverstationUserIdUpdatedAtIdx.to_string()) | ||
.to_owned(), | ||
) | ||
.await?; | ||
|
||
manager | ||
.drop_table(Table::drop().table(Conversation::Table).to_owned()) | ||
.await | ||
} | ||
} | ||
|
||
#[derive(DeriveIden)] | ||
enum Conversation { | ||
Table, | ||
Id, | ||
UserId, | ||
ConverstationUserIdUpdatedAtIdx, | ||
State, | ||
Seq, | ||
Summary, | ||
UpdatedAt, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters