diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 000000000..3d4118bfd --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +*.html.tera linguist-language=HTML diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index cd506c06f..c4a4821a3 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -137,7 +137,7 @@ jobs: toolchain: stable override: true - - uses: actions/cache@v2 + - uses: actions/cache@v3 with: path: | ~/.cargo/registry @@ -176,7 +176,7 @@ jobs: toolchain: stable override: true - - uses: actions/cache@v2 + - uses: actions/cache@v3 with: path: | ~/.cargo/registry @@ -215,7 +215,7 @@ jobs: toolchain: stable override: true - - uses: actions/cache@v2 + - uses: actions/cache@v3 with: path: | ~/.cargo/registry @@ -380,7 +380,7 @@ jobs: toolchain: stable override: true - - uses: actions/cache@v2 + - uses: actions/cache@v3 with: path: | ~/.cargo/registry @@ -449,7 +449,7 @@ jobs: toolchain: stable override: true - - uses: actions/cache@v2 + - uses: actions/cache@v3 with: path: | ~/.cargo/registry @@ -518,7 +518,7 @@ jobs: toolchain: stable override: true - - uses: actions/cache@v2 + - uses: actions/cache@v3 with: path: | ~/.cargo/registry @@ -576,7 +576,7 @@ jobs: toolchain: stable override: true - - uses: actions/cache@v2 + - uses: actions/cache@v3 with: path: | ~/.cargo/registry diff --git a/CHANGELOG.md b/CHANGELOG.md index 31793c8b7..8fdaf46cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## 0.9.0 - Pending + +* Improve sea-orm-cli logging (#735) + +## sea-orm-migration 0.8.3 + +* Removed `async-std` from dependency https://github.com/SeaQL/sea-orm/pull/758 + ## 0.8.0 - 2022-05-10 ### New Features diff --git a/COMMUNITY.md b/COMMUNITY.md index 4943ccc4e..f07c45040 100644 --- a/COMMUNITY.md +++ b/COMMUNITY.md @@ -36,3 +36,5 @@ If you have built an app using SeaORM and want to showcase it, feel free to open - [Quasar](https://github.com/Technik97/Quasar) | Rust REST API using Actix-Web, SeaOrm and Postgres with Svelte Typescript Frontend - [snmp-sim-rust](https://github.com/sonalake/snmp-sim-rust) | SNMP Simulator - [template_flow](https://github.com/hilary888/template_flow) | An experiment exploring replacing placeholders in pre-prepared templates with their actual values +- [actix-react-starter-template](https://github.com/aslamplr/actix-react-starter-template) | Actix web + SeaORM + React + Redux + Redux Saga project starter template +- [ZAPP](https://zapp.epics.dev) ([repository](https://github.com/EpicsDAO/zapp)) | ZAPP is a serverless framework made by Rust. Quickly build a scalable GraphQL API web server. diff --git a/examples/actix3_example/migration/Cargo.toml b/examples/actix3_example/migration/Cargo.toml index 6a1b3c9fb..2ba5e0a1a 100644 --- a/examples/actix3_example/migration/Cargo.toml +++ b/examples/actix3_example/migration/Cargo.toml @@ -9,8 +9,13 @@ name = "migration" path = "src/lib.rs" [dependencies] -entity = { path = "../entity" } +async-std = { version = "^1", features = ["attributes", "tokio1"] } [dependencies.sea-orm-migration] path = "../../../sea-orm-migration" # remove this line in your own project version = "^0.8.0" +features = [ + # Enable following runtime and db backend features if you want to run migration via CLI + # "runtime-async-std-native-tls", + # "sqlx-mysql", +] diff --git a/examples/actix3_example/migration/src/m20220120_000001_create_post_table.rs b/examples/actix3_example/migration/src/m20220120_000001_create_post_table.rs index 097c22670..e544701e0 100644 --- a/examples/actix3_example/migration/src/m20220120_000001_create_post_table.rs +++ b/examples/actix3_example/migration/src/m20220120_000001_create_post_table.rs @@ -1,4 +1,3 @@ -use entity::post::*; use sea_orm_migration::prelude::*; pub struct Migration; @@ -15,17 +14,17 @@ impl MigrationTrait for Migration { manager .create_table( Table::create() - .table(Entity) + .table(Post::Table) .if_not_exists() .col( - ColumnDef::new(Column::Id) + ColumnDef::new(Post::Id) .integer() .not_null() .auto_increment() .primary_key(), ) - .col(ColumnDef::new(Column::Title).string().not_null()) - .col(ColumnDef::new(Column::Text).string().not_null()) + .col(ColumnDef::new(Post::Title).string().not_null()) + .col(ColumnDef::new(Post::Text).string().not_null()) .to_owned(), ) .await @@ -33,7 +32,16 @@ impl MigrationTrait for Migration { async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { manager - .drop_table(Table::drop().table(Entity).to_owned()) + .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, + Title, + Text, +} diff --git a/examples/actix_example/migration/Cargo.toml b/examples/actix_example/migration/Cargo.toml index 6a1b3c9fb..c113ac97b 100644 --- a/examples/actix_example/migration/Cargo.toml +++ b/examples/actix_example/migration/Cargo.toml @@ -9,8 +9,13 @@ name = "migration" path = "src/lib.rs" [dependencies] -entity = { path = "../entity" } +async-std = { version = "^1", features = ["attributes", "tokio1"] } [dependencies.sea-orm-migration] path = "../../../sea-orm-migration" # remove this line in your own project version = "^0.8.0" +features = [ + # Enable following runtime and db backend features if you want to run migration via CLI + # "runtime-actix-native-tls", + # "sqlx-mysql", +] diff --git a/examples/actix_example/migration/src/m20220120_000001_create_post_table.rs b/examples/actix_example/migration/src/m20220120_000001_create_post_table.rs index 097c22670..e544701e0 100644 --- a/examples/actix_example/migration/src/m20220120_000001_create_post_table.rs +++ b/examples/actix_example/migration/src/m20220120_000001_create_post_table.rs @@ -1,4 +1,3 @@ -use entity::post::*; use sea_orm_migration::prelude::*; pub struct Migration; @@ -15,17 +14,17 @@ impl MigrationTrait for Migration { manager .create_table( Table::create() - .table(Entity) + .table(Post::Table) .if_not_exists() .col( - ColumnDef::new(Column::Id) + ColumnDef::new(Post::Id) .integer() .not_null() .auto_increment() .primary_key(), ) - .col(ColumnDef::new(Column::Title).string().not_null()) - .col(ColumnDef::new(Column::Text).string().not_null()) + .col(ColumnDef::new(Post::Title).string().not_null()) + .col(ColumnDef::new(Post::Text).string().not_null()) .to_owned(), ) .await @@ -33,7 +32,16 @@ impl MigrationTrait for Migration { async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { manager - .drop_table(Table::drop().table(Entity).to_owned()) + .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, + Title, + Text, +} diff --git a/examples/axum_example/migration/Cargo.toml b/examples/axum_example/migration/Cargo.toml index 6a1b3c9fb..d816bce11 100644 --- a/examples/axum_example/migration/Cargo.toml +++ b/examples/axum_example/migration/Cargo.toml @@ -9,8 +9,13 @@ name = "migration" path = "src/lib.rs" [dependencies] -entity = { path = "../entity" } +async-std = { version = "^1", features = ["attributes", "tokio1"] } [dependencies.sea-orm-migration] path = "../../../sea-orm-migration" # remove this line in your own project version = "^0.8.0" +features = [ + # Enable following runtime and db backend features if you want to run migration via CLI + # "runtime-tokio-native-tls", + # "sqlx-postgres", +] diff --git a/examples/axum_example/migration/src/m20220120_000001_create_post_table.rs b/examples/axum_example/migration/src/m20220120_000001_create_post_table.rs index 097c22670..e544701e0 100644 --- a/examples/axum_example/migration/src/m20220120_000001_create_post_table.rs +++ b/examples/axum_example/migration/src/m20220120_000001_create_post_table.rs @@ -1,4 +1,3 @@ -use entity::post::*; use sea_orm_migration::prelude::*; pub struct Migration; @@ -15,17 +14,17 @@ impl MigrationTrait for Migration { manager .create_table( Table::create() - .table(Entity) + .table(Post::Table) .if_not_exists() .col( - ColumnDef::new(Column::Id) + ColumnDef::new(Post::Id) .integer() .not_null() .auto_increment() .primary_key(), ) - .col(ColumnDef::new(Column::Title).string().not_null()) - .col(ColumnDef::new(Column::Text).string().not_null()) + .col(ColumnDef::new(Post::Title).string().not_null()) + .col(ColumnDef::new(Post::Text).string().not_null()) .to_owned(), ) .await @@ -33,7 +32,16 @@ impl MigrationTrait for Migration { async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { manager - .drop_table(Table::drop().table(Entity).to_owned()) + .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, + Title, + Text, +} diff --git a/examples/graphql_example/migration/Cargo.toml b/examples/graphql_example/migration/Cargo.toml index b3432a967..8101cb8bd 100644 --- a/examples/graphql_example/migration/Cargo.toml +++ b/examples/graphql_example/migration/Cargo.toml @@ -10,8 +10,13 @@ path = "src/lib.rs" [dependencies] dotenv = "0.15.0" -entity = { path = "../entity" } +async-std = { version = "^1", features = ["attributes", "tokio1"] } [dependencies.sea-orm-migration] path = "../../../sea-orm-migration" # remove this line in your own project version = "^0.8.0" +features = [ + # Enable following runtime and db backend features if you want to run migration via CLI + # "runtime-tokio-native-tls", + # "sqlx-sqlite", +] diff --git a/examples/graphql_example/migration/src/m20220101_000001_create_table.rs b/examples/graphql_example/migration/src/m20220101_000001_create_table.rs index 9a69fe7a6..631cafbfd 100644 --- a/examples/graphql_example/migration/src/m20220101_000001_create_table.rs +++ b/examples/graphql_example/migration/src/m20220101_000001_create_table.rs @@ -1,22 +1,7 @@ -use entity::note; -use sea_orm::{DbBackend, EntityTrait, Schema}; use sea_orm_migration::prelude::*; pub struct Migration; -fn get_seaorm_create_stmt(e: E) -> TableCreateStatement { - let schema = Schema::new(DbBackend::Sqlite); - - schema - .create_table_from_entity(e) - .if_not_exists() - .to_owned() -} - -fn get_seaorm_drop_stmt(e: E) -> TableDropStatement { - Table::drop().table(e).if_exists().to_owned() -} - impl MigrationName for Migration { fn name(&self) -> &str { "m20220101_000001_create_table" @@ -26,22 +11,37 @@ impl MigrationName for Migration { #[async_trait::async_trait] impl MigrationTrait for Migration { async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { - let stmts = vec![get_seaorm_create_stmt(note::Entity)]; - - for stmt in stmts { - manager.create_table(stmt.to_owned()).await?; - } - - Ok(()) + manager + .create_table( + Table::create() + .table(Notes::Table) + .if_not_exists() + .col( + ColumnDef::new(Notes::Id) + .integer() + .not_null() + .auto_increment() + .primary_key(), + ) + .col(ColumnDef::new(Notes::Title).string().not_null()) + .col(ColumnDef::new(Notes::Text).string().not_null()) + .to_owned(), + ) + .await } async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { - let stmts = vec![get_seaorm_drop_stmt(note::Entity)]; - - for stmt in stmts { - manager.drop_table(stmt.to_owned()).await?; - } - - Ok(()) + manager + .drop_table(Table::drop().table(Notes::Table).to_owned()) + .await } } + +/// Learn more at https://docs.rs/sea-query#iden +#[derive(Iden)] +enum Notes { + Table, + Id, + Title, + Text, +} diff --git a/examples/jsonrpsee_example/migration/Cargo.toml b/examples/jsonrpsee_example/migration/Cargo.toml index 6a1b3c9fb..d860e7e31 100644 --- a/examples/jsonrpsee_example/migration/Cargo.toml +++ b/examples/jsonrpsee_example/migration/Cargo.toml @@ -9,8 +9,13 @@ name = "migration" path = "src/lib.rs" [dependencies] -entity = { path = "../entity" } +async-std = { version = "^1", features = ["attributes", "tokio1"] } [dependencies.sea-orm-migration] path = "../../../sea-orm-migration" # remove this line in your own project version = "^0.8.0" +features = [ + # Enable following runtime and db backend features if you want to run migration via CLI + # "runtime-tokio-native-tls", + # "sqlx-sqlite", +] diff --git a/examples/jsonrpsee_example/migration/src/m20220120_000001_create_post_table.rs b/examples/jsonrpsee_example/migration/src/m20220120_000001_create_post_table.rs index 097c22670..e544701e0 100644 --- a/examples/jsonrpsee_example/migration/src/m20220120_000001_create_post_table.rs +++ b/examples/jsonrpsee_example/migration/src/m20220120_000001_create_post_table.rs @@ -1,4 +1,3 @@ -use entity::post::*; use sea_orm_migration::prelude::*; pub struct Migration; @@ -15,17 +14,17 @@ impl MigrationTrait for Migration { manager .create_table( Table::create() - .table(Entity) + .table(Post::Table) .if_not_exists() .col( - ColumnDef::new(Column::Id) + ColumnDef::new(Post::Id) .integer() .not_null() .auto_increment() .primary_key(), ) - .col(ColumnDef::new(Column::Title).string().not_null()) - .col(ColumnDef::new(Column::Text).string().not_null()) + .col(ColumnDef::new(Post::Title).string().not_null()) + .col(ColumnDef::new(Post::Text).string().not_null()) .to_owned(), ) .await @@ -33,7 +32,16 @@ impl MigrationTrait for Migration { async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { manager - .drop_table(Table::drop().table(Entity).to_owned()) + .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, + Title, + Text, +} diff --git a/examples/poem_example/migration/Cargo.toml b/examples/poem_example/migration/Cargo.toml index 6a1b3c9fb..d860e7e31 100644 --- a/examples/poem_example/migration/Cargo.toml +++ b/examples/poem_example/migration/Cargo.toml @@ -9,8 +9,13 @@ name = "migration" path = "src/lib.rs" [dependencies] -entity = { path = "../entity" } +async-std = { version = "^1", features = ["attributes", "tokio1"] } [dependencies.sea-orm-migration] path = "../../../sea-orm-migration" # remove this line in your own project version = "^0.8.0" +features = [ + # Enable following runtime and db backend features if you want to run migration via CLI + # "runtime-tokio-native-tls", + # "sqlx-sqlite", +] diff --git a/examples/poem_example/migration/src/m20220120_000001_create_post_table.rs b/examples/poem_example/migration/src/m20220120_000001_create_post_table.rs index 097c22670..e544701e0 100644 --- a/examples/poem_example/migration/src/m20220120_000001_create_post_table.rs +++ b/examples/poem_example/migration/src/m20220120_000001_create_post_table.rs @@ -1,4 +1,3 @@ -use entity::post::*; use sea_orm_migration::prelude::*; pub struct Migration; @@ -15,17 +14,17 @@ impl MigrationTrait for Migration { manager .create_table( Table::create() - .table(Entity) + .table(Post::Table) .if_not_exists() .col( - ColumnDef::new(Column::Id) + ColumnDef::new(Post::Id) .integer() .not_null() .auto_increment() .primary_key(), ) - .col(ColumnDef::new(Column::Title).string().not_null()) - .col(ColumnDef::new(Column::Text).string().not_null()) + .col(ColumnDef::new(Post::Title).string().not_null()) + .col(ColumnDef::new(Post::Text).string().not_null()) .to_owned(), ) .await @@ -33,7 +32,16 @@ impl MigrationTrait for Migration { async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { manager - .drop_table(Table::drop().table(Entity).to_owned()) + .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, + Title, + Text, +} diff --git a/examples/rocket_example/migration/Cargo.toml b/examples/rocket_example/migration/Cargo.toml index db8900581..757b5f299 100644 --- a/examples/rocket_example/migration/Cargo.toml +++ b/examples/rocket_example/migration/Cargo.toml @@ -9,9 +9,14 @@ name = "migration" path = "src/lib.rs" [dependencies] -entity = { path = "../entity" } rocket = { version = "0.5.0-rc.1" } +async-std = { version = "^1", features = ["attributes", "tokio1"] } [dependencies.sea-orm-migration] path = "../../../sea-orm-migration" # remove this line in your own project version = "^0.8.0" +features = [ + # Enable following runtime and db backend features if you want to run migration via CLI + # "runtime-tokio-native-tls", + # "sqlx-postgres", +] diff --git a/examples/rocket_example/migration/src/m20220120_000001_create_post_table.rs b/examples/rocket_example/migration/src/m20220120_000001_create_post_table.rs index 097c22670..e544701e0 100644 --- a/examples/rocket_example/migration/src/m20220120_000001_create_post_table.rs +++ b/examples/rocket_example/migration/src/m20220120_000001_create_post_table.rs @@ -1,4 +1,3 @@ -use entity::post::*; use sea_orm_migration::prelude::*; pub struct Migration; @@ -15,17 +14,17 @@ impl MigrationTrait for Migration { manager .create_table( Table::create() - .table(Entity) + .table(Post::Table) .if_not_exists() .col( - ColumnDef::new(Column::Id) + ColumnDef::new(Post::Id) .integer() .not_null() .auto_increment() .primary_key(), ) - .col(ColumnDef::new(Column::Title).string().not_null()) - .col(ColumnDef::new(Column::Text).string().not_null()) + .col(ColumnDef::new(Post::Title).string().not_null()) + .col(ColumnDef::new(Post::Text).string().not_null()) .to_owned(), ) .await @@ -33,7 +32,16 @@ impl MigrationTrait for Migration { async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { manager - .drop_table(Table::drop().table(Entity).to_owned()) + .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, + Title, + Text, +} diff --git a/examples/tonic_example/migration/Cargo.toml b/examples/tonic_example/migration/Cargo.toml index 6a1b3c9fb..b3b55c135 100644 --- a/examples/tonic_example/migration/Cargo.toml +++ b/examples/tonic_example/migration/Cargo.toml @@ -9,8 +9,13 @@ name = "migration" path = "src/lib.rs" [dependencies] -entity = { path = "../entity" } +async-std = { version = "^1", features = ["attributes", "tokio1"] } [dependencies.sea-orm-migration] path = "../../../sea-orm-migration" # remove this line in your own project version = "^0.8.0" +features = [ + # Enable following runtime and db backend features if you want to run migration via CLI + # "runtime-tokio-rustls", + # "sqlx-postgres", +] diff --git a/examples/tonic_example/migration/src/m20220120_000001_create_post_table.rs b/examples/tonic_example/migration/src/m20220120_000001_create_post_table.rs index 097c22670..e544701e0 100644 --- a/examples/tonic_example/migration/src/m20220120_000001_create_post_table.rs +++ b/examples/tonic_example/migration/src/m20220120_000001_create_post_table.rs @@ -1,4 +1,3 @@ -use entity::post::*; use sea_orm_migration::prelude::*; pub struct Migration; @@ -15,17 +14,17 @@ impl MigrationTrait for Migration { manager .create_table( Table::create() - .table(Entity) + .table(Post::Table) .if_not_exists() .col( - ColumnDef::new(Column::Id) + ColumnDef::new(Post::Id) .integer() .not_null() .auto_increment() .primary_key(), ) - .col(ColumnDef::new(Column::Title).string().not_null()) - .col(ColumnDef::new(Column::Text).string().not_null()) + .col(ColumnDef::new(Post::Title).string().not_null()) + .col(ColumnDef::new(Post::Text).string().not_null()) .to_owned(), ) .await @@ -33,7 +32,16 @@ impl MigrationTrait for Migration { async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { manager - .drop_table(Table::drop().table(Entity).to_owned()) + .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, + Title, + Text, +} diff --git a/sea-orm-cli/Cargo.toml b/sea-orm-cli/Cargo.toml index 1c09cc266..7f5c41c69 100644 --- a/sea-orm-cli/Cargo.toml +++ b/sea-orm-cli/Cargo.toml @@ -33,7 +33,7 @@ clap = { version = "^2.33.3" } dotenv = { version = "^0.15" } async-std = { version = "^1.9", features = [ "attributes", "tokio1" ] } sea-orm-codegen = { version = "^0.8.0", path = "../sea-orm-codegen", optional = true } -sea-schema = { version = "^0.8.0" } +sea-schema = { version = "^0.8.1" } sqlx = { version = "^0.5", default-features = false, features = [ "mysql", "postgres" ], optional = true } tracing-subscriber = { version = "0.3", features = ["env-filter"] } tracing = { version = "0.1" } diff --git a/sea-orm-cli/src/commands.rs b/sea-orm-cli/src/commands.rs index dadb3ad2f..04a3d3b0b 100644 --- a/sea-orm-cli/src/commands.rs +++ b/sea-orm-cli/src/commands.rs @@ -3,6 +3,7 @@ use clap::ArgMatches; use regex::Regex; use sea_orm_codegen::{EntityTransformer, OutputFile, WithSerde}; use std::{error::Error, fmt::Display, fs, io::Write, path::Path, process::Command, str::FromStr}; +use tracing_subscriber::{prelude::*, EnvFilter}; use url::Url; pub async fn run_generate_command(matches: &ArgMatches<'_>) -> Result<(), Box> { @@ -21,6 +22,17 @@ pub async fn run_generate_command(matches: &ArgMatches<'_>) -> Result<(), Box Result<(), DbErr> { - todo!() + // Replace the sample below with your own migration scripts + todo!(); + + manager + .create_table( + Table::create() + .table(Post::Table) + .if_not_exists() + .col( + ColumnDef::new(Post::Id) + .integer() + .not_null() + .auto_increment() + .primary_key(), + ) + .col(ColumnDef::new(Post::Title).string().not_null()) + .col(ColumnDef::new(Post::Text).string().not_null()) + .to_owned(), + ) + .await } async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { - todo!() + // Replace the sample below with your own migration scripts + todo!(); + + 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, + Title, + Text, +} diff --git a/sea-orm-codegen/Cargo.toml b/sea-orm-codegen/Cargo.toml index 5746e360f..a4db24507 100644 --- a/sea-orm-codegen/Cargo.toml +++ b/sea-orm-codegen/Cargo.toml @@ -25,6 +25,7 @@ syn = { version = "^1", default-features = false, features = [ quote = "^1" heck = "^0.3" proc-macro2 = "^1" +tracing = { version = "0.1", features = ["log"] } [dev-dependencies] pretty_assertions = { version = "^0.7" } diff --git a/sea-orm-codegen/src/entity/column.rs b/sea-orm-codegen/src/entity/column.rs index 3ba09446b..8f8c95534 100644 --- a/sea-orm-codegen/src/entity/column.rs +++ b/sea-orm-codegen/src/entity/column.rs @@ -3,6 +3,7 @@ use heck::{CamelCase, SnakeCase}; use proc_macro2::{Ident, TokenStream}; use quote::{format_ident, quote}; use sea_query::{ColumnDef, ColumnSpec, ColumnType}; +use std::fmt::Write as FmtWrite; #[derive(Clone, Debug)] pub struct Column { @@ -143,6 +144,33 @@ impl Column { } col_def } + + pub fn get_info(&self) -> String { + let mut info = String::new(); + let type_info = self.get_rs_type().to_string().replace(' ', ""); + let col_info = self.col_info(); + write!( + &mut info, + "Column `{}`: {}{}", + self.name, type_info, col_info + ) + .unwrap(); + info + } + + fn col_info(&self) -> String { + let mut info = String::new(); + if self.auto_increment { + write!(&mut info, ", auto_increment").unwrap(); + } + if self.not_null { + write!(&mut info, ", not_null").unwrap(); + } + if self.unique { + write!(&mut info, ", unique").unwrap(); + } + info + } } impl From for Column { @@ -361,6 +389,42 @@ mod tests { } } + #[test] + fn test_get_info() { + let column: Column = ColumnDef::new(Alias::new("id")).string().to_owned().into(); + assert_eq!(column.get_info().as_str(), "Column `id`: Option"); + + let column: Column = ColumnDef::new(Alias::new("id")) + .string() + .not_null() + .to_owned() + .into(); + assert_eq!(column.get_info().as_str(), "Column `id`: String, not_null"); + + let column: Column = ColumnDef::new(Alias::new("id")) + .string() + .not_null() + .unique_key() + .to_owned() + .into(); + assert_eq!( + column.get_info().as_str(), + "Column `id`: String, not_null, unique" + ); + + let column: Column = ColumnDef::new(Alias::new("id")) + .string() + .not_null() + .unique_key() + .auto_increment() + .to_owned() + .into(); + assert_eq!( + column.get_info().as_str(), + "Column `id`: String, auto_increment, not_null, unique" + ); + } + #[test] fn test_from_column_def() { let column: Column = ColumnDef::new(Alias::new("id")).string().to_owned().into(); diff --git a/sea-orm-codegen/src/entity/writer.rs b/sea-orm-codegen/src/entity/writer.rs index 88d72ef32..922f4bced 100644 --- a/sea-orm-codegen/src/entity/writer.rs +++ b/sea-orm-codegen/src/entity/writer.rs @@ -4,6 +4,7 @@ use proc_macro2::TokenStream; use quote::{format_ident, quote}; use std::{collections::HashMap, str::FromStr}; use syn::{punctuated::Punctuated, token::Comma}; +use tracing::info; #[derive(Clone, Debug)] pub struct EntityWriter { @@ -94,6 +95,18 @@ impl EntityWriter { self.entities .iter() .map(|entity| { + let entity_file = format!("{}.rs", entity.get_table_name_snake_case()); + let column_info = entity + .columns + .iter() + .map(|column| column.get_info()) + .collect::>(); + + info!("Generating {}", entity_file); + for info in column_info.iter() { + info!(" > {}", info); + } + let mut lines = Vec::new(); Self::write_doc_comment(&mut lines); let code_blocks = if expanded_format { @@ -103,7 +116,7 @@ impl EntityWriter { }; Self::write(&mut lines, code_blocks); OutputFile { - name: format!("{}.rs", entity.get_table_name_snake_case()), + name: entity_file, content: lines.join("\n\n"), } }) diff --git a/sea-orm-migration/Cargo.toml b/sea-orm-migration/Cargo.toml index aa78e502f..e986cc673 100644 --- a/sea-orm-migration/Cargo.toml +++ b/sea-orm-migration/Cargo.toml @@ -3,7 +3,7 @@ [package] name = "sea-orm-migration" -version = "0.8.2" +version = "0.8.3" authors = [ "Billy Chan " ] edition = "2021" description = "Migration utility for SeaORM" @@ -18,16 +18,18 @@ name = "sea_orm_migration" path = "src/lib.rs" [dependencies] -async-std = { version = "^1", features = ["attributes", "tokio1"] } async-trait = { version = "^0.1" } clap = { version = "^2.33" } dotenv = { version = "^0.15" } sea-orm = { version = "^0.8.0", path = "../", default-features = false, features = ["macros"] } sea-orm-cli = { version = "^0.8.1", path = "../sea-orm-cli", default-features = false } -sea-schema = { version = "^0.8.0", git = "https://github.com/SeaQL/sea-schema", branch = "bump-for-sea-orm-cursor-pagination" } +sea-schema = { version = "^0.8.1" } tracing = { version = "0.1", features = ["log"] } tracing-subscriber = { version = "0.3", features = ["env-filter"] } +[dev-dependencies] +async-std = { version = "^1", features = ["attributes", "tokio1"] } + [features] sqlx-mysql = ["sea-orm/sqlx-mysql"] sqlx-postgres = ["sea-orm/sqlx-postgres"] @@ -37,4 +39,4 @@ runtime-async-std-native-tls = [ "sea-orm/runtime-async-std-native-tls" ] runtime-tokio-native-tls = [ "sea-orm/runtime-tokio-native-tls" ] runtime-actix-rustls = [ "sea-orm/runtime-actix-rustls" ] runtime-async-std-rustls = [ "sea-orm/runtime-async-std-rustls" ] -runtime-tokio-rustls = [ "sea-orm/runtime-tokio-rustls" ] \ No newline at end of file +runtime-tokio-rustls = [ "sea-orm/runtime-tokio-rustls" ] diff --git a/sea-orm-migration/src/lib.rs b/sea-orm-migration/src/lib.rs index c9f2dc858..b5d440fee 100644 --- a/sea-orm-migration/src/lib.rs +++ b/sea-orm-migration/src/lib.rs @@ -7,7 +7,6 @@ pub mod seaql_migrations; pub use manager::*; pub use migrator::*; -pub use async_std; pub use async_trait; pub use sea_orm; pub use sea_orm::sea_query; diff --git a/sea-orm-migration/src/prelude.rs b/sea-orm-migration/src/prelude.rs index dfce2d6ab..c80476583 100644 --- a/sea-orm-migration/src/prelude.rs +++ b/sea-orm-migration/src/prelude.rs @@ -2,7 +2,6 @@ pub use super::cli; pub use super::manager::SchemaManager; pub use super::migrator::MigratorTrait; pub use super::{MigrationName, MigrationTrait}; -pub use async_std; pub use async_trait; pub use sea_orm; pub use sea_orm::sea_query; diff --git a/sea-orm-migration/tests/migrator/m20220118_000001_create_cake_table.rs b/sea-orm-migration/tests/migrator/m20220118_000001_create_cake_table.rs index bc70ba3b4..b1b3d9cf7 100644 --- a/sea-orm-migration/tests/migrator/m20220118_000001_create_cake_table.rs +++ b/sea-orm-migration/tests/migrator/m20220118_000001_create_cake_table.rs @@ -35,6 +35,7 @@ impl MigrationTrait for Migration { } } +/// Learn more at https://docs.rs/sea-query#iden #[derive(Iden)] pub enum Cake { Table, diff --git a/sea-orm-migration/tests/migrator/m20220118_000002_create_fruit_table.rs b/sea-orm-migration/tests/migrator/m20220118_000002_create_fruit_table.rs index 51c21321f..6ffd0a2c6 100644 --- a/sea-orm-migration/tests/migrator/m20220118_000002_create_fruit_table.rs +++ b/sea-orm-migration/tests/migrator/m20220118_000002_create_fruit_table.rs @@ -54,6 +54,7 @@ impl MigrationTrait for Migration { } } +/// Learn more at https://docs.rs/sea-query#iden #[derive(Iden)] pub enum Fruit { Table, diff --git a/src/database/mod.rs b/src/database/mod.rs index ea00f7426..879f7a371 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -13,6 +13,7 @@ pub use db_connection::*; #[cfg(feature = "mock")] pub use mock::*; pub use statement::*; +use std::borrow::Cow; pub use stream::*; use tracing::instrument; pub use transaction::*; @@ -24,7 +25,7 @@ use crate::DbErr; pub struct Database; /// Defines the configuration options of a database -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct ConnectOptions { /// The URI of the database pub(crate) url: String, @@ -41,6 +42,8 @@ pub struct ConnectOptions { pub(crate) max_lifetime: Option, /// Enable SQLx statement logging pub(crate) sqlx_logging: bool, + /// set sqlcipher key + pub(crate) sqlcipher_key: Option>, } impl Database { @@ -104,6 +107,7 @@ impl ConnectOptions { idle_timeout: None, max_lifetime: None, sqlx_logging: true, + sqlcipher_key: None, } } @@ -206,4 +210,13 @@ impl ConnectOptions { pub fn get_sqlx_logging(&self) -> bool { self.sqlx_logging } + + /// set key for sqlcipher + pub fn sqlcipher_key(&mut self, value: T) -> &mut Self + where + T: Into>, + { + self.sqlcipher_key = Some(value.into()); + self + } } diff --git a/src/driver/sqlx_sqlite.rs b/src/driver/sqlx_sqlite.rs index 9940b7fac..ae3edad24 100644 --- a/src/driver/sqlx_sqlite.rs +++ b/src/driver/sqlx_sqlite.rs @@ -47,6 +47,9 @@ impl SqlxSqliteConnector { .url .parse::() .map_err(|e| DbErr::Conn(e.to_string()))?; + if options.sqlcipher_key.is_some() { + opt = opt.pragma("key", options.sqlcipher_key.clone().unwrap()); + } if !options.sqlx_logging { use sqlx::ConnectOptions; opt.disable_statement_logging(); diff --git a/tests/common/features/json_vec.rs b/tests/common/features/json_vec.rs new file mode 100644 index 000000000..7b347c7b3 --- /dev/null +++ b/tests/common/features/json_vec.rs @@ -0,0 +1,51 @@ +use sea_orm::entity::prelude::*; +use sea_orm::{TryGetError, TryGetable}; +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel)] +#[sea_orm(table_name = "json_vec")] +pub struct Model { + #[sea_orm(primary_key)] + pub id: i32, + pub str_vec: StringVec, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation {} + +impl ActiveModelBehavior for ActiveModel {} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct StringVec(pub Vec); + +impl From for Value { + fn from(source: StringVec) -> Self { + Value::String(serde_json::to_string(&source).ok().map(|s| Box::new(s))) + } +} + +impl TryGetable for StringVec { + fn try_get(res: &QueryResult, pre: &str, col: &str) -> Result { + let json_str: String = res.try_get(pre, col).map_err(TryGetError::DbErr)?; + serde_json::from_str(&json_str).map_err(|e| TryGetError::DbErr(DbErr::Json(e.to_string()))) + } +} + +impl sea_query::ValueType for StringVec { + fn try_from(v: Value) -> Result { + match v { + Value::String(Some(x)) => Ok(StringVec( + serde_json::from_str(&x).map_err(|_| sea_query::ValueTypeErr)?, + )), + _ => Err(sea_query::ValueTypeErr), + } + } + + fn type_name() -> String { + stringify!(StringVec).to_owned() + } + + fn column_type() -> sea_query::ColumnType { + sea_query::ColumnType::String(None) + } +} diff --git a/tests/common/features/mod.rs b/tests/common/features/mod.rs index 4dad7956a..fc9ac047e 100644 --- a/tests/common/features/mod.rs +++ b/tests/common/features/mod.rs @@ -3,6 +3,7 @@ pub mod active_enum_child; pub mod applog; pub mod byte_primary_key; pub mod insert_default; +pub mod json_vec; pub mod metadata; pub mod repository; pub mod satellite; @@ -16,6 +17,7 @@ pub use active_enum_child::Entity as ActiveEnumChild; pub use applog::Entity as Applog; pub use byte_primary_key::Entity as BytePrimaryKey; pub use insert_default::Entity as InsertDefault; +pub use json_vec::Entity as JsonVec; pub use metadata::Entity as Metadata; pub use repository::Entity as Repository; pub use satellite::Entity as Satellite; diff --git a/tests/common/features/schema.rs b/tests/common/features/schema.rs index 68f2f2812..10fd038c3 100644 --- a/tests/common/features/schema.rs +++ b/tests/common/features/schema.rs @@ -18,6 +18,7 @@ pub async fn create_tables(db: &DatabaseConnection) -> Result<(), DbErr> { create_byte_primary_key_table(db).await?; create_satellites_table(db).await?; create_transaction_log_table(db).await?; + create_json_vec_table(db).await?; let create_enum_stmts = match db_backend { DbBackend::MySql | DbBackend::Sqlite => Vec::new(), @@ -286,3 +287,23 @@ pub async fn create_insert_default_table(db: &DbConn) -> Result Result { + let create_table_stmt = sea_query::Table::create() + .table(json_vec::Entity.table_ref()) + .col( + ColumnDef::new(json_vec::Column::Id) + .integer() + .not_null() + .auto_increment() + .primary_key(), + ) + .col( + ColumnDef::new(json_vec::Column::StrVec) + .string() + .not_null(), + ) + .to_owned(); + + create_table(db, &create_table_stmt, JsonVec).await +} diff --git a/tests/json_vec_tests.rs b/tests/json_vec_tests.rs new file mode 100644 index 000000000..de8c819bc --- /dev/null +++ b/tests/json_vec_tests.rs @@ -0,0 +1,47 @@ +pub mod common; + +pub use common::{features::*, setup::*, TestContext}; +use pretty_assertions::assert_eq; +use sea_orm::{entity::prelude::*, entity::*, DatabaseConnection}; + +#[sea_orm_macros::test] +#[cfg(any( + feature = "sqlx-mysql", + feature = "sqlx-sqlite", + feature = "sqlx-postgres" +))] +async fn main() -> Result<(), DbErr> { + let ctx = TestContext::new("json_vec_tests").await; + create_tables(&ctx.db).await?; + insert_json_vec(&ctx.db).await?; + ctx.delete().await; + + Ok(()) +} + +pub async fn insert_json_vec(db: &DatabaseConnection) -> Result<(), DbErr> { + let json_vec = json_vec::Model { + id: 1, + str_vec: json_vec::StringVec(vec![ + "1".to_string(), + "2".to_string(), + "3".to_string(), + ]) + }; + + let result = json_vec.clone().into_active_model().insert(db).await?; + + assert_eq!(result, json_vec); + + let model = json_vec::Entity::find() + .filter(json_vec::Column::Id.eq(json_vec.id)) + .one(db) + .await?; + + assert_eq!( + model, + Some(json_vec) + ); + + Ok(()) +}