2021-02-14 09:07:52 +03:00
|
|
|
module Hasura.Backends.Postgres.DDL.Source
|
2021-02-23 20:37:27 +03:00
|
|
|
( resolveSourceConfig
|
|
|
|
, postDropSourceHook
|
|
|
|
, resolveDatabaseMetadata
|
|
|
|
) where
|
2021-02-14 09:07:52 +03:00
|
|
|
|
2021-04-22 00:44:37 +03:00
|
|
|
import Hasura.Prelude
|
|
|
|
|
2021-02-14 09:07:52 +03:00
|
|
|
import qualified Data.HashMap.Strict as Map
|
|
|
|
import qualified Database.PG.Query as Q
|
2021-02-22 17:59:13 +03:00
|
|
|
import qualified Language.Haskell.TH.Lib as TH
|
|
|
|
import qualified Language.Haskell.TH.Syntax as TH
|
2021-02-14 09:07:52 +03:00
|
|
|
|
|
|
|
import Control.Monad.Trans.Control (MonadBaseControl)
|
2021-03-16 20:35:35 +03:00
|
|
|
import Data.FileEmbed (makeRelativeToProject)
|
2021-02-14 09:07:52 +03:00
|
|
|
|
|
|
|
import Hasura.Backends.Postgres.Connection
|
|
|
|
import Hasura.Backends.Postgres.SQL.Types
|
2021-04-22 00:44:37 +03:00
|
|
|
import Hasura.RQL.Types.Backend
|
2021-02-14 09:07:52 +03:00
|
|
|
import Hasura.RQL.Types.Common
|
|
|
|
import Hasura.RQL.Types.Error
|
|
|
|
import Hasura.RQL.Types.Function
|
|
|
|
import Hasura.RQL.Types.Source
|
|
|
|
import Hasura.RQL.Types.Table
|
|
|
|
import Hasura.SQL.Backend
|
2021-02-22 17:59:13 +03:00
|
|
|
import Hasura.Server.Migrate.Internal
|
2021-04-21 13:55:18 +03:00
|
|
|
import Hasura.Server.Types (MaintenanceMode (..))
|
2021-02-14 09:07:52 +03:00
|
|
|
|
2021-04-22 00:44:37 +03:00
|
|
|
|
2021-02-22 10:52:42 +03:00
|
|
|
resolveSourceConfig
|
|
|
|
:: (MonadIO m, MonadResolveSource m)
|
2021-04-22 00:44:37 +03:00
|
|
|
=> SourceName -> PostgresConnConfiguration -> m (Either QErr (SourceConfig ('Postgres pgKind)))
|
2021-02-22 10:52:42 +03:00
|
|
|
resolveSourceConfig name config = runExceptT do
|
2021-02-14 09:07:52 +03:00
|
|
|
sourceResolver <- getSourceResolver
|
2021-02-22 10:52:42 +03:00
|
|
|
liftEitherM $ liftIO $ sourceResolver name config
|
2021-02-14 09:07:52 +03:00
|
|
|
|
2021-02-22 10:52:42 +03:00
|
|
|
resolveDatabaseMetadata
|
2021-04-22 00:44:37 +03:00
|
|
|
:: forall pgKind m
|
|
|
|
. (Backend ('Postgres pgKind), MonadIO m, MonadBaseControl IO m)
|
|
|
|
=> SourceConfig ('Postgres pgKind) -> MaintenanceMode -> m (Either QErr (ResolvedSource ('Postgres pgKind)))
|
2021-04-21 13:55:18 +03:00
|
|
|
resolveDatabaseMetadata sourceConfig maintenanceMode = runExceptT do
|
2021-02-14 09:07:52 +03:00
|
|
|
(tablesMeta, functionsMeta, pgScalars) <- runLazyTx (_pscExecCtx sourceConfig) Q.ReadWrite $ do
|
2021-04-21 13:55:18 +03:00
|
|
|
initSource maintenanceMode
|
2021-02-14 09:07:52 +03:00
|
|
|
tablesMeta <- fetchTableMetadata
|
|
|
|
functionsMeta <- fetchFunctionMetadata
|
|
|
|
pgScalars <- fetchPgScalars
|
|
|
|
pure (tablesMeta, functionsMeta, pgScalars)
|
|
|
|
pure $ ResolvedSource sourceConfig tablesMeta functionsMeta pgScalars
|
|
|
|
|
2021-04-21 13:55:18 +03:00
|
|
|
initSource :: MonadTx m => MaintenanceMode -> m ()
|
|
|
|
initSource maintenanceMode = do
|
2021-02-14 09:07:52 +03:00
|
|
|
hdbCatalogExist <- doesSchemaExist "hdb_catalog"
|
|
|
|
eventLogTableExist <- doesTableExist "hdb_catalog" "event_log"
|
|
|
|
sourceVersionTableExist <- doesTableExist "hdb_catalog" "hdb_source_catalog_version"
|
2021-04-21 13:55:18 +03:00
|
|
|
-- when maintenance mode is enabled, don't perform any migrations
|
|
|
|
if | maintenanceMode == MaintenanceModeEnabled -> pure ()
|
2021-02-14 09:07:52 +03:00
|
|
|
-- Fresh database
|
2021-04-21 13:55:18 +03:00
|
|
|
| not hdbCatalogExist -> liftTx do
|
2021-02-22 17:59:13 +03:00
|
|
|
Q.unitQE defaultTxErrorHandler "CREATE SCHEMA hdb_catalog" () False
|
|
|
|
enablePgcryptoExtension
|
|
|
|
initPgSourceCatalog
|
2021-02-14 09:07:52 +03:00
|
|
|
-- Only 'hdb_catalog' schema defined
|
|
|
|
| not sourceVersionTableExist && not eventLogTableExist ->
|
2021-02-22 17:59:13 +03:00
|
|
|
liftTx initPgSourceCatalog
|
2021-02-14 09:07:52 +03:00
|
|
|
-- Source is initialised by pre multisource support servers
|
2021-02-22 17:59:13 +03:00
|
|
|
| not sourceVersionTableExist && eventLogTableExist -> do
|
|
|
|
-- Update the Source Catalog to v43 to include the new migration
|
|
|
|
-- changes. Skipping this step will result in errors.
|
2021-04-21 13:55:18 +03:00
|
|
|
currCatalogVersion <- liftTx getCatalogVersion
|
2021-02-22 17:59:13 +03:00
|
|
|
migrateTo43 currCatalogVersion
|
|
|
|
liftTx createVersionTable
|
2021-02-14 09:07:52 +03:00
|
|
|
| otherwise -> migrateSourceCatalog
|
|
|
|
where
|
|
|
|
initPgSourceCatalog = do
|
2021-03-16 20:35:35 +03:00
|
|
|
() <- Q.multiQE defaultTxErrorHandler $(makeRelativeToProject "src-rsr/init_pg_source.sql" >>= Q.sqlFromFile)
|
2021-02-14 09:07:52 +03:00
|
|
|
setSourceCatalogVersion
|
|
|
|
|
|
|
|
createVersionTable = do
|
|
|
|
() <- Q.multiQE defaultTxErrorHandler
|
|
|
|
[Q.sql|
|
|
|
|
CREATE TABLE hdb_catalog.hdb_source_catalog_version(
|
|
|
|
version TEXT NOT NULL,
|
|
|
|
upgraded_on TIMESTAMPTZ NOT NULL
|
|
|
|
);
|
|
|
|
|
|
|
|
CREATE UNIQUE INDEX hdb_source_catalog_version_one_row
|
|
|
|
ON hdb_catalog.hdb_source_catalog_version((version IS NOT NULL));
|
|
|
|
|]
|
|
|
|
setSourceCatalogVersion
|
|
|
|
|
|
|
|
migrateSourceCatalog = do
|
|
|
|
version <- getSourceCatalogVersion
|
|
|
|
case version of
|
|
|
|
"1" -> pure ()
|
|
|
|
_ -> throw500 $ "unexpected source catalog version: " <> version
|
|
|
|
|
2021-02-22 17:59:13 +03:00
|
|
|
migrateTo43 prevVersion = do
|
|
|
|
let neededMigrations = dropWhile ((/= prevVersion) . fst) upMigrationsUntil43
|
|
|
|
traverse_ snd neededMigrations
|
|
|
|
|
|
|
|
-- Upgrade the hdb_catalog schema to v43
|
|
|
|
upMigrationsUntil43 :: MonadTx m => [(Text, m ())]
|
|
|
|
upMigrationsUntil43 =
|
|
|
|
$(let migrationFromFile from to =
|
|
|
|
let path = "src-rsr/migrations/" <> from <> "_to_" <> to <> ".sql"
|
2021-03-16 20:35:35 +03:00
|
|
|
in [| runTx $(makeRelativeToProject path >>= Q.sqlFromFile) |]
|
2021-02-22 17:59:13 +03:00
|
|
|
|
|
|
|
migrationsFromFile = map $ \(to :: Integer) ->
|
|
|
|
let from = to - 1
|
|
|
|
in [| ( $(TH.lift $ tshow from)
|
|
|
|
, $(migrationFromFile (show from) (show to))
|
|
|
|
) |]
|
|
|
|
in TH.listE
|
|
|
|
-- version 0.8 is the only non-integral catalog version
|
|
|
|
$ [| ("0.8", $(migrationFromFile "08" "1")) |]
|
|
|
|
: migrationsFromFile [2..3]
|
|
|
|
++ [| ("3", from3To4) |]
|
|
|
|
: migrationsFromFile [5..43]
|
|
|
|
)
|
|
|
|
|
2021-02-14 09:07:52 +03:00
|
|
|
currentSourceCatalogVersion :: Text
|
|
|
|
currentSourceCatalogVersion = "1"
|
|
|
|
|
|
|
|
setSourceCatalogVersion :: MonadTx m => m ()
|
|
|
|
setSourceCatalogVersion = liftTx $ Q.unitQE defaultTxErrorHandler [Q.sql|
|
|
|
|
INSERT INTO hdb_catalog.hdb_source_catalog_version(version, upgraded_on)
|
|
|
|
VALUES ($1, NOW())
|
|
|
|
ON CONFLICT ((version IS NOT NULL))
|
|
|
|
DO UPDATE SET version = $1, upgraded_on = NOW()
|
|
|
|
|] (Identity currentSourceCatalogVersion) False
|
|
|
|
|
|
|
|
getSourceCatalogVersion :: MonadTx m => m Text
|
|
|
|
getSourceCatalogVersion = liftTx $ runIdentity . Q.getRow <$> Q.withQE defaultTxErrorHandler
|
|
|
|
[Q.sql| SELECT version FROM hdb_catalog.hdb_source_catalog_version |] () False
|
|
|
|
|
|
|
|
-- | Fetch Postgres metadata of all user tables
|
2021-04-22 00:44:37 +03:00
|
|
|
fetchTableMetadata
|
|
|
|
:: forall pgKind m
|
|
|
|
. (Backend ('Postgres pgKind), MonadTx m)
|
|
|
|
=> m (DBTablesMetadata ('Postgres pgKind))
|
2021-02-14 09:07:52 +03:00
|
|
|
fetchTableMetadata = do
|
|
|
|
results <- liftTx $ Q.withQE defaultTxErrorHandler
|
2021-03-16 20:35:35 +03:00
|
|
|
$(makeRelativeToProject "src-rsr/pg_table_metadata.sql" >>= Q.sqlFromFile) () True
|
2021-02-14 09:07:52 +03:00
|
|
|
pure $ Map.fromList $ flip map results $
|
|
|
|
\(schema, table, Q.AltJ info) -> (QualifiedObject schema table, info)
|
|
|
|
|
|
|
|
-- | Fetch Postgres metadata for all user functions
|
2021-04-22 00:44:37 +03:00
|
|
|
fetchFunctionMetadata :: (MonadTx m) => m (DBFunctionsMetadata ('Postgres pgKind))
|
2021-02-14 09:07:52 +03:00
|
|
|
fetchFunctionMetadata = do
|
|
|
|
results <- liftTx $ Q.withQE defaultTxErrorHandler
|
2021-03-16 20:35:35 +03:00
|
|
|
$(makeRelativeToProject "src-rsr/pg_function_metadata.sql" >>= Q.sqlFromFile) () True
|
2021-02-14 09:07:52 +03:00
|
|
|
pure $ Map.fromList $ flip map results $
|
|
|
|
\(schema, table, Q.AltJ infos) -> (QualifiedObject schema table, infos)
|
|
|
|
|
|
|
|
-- | Fetch all scalar types from Postgres
|
|
|
|
fetchPgScalars :: MonadTx m => m (HashSet PGScalarType)
|
|
|
|
fetchPgScalars =
|
|
|
|
liftTx $ Q.getAltJ . runIdentity . Q.getRow
|
|
|
|
<$> Q.withQE defaultTxErrorHandler
|
|
|
|
[Q.sql|
|
|
|
|
SELECT coalesce(json_agg(typname), '[]')
|
|
|
|
FROM pg_catalog.pg_type where typtype = 'b'
|
|
|
|
|] () True
|
2021-02-23 20:37:27 +03:00
|
|
|
|
|
|
|
-- | Clean source database after dropping in metadata
|
|
|
|
postDropSourceHook
|
|
|
|
:: (MonadIO m, MonadError QErr m, MonadBaseControl IO m)
|
|
|
|
=> PGSourceConfig -> m ()
|
|
|
|
postDropSourceHook sourceConfig = do
|
|
|
|
-- Clean traces of Hasura in source database
|
|
|
|
liftEitherM $ runPgSourceWriteTx sourceConfig $ do
|
|
|
|
hdbMetadataTableExist <- doesTableExist "hdb_catalog" "hdb_metadata"
|
|
|
|
eventLogTableExist <- doesTableExist "hdb_catalog" "event_log"
|
|
|
|
-- If "hdb_metadata" and "event_log" tables found in the "hdb_catalog" schema
|
|
|
|
-- then this infers the source is being used as default potgres source (--database-url option).
|
|
|
|
-- In this case don't drop any thing in the catalog schema.
|
|
|
|
if | hdbMetadataTableExist && eventLogTableExist -> pure ()
|
|
|
|
-- Otherwise, if only "hdb_metadata" table exist, then this infers the source is
|
|
|
|
-- being used as metadata storage (--metadata-database-url option). In this case
|
|
|
|
-- drop only source related tables and not "hdb_catalog" schema
|
|
|
|
| hdbMetadataTableExist ->
|
2021-03-16 20:35:35 +03:00
|
|
|
Q.multiQE defaultTxErrorHandler $(makeRelativeToProject "src-rsr/drop_pg_source.sql" >>= Q.sqlFromFile)
|
2021-02-23 20:37:27 +03:00
|
|
|
-- Otherwise, drop "hdb_catalog" schema.
|
|
|
|
| otherwise -> dropHdbCatalogSchema
|
|
|
|
|
|
|
|
-- Destory postgres source connection
|
|
|
|
liftIO $ _pecDestroyConn $ _pscExecCtx sourceConfig
|