Skip to content

Commit 393cb0e

Browse files
committed
wtf is going on
1 parent 16b5db8 commit 393cb0e

File tree

20 files changed

+110
-82
lines changed

20 files changed

+110
-82
lines changed

attr/src/derive.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,6 @@ pub enum Privacy {
221221
panic!()
222222
};
223223
let attr = DeriveParser::from_attributes(&item.attrs);
224-
assert_eq!(attr.has_derive("ormlite", "ManualType"), true);
224+
assert!(attr.has_derive("ormlite", "ManualType"));
225225
}
226226
}

attr/src/ident.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use quote::TokenStreamExt;
55
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
66
pub struct Ident(String);
77

8-
impl Ident {
9-
pub fn as_ref(&self) -> &String {
8+
impl AsRef<String> for Ident {
9+
fn as_ref(&self) -> &String {
1010
&self.0
1111
}
1212
}

attr/src/metadata/column.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ impl ColumnMeta {
4242
}
4343

4444
pub fn from_fields<'a>(fields: impl Iterator<Item = &'a Field>) -> Vec<Self> {
45-
fields.map(|f| ColumnMeta::from_field(f)).collect()
45+
fn fun_name(f: &Field) -> ColumnMeta {
46+
ColumnMeta::from_field(f)
47+
}
48+
fields.map(fun_name).collect()
4649
}
4750

4851
pub fn from_syn(ident: &syn::Ident, ty: &syn::Type) -> Self {
@@ -268,8 +271,8 @@ pub name: String
268271
let column = ColumnMeta::from_field(field);
269272
assert_eq!(column.name, "name");
270273
assert_eq!(column.ty, "String");
271-
assert_eq!(column.marked_primary_key, false);
272-
assert_eq!(column.has_database_default, false);
274+
assert!(!column.marked_primary_key);
275+
assert!(!column.has_database_default);
273276
assert_eq!(column.rust_default, Some("\"foo\".to_string()".to_string()));
274277
assert_eq!(column.ident, "name");
275278
}

attr/src/metadata/model.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ impl ModelMeta {
2828
pub fn from_derive(ast: &DeriveInput) -> Self {
2929
let attrs = TableAttr::from_attrs(&ast.attrs);
3030
let table = TableMeta::new(ast, &attrs);
31-
let pkey = table.pkey.as_deref().expect(&format!(
32-
"No column marked with #[ormlite(primary_key)], and no column named id, uuid, {0}_id, or {0}_uuid",
33-
table.name,
34-
));
31+
let pkey = table.pkey.as_deref().unwrap_or_else(|| {
32+
panic!(
33+
"No column marked with #[ormlite(primary_key)], and no column named id, uuid, {0}_id, or {0}_uuid",
34+
table.name
35+
)
36+
});
3537
let mut insert_struct = None;
3638
let mut extra_derives: Option<Vec<syn::Ident>> = None;
3739
for attr in attrs {
@@ -48,13 +50,18 @@ impl ModelMeta {
4850
}
4951
}
5052
let pkey = table.columns.iter().find(|&c| c.name == pkey).unwrap().clone();
51-
let insert_struct = insert_struct.map(|v| Ident::from(v));
52-
let extra_derives = extra_derives.take().map(|vec| vec.into_iter().map(|v| v.to_string()).map(Ident::from).collect());
53-
53+
fn fun_name(v: String) -> Ident {
54+
Ident::from(v)
55+
}
56+
let insert_struct = insert_struct.map(fun_name);
57+
let extra_derives = extra_derives
58+
.take()
59+
.map(|vec| vec.into_iter().map(|v| v.to_string()).map(Ident::from).collect());
60+
5461
Self {
5562
table,
5663
insert_struct,
57-
extra_derives,
64+
extra_derives,
5865
pkey,
5966
}
6067
}

attr/src/metadata/table.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl TableMeta {
3131
let mut pkey = columns
3232
.iter()
3333
.find(|&c| c.marked_primary_key)
34-
.map(|c| c.clone())
34+
.cloned()
3535
.map(|c| c.name.clone());
3636
if pkey.is_none() {
3737
let candidates = sqlmo::util::pkey_column_names(&name);

cli/src/command/down.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ use std::path::Path;
77

88
use crate::command::{get_executed_migrations, get_pending_migrations, MigrationType};
99
use crate::util::{create_runtime, CommandSuccess};
10+
use anyhow::anyhow;
1011
use ormlite::postgres::{PgArguments, PgConnection};
1112
use ormlite::Arguments;
1213
use ormlite::{Acquire, Connection, Executor};
1314
use ormlite_core::config::{get_var_database_url, get_var_migration_folder, get_var_snapshot_folder};
1415
use url::Url;
15-
use anyhow::anyhow;
1616

1717
#[derive(Parser, Debug)]
1818
pub struct Down {
@@ -68,7 +68,7 @@ impl Down {
6868
let target = if let Some(target) = self.target {
6969
target
7070
} else if executed.len() > 1 {
71-
executed.iter().nth(1).unwrap().name.clone()
71+
executed.get(1).unwrap().name.clone()
7272
} else if executed.len() == 1 {
7373
"0_empty".to_string()
7474
} else {

cli/src/command/migrate.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,9 @@ fn check_for_pending_migrations(
172172

173173
fn check_reversible_compatibility(reversible: bool, migration_environment: Option<MigrationType>) -> Result<()> {
174174
if let Some(migration_environment) = migration_environment {
175-
if reversible && migration_environment == MigrationType::Simple {
176-
return Err(anyhow!("You cannot mix reversible and non-reversible migrations"));
177-
} else if !reversible && migration_environment != MigrationType::Simple {
175+
if (reversible && migration_environment == MigrationType::Simple)
176+
|| (!reversible && migration_environment != MigrationType::Simple)
177+
{
178178
return Err(anyhow!("You cannot mix reversible and non-reversible migrations"));
179179
}
180180
}
@@ -209,7 +209,7 @@ fn autogenerate_migration(
209209
let mut current = runtime.block_on(Schema::try_from_postgres(conn, "public"))?;
210210
current.tables.retain(|t| t.name != "_sqlx_migrations");
211211

212-
let mut desired = schema_from_ormlite_project(codebase_path, &c)?;
212+
let mut desired = schema_from_ormlite_project(codebase_path, c)?;
213213
experimental_modifications_to_schema(&mut desired)?;
214214

215215
let migration = current.migrate_to(

cli/src/schema.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use std::collections::HashMap;
2-
use std::path::Path;
3-
use sqlmo::{Constraint, Schema, Table};
1+
use crate::config::Config;
2+
use anyhow::Result as AnyResult;
43
use ormlite_attr::{schema_from_filepaths, Ident, InnerType, Type};
54
use ormlite_core::schema::FromMeta;
6-
use anyhow::Result as AnyResult;
7-
use crate::config::Config;
5+
use sqlmo::{Constraint, Schema, Table};
6+
use std::collections::HashMap;
7+
use std::path::Path;
88

99
pub fn schema_from_ormlite_project(paths: &[&Path], c: &Config) -> AnyResult<Schema> {
1010
let mut schema = Schema::default();
@@ -29,7 +29,7 @@ pub fn schema_from_ormlite_project(paths: &[&Path], c: &Config) -> AnyResult<Sch
2929
let model_name = c.ty.inner_type_name();
3030
let pkey = primary_key_type
3131
.get(&model_name)
32-
.expect(&format!("Could not find model {} for join", model_name));
32+
.unwrap_or_else(|| panic!("Could not find model {} for join", model_name));
3333
c.ty = Type::Inner(pkey.clone());
3434
}
3535
}
@@ -38,8 +38,11 @@ pub fn schema_from_ormlite_project(paths: &[&Path], c: &Config) -> AnyResult<Sch
3838
let table = Table::from_meta(&table);
3939
schema.tables.push(table);
4040
}
41-
let mut table_names: HashMap<String, (String, String)> =
42-
schema.tables.iter().map(|t| (t.name.clone(), (t.name.clone(), t.primary_key().unwrap().name.clone()))).collect();
41+
let mut table_names: HashMap<String, (String, String)> = schema
42+
.tables
43+
.iter()
44+
.map(|t| (t.name.clone(), (t.name.clone(), t.primary_key().unwrap().name.clone())))
45+
.collect();
4346
for (alias, real) in &c.table.aliases {
4447
let Some(real) = table_names.get(real) else {
4548
continue;
@@ -63,4 +66,5 @@ pub fn schema_from_ormlite_project(paths: &[&Path], c: &Config) -> AnyResult<Sch
6366
}
6467
}
6568
Ok(schema)
66-
}
69+
}
70+

core/src/query_builder/args.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use core::default::Default;
22
use sqlx::{Arguments, Database, IntoArguments};
33

4-
pub struct QueryBuilderArgs<'q, DB: Database>(pub Box<DB::Arguments<'q>>, usize);
4+
pub struct QueryBuilderArgs<'q, DB: Database>(pub Box<DB::Arguments<'q>>, pub usize);
55

66
impl<'q, DB: Database> QueryBuilderArgs<'q, DB> {
77
pub fn add<T: 'q + Send + sqlx::Encode<'q, DB> + sqlx::Type<DB>>(&mut self, arg: T) {
@@ -12,6 +12,10 @@ impl<'q, DB: Database> QueryBuilderArgs<'q, DB> {
1212
pub fn len(&self) -> usize {
1313
self.1
1414
}
15+
16+
pub fn is_empty(&self) -> bool {
17+
self.1 == 0
18+
}
1519
}
1620

1721
impl<'q, DB: Database> IntoArguments<'q, DB> for QueryBuilderArgs<'q, DB> {

core/src/query_builder/select.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,10 @@ where
212212
let q = self.query.to_sql(DB::dialect());
213213
let args = self.arguments;
214214
let (q, placeholder_count) = util::replace_placeholders(&q, &mut self.gen)?;
215-
if placeholder_count != args.len() {
215+
if placeholder_count != {
216+
let this = &args;
217+
this.1
218+
} {
216219
return Err(Error::OrmliteError(format!(
217220
"Failing to build query. {} placeholders were found in the query, but \
218221
{} arguments were provided.",

0 commit comments

Comments
 (0)