v0.3.8: strip select-field type aliases in derive; new_uuid -> Uuid::now_v7

- sqlx-record-derive: select_fields() may emit annotated forms like
  `id as "id: Uuid"` for custom-typed columns; strip the alias before
  building the order_by / select-field validation sets so callers can pass
  bare column names (fixes field-filtering for entities with custom types).
- new_uuid(): use Uuid::now_v7() instead of hand-rolled timestamp+random.

Tagged for downstream git consumption (st-peter pins this).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Michael Netshipise 2026-06-05 11:08:52 +02:00
parent acf7321c1b
commit b9de877ba1
3 changed files with 19 additions and 7 deletions

View File

@ -5,7 +5,7 @@ edition.workspace = true
description = "Entity CRUD and change tracking for SQL databases with SQLx" description = "Entity CRUD and change tracking for SQL databases with SQLx"
[workspace.package] [workspace.package]
version = "0.3.7" version = "0.3.8"
edition = "2021" edition = "2021"
[dependencies] [dependencies]

View File

@ -841,8 +841,14 @@ fn generate_get_impl(
let index_clause = ::sqlx_record::prelude::build_index_clause(index); let index_clause = ::sqlx_record::prelude::build_index_clause(index);
//Filter order_by fields to only those managed //Filter order_by fields to only those managed. select_fields()
let fields = Self::select_fields().into_iter().collect::<::std::collections::HashSet<_>>(); //may emit annotated forms like `id as "id: Uuid"` for fields that
//carry a custom type; strip the alias before matching so callers
//can pass bare column names.
let fields = Self::select_fields()
.into_iter()
.map(|s| s.split(" ").next().unwrap_or(s))
.collect::<::std::collections::HashSet<_>>();
let order_by = order_by.iter() let order_by = order_by.iter()
.filter(|(field, _)| fields.contains(field)) .filter(|(field, _)| fields.contains(field))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
@ -977,8 +983,13 @@ fn generate_get_impl(
{ {
use ::sqlx_record::prelude::{Filter, bind_values}; use ::sqlx_record::prelude::{Filter, bind_values};
// Validate fields exist // Validate fields exist. select_fields() may emit annotated
let valid_fields: ::std::collections::HashSet<_> = Self::select_fields().into_iter().collect(); // forms like `id as "id: Uuid"`; strip the alias before
// matching so callers can pass bare column names.
let valid_fields: ::std::collections::HashSet<_> = Self::select_fields()
.into_iter()
.map(|s| s.split(" ").next().unwrap_or(s))
.collect();
let selected: Vec<_> = select_fields.iter() let selected: Vec<_> = select_fields.iter()
.filter(|f| valid_fields.contains(*f)) .filter(|f| valid_fields.contains(*f))
.copied() .copied()

View File

@ -1,5 +1,3 @@
use chrono::Utc;
use rand::random;
use uuid::Uuid; use uuid::Uuid;
mod conn_provider; mod conn_provider;
@ -23,6 +21,7 @@ pub use sqlx_record_derive::{Entity, Update};
/// Last 8 bytes: random data /// Last 8 bytes: random data
#[inline] #[inline]
pub fn new_uuid() -> Uuid { pub fn new_uuid() -> Uuid {
/*
let timestamp = Utc::now().timestamp_millis() as u64; let timestamp = Utc::now().timestamp_millis() as u64;
let random = random::<u64>(); let random = random::<u64>();
let mut bytes = [0u8; 16]; let mut bytes = [0u8; 16];
@ -30,6 +29,8 @@ pub fn new_uuid() -> Uuid {
bytes[8..].copy_from_slice(&random.to_be_bytes()); bytes[8..].copy_from_slice(&random.to_be_bytes());
Uuid::from_bytes(bytes) Uuid::from_bytes(bytes)
*/
Uuid::now_v7()
} }
/// Creates a lookup table entity struct with an associated code enum. /// Creates a lookup table entity struct with an associated code enum.