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).
This commit is contained in:
Claude
2026-07-21 19:06:35 +00:00
parent 0d43a3fe13
commit 4c314c2f0b
31 changed files with 802 additions and 35 deletions
+159
View File
@@ -0,0 +1,159 @@
#![cfg(feature = "sqlite")]
use georm::Georm;
use rand::seq::SliceRandom;
use models::Author;
mod models;
#[sqlx::test(migrations = "./migrations/sqlite", fixtures("simple_struct"))]
async fn find_all_query_works(pool: sqlx::SqlitePool) -> sqlx::Result<()> {
let result = Author::find_all(&pool).await?;
assert_eq!(3, result.len());
Ok(())
}
#[sqlx::test(migrations = "./migrations/sqlite")]
async fn find_all_returns_empty_vec_on_empty_table(pool: sqlx::SqlitePool) -> sqlx::Result<()> {
let result = Author::find_all(&pool).await?;
assert_eq!(0, result.len());
Ok(())
}
#[sqlx::test(migrations = "./migrations/sqlite", fixtures("simple_struct"))]
async fn find_query_works(pool: sqlx::SqlitePool) -> sqlx::Result<()> {
let id = 1;
let res = Author::find(&pool, &id).await?;
assert!(res.is_some());
let res = res.unwrap();
assert_eq!(String::from("J.R.R. Tolkien"), res.name);
assert_eq!(1, res.id);
Ok(())
}
#[sqlx::test(migrations = "./migrations/sqlite")]
async fn find_returns_none_if_not_found(pool: sqlx::SqlitePool) -> sqlx::Result<()> {
let res = Author::find(&pool, &420).await?;
assert!(res.is_none());
Ok(())
}
#[sqlx::test(migrations = "./migrations/sqlite")]
async fn create_works(pool: sqlx::SqlitePool) -> sqlx::Result<()> {
let author = Author {
id: 1,
name: "J.R.R. Tolkien".into(),
..Default::default()
};
author.create(&pool).await?;
let all_authors = Author::find_all(&pool).await?;
assert_eq!(1, all_authors.len());
assert_eq!(vec![author], all_authors);
Ok(())
}
#[sqlx::test(migrations = "./migrations/sqlite", fixtures("simple_struct"))]
async fn create_fails_if_already_exists(pool: sqlx::SqlitePool) -> sqlx::Result<()> {
let author = Author {
id: 2,
name: "Miura Kentaro".into(),
..Default::default()
};
let result = author.create(&pool).await;
assert!(result.is_err());
Ok(())
}
#[sqlx::test(migrations = "./migrations/sqlite", fixtures("simple_struct"))]
async fn update_works(pool: sqlx::SqlitePool) -> sqlx::Result<()> {
let expected_initial = Author {
name: "J.R.R. Tolkien".into(),
id: 1,
biography_id: Some(2),
};
let expected_final = Author {
name: "Jolkien Rolkien Rolkien Tolkien".into(),
id: 1,
biography_id: Some(2),
};
let tolkien = Author::find(&pool, &1).await?;
assert!(tolkien.is_some());
let mut tolkien = tolkien.unwrap();
assert_eq!(expected_initial, tolkien);
tolkien.name = expected_final.name.clone();
let updated = tolkien.update(&pool).await?;
assert_eq!(expected_final, updated);
Ok(())
}
#[sqlx::test(migrations = "./migrations/sqlite")]
async fn update_fails_if_not_already_exists(pool: sqlx::SqlitePool) -> sqlx::Result<()> {
let author = Author {
id: 2,
name: "Miura Kentaro".into(),
..Default::default()
};
let result = author.update(&pool).await;
assert!(result.is_err());
Ok(())
}
#[sqlx::test(migrations = "./migrations/sqlite")]
async fn should_create_if_does_not_exist(pool: sqlx::SqlitePool) -> sqlx::Result<()> {
let all_authors = Author::find_all(&pool).await?;
assert_eq!(0, all_authors.len());
let author = Author {
id: 4,
name: "Miura Kentaro".into(),
..Default::default()
};
author.upsert(&pool).await?;
let all_authors = Author::find_all(&pool).await?;
assert_eq!(1, all_authors.len());
Ok(())
}
#[sqlx::test(migrations = "./migrations/sqlite", fixtures("simple_struct"))]
async fn should_update_if_exist(pool: sqlx::SqlitePool) -> sqlx::Result<()> {
let all_authors = Author::find_all(&pool).await?;
assert_eq!(3, all_authors.len());
let author = Author {
id: 2,
name: "Miura Kentaro".into(),
..Default::default()
};
author.upsert(&pool).await?;
let mut all_authors = Author::find_all(&pool).await?;
all_authors.sort();
assert_eq!(3, all_authors.len());
assert_eq!(author, all_authors[1]);
Ok(())
}
#[sqlx::test(migrations = "./migrations/sqlite", fixtures("simple_struct"))]
async fn delete_by_id_should_delete_only_one_entry(pool: sqlx::SqlitePool) -> sqlx::Result<()> {
let id = 2;
let all_authors = Author::find_all(&pool).await?;
assert_eq!(3, all_authors.len());
assert!(all_authors.iter().any(|author| author.get_id() == id));
let result = Author::delete_by_id(&pool, &id).await?;
assert_eq!(1, result);
let all_authors = Author::find_all(&pool).await?;
assert_eq!(2, all_authors.len());
assert!(all_authors.iter().all(|author| author.get_id() != id));
Ok(())
}
#[sqlx::test(migrations = "./migrations/sqlite", fixtures("simple_struct"))]
async fn delete_should_delete_current_entity_from_db(pool: sqlx::SqlitePool) -> sqlx::Result<()> {
let mut all_authors = Author::find_all(&pool).await?;
assert_eq!(3, all_authors.len());
all_authors.shuffle(&mut rand::rng());
let author = all_authors.first().unwrap();
let result = author.delete(&pool).await?;
assert_eq!(1, result);
let all_authors = Author::find_all(&pool).await?;
assert_eq!(2, all_authors.len());
assert!(all_authors.iter().all(|a| a.get_id() != author.get_id()));
Ok(())
}