added explicit lookup_fields to entity types
This commit is contained in:
@ -85,6 +85,10 @@ impl DatabaseExecutor for MockExecutor {
|
||||
Ok("2026-03-10T00:00:00Z".to_string())
|
||||
}
|
||||
|
||||
fn is_mock(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn get_queries(&self) -> Vec<String> {
|
||||
MOCK_STATE.with(|state| state.borrow().captured_queries.clone())
|
||||
|
||||
@ -20,6 +20,11 @@ pub trait DatabaseExecutor: Send + Sync {
|
||||
/// Returns the current transaction timestamp
|
||||
fn timestamp(&self) -> Result<String, String>;
|
||||
|
||||
/// Returns whether this is a mock executor (bypassing pg_catalog index checks)
|
||||
fn is_mock(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn get_queries(&self) -> Vec<String>;
|
||||
|
||||
|
||||
@ -21,16 +21,22 @@ use executors::pgrx::SpiExecutor;
|
||||
#[cfg(test)]
|
||||
use executors::mock::MockExecutor;
|
||||
|
||||
use indexmap::IndexMap;
|
||||
use punc::Punc;
|
||||
use relation::Relation;
|
||||
use schema::Schema;
|
||||
use serde_json::Value;
|
||||
use indexmap::IndexMap;
|
||||
use std::sync::Arc;
|
||||
use r#type::Type;
|
||||
|
||||
use crate::drop::{Drop, Error, ErrorDetails};
|
||||
|
||||
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
|
||||
pub struct IndexInfo {
|
||||
pub table: String,
|
||||
pub columns: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
pub struct Database {
|
||||
pub enums: IndexMap<String, Enum>,
|
||||
@ -38,6 +44,8 @@ pub struct Database {
|
||||
pub puncs: IndexMap<String, Punc>,
|
||||
pub relations: IndexMap<String, Relation>,
|
||||
#[serde(skip)]
|
||||
pub indexes: Vec<IndexInfo>,
|
||||
#[serde(skip)]
|
||||
pub schemas: IndexMap<String, Arc<Schema>>,
|
||||
#[serde(skip)]
|
||||
pub executor: Box<dyn DatabaseExecutor + Send + Sync>,
|
||||
@ -50,6 +58,7 @@ impl Database {
|
||||
types: IndexMap::new(),
|
||||
relations: IndexMap::new(),
|
||||
puncs: IndexMap::new(),
|
||||
indexes: Vec::new(),
|
||||
schemas: IndexMap::new(),
|
||||
#[cfg(not(test))]
|
||||
executor: Box::new(SpiExecutor::new()),
|
||||
@ -177,6 +186,25 @@ impl Database {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(serde_json::Value::Array(arr)) = map.remove("indexes") {
|
||||
for item in arr {
|
||||
match serde_json::from_value::<IndexInfo>(item) {
|
||||
Ok(idx) => {
|
||||
db.indexes.push(idx);
|
||||
}
|
||||
Err(e) => {
|
||||
errors.push(Error {
|
||||
code: "DATABASE_INDEX_PARSE_FAILED".to_string(),
|
||||
values: Some(IndexMap::from([("reason".to_string(), e.to_string())])),
|
||||
details: ErrorDetails {
|
||||
..Default::default()
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
db.compile(&mut errors);
|
||||
@ -214,7 +242,6 @@ impl Database {
|
||||
self.executor.timestamp()
|
||||
}
|
||||
|
||||
|
||||
pub fn compile(&mut self, errors: &mut Vec<crate::drop::Error>) {
|
||||
// Phase 1: Registration
|
||||
self.collect_schemas(errors);
|
||||
@ -260,6 +287,43 @@ impl Database {
|
||||
.compile(self, root_id, id.clone(), errors);
|
||||
}
|
||||
}
|
||||
|
||||
// Phase 5: Verify unique indexes for defined lookup fields
|
||||
for (_, type_def) in &self.types {
|
||||
if !type_def.lookup_fields.is_empty() {
|
||||
let mut index_found = false;
|
||||
for index in &self.indexes {
|
||||
if index.table == type_def.name {
|
||||
if index.columns.len() == type_def.lookup_fields.len()
|
||||
&& index
|
||||
.columns
|
||||
.iter()
|
||||
.all(|c| type_def.lookup_fields.contains(c))
|
||||
{
|
||||
index_found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if !index_found {
|
||||
errors.push(Error {
|
||||
code: "MISSING_LOOKUP_INDEX".to_string(),
|
||||
values: Some(IndexMap::from([
|
||||
("type".to_string(), type_def.name.clone()),
|
||||
(
|
||||
"lookup_fields".to_string(),
|
||||
format!("{:?}", type_def.lookup_fields),
|
||||
),
|
||||
])),
|
||||
details: ErrorDetails {
|
||||
path: Some(format!("/types/{}", type_def.name)),
|
||||
schema: Some(type_def.name.clone()),
|
||||
..Default::default()
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Synthesizes Composed Filter References for all table-backed boundaries.
|
||||
@ -395,7 +459,6 @@ impl Database {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Inspects the Postgres pg_constraint relations catalog to securely identify
|
||||
/// the precise Foreign Key connecting a parent and child hierarchy path.
|
||||
pub fn resolve_relation<'a>(
|
||||
|
||||
@ -677,25 +677,32 @@ impl Merger {
|
||||
let id_val = entity_fields.get("id");
|
||||
let entity_type_name = entity_type.name.as_str();
|
||||
|
||||
let mut lookup_complete = false;
|
||||
if !entity_type.lookup_fields.is_empty() {
|
||||
lookup_complete = true;
|
||||
for column in &entity_type.lookup_fields {
|
||||
match entity_fields.get(column) {
|
||||
Some(Value::Null) | None => {
|
||||
lookup_complete = false;
|
||||
break;
|
||||
let mut lookup_satisfied_keys = Vec::new();
|
||||
for parent_type_name in entity_type.hierarchy.iter().rev() {
|
||||
if let Some(parent_type) = self.db.types.get(parent_type_name) {
|
||||
if !parent_type.lookup_fields.is_empty() {
|
||||
let mut lookup_complete = true;
|
||||
for column in &parent_type.lookup_fields {
|
||||
match entity_fields.get(column) {
|
||||
Some(Value::Null) | None => {
|
||||
lookup_complete = false;
|
||||
break;
|
||||
}
|
||||
Some(Value::String(s)) if s.is_empty() => {
|
||||
lookup_complete = false;
|
||||
break;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Some(Value::String(s)) if s.is_empty() => {
|
||||
lookup_complete = false;
|
||||
break;
|
||||
if lookup_complete {
|
||||
lookup_satisfied_keys.push(&parent_type.lookup_fields);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if id_val.is_none() && !lookup_complete {
|
||||
if id_val.is_none() && lookup_satisfied_keys.is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
@ -727,9 +734,9 @@ impl Merger {
|
||||
where_parts.push(format!("t1.id = {}", Self::quote_literal(id)));
|
||||
}
|
||||
|
||||
if lookup_complete {
|
||||
for lookup_fields in lookup_satisfied_keys {
|
||||
let mut lookup_predicates = Vec::new();
|
||||
for column in &entity_type.lookup_fields {
|
||||
for column in lookup_fields {
|
||||
let val = entity_fields.get(column).unwrap_or(&Value::Null);
|
||||
if column == "type" {
|
||||
lookup_predicates.push(format!("t1.\"{}\" = {}", column, Self::quote_literal(val)));
|
||||
|
||||
@ -3407,6 +3407,18 @@ fn test_database_6_0() {
|
||||
crate::tests::runner::run_test_case(&path, 6, 0).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_database_7_0() {
|
||||
let path = format!("{}/fixtures/database.json", env!("CARGO_MANIFEST_DIR"));
|
||||
crate::tests::runner::run_test_case(&path, 7, 0).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_database_8_0() {
|
||||
let path = format!("{}/fixtures/database.json", env!("CARGO_MANIFEST_DIR"));
|
||||
crate::tests::runner::run_test_case(&path, 8, 0).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cases_0_0() {
|
||||
let path = format!("{}/fixtures/cases.json", env!("CARGO_MANIFEST_DIR"));
|
||||
@ -7666,3 +7678,9 @@ fn test_merger_0_15() {
|
||||
let path = format!("{}/fixtures/merger.json", env!("CARGO_MANIFEST_DIR"));
|
||||
crate::tests::runner::run_test_case(&path, 0, 15).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_merger_0_16() {
|
||||
let path = format!("{}/fixtures/merger.json", env!("CARGO_MANIFEST_DIR"));
|
||||
crate::tests::runner::run_test_case(&path, 0, 16).unwrap();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user