Initial work re-implementing model for TimeEntries
This commit is contained in:
parent
31b322d643
commit
8f39bf7d23
1
.env
Normal file
1
.env
Normal file
@ -0,0 +1 @@
|
|||||||
|
DATABASE_URL=postgres://postgres@localhost:5432/toggl_portal
|
||||||
10
Cargo.toml
10
Cargo.toml
@ -18,3 +18,13 @@ hyper = "0.14.27"
|
|||||||
reqwest = { version = "0.11.20", features = ["rustls", "json"] }
|
reqwest = { version = "0.11.20", features = ["rustls", "json"] }
|
||||||
base64 = "0.21.4"
|
base64 = "0.21.4"
|
||||||
serde_with = "3.3.0"
|
serde_with = "3.3.0"
|
||||||
|
|
||||||
|
sea-orm = { version = "0.12", features = [
|
||||||
|
"sqlx-postgres",
|
||||||
|
"runtime-tokio-rustls",
|
||||||
|
"macros",
|
||||||
|
"with-chrono",
|
||||||
|
"with-json",
|
||||||
|
] }
|
||||||
|
|
||||||
|
migration = { path = "./migration" }
|
||||||
|
|||||||
13
justfile
Executable file
13
justfile
Executable file
@ -0,0 +1,13 @@
|
|||||||
|
#!/usr/bin/env just --justfile
|
||||||
|
|
||||||
|
migrate:
|
||||||
|
sea-orm-cli migrate up
|
||||||
|
|
||||||
|
generate-entity:
|
||||||
|
sea-orm-cli generate entity --with-serde both -o src/entity
|
||||||
|
|
||||||
|
release:
|
||||||
|
cargo build --release
|
||||||
|
|
||||||
|
lint:
|
||||||
|
cargo clippy
|
||||||
19
migration/Cargo.toml
Normal file
19
migration/Cargo.toml
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
[package]
|
||||||
|
name = "migration"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
publish = false
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
name = "migration"
|
||||||
|
path = "src/lib.rs"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
async-std = { version = "1", features = ["attributes", "tokio1"] }
|
||||||
|
|
||||||
|
[dependencies.sea-orm-migration]
|
||||||
|
version = "0.12.0"
|
||||||
|
features = [
|
||||||
|
"runtime-tokio-rustls", # `ASYNC_RUNTIME` feature
|
||||||
|
"sqlx-postgres", # `DATABASE_DRIVER` feature
|
||||||
|
]
|
||||||
41
migration/README.md
Normal file
41
migration/README.md
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
# Running Migrator CLI
|
||||||
|
|
||||||
|
- Generate a new migration file
|
||||||
|
```sh
|
||||||
|
cargo run -- generate MIGRATION_NAME
|
||||||
|
```
|
||||||
|
- Apply all pending migrations
|
||||||
|
```sh
|
||||||
|
cargo run
|
||||||
|
```
|
||||||
|
```sh
|
||||||
|
cargo run -- up
|
||||||
|
```
|
||||||
|
- Apply first 10 pending migrations
|
||||||
|
```sh
|
||||||
|
cargo run -- up -n 10
|
||||||
|
```
|
||||||
|
- Rollback last applied migrations
|
||||||
|
```sh
|
||||||
|
cargo run -- down
|
||||||
|
```
|
||||||
|
- Rollback last 10 applied migrations
|
||||||
|
```sh
|
||||||
|
cargo run -- down -n 10
|
||||||
|
```
|
||||||
|
- Drop all tables from the database, then reapply all migrations
|
||||||
|
```sh
|
||||||
|
cargo run -- fresh
|
||||||
|
```
|
||||||
|
- Rollback all applied migrations, then reapply all migrations
|
||||||
|
```sh
|
||||||
|
cargo run -- refresh
|
||||||
|
```
|
||||||
|
- Rollback all applied migrations
|
||||||
|
```sh
|
||||||
|
cargo run -- reset
|
||||||
|
```
|
||||||
|
- Check the status of all migrations
|
||||||
|
```sh
|
||||||
|
cargo run -- status
|
||||||
|
```
|
||||||
14
migration/src/lib.rs
Normal file
14
migration/src/lib.rs
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
pub use sea_orm_migration::prelude::*;
|
||||||
|
|
||||||
|
mod m20231101_172500_create_time_entry_table;
|
||||||
|
|
||||||
|
pub struct Migrator;
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl MigratorTrait for Migrator {
|
||||||
|
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
|
||||||
|
vec![
|
||||||
|
Box::new(m20231101_172500_create_time_entry_table::Migration),
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
47
migration/src/m20231101_172500_create_time_entry_table.rs
Normal file
47
migration/src/m20231101_172500_create_time_entry_table.rs
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
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(TimeEntry::Table)
|
||||||
|
.if_not_exists()
|
||||||
|
.col(
|
||||||
|
ColumnDef::new(TimeEntry::Id)
|
||||||
|
.big_integer()
|
||||||
|
.not_null()
|
||||||
|
.auto_increment()
|
||||||
|
.primary_key(),
|
||||||
|
)
|
||||||
|
.col(ColumnDef::new(TimeEntry::Description).string().not_null())
|
||||||
|
.col(ColumnDef::new(TimeEntry::ProjectId).big_integer())
|
||||||
|
.col(ColumnDef::new(TimeEntry::Start).timestamp().not_null())
|
||||||
|
.col(ColumnDef::new(TimeEntry::Stop).timestamp().not_null())
|
||||||
|
.col(ColumnDef::new(TimeEntry::RawJson).json_binary().not_null())
|
||||||
|
.to_owned(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||||
|
manager
|
||||||
|
.drop_table(Table::drop().table(TimeEntry::Table).to_owned())
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(DeriveIden)]
|
||||||
|
enum TimeEntry {
|
||||||
|
Table,
|
||||||
|
Id,
|
||||||
|
Description,
|
||||||
|
ProjectId,
|
||||||
|
Start,
|
||||||
|
Stop,
|
||||||
|
RawJson
|
||||||
|
}
|
||||||
6
migration/src/main.rs
Normal file
6
migration/src/main.rs
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
use sea_orm_migration::prelude::*;
|
||||||
|
|
||||||
|
#[async_std::main]
|
||||||
|
async fn main() {
|
||||||
|
cli::run_cli(migration::Migrator).await;
|
||||||
|
}
|
||||||
5
src/entity/mod.rs
Normal file
5
src/entity/mod.rs
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.2
|
||||||
|
|
||||||
|
pub mod prelude;
|
||||||
|
|
||||||
|
pub mod time_entry;
|
||||||
3
src/entity/prelude.rs
Normal file
3
src/entity/prelude.rs
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.2
|
||||||
|
|
||||||
|
pub use super::time_entry::Entity as TimeEntry;
|
||||||
22
src/entity/time_entry.rs
Normal file
22
src/entity/time_entry.rs
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
//! `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 = "time_entry")]
|
||||||
|
pub struct Model {
|
||||||
|
#[sea_orm(primary_key)]
|
||||||
|
pub id: i64,
|
||||||
|
pub description: String,
|
||||||
|
pub project_id: Option<i64>,
|
||||||
|
pub start: DateTime,
|
||||||
|
pub stop: DateTime,
|
||||||
|
#[sea_orm(column_type = "JsonBinary")]
|
||||||
|
pub raw_json: Json,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||||
|
pub enum Relation {}
|
||||||
|
|
||||||
|
impl ActiveModelBehavior for ActiveModel {}
|
||||||
13
src/main.rs
13
src/main.rs
@ -13,6 +13,7 @@ use serde_json::Value;
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use tower_http::trace::TraceLayer;
|
use tower_http::trace::TraceLayer;
|
||||||
|
use migration::{Migrator, MigratorTrait};
|
||||||
|
|
||||||
mod client;
|
mod client;
|
||||||
mod types;
|
mod types;
|
||||||
@ -27,6 +28,9 @@ struct Config {
|
|||||||
|
|
||||||
#[arg(long = "addr", short, env, default_value = "0.0.0.0:3000")]
|
#[arg(long = "addr", short, env, default_value = "0.0.0.0:3000")]
|
||||||
address: SocketAddr,
|
address: SocketAddr,
|
||||||
|
|
||||||
|
#[arg(long = "db", short, env)]
|
||||||
|
database_url: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn report(
|
pub async fn report(
|
||||||
@ -70,6 +74,14 @@ async fn main() -> Result<()> {
|
|||||||
&STANDARD.encode(&format!("{}:api_token", config.toggl_api_token)),
|
&STANDARD.encode(&format!("{}:api_token", config.toggl_api_token)),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let db = sea_orm::Database::connect(config.database_url)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
Migrator::up(&db, None)
|
||||||
|
.await
|
||||||
|
.expect("Failed to migrate");
|
||||||
|
|
||||||
// build our application with a route
|
// build our application with a route
|
||||||
let app = Router::new()
|
let app = Router::new()
|
||||||
.route("/health", get(health))
|
.route("/health", get(health))
|
||||||
@ -77,6 +89,7 @@ async fn main() -> Result<()> {
|
|||||||
.route("/report", post(report))
|
.route("/report", post(report))
|
||||||
.route("/start_time_entry", post(start_time_entry))
|
.route("/start_time_entry", post(start_time_entry))
|
||||||
.layer(Extension(toggl_client))
|
.layer(Extension(toggl_client))
|
||||||
|
.layer(Extension(db))
|
||||||
.layer(TraceLayer::new_for_http());
|
.layer(TraceLayer::new_for_http());
|
||||||
|
|
||||||
tracing::info!("Listening on {}", config.address);
|
tracing::info!("Listening on {}", config.address);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user