graphql-engine/server/src-lib/Hasura/Backends/BigQuery/Schema/Introspection.hs
Tom Harding 3cef692dd7 feature(server): Get _all_ source tables
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/8649
GitOrigin-RevId: 4bb9311d1d7ab4e8ee641bde5df2babcb1b8b306
2023-04-05 20:16:14 +00:00

58 lines
2.3 KiB
Haskell

{-# LANGUAGE QuasiQuotes #-}
module Hasura.Backends.BigQuery.Schema.Introspection
( listAllTables,
)
where
import Data.Aeson (toJSON)
import Data.HashMap.Strict.InsOrd qualified as InsOrdHashMap
import Data.String.Interpolate (i)
import Data.Text.Lazy qualified as LT
import Hasura.Backends.BigQuery.Execute (BigQuery (..), OutputValue (..), RecordSet (..), ShowDetails (..))
import Hasura.Backends.BigQuery.Execute qualified as Execute
import Hasura.Backends.BigQuery.Source (BigQueryDataset (..), BigQuerySourceConfig (..))
import Hasura.Backends.BigQuery.Types (TableName (..))
import Hasura.Base.Error (QErr, throw500, throw500WithDetail)
import Hasura.Prelude
import Hasura.RQL.Types.Common (SourceName)
import Hasura.RQL.Types.Metadata (MetadataM)
import Hasura.RQL.Types.SchemaCache (CacheRM, askSourceConfig)
import Hasura.SQL.Backend qualified as Backend
-- | List all tables, tracked or untracked, on a given BigQuery source. All
-- given datasets' tables will be included.
listAllTables :: (CacheRM m, MetadataM m, MonadError QErr m, MonadIO m) => env -> SourceName -> m [TableName]
listAllTables _ sourceName = do
sourceConfig <- askSourceConfig @'Backend.BigQuery sourceName
let queryPerDataset :: BigQueryDataset -> LT.Text
queryPerDataset (BigQueryDataset dataset) =
[i|
select table_name, table_schema
FROM #{dataset}.INFORMATION_SCHEMA.TABLES
|]
query :: LT.Text
query =
LT.intercalate "union all" $
map queryPerDataset $
_scDatasets sourceConfig
(_, recordSet) <-
Execute.streamBigQuery (_scConnection sourceConfig) (BigQuery query mempty)
`onLeftM` \err -> throw500WithDetail (Execute.executeProblemMessage HideDetails err) (toJSON err)
results <- for (toList (rows recordSet)) \row -> do
tableName <- case InsOrdHashMap.lookup "table_name" row of
Just (TextOutputValue tableName) -> pure tableName
_ -> throw500 "Unexpected BigQuery introspection result (table_name)"
tableNameSchema <- case InsOrdHashMap.lookup "table_schema" row of
Just (TextOutputValue tableNameSchema) -> pure tableNameSchema
_ -> throw500 "Unexpected BigQuery introspection result (table_schema)"
pure (tableName, tableNameSchema)
pure [TableName {..} | (tableName, tableNameSchema) <- results]