2022-03-16 03:39:21 +03:00
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
2022-03-10 14:18:13 +03:00
|
|
|
{-# LANGUAGE ViewPatterns #-}
|
2022-02-09 18:26:14 +03:00
|
|
|
|
2022-03-16 03:39:21 +03:00
|
|
|
{-# OPTIONS -Wno-redundant-constraints #-}
|
|
|
|
|
2022-02-09 18:26:14 +03:00
|
|
|
-- | BigQuery helpers.
|
|
|
|
module Harness.Backend.BigQuery
|
|
|
|
( run_,
|
|
|
|
getServiceAccount,
|
|
|
|
getProjectId,
|
2022-03-01 01:47:51 +03:00
|
|
|
createTable,
|
|
|
|
insertTable,
|
|
|
|
trackTable,
|
|
|
|
dropTable,
|
|
|
|
untrackTable,
|
|
|
|
setup,
|
|
|
|
teardown,
|
2022-02-09 18:26:14 +03:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
2022-03-01 01:47:51 +03:00
|
|
|
import Data.Bool (bool)
|
|
|
|
import Data.Foldable (for_)
|
2022-02-09 18:26:14 +03:00
|
|
|
import Data.String
|
2022-03-01 01:47:51 +03:00
|
|
|
import Data.Text (Text)
|
|
|
|
import Data.Text qualified as T
|
|
|
|
import Data.Text.Extended (commaSeparated)
|
2022-02-09 18:26:14 +03:00
|
|
|
import Harness.Constants as Constants
|
|
|
|
import Harness.Env
|
2022-04-04 17:45:12 +03:00
|
|
|
import Harness.Exceptions (HasCallStack, forFinally_)
|
2022-03-01 01:47:51 +03:00
|
|
|
import Harness.GraphqlEngine qualified as GraphqlEngine
|
|
|
|
import Harness.Quoter.Yaml (yaml)
|
|
|
|
import Harness.State (State)
|
2022-03-15 19:08:47 +03:00
|
|
|
import Harness.Test.Context (BackendType (BigQuery), defaultBackendTypeString, defaultSource)
|
2022-03-01 01:47:51 +03:00
|
|
|
import Harness.Test.Schema qualified as Schema
|
2022-02-09 18:26:14 +03:00
|
|
|
import Hasura.Backends.BigQuery.Connection (initConnection)
|
2022-03-30 16:53:14 +03:00
|
|
|
import Hasura.Backends.BigQuery.Execute qualified as Execute
|
2022-02-09 18:26:14 +03:00
|
|
|
import Hasura.Backends.BigQuery.Source (ServiceAccount)
|
2022-03-30 16:53:14 +03:00
|
|
|
import Hasura.Prelude (onLeft)
|
2022-03-01 01:47:51 +03:00
|
|
|
import Prelude
|
2022-02-09 18:26:14 +03:00
|
|
|
|
2022-02-23 22:32:18 +03:00
|
|
|
getServiceAccount :: HasCallStack => IO ServiceAccount
|
2022-03-14 10:49:36 +03:00
|
|
|
getServiceAccount = getEnvJson Constants.bigqueryServiceKeyVar
|
2022-02-09 18:26:14 +03:00
|
|
|
|
|
|
|
getProjectId :: (HasCallStack) => IO Text
|
|
|
|
getProjectId = getEnvString Constants.bigqueryProjectIdVar
|
|
|
|
|
|
|
|
-- | Run a plain Standard SQL string against the server, ignore the
|
|
|
|
-- result. Just checks for errors.
|
|
|
|
run_ :: (HasCallStack) => ServiceAccount -> Text -> String -> IO ()
|
2022-03-30 16:53:14 +03:00
|
|
|
run_ serviceAccount projectId query = do
|
|
|
|
conn <- initConnection serviceAccount projectId Nothing
|
|
|
|
res <- Execute.executeBigQuery conn Execute.BigQuery {Execute.query = fromString query, Execute.parameters = mempty}
|
|
|
|
res `onLeft` (`bigQueryError` query)
|
2022-02-09 18:26:14 +03:00
|
|
|
|
2022-03-30 16:53:14 +03:00
|
|
|
bigQueryError :: HasCallStack => Execute.ExecuteProblem -> String -> IO ()
|
2022-02-09 18:26:14 +03:00
|
|
|
bigQueryError e query =
|
|
|
|
error
|
|
|
|
( unlines
|
|
|
|
[ "BigQuery query error:",
|
2022-03-30 16:53:14 +03:00
|
|
|
T.unpack (Execute.executeProblemMessage e),
|
2022-02-09 18:26:14 +03:00
|
|
|
"SQL was:",
|
|
|
|
query
|
|
|
|
]
|
|
|
|
)
|
2022-03-01 01:47:51 +03:00
|
|
|
|
|
|
|
-- | Serialize Table into a SQL statement, as needed, and execute it on the BigQuery backend
|
|
|
|
createTable :: Schema.Table -> IO ()
|
|
|
|
createTable Schema.Table {tableName, tableColumns} = do
|
|
|
|
serviceAccount <- getServiceAccount
|
|
|
|
projectId <- getProjectId
|
|
|
|
run_
|
|
|
|
serviceAccount
|
|
|
|
projectId
|
|
|
|
$ T.unpack $
|
|
|
|
T.unwords
|
|
|
|
[ "CREATE TABLE",
|
|
|
|
T.pack Constants.bigqueryDataset <> "." <> tableName,
|
|
|
|
"(",
|
|
|
|
commaSeparated $
|
|
|
|
(mkColumn <$> tableColumns),
|
|
|
|
-- Primary keys are not supported by BigQuery
|
|
|
|
-- Foreign keys are not support by BigQuery
|
|
|
|
");"
|
|
|
|
]
|
2022-03-10 14:18:13 +03:00
|
|
|
|
|
|
|
scalarType :: HasCallStack => Schema.ScalarType -> Text
|
|
|
|
scalarType = \case
|
|
|
|
Schema.TInt -> "INT64"
|
|
|
|
Schema.TStr -> "STRING"
|
|
|
|
Schema.TUTCTime -> "DATETIME"
|
|
|
|
Schema.TBool -> "BIT"
|
|
|
|
t -> error $ "Unexpected scalar type used for BigQuery: " <> show t
|
|
|
|
|
|
|
|
mkColumn :: Schema.Column -> Text
|
|
|
|
mkColumn Schema.Column {columnName, columnType, columnNullable, columnDefault} =
|
|
|
|
T.unwords
|
|
|
|
[ columnName,
|
|
|
|
scalarType columnType,
|
|
|
|
bool "NOT NULL" "DEFAULT NULL" columnNullable,
|
|
|
|
maybe "" ("DEFAULT " <>) columnDefault
|
|
|
|
]
|
2022-03-01 01:47:51 +03:00
|
|
|
|
|
|
|
-- | Serialize tableData into an SQL insert statement and execute it.
|
|
|
|
insertTable :: Schema.Table -> IO ()
|
2022-03-15 19:08:47 +03:00
|
|
|
insertTable Schema.Table {tableName, tableColumns, tableData}
|
|
|
|
| null tableData = pure ()
|
|
|
|
| otherwise = do
|
|
|
|
serviceAccount <- getServiceAccount
|
|
|
|
projectId <- getProjectId
|
|
|
|
run_
|
|
|
|
serviceAccount
|
|
|
|
projectId
|
|
|
|
$ T.unpack $
|
|
|
|
T.unwords
|
|
|
|
[ "INSERT INTO",
|
|
|
|
T.pack Constants.bigqueryDataset <> "." <> tableName,
|
|
|
|
"(",
|
|
|
|
commaSeparated (Schema.columnName <$> tableColumns),
|
|
|
|
")",
|
|
|
|
"VALUES",
|
|
|
|
commaSeparated $ mkRow <$> tableData,
|
|
|
|
";"
|
|
|
|
]
|
|
|
|
|
|
|
|
mkRow :: [Schema.ScalarValue] -> Text
|
|
|
|
mkRow row =
|
|
|
|
T.unwords
|
|
|
|
[ "(",
|
|
|
|
commaSeparated $ Schema.serialize <$> row,
|
|
|
|
")"
|
|
|
|
]
|
|
|
|
|
|
|
|
-- | Serialize Table into an SQL DROP statement and execute it
|
|
|
|
dropTable :: Schema.Table -> IO ()
|
|
|
|
dropTable Schema.Table {tableName} = do
|
2022-03-01 01:47:51 +03:00
|
|
|
serviceAccount <- getServiceAccount
|
|
|
|
projectId <- getProjectId
|
|
|
|
run_
|
|
|
|
serviceAccount
|
|
|
|
projectId
|
|
|
|
$ T.unpack $
|
|
|
|
T.unwords
|
2022-03-15 19:08:47 +03:00
|
|
|
[ "DROP TABLE", -- we don't want @IF EXISTS@ here, because we don't want this to fail silently
|
2022-03-01 01:47:51 +03:00
|
|
|
T.pack Constants.bigqueryDataset <> "." <> tableName,
|
|
|
|
";"
|
|
|
|
]
|
|
|
|
|
2022-03-10 14:18:13 +03:00
|
|
|
-- | Post an http request to start tracking
|
|
|
|
-- Overriding here because bigquery's API is uncommon
|
2022-03-01 01:47:51 +03:00
|
|
|
trackTable :: State -> Schema.Table -> IO ()
|
|
|
|
trackTable state Schema.Table {tableName} = do
|
2022-03-10 14:18:13 +03:00
|
|
|
let datasetName = T.pack Constants.bigqueryDataset
|
2022-03-15 19:08:47 +03:00
|
|
|
source = defaultSource BigQuery
|
2022-03-10 14:18:13 +03:00
|
|
|
GraphqlEngine.postMetadata_
|
|
|
|
state
|
2022-03-01 01:47:51 +03:00
|
|
|
[yaml|
|
|
|
|
type: bigquery_track_table
|
|
|
|
args:
|
2022-03-15 19:08:47 +03:00
|
|
|
source: *source
|
2022-03-01 01:47:51 +03:00
|
|
|
table:
|
2022-03-10 14:18:13 +03:00
|
|
|
dataset: *datasetName
|
2022-03-01 01:47:51 +03:00
|
|
|
name: *tableName
|
|
|
|
|]
|
|
|
|
|
|
|
|
-- | Post an http request to stop tracking the table
|
2022-03-10 14:18:13 +03:00
|
|
|
-- Overriding `Schema.trackTable` here because bigquery's API expects a `dataset` key
|
2022-03-01 01:47:51 +03:00
|
|
|
untrackTable :: State -> Schema.Table -> IO ()
|
|
|
|
untrackTable state Schema.Table {tableName} = do
|
2022-03-10 14:18:13 +03:00
|
|
|
let datasetName = T.pack Constants.bigqueryDataset
|
2022-03-15 19:08:47 +03:00
|
|
|
source = defaultSource BigQuery
|
2022-03-10 14:18:13 +03:00
|
|
|
GraphqlEngine.postMetadata_
|
|
|
|
state
|
2022-03-01 01:47:51 +03:00
|
|
|
[yaml|
|
|
|
|
type: bigquery_untrack_table
|
|
|
|
args:
|
2022-03-15 19:08:47 +03:00
|
|
|
source: *source
|
2022-03-01 01:47:51 +03:00
|
|
|
table:
|
2022-03-10 14:18:13 +03:00
|
|
|
dataset: *datasetName
|
2022-03-01 01:47:51 +03:00
|
|
|
name: *tableName
|
|
|
|
|]
|
|
|
|
|
|
|
|
-- | Setup the schema in the most expected way.
|
|
|
|
-- NOTE: Certain test modules may warrant having their own local version.
|
|
|
|
setup :: [Schema.Table] -> (State, ()) -> IO ()
|
|
|
|
setup tables (state, _) = do
|
|
|
|
let dataset = Constants.bigqueryDataset
|
2022-03-15 19:08:47 +03:00
|
|
|
source = defaultSource BigQuery
|
|
|
|
backendType = defaultBackendTypeString BigQuery
|
2022-03-01 01:47:51 +03:00
|
|
|
-- Clear and reconfigure the metadata
|
|
|
|
serviceAccount <- getServiceAccount
|
|
|
|
projectId <- getProjectId
|
|
|
|
GraphqlEngine.postMetadata_
|
|
|
|
state
|
|
|
|
[yaml|
|
|
|
|
type: replace_metadata
|
|
|
|
args:
|
|
|
|
version: 3
|
|
|
|
sources:
|
2022-03-15 19:08:47 +03:00
|
|
|
- name: *source
|
|
|
|
kind: *backendType
|
2022-03-01 01:47:51 +03:00
|
|
|
tables: []
|
|
|
|
configuration:
|
|
|
|
service_account: *serviceAccount
|
|
|
|
project_id: *projectId
|
|
|
|
datasets: [*dataset]
|
|
|
|
|]
|
|
|
|
-- Setup and track tables
|
|
|
|
for_ tables $ \table -> do
|
|
|
|
createTable table
|
|
|
|
insertTable table
|
|
|
|
trackTable state table
|
2022-03-10 14:18:13 +03:00
|
|
|
-- Setup relationships
|
|
|
|
for_ tables $ \table -> do
|
2022-03-15 19:08:47 +03:00
|
|
|
Schema.trackObjectRelationships BigQuery table state
|
|
|
|
Schema.trackArrayRelationships BigQuery table state
|
2022-03-01 01:47:51 +03:00
|
|
|
|
|
|
|
-- | Teardown the schema and tracking in the most expected way.
|
|
|
|
-- NOTE: Certain test modules may warrant having their own version.
|
|
|
|
teardown :: [Schema.Table] -> (State, ()) -> IO ()
|
2022-03-10 14:18:13 +03:00
|
|
|
teardown (reverse -> tables) (state, _) = do
|
|
|
|
-- Teardown relationships first
|
2022-04-04 17:45:12 +03:00
|
|
|
forFinally_ tables $ \table ->
|
2022-03-15 19:08:47 +03:00
|
|
|
Schema.untrackRelationships BigQuery table state
|
2022-03-10 14:18:13 +03:00
|
|
|
-- Then teardown tables
|
2022-04-04 17:45:12 +03:00
|
|
|
forFinally_ tables $ \table -> do
|
2022-03-01 01:47:51 +03:00
|
|
|
untrackTable state table
|
|
|
|
dropTable table
|