Skip to content

Commit

Permalink
Merge branch 'SeaQL:master' into okapi-support
Browse files Browse the repository at this point in the history
  • Loading branch information
eum602 authored Sep 25, 2022
2 parents 4eb15f5 + f3b7feb commit 4fdd4d5
Show file tree
Hide file tree
Showing 58 changed files with 916 additions and 141 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## 0.10.0 - Pending

### New Features
* Support `distinct` & `distinct_on` expression https://github.com/SeaQL/sea-orm/pull/902
* Generate entity files as a library or module https://github.com/SeaQL/sea-orm/pull/953
* Generate a new migration template with name prefix of unix timestamp https://github.com/SeaQL/sea-orm/pull/947

### Breaking changes

* Replaced `usize` with `u64` in `PaginatorTrait` https://github.com/SeaQL/sea-orm/pull/789
Expand All @@ -16,6 +21,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
* `fn column()` also handle enum type https://github.com/SeaQL/sea-orm/pull/973
* Generate migration in modules https://github.com/SeaQL/sea-orm/pull/933
* Added `acquire_timeout` on `ConnectOptions` https://github.com/SeaQL/sea-orm/pull/897
* Generate `DeriveRelation` on empty `Relation` enum https://github.com/SeaQL/sea-orm/pull/1019
* `migrate fresh` command will drop all PostgreSQL types https://github.com/SeaQL/sea-orm/pull/864, https://github.com/SeaQL/sea-orm/pull/991
* Generate entity derive `Eq` if possible https://github.com/SeaQL/sea-orm/pull/988


## 0.9.2 - 2022-08-20

Expand Down
8 changes: 1 addition & 7 deletions examples/axum_example/entity/src/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@ pub struct Model {
pub text: String,
}

#[derive(Copy, Clone, Debug, EnumIter)]
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl RelationTrait for Relation {
fn def(&self) -> RelationDef {
panic!("No RelationDef")
}
}

impl ActiveModelBehavior for ActiveModel {}
8 changes: 1 addition & 7 deletions examples/graphql_example/entity/src/note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,9 @@ pub struct Model {
pub text: String,
}

#[derive(Copy, Clone, Debug, EnumIter)]
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl RelationTrait for Relation {
fn def(&self) -> RelationDef {
panic!("No RelationDef")
}
}

impl ActiveModelBehavior for ActiveModel {}

impl Entity {
Expand Down
8 changes: 1 addition & 7 deletions issues/471/src/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@ pub struct Model {
pub text: String,
}

#[derive(Copy, Clone, Debug, EnumIter)]
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl RelationTrait for Relation {
fn def(&self) -> RelationDef {
panic!("No RelationDef")
}
}

impl ActiveModelBehavior for ActiveModel {}
8 changes: 1 addition & 7 deletions issues/630/src/entity/underscores.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,9 @@ pub struct Model {
pub aa_b_c_d: i32,
}

#[derive(Copy, Clone, Debug, EnumIter)]
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl RelationTrait for Relation {
fn def(&self) -> RelationDef {
panic!("No RelationDef")
}
}

impl ActiveModelBehavior for ActiveModel {}

#[cfg(test)]
Expand Down
8 changes: 1 addition & 7 deletions issues/630/src/entity/underscores_workaround.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,9 @@ pub struct Model {
pub aa_b_c_d: i32,
}

#[derive(Copy, Clone, Debug, EnumIter)]
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl RelationTrait for Relation {
fn def(&self) -> RelationDef {
panic!("No RelationDef")
}
}

impl ActiveModelBehavior for ActiveModel {}

#[cfg(test)]
Expand Down
17 changes: 17 additions & 0 deletions sea-orm-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ pub enum MigrateSubcommands {
help = "Name of the new migration"
)]
migration_name: String,

#[clap(
action,
short,
long,
help = "Generate migration file based on Utc time instead of Local time"
)]
universal_time: bool,
},
#[clap(about = "Drop all tables from the database, then reapply all migrations")]
Fresh,
Expand Down Expand Up @@ -191,6 +199,15 @@ pub enum GenerateSubcommands {
help = "The datetime crate to use for generating entities."
)]
date_time_crate: DateTimeCrate,

#[clap(
action,
long,
short = 'l',
default_value = "false",
help = "Generate index file as `lib.rs` instead of `mod.rs`."
)]
lib: bool,
},
}

Expand Down
2 changes: 2 additions & 0 deletions sea-orm-cli/src/commands/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub async fn run_generate_command(
with_serde,
with_copy_enums,
date_time_crate,
lib,
} => {
if verbose {
let _ = tracing_subscriber::fmt()
Expand Down Expand Up @@ -171,6 +172,7 @@ pub async fn run_generate_command(
with_copy_enums,
date_time_crate.into(),
schema_name,
lib,
);
let output = EntityTransformer::transform(table_stmts)?.generate(&writer_context);

Expand Down
19 changes: 13 additions & 6 deletions sea-orm-cli/src/commands/migrate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use chrono::Local;
use chrono::{Local, Utc};
use regex::Regex;
use std::{
error::Error,
Expand All @@ -17,9 +17,10 @@ pub fn run_migrate_command(
) -> Result<(), Box<dyn Error>> {
match command {
Some(MigrateSubcommands::Init) => run_migrate_init(migration_dir)?,
Some(MigrateSubcommands::Generate { migration_name }) => {
run_migrate_generate(migration_dir, &migration_name)?
}
Some(MigrateSubcommands::Generate {
migration_name,
universal_time,
}) => run_migrate_generate(migration_dir, &migration_name, universal_time)?,
_ => {
let (subcommand, migration_dir, steps, verbose) = match command {
Some(MigrateSubcommands::Fresh) => ("fresh", migration_dir, None, verbose),
Expand Down Expand Up @@ -110,12 +111,18 @@ pub fn run_migrate_init(migration_dir: &str) -> Result<(), Box<dyn Error>> {
pub fn run_migrate_generate(
migration_dir: &str,
migration_name: &str,
universal_time: bool,
) -> Result<(), Box<dyn Error>> {
println!("Generating new migration...");

// build new migration filename
let now = Local::now();
let migration_name = format!("m{}_{}", now.format("%Y%m%d_%H%M%S"), migration_name);
const FMT: &str = "%Y%m%d_%H%M%S";
let formatted_now = if universal_time {
Utc::now().format(FMT)
} else {
Local::now().format(FMT)
};
let migration_name = format!("m{}_{}", formatted_now, migration_name);

create_new_migration(&migration_name, migration_dir)?;
update_migrator(&migration_name, migration_dir)?;
Expand Down
5 changes: 3 additions & 2 deletions sea-orm-codegen/src/entity/active_enum.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::WithSerde;
use heck::CamelCase;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};

use crate::WithSerde;

#[derive(Clone, Debug)]
pub struct ActiveEnum {
pub(crate) enum_name: String,
Expand Down Expand Up @@ -30,7 +31,7 @@ impl ActiveEnum {
};

quote! {
#[derive(Debug, Clone, PartialEq, EnumIter, DeriveActiveEnum #copy_derive #extra_derive)]
#[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum #copy_derive #extra_derive)]
#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = #enum_name)]
pub enum #enum_iden {
#(
Expand Down
32 changes: 29 additions & 3 deletions sea-orm-codegen/src/entity/base_entity.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::{Column, ConjunctRelation, DateTimeCrate, PrimaryKey, Relation};
use heck::{CamelCase, SnakeCase};
use proc_macro2::{Ident, TokenStream};
use quote::format_ident;
use quote::quote;
use sea_query::ColumnType;

use crate::{Column, ConjunctRelation, DateTimeCrate, PrimaryKey, Relation};

#[derive(Clone, Debug)]
pub struct Entity {
Expand Down Expand Up @@ -145,14 +148,29 @@ impl Entity {
.map(|con_rel| con_rel.get_to_camel_case())
.collect()
}

pub fn get_eq_needed(&self) -> TokenStream {
self.columns
.iter()
.find(|column| {
matches!(
column.col_type,
ColumnType::Float(_) | ColumnType::Double(_)
)
})
// check if float or double exist.
// if exist, return nothing
.map_or(quote! {, Eq}, |_| quote! {})
}
}

#[cfg(test)]
mod tests {
use crate::{Column, DateTimeCrate, Entity, PrimaryKey, Relation, RelationType};
use quote::format_ident;
use quote::{format_ident, quote};
use sea_query::{ColumnType, ForeignKeyAction};

use crate::{Column, DateTimeCrate, Entity, PrimaryKey, Relation, RelationType};

fn setup() -> Entity {
Entity {
table_name: "special_cake".to_owned(),
Expand Down Expand Up @@ -416,4 +434,12 @@ mod tests {
assert_eq!(elem, entity.conjunct_relations[i].get_to_camel_case());
}
}

#[test]
fn test_get_eq_needed() {
let entity = setup();
let expected = quote! {, Eq};

assert_eq!(entity.get_eq_needed().to_string(), expected.to_string());
}
}
Loading

0 comments on commit 4fdd4d5

Please sign in to comment.