2022-03-16 03:39:21 +03:00
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
|
|
|
|
2022-03-01 01:47:51 +03:00
|
|
|
-- | Common interface for setup/teardown for all backends - schema and data
|
|
|
|
module Harness.Test.Schema
|
|
|
|
( Table (..),
|
2022-06-08 02:24:42 +03:00
|
|
|
table,
|
2022-03-01 01:47:51 +03:00
|
|
|
Reference (..),
|
|
|
|
Column (..),
|
|
|
|
ScalarType (..),
|
|
|
|
ScalarValue (..),
|
2022-06-17 11:44:04 +03:00
|
|
|
UniqueConstraint (..),
|
2022-04-12 18:39:36 +03:00
|
|
|
BackendScalarType (..),
|
|
|
|
BackendScalarValue (..),
|
2022-04-19 18:39:02 +03:00
|
|
|
BackendScalarValueType (..),
|
2022-05-03 10:37:37 +03:00
|
|
|
ManualRelationship (..),
|
2022-04-19 18:39:02 +03:00
|
|
|
quotedValue,
|
|
|
|
unquotedValue,
|
|
|
|
backendScalarValue,
|
2022-03-01 01:47:51 +03:00
|
|
|
column,
|
2022-03-10 14:18:13 +03:00
|
|
|
columnNull,
|
2022-04-12 18:39:36 +03:00
|
|
|
defaultBackendScalarType,
|
|
|
|
getBackendScalarType,
|
|
|
|
defaultBackendScalarValue,
|
2022-04-19 18:39:02 +03:00
|
|
|
formatBackendScalarValueType,
|
2022-03-10 14:18:13 +03:00
|
|
|
parseUTCTimeOrError,
|
|
|
|
trackTable,
|
|
|
|
untrackTable,
|
|
|
|
trackObjectRelationships,
|
|
|
|
trackArrayRelationships,
|
|
|
|
untrackRelationships,
|
2022-05-03 10:37:37 +03:00
|
|
|
mkObjectRelationshipName,
|
|
|
|
mkArrayRelationshipName,
|
2022-03-01 01:47:51 +03:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
2022-03-10 14:18:13 +03:00
|
|
|
import Data.Foldable (for_)
|
2022-04-12 18:39:36 +03:00
|
|
|
import Data.Text (Text)
|
|
|
|
import Data.Time (UTCTime, defaultTimeLocale)
|
2022-03-10 14:18:13 +03:00
|
|
|
import Data.Time.Format (parseTimeOrError)
|
2022-03-15 19:08:47 +03:00
|
|
|
import Harness.Exceptions
|
2022-03-10 14:18:13 +03:00
|
|
|
import Harness.GraphqlEngine qualified as GraphqlEngine
|
|
|
|
import Harness.Quoter.Yaml (yaml)
|
2022-03-15 19:08:47 +03:00
|
|
|
import Harness.Test.Context (BackendType, defaultBackendTypeString, defaultSchema, defaultSource)
|
2022-04-20 20:15:42 +03:00
|
|
|
import Harness.TestEnvironment (TestEnvironment)
|
2022-03-01 01:47:51 +03:00
|
|
|
import Prelude
|
|
|
|
|
|
|
|
-- | Generic type to use to specify schema tables for all backends.
|
|
|
|
-- Usually a list of these make up a "schema" to pass to the respective
|
|
|
|
-- @Harness.Backend.<Backend>.{setup,teardown}@ functions
|
|
|
|
--
|
|
|
|
-- NOTE: There is neither a type-level check to assert that the length of
|
|
|
|
-- tableColumns matches the length of each row in tableData, nor that the
|
|
|
|
-- tablePrimaryKey only contains names of columns already in tableColumns or
|
|
|
|
-- that tableReferences are valid references to other Tables. Test author will
|
|
|
|
-- need to be just a bit careful while constructing Tables.
|
|
|
|
data Table = Table
|
|
|
|
{ tableName :: Text,
|
2022-03-10 14:18:13 +03:00
|
|
|
-- | Columns that are references (foreign keys) should be null-able
|
2022-03-01 01:47:51 +03:00
|
|
|
tableColumns :: [Column],
|
|
|
|
tablePrimaryKey :: [Text],
|
|
|
|
tableReferences :: [Reference],
|
2022-06-17 11:44:04 +03:00
|
|
|
tableData :: [[ScalarValue]],
|
|
|
|
tableUniqueConstraints :: [UniqueConstraint]
|
2022-03-01 01:47:51 +03:00
|
|
|
}
|
2022-03-10 14:18:13 +03:00
|
|
|
deriving (Show, Eq)
|
2022-03-01 01:47:51 +03:00
|
|
|
|
2022-06-17 11:44:04 +03:00
|
|
|
data UniqueConstraint = UniqueConstraintColumns [Text] | UniqueConstraintExpression Text
|
|
|
|
deriving (Show, Eq)
|
|
|
|
|
2022-06-08 02:24:42 +03:00
|
|
|
-- | Create a table from just a name.
|
|
|
|
-- Use record updates to modify the result.
|
|
|
|
table :: Text -> Table
|
2022-06-17 11:44:04 +03:00
|
|
|
table tableName = Table tableName [] [] [] [] []
|
2022-06-08 02:24:42 +03:00
|
|
|
|
2022-03-01 01:47:51 +03:00
|
|
|
-- | Foreign keys for backends that support it.
|
|
|
|
data Reference = Reference
|
|
|
|
{ referenceLocalColumn :: Text,
|
|
|
|
referenceTargetTable :: Text,
|
|
|
|
referenceTargetColumn :: Text
|
|
|
|
}
|
2022-03-10 14:18:13 +03:00
|
|
|
deriving (Show, Eq)
|
2022-03-01 01:47:51 +03:00
|
|
|
|
2022-05-03 10:37:37 +03:00
|
|
|
-- | Type representing manual relationship between tables. This is
|
|
|
|
-- only used for BigQuery backend currently where additional
|
|
|
|
-- relationships has to be manually specified.
|
|
|
|
data ManualRelationship = ManualRelationship
|
|
|
|
{ relSourceTable :: Text,
|
|
|
|
relTargetTable :: Text,
|
|
|
|
relSourceColumn :: Text,
|
|
|
|
relTargetColumn :: Text
|
|
|
|
}
|
|
|
|
deriving (Show, Eq)
|
|
|
|
|
2022-03-01 01:47:51 +03:00
|
|
|
-- | Generic type to construct columns for all backends
|
|
|
|
data Column = Column
|
|
|
|
{ columnName :: Text,
|
|
|
|
columnType :: ScalarType,
|
|
|
|
columnNullable :: Bool,
|
|
|
|
columnDefault :: Maybe Text
|
|
|
|
}
|
2022-03-10 14:18:13 +03:00
|
|
|
deriving (Show, Eq)
|
2022-03-01 01:47:51 +03:00
|
|
|
|
2022-04-12 18:39:36 +03:00
|
|
|
-- | Generic type to represent ScalarType for multiple backends. This
|
|
|
|
-- type can be used to encapsulate the column types for different
|
|
|
|
-- backends by providing explicit name of the datatype. This provides
|
|
|
|
-- flexibility and scalability which is difficult to achieve by just
|
|
|
|
-- extending ScalarType.
|
|
|
|
--
|
|
|
|
-- To give a concrete usecase, right now we have 'ScalarType' with
|
|
|
|
-- value 'TUTCTime'. This is treated as TIMESTAMP for Citus and
|
|
|
|
-- DATETIME for MSSQL server. There might be usecases where you want
|
|
|
|
-- your table column to treat it as TIMESTAMP for Citus and
|
|
|
|
-- <https://docs.microsoft.com/en-us/sql/t-sql/data-types/datetime2-transact-sql?redirectedfrom=MSDN&view=sql-server-ver15
|
|
|
|
-- DATETIME2> for MSSQL server. BackendScalarType makes such use case
|
|
|
|
-- very simple to achive instead of making you define a new sum type
|
|
|
|
-- and handling it.
|
|
|
|
data BackendScalarType = BackendScalarType
|
|
|
|
{ bstMysql :: Maybe Text,
|
|
|
|
bstCitus :: Maybe Text,
|
|
|
|
bstPostgres :: Maybe Text,
|
|
|
|
bstBigQuery :: Maybe Text,
|
|
|
|
bstMssql :: Maybe Text
|
|
|
|
}
|
|
|
|
deriving (Show, Eq)
|
|
|
|
|
|
|
|
-- | Default value for 'BackendScalarType' initialized with 'Nothing'
|
|
|
|
-- for all the fields.
|
|
|
|
defaultBackendScalarType :: BackendScalarType
|
|
|
|
defaultBackendScalarType =
|
|
|
|
BackendScalarType
|
|
|
|
{ bstMysql = Nothing,
|
|
|
|
bstCitus = Nothing,
|
|
|
|
bstMssql = Nothing,
|
|
|
|
bstPostgres = Nothing,
|
|
|
|
bstBigQuery = Nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
-- | Access specific backend scalar type out of 'BackendScalarType'
|
|
|
|
getBackendScalarType :: BackendScalarType -> (BackendScalarType -> Maybe Text) -> Text
|
|
|
|
getBackendScalarType bst fn =
|
|
|
|
case fn bst of
|
|
|
|
Just scalarType -> scalarType
|
|
|
|
Nothing -> error $ "getBackendScalarType: BackendScalarType is Nothing, passed " <> show bst
|
|
|
|
|
2022-04-19 18:39:02 +03:00
|
|
|
-- | This type represents how the serialization of a value should
|
|
|
|
-- happen for a particular item. 'Quoted' text indicates that the text
|
|
|
|
-- will be enclosed with double quotes whereas 'Unqouted' text will have
|
|
|
|
-- none.
|
|
|
|
--
|
|
|
|
-- Usually, texts (or strings) should be represented as quoted and
|
|
|
|
-- numbers might not require any quotes. Although, consult the
|
|
|
|
-- particular database backend for the exact behavior. This type has
|
|
|
|
-- been introduced to allow flexibility while construting values for
|
|
|
|
-- the columns.
|
|
|
|
data BackendScalarValueType = Quoted Text | Unquoted Text deriving (Show, Eq)
|
|
|
|
|
2022-04-22 13:32:35 +03:00
|
|
|
quotedValue :: Text -> Maybe BackendScalarValueType
|
|
|
|
quotedValue = Just . Quoted
|
2022-04-19 18:39:02 +03:00
|
|
|
|
2022-04-22 13:32:35 +03:00
|
|
|
unquotedValue :: Text -> Maybe BackendScalarValueType
|
|
|
|
unquotedValue = Just . Unquoted
|
2022-04-19 18:39:02 +03:00
|
|
|
|
|
|
|
formatBackendScalarValueType :: BackendScalarValueType -> Text
|
|
|
|
formatBackendScalarValueType (Quoted text) = "'" <> text <> "'"
|
|
|
|
formatBackendScalarValueType (Unquoted text) = text
|
|
|
|
|
2022-04-12 18:39:36 +03:00
|
|
|
-- | Generic type to represent ScalarValue for multiple backends. This
|
|
|
|
-- type can be used to encapsulate the column values for different
|
|
|
|
-- backends by providing explicit data for individual backend. This provides
|
|
|
|
-- flexibility and scalability which is difficult to achieve by just
|
|
|
|
-- extending ScalarValue.
|
|
|
|
--
|
|
|
|
-- To give a concrete usecase, right now we have timestamp column for
|
|
|
|
-- out database. Depending on the database, the value can be
|
|
|
|
-- different. For postgres backend, we use 2017-09-21T09:39:44 to
|
|
|
|
-- represent timestamp. But we would want to use 2017-09-21T09:39:44Z
|
|
|
|
-- for Microsoft's SQL server backend. This type provides flexibility
|
|
|
|
-- to provide such options.
|
|
|
|
data BackendScalarValue = BackendScalarValue
|
2022-04-19 18:39:02 +03:00
|
|
|
{ bsvMysql :: Maybe BackendScalarValueType,
|
|
|
|
bsvCitus :: Maybe BackendScalarValueType,
|
|
|
|
bsvPostgres :: Maybe BackendScalarValueType,
|
|
|
|
bsvBigQuery :: Maybe BackendScalarValueType,
|
|
|
|
bsvMssql :: Maybe BackendScalarValueType
|
2022-04-12 18:39:36 +03:00
|
|
|
}
|
|
|
|
deriving (Show, Eq)
|
|
|
|
|
|
|
|
-- | Default value for 'BackendScalarValue' initialized with 'Nothing'
|
|
|
|
-- for all the fields.
|
|
|
|
defaultBackendScalarValue :: BackendScalarValue
|
|
|
|
defaultBackendScalarValue =
|
|
|
|
BackendScalarValue
|
|
|
|
{ bsvMysql = Nothing,
|
|
|
|
bsvCitus = Nothing,
|
|
|
|
bsvPostgres = Nothing,
|
|
|
|
bsvBigQuery = Nothing,
|
|
|
|
bsvMssql = Nothing
|
|
|
|
}
|
|
|
|
|
2022-03-01 01:47:51 +03:00
|
|
|
-- | Generic scalar type for all backends, for simplicity.
|
|
|
|
-- Ideally, we would be wiring in @'Backend@ specific scalar types here to make
|
|
|
|
-- sure all backend-specific scalar types are also covered by tests, perhaps in
|
|
|
|
-- a future iteration.
|
|
|
|
data ScalarType
|
|
|
|
= TInt
|
|
|
|
| TStr
|
2022-03-10 14:18:13 +03:00
|
|
|
| TUTCTime
|
|
|
|
| TBool
|
2022-04-12 18:39:36 +03:00
|
|
|
| TCustomType BackendScalarType
|
2022-03-10 14:18:13 +03:00
|
|
|
deriving (Show, Eq)
|
2022-03-01 01:47:51 +03:00
|
|
|
|
|
|
|
-- | Generic scalar value type for all backends, that should directly correspond
|
|
|
|
-- to 'ScalarType'
|
|
|
|
data ScalarValue
|
|
|
|
= VInt Int
|
|
|
|
| VStr Text
|
2022-03-10 14:18:13 +03:00
|
|
|
| VUTCTime UTCTime
|
|
|
|
| VBool Bool
|
|
|
|
| VNull
|
2022-04-12 18:39:36 +03:00
|
|
|
| VCustomValue BackendScalarValue
|
2022-03-10 14:18:13 +03:00
|
|
|
deriving (Show, Eq)
|
2022-03-01 01:47:51 +03:00
|
|
|
|
2022-04-19 18:39:02 +03:00
|
|
|
backendScalarValue :: BackendScalarValue -> (BackendScalarValue -> Maybe BackendScalarValueType) -> BackendScalarValueType
|
|
|
|
backendScalarValue bsv fn = case fn bsv of
|
|
|
|
Nothing -> error $ "backendScalarValue: Retrieved value is Nothing, passed " <> show bsv
|
2022-04-12 18:39:36 +03:00
|
|
|
Just scalarValue -> scalarValue
|
2022-03-01 01:47:51 +03:00
|
|
|
|
|
|
|
-- | Helper function to construct 'Column's with common defaults
|
|
|
|
column :: Text -> ScalarType -> Column
|
|
|
|
column name typ = Column name typ False Nothing
|
2022-03-10 14:18:13 +03:00
|
|
|
|
|
|
|
-- | Helper function to construct 'Column's that are null-able
|
|
|
|
columnNull :: Text -> ScalarType -> Column
|
|
|
|
columnNull name typ = Column name typ True Nothing
|
|
|
|
|
|
|
|
-- | Helper to construct UTCTime using @%F %T@ format. For e.g. @YYYY-MM-DD HH:MM:SS@
|
|
|
|
parseUTCTimeOrError :: String -> ScalarValue
|
|
|
|
parseUTCTimeOrError = VUTCTime . parseTimeOrError True defaultTimeLocale "%F %T"
|
|
|
|
|
|
|
|
-- | Unified track table
|
2022-04-20 20:15:42 +03:00
|
|
|
trackTable :: HasCallStack => BackendType -> String -> Table -> TestEnvironment -> IO ()
|
|
|
|
trackTable backend source Table {tableName} testEnvironment = do
|
2022-03-15 19:08:47 +03:00
|
|
|
let backendType = defaultBackendTypeString backend
|
|
|
|
schema = defaultSchema backend
|
|
|
|
requestType = backendType <> "_track_table"
|
2022-03-10 14:18:13 +03:00
|
|
|
GraphqlEngine.postMetadata_
|
2022-04-20 20:15:42 +03:00
|
|
|
testEnvironment
|
2022-03-10 14:18:13 +03:00
|
|
|
[yaml|
|
|
|
|
type: *requestType
|
|
|
|
args:
|
2022-03-15 19:08:47 +03:00
|
|
|
source: *source
|
2022-03-10 14:18:13 +03:00
|
|
|
table:
|
|
|
|
schema: *schema
|
|
|
|
name: *tableName
|
|
|
|
|]
|
|
|
|
|
|
|
|
-- | Unified untrack table
|
2022-04-20 20:15:42 +03:00
|
|
|
untrackTable :: HasCallStack => BackendType -> String -> Table -> TestEnvironment -> IO ()
|
|
|
|
untrackTable backend source Table {tableName} testEnvironment = do
|
2022-03-15 19:08:47 +03:00
|
|
|
let backendType = defaultBackendTypeString backend
|
|
|
|
schema = defaultSchema backend
|
2022-03-10 14:18:13 +03:00
|
|
|
let requestType = backendType <> "_untrack_table"
|
|
|
|
GraphqlEngine.postMetadata_
|
2022-04-20 20:15:42 +03:00
|
|
|
testEnvironment
|
2022-03-10 14:18:13 +03:00
|
|
|
[yaml|
|
|
|
|
type: *requestType
|
|
|
|
args:
|
2022-03-15 19:08:47 +03:00
|
|
|
source: *source
|
2022-03-10 14:18:13 +03:00
|
|
|
table:
|
|
|
|
schema: *schema
|
|
|
|
name: *tableName
|
|
|
|
|]
|
|
|
|
|
|
|
|
-- | Helper to create the object relationship name
|
|
|
|
mkObjectRelationshipName :: Reference -> Text
|
|
|
|
mkObjectRelationshipName Reference {referenceLocalColumn, referenceTargetTable} = referenceTargetTable <> "_by_" <> referenceLocalColumn
|
|
|
|
|
|
|
|
-- | Unified track object relationships
|
2022-04-20 20:15:42 +03:00
|
|
|
trackObjectRelationships :: HasCallStack => BackendType -> Table -> TestEnvironment -> IO ()
|
|
|
|
trackObjectRelationships backend Table {tableName, tableReferences} testEnvironment = do
|
2022-03-15 19:08:47 +03:00
|
|
|
let source = defaultSource backend
|
|
|
|
schema = defaultSchema backend
|
|
|
|
requestType = source <> "_create_object_relationship"
|
2022-03-10 14:18:13 +03:00
|
|
|
for_ tableReferences $ \ref@Reference {referenceLocalColumn} -> do
|
|
|
|
let relationshipName = mkObjectRelationshipName ref
|
|
|
|
GraphqlEngine.postMetadata_
|
2022-04-20 20:15:42 +03:00
|
|
|
testEnvironment
|
2022-03-10 14:18:13 +03:00
|
|
|
[yaml|
|
|
|
|
type: *requestType
|
|
|
|
args:
|
|
|
|
source: *source
|
|
|
|
table:
|
|
|
|
name: *tableName
|
|
|
|
schema: *schema
|
|
|
|
name: *relationshipName
|
|
|
|
using:
|
|
|
|
foreign_key_constraint_on: *referenceLocalColumn
|
|
|
|
|]
|
|
|
|
|
|
|
|
-- | Helper to create the array relationship name
|
|
|
|
mkArrayRelationshipName :: Text -> Text -> Text
|
|
|
|
mkArrayRelationshipName tableName referenceLocalColumn = tableName <> "s_by_" <> referenceLocalColumn
|
|
|
|
|
|
|
|
-- | Unified track array relationships
|
2022-04-20 20:15:42 +03:00
|
|
|
trackArrayRelationships :: HasCallStack => BackendType -> Table -> TestEnvironment -> IO ()
|
|
|
|
trackArrayRelationships backend Table {tableName, tableReferences} testEnvironment = do
|
2022-03-15 19:08:47 +03:00
|
|
|
let source = defaultSource backend
|
|
|
|
schema = defaultSchema backend
|
|
|
|
requestType = source <> "_create_array_relationship"
|
2022-03-10 14:18:13 +03:00
|
|
|
for_ tableReferences $ \Reference {referenceLocalColumn, referenceTargetTable} -> do
|
|
|
|
let relationshipName = mkArrayRelationshipName tableName referenceLocalColumn
|
|
|
|
GraphqlEngine.postMetadata_
|
2022-04-20 20:15:42 +03:00
|
|
|
testEnvironment
|
2022-03-10 14:18:13 +03:00
|
|
|
[yaml|
|
|
|
|
type: *requestType
|
|
|
|
args:
|
|
|
|
source: *source
|
|
|
|
table:
|
|
|
|
name: *referenceTargetTable
|
|
|
|
schema: *schema
|
|
|
|
name: *relationshipName
|
|
|
|
using:
|
|
|
|
foreign_key_constraint_on:
|
|
|
|
table:
|
|
|
|
name: *tableName
|
|
|
|
schema: *schema
|
|
|
|
column: *referenceLocalColumn
|
|
|
|
|]
|
|
|
|
|
|
|
|
-- | Unified untrack relationships
|
2022-04-20 20:15:42 +03:00
|
|
|
untrackRelationships :: HasCallStack => BackendType -> Table -> TestEnvironment -> IO ()
|
|
|
|
untrackRelationships backend Table {tableName, tableReferences} testEnvironment = do
|
2022-03-15 19:08:47 +03:00
|
|
|
let source = defaultSource backend
|
|
|
|
schema = defaultSchema backend
|
|
|
|
requestType = source <> "_drop_relationship"
|
2022-03-10 14:18:13 +03:00
|
|
|
for_ tableReferences $ \ref@Reference {referenceLocalColumn, referenceTargetTable} -> do
|
|
|
|
let arrayRelationshipName = mkArrayRelationshipName tableName referenceLocalColumn
|
|
|
|
objectRelationshipName = mkObjectRelationshipName ref
|
|
|
|
-- drop array relationships
|
|
|
|
GraphqlEngine.postMetadata_
|
2022-04-20 20:15:42 +03:00
|
|
|
testEnvironment
|
2022-03-10 14:18:13 +03:00
|
|
|
[yaml|
|
|
|
|
type: *requestType
|
|
|
|
args:
|
|
|
|
source: *source
|
|
|
|
table:
|
|
|
|
schema: *schema
|
|
|
|
name: *referenceTargetTable
|
|
|
|
relationship: *arrayRelationshipName
|
|
|
|
|]
|
|
|
|
-- drop object relationships
|
|
|
|
GraphqlEngine.postMetadata_
|
2022-04-20 20:15:42 +03:00
|
|
|
testEnvironment
|
2022-03-10 14:18:13 +03:00
|
|
|
[yaml|
|
|
|
|
type: *requestType
|
|
|
|
args:
|
|
|
|
source: *source
|
|
|
|
table:
|
|
|
|
schema: *schema
|
|
|
|
name: *tableName
|
|
|
|
relationship: *objectRelationshipName
|
|
|
|
|]
|