(toggl-portal): Add projects to database

This commit is contained in:
Joshua Coles 2023-11-06 20:03:26 +00:00
parent 84cb5f5379
commit 8e9f8538de
6 changed files with 115 additions and 1 deletions

View File

@ -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),
]
}

View File

@ -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,
}

View File

@ -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<super::project::Entity> for Entity {
fn to() -> RelationDef {
Relation::Project.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

View File

@ -3,5 +3,6 @@
pub mod prelude;
pub mod client;
pub mod project;
pub mod time_entry;
pub mod toggl_portal_seaql_migrations;

View File

@ -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;

37
src/entity/project.rs Normal file
View File

@ -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<i32>,
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<super::client::Entity> for Entity {
fn to() -> RelationDef {
Relation::Client.def()
}
}
impl ActiveModelBehavior for ActiveModel {}