Files
georm/tests/models.rs
Claude 4c314c2f0b feat: add SQLite support behind a sqlite Cargo feature
Adds a SqliteDialect alongside PostgresDialect, gated by mutually exclusive
postgres (default) / sqlite Cargo features, plus a feature-selected
ActiveDatabase type alias used throughout the public Georm/Defaultable
traits. Basic CRUD and Defaultable are fully covered by a new SQLite test
suite (tests/sqlite/*) running against a real SQLite database, alongside
split migrations/sqlite/*.

Two real dialect differences surfaced beyond placeholder syntax:
- SQLite's INTEGER columns type-infer as i64 in sqlx's compile-time query
  macros (unlike Postgres SERIAL -> i32), so the SQLite test models use i64
  for integer PK/FK fields.
- sqlx's SQLite query macros need bind arguments bound to a place rather
  than passed as an inline expression (fixed once, centrally, in
  SqlDialect::generate_relation_lookup).

Composite-key entities with chrono::DateTime columns (tests/composite_key.rs)
are not yet ported to SQLite: SQLite has no TIMESTAMPTZ equivalent, so sqlx
infers TEXT date/time columns as String rather than DateTime<Utc> unless the
query explicitly overrides the column type. That needs per-field type
overrides threaded through the generated queries, left as follow-up.

CI now matrixes over both backends; formatting/audit only run once since
they don't vary by backend. examples/postgres/* stays out of
--no-default-features builds by scoping sqlite lint/test just recipes to
`-p georm` (it depends on georm's default postgres feature via Cargo
feature unification, so building it under --features sqlite at the
workspace level double-activates both dialects).
2026-07-21 19:06:35 +00:00

137 lines
3.3 KiB
Rust

#![cfg(feature = "postgres")]
use georm::Georm;
use sqlx::{Postgres, types::BigDecimal};
#[derive(Debug, Georm, PartialEq, Eq, Default)]
#[georm(
table = "biographies",
one_to_one = [{
name = "author", remote_id = "biography_id", table = "authors", entity = Author
}]
)]
pub struct Biography {
#[georm(id)]
pub id: i32,
pub content: String,
}
#[derive(Debug, Georm, PartialEq, Eq, Default)]
#[georm(table = "authors")]
pub struct Author {
#[georm(id)]
pub id: i32,
pub name: String,
#[georm(relation = {entity = Biography, table = "biographies", name = "biography", nullable = true})]
pub biography_id: Option<i32>,
}
impl PartialOrd for Author {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.id.cmp(&other.id))
}
}
impl Ord for Author {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.id.cmp(&other.id)
}
}
#[derive(Debug, Georm, PartialEq, Eq, Default)]
#[georm(
table = "books",
one_to_many = [
{ name = "reviews", remote_id = "book_id", table = "reviews", entity = Review }
],
many_to_many = [{
name = "genres",
table = "genres",
entity = Genre,
link = { table = "book_genres", from = "book_id", to = "genre_id" }
}]
)]
pub struct Book {
#[georm(id)]
ident: i32,
title: String,
#[georm(relation = {entity = Author, table = "authors", name = "author"})]
author_id: i32,
}
impl PartialOrd for Book {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.ident.cmp(&other.ident))
}
}
impl Ord for Book {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.ident.cmp(&other.ident)
}
}
#[derive(Debug, Georm, PartialEq, Eq)]
#[georm(table = "reviews")]
pub struct Review {
#[georm(id)]
pub id: i32,
#[georm(relation = {entity = Book, table = "books", remote_id = "ident", name = "book"})]
pub book_id: i32,
pub review: String,
}
#[derive(Debug, Georm, PartialEq, Eq)]
#[georm(
table = "genres",
many_to_many = [{
name = "books",
table = "books",
entity = Book,
remote_id = "ident",
link = { table = "book_genres", from = "genre_id", to = "book_id" }
}]
)]
pub struct Genre {
#[georm(id)]
id: i32,
name: String,
}
#[derive(Debug, Georm, PartialEq, Eq, Default)]
#[georm(table = "UserRoles")]
pub struct UserRole {
#[georm(id)]
pub user_id: i32,
#[georm(id)]
pub role_id: i32,
#[georm(defaultable)]
pub assigned_at: chrono::DateTime<chrono::Utc>,
}
#[derive(Debug, Georm, PartialEq, Default, Clone)]
#[georm(table = "products")]
pub struct Product {
#[georm(id, generated_always)]
pub id: i32,
#[georm(generated)]
pub sku_number: i32,
pub name: String,
pub price: BigDecimal,
pub discount_percent: i32,
#[georm(generated_always)]
pub final_price: Option<BigDecimal>, // Apparently this can be null ?
}
impl Product {
#[allow(dead_code)]
pub async fn find_by_name<'e, E>(name: String, executor: E) -> ::sqlx::Result<Self>
where
E: sqlx::Executor<'e, Database = Postgres>,
{
::sqlx::query_as!(Self, "SELECT * FROM products WHERE name = $1", name)
.fetch_one(executor)
.await
}
}