diff --git a/migration/src/lib.rs b/migration/src/lib.rs index cffbe1d..c633293 100644 --- a/migration/src/lib.rs +++ b/migration/src/lib.rs @@ -2,6 +2,7 @@ pub use sea_orm_migration::prelude::*; mod m20231101_172500_create_time_entry_table; mod m20231106_134950_create_clients; +mod m20231106_195401_create_projects; pub struct Migrator; @@ -11,6 +12,7 @@ impl MigratorTrait for Migrator { vec![ Box::new(m20231101_172500_create_time_entry_table::Migration), Box::new(m20231106_134950_create_clients::Migration), + Box::new(m20231106_195401_create_projects::Migration), ] } diff --git a/migration/src/m20231106_195401_create_projects.rs b/migration/src/m20231106_195401_create_projects.rs new file mode 100644 index 0000000..9ca3e37 --- /dev/null +++ b/migration/src/m20231106_195401_create_projects.rs @@ -0,0 +1,64 @@ +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(Project::Table) + .if_not_exists() + .col(ColumnDef::new(Project::Id).integer().not_null()) + .col(ColumnDef::new(Project::TogglId).big_unsigned().not_null()) + .col( + ColumnDef::new(Project::WorkspaceId) + .big_unsigned() + .not_null(), + ) + .col(ColumnDef::new(Project::ClientId).integer()) + .col(ColumnDef::new(Project::Name).string().not_null()) + .col(ColumnDef::new(Project::Active).boolean().not_null()) + .col(ColumnDef::new(Project::RawJson).json_binary().not_null()) + .to_owned(), + ) + .await?; + + // Create foreign key + manager.create_foreign_key( + ForeignKey::create() + .name("project_client_id") + .from(Project::Table, Project::ClientId) + .to(Client::Table, Client::Id) + .to_owned(), + ).await?; + + Ok(()) + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .drop_table(Table::drop().table(Project::Table).to_owned()) + .await + } +} + +#[derive(DeriveIden)] +enum Project { + Table, + Id, + TogglId, + WorkspaceId, + ClientId, + Name, + Active, + RawJson, +} + +#[derive(DeriveIden)] +enum Client { + Table, + Id, +} diff --git a/src/entity/client.rs b/src/entity/client.rs index bad6bb5..fcb0f34 100644 --- a/src/entity/client.rs +++ b/src/entity/client.rs @@ -16,6 +16,15 @@ pub struct Model { } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] -pub enum Relation {} +pub enum Relation { + #[sea_orm(has_many = "super::project::Entity")] + Project, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Project.def() + } +} impl ActiveModelBehavior for ActiveModel {} diff --git a/src/entity/mod.rs b/src/entity/mod.rs index 67366d5..5ecbef7 100644 --- a/src/entity/mod.rs +++ b/src/entity/mod.rs @@ -3,5 +3,6 @@ pub mod prelude; pub mod client; +pub mod project; pub mod time_entry; pub mod toggl_portal_seaql_migrations; diff --git a/src/entity/prelude.rs b/src/entity/prelude.rs index c0edf82..b2dfc87 100644 --- a/src/entity/prelude.rs +++ b/src/entity/prelude.rs @@ -1,5 +1,6 @@ //! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.2 pub use super::client::Entity as Client; +pub use super::project::Entity as Project; pub use super::time_entry::Entity as TimeEntry; pub use super::toggl_portal_seaql_migrations::Entity as TogglPortalSeaqlMigrations; diff --git a/src/entity/project.rs b/src/entity/project.rs new file mode 100644 index 0000000..6861d53 --- /dev/null +++ b/src/entity/project.rs @@ -0,0 +1,37 @@ +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.2 + +use sea_orm::entity::prelude::*; +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)] +#[sea_orm(table_name = "project")] +pub struct Model { + pub id: i32, + pub toggl_id: i64, + pub workspace_id: i64, + pub client_id: Option, + pub name: String, + pub active: bool, + #[sea_orm(column_type = "JsonBinary")] + pub raw_json: Json, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm( + belongs_to = "super::client::Entity", + from = "Column::ClientId", + to = "super::client::Column::Id", + on_update = "NoAction", + on_delete = "NoAction" + )] + Client, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Client.def() + } +} + +impl ActiveModelBehavior for ActiveModel {}