1
Fork 0
mirror of https://github.com/Airsequel/AirGQL.git synced 2025-08-12 09:46:57 +03:00

Add backend tests for reference following

This commit is contained in:
prescientmoon 2024-08-30 21:23:09 +02:00
commit 0d52017745
2 changed files with 31 additions and 15 deletions
source/AirGQL
tests

View file

@ -611,23 +611,29 @@ getEnrichedTables connection = do
{-| SQLite allows references constraints to not specify the exact column they
are referencing. This functions tries to recover that information by
looking for primary keys among the columns of the referenced table.
Note: we currently do not support having composite primary keys
referenced implicitly, as that would lead to multiple complications like:
- figuring out the correct order for the references
- having to perform the "enrichTableEntry" computation in two separate passes
-}
resolveReferencesConstraint :: [TableEntry] -> Text -> Maybe Text
resolveReferencesConstraint tables referencedTable =
-- => [(TableEntry, [ColumnEntry])]
tables
-- => Maybe (TableEntry, [ColumnEntry])
& P.find (\table -> table.tbl_name == referencedTable)
-- => Maybe [ColumnEntry]
<&> (\table -> table.columns)
-- => Maybe ColumnEntry
>>= P.find (\column -> column.primary_key)
-- => Maybe Text
<&> (.column_name)
resolveReferencesConstraint tables referencedTable = do
table <-
P.find
(\table -> table.tbl_name == referencedTable)
tables
let columns = table.columns
let pks = P.filter (\column -> column.primary_key) columns
-- TODO: do we need to handle rowid columns that are called something else??
let nonRowidPks = P.filter (\column -> column.column_name /= "rowid") pks
case nonRowidPks of
[] -> pure "rowid"
[column] -> pure column.column_name
-- Note: we currently do not support having composite primary keys
-- referenced implicitly, as that would lead to multiple complications like:
-- - figuring out the correct order for the references
-- - having to perform the "enrichTableEntry" computation in two separate passes
--
-- Note 2: Is this that hard to handle? I think there's a good chance we could
-- do it as long as we keep track of the column order. Not sure it's worth the
-- hassle though...
_ -> Nothing
-- See the docs for `resolveReferencesConstraint` for details

View file

@ -274,6 +274,7 @@ testSuite = do
, isUnique = True
, isOmittable = True
, isGenerated = False
, isReference = False
, dflt_value = Nothing
, primary_key = True
}
@ -287,6 +288,7 @@ testSuite = do
, isUnique = False
, isOmittable = True
, isGenerated = False
, isReference = False
, dflt_value = Nothing
, primary_key = False
}
@ -300,6 +302,7 @@ testSuite = do
, isUnique = True
, isOmittable = False
, isGenerated = False
, isReference = False
, dflt_value = Nothing
, primary_key = True
}
@ -313,6 +316,7 @@ testSuite = do
, isUnique = False
, isOmittable = False
, isGenerated = False
, isReference = False
, dflt_value = Nothing
, primary_key = False
}
@ -326,6 +330,7 @@ testSuite = do
, isUnique = False
, isOmittable = True
, isGenerated = False
, isReference = False
, dflt_value = Nothing
, primary_key = False
}
@ -339,6 +344,7 @@ testSuite = do
, isUnique = False
, isOmittable = True
, isGenerated = False
, isReference = False
, dflt_value = Nothing
, primary_key = False
}
@ -376,6 +382,7 @@ testSuite = do
, isUnique = False
, isOmittable = True
, isGenerated = False
, isReference = False
, dflt_value = Nothing
, primary_key = False
}
@ -412,6 +419,7 @@ testSuite = do
, isUnique = False
, isOmittable = False
, isGenerated = False
, isReference = False
, dflt_value = Nothing
, primary_key = False
}
@ -448,6 +456,7 @@ testSuite = do
, isUnique = False
, isOmittable = True
, isGenerated = False
, isReference = False
, dflt_value = Nothing
, primary_key = False
}
@ -479,6 +488,7 @@ testSuite = do
, isUnique = True
, isOmittable = True
, isGenerated = False
, isReference = False
, dflt_value = Nothing
, primary_key = True
}