2021-10-22 17:49:15 +03:00
|
|
|
module Hasura.RQL.DDL.Schema.Diff
|
|
|
|
( TableMeta (..),
|
|
|
|
FunctionMeta (..),
|
2022-03-08 16:02:13 +03:00
|
|
|
TablesDiff (..),
|
2021-10-22 17:49:15 +03:00
|
|
|
FunctionsDiff (..),
|
|
|
|
ComputedFieldMeta (..),
|
|
|
|
getTablesDiff,
|
|
|
|
processTablesDiff,
|
2022-04-11 14:24:11 +03:00
|
|
|
getIndirectDependenciesFromTableDiff,
|
2021-10-22 17:49:15 +03:00
|
|
|
getFunctionsDiff,
|
|
|
|
getOverloadedFunctions,
|
|
|
|
)
|
|
|
|
where
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
import Control.Lens ((.~))
|
|
|
|
import Data.Aeson
|
|
|
|
import Data.HashMap.Strict qualified as M
|
|
|
|
import Data.HashMap.Strict.InsOrd qualified as OMap
|
|
|
|
import Data.HashSet qualified as HS
|
|
|
|
import Data.List.Extended
|
|
|
|
import Data.List.NonEmpty qualified as NE
|
|
|
|
import Data.Text.Extended
|
|
|
|
import Hasura.Backends.Postgres.SQL.Types hiding (ConstraintName, FunctionName, TableName)
|
|
|
|
import Hasura.Base.Error
|
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.DDL.Schema.Rename
|
|
|
|
import Hasura.RQL.DDL.Schema.Table
|
2022-04-27 16:57:28 +03:00
|
|
|
import Hasura.RQL.Types.Backend
|
|
|
|
import Hasura.RQL.Types.Column
|
|
|
|
import Hasura.RQL.Types.Common
|
|
|
|
import Hasura.RQL.Types.ComputedField
|
|
|
|
import Hasura.RQL.Types.Function
|
|
|
|
import Hasura.RQL.Types.Metadata hiding (fmFunction, tmComputedFields, tmTable)
|
|
|
|
import Hasura.RQL.Types.Metadata.Backend
|
|
|
|
import Hasura.RQL.Types.SchemaCache
|
|
|
|
import Hasura.RQL.Types.SchemaCacheTypes
|
|
|
|
import Hasura.RQL.Types.Table
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.SQL.AnyBackend qualified as AB
|
2022-04-27 16:57:28 +03:00
|
|
|
import Hasura.SQL.Backend
|
2021-09-24 01:56:37 +03:00
|
|
|
import Language.GraphQL.Draft.Syntax qualified as G
|
|
|
|
|
|
|
|
data FunctionMeta b = FunctionMeta
|
2022-08-01 12:32:04 +03:00
|
|
|
{ fmOid :: OID,
|
|
|
|
fmFunction :: FunctionName b,
|
|
|
|
fmType :: FunctionVolatility
|
2021-09-24 01:56:37 +03:00
|
|
|
}
|
|
|
|
deriving (Generic)
|
|
|
|
|
2021-05-27 18:06:13 +03:00
|
|
|
deriving instance (Backend b) => Show (FunctionMeta b)
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2021-05-27 18:06:13 +03:00
|
|
|
deriving instance (Backend b) => Eq (FunctionMeta b)
|
|
|
|
|
|
|
|
instance (Backend b) => FromJSON (FunctionMeta b) where
|
|
|
|
parseJSON = genericParseJSON hasuraJSON
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2021-05-27 18:06:13 +03:00
|
|
|
instance (Backend b) => ToJSON (FunctionMeta b) where
|
|
|
|
toJSON = genericToJSON hasuraJSON
|
2019-10-18 11:29:47 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
data ComputedFieldMeta b = ComputedFieldMeta
|
2022-08-01 12:32:04 +03:00
|
|
|
{ ccmName :: ComputedFieldName,
|
|
|
|
ccmFunctionMeta :: FunctionMeta b
|
2021-09-24 01:56:37 +03:00
|
|
|
}
|
|
|
|
deriving (Generic, Show, Eq)
|
2021-05-27 18:06:13 +03:00
|
|
|
|
|
|
|
instance (Backend b) => FromJSON (ComputedFieldMeta b) where
|
2021-09-24 01:56:37 +03:00
|
|
|
parseJSON = genericParseJSON hasuraJSON {omitNothingFields = True}
|
|
|
|
|
2021-05-27 18:06:13 +03:00
|
|
|
instance (Backend b) => ToJSON (ComputedFieldMeta b) where
|
2021-09-24 01:56:37 +03:00
|
|
|
toJSON = genericToJSON hasuraJSON {omitNothingFields = True}
|
|
|
|
|
|
|
|
data TableMeta (b :: BackendType) = TableMeta
|
2022-08-01 12:32:04 +03:00
|
|
|
{ tmTable :: TableName b,
|
|
|
|
tmInfo :: DBTableMetadata b,
|
|
|
|
tmComputedFields :: [ComputedFieldMeta b]
|
2021-05-27 18:06:13 +03:00
|
|
|
}
|
2021-09-24 01:56:37 +03:00
|
|
|
deriving (Show, Eq)
|
|
|
|
|
|
|
|
data ComputedFieldDiff (b :: BackendType) = ComputedFieldDiff
|
|
|
|
{ _cfdDropped :: [ComputedFieldName],
|
|
|
|
_cfdAltered :: [(ComputedFieldMeta b, ComputedFieldMeta b)],
|
|
|
|
_cfdOverloaded :: [(ComputedFieldName, FunctionName b)]
|
|
|
|
}
|
|
|
|
|
2021-05-27 18:06:13 +03:00
|
|
|
deriving instance (Backend b) => Show (ComputedFieldDiff b)
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2021-05-27 18:06:13 +03:00
|
|
|
deriving instance (Backend b) => Eq (ComputedFieldDiff b)
|
2019-10-18 11:29:47 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
data TableDiff (b :: BackendType) = TableDiff
|
2022-08-01 12:32:04 +03:00
|
|
|
{ _tdNewName :: Maybe (TableName b),
|
|
|
|
_tdDroppedCols :: [Column b],
|
|
|
|
_tdAlteredCols :: [(RawColumnInfo b, RawColumnInfo b)],
|
|
|
|
_tdDroppedFKeyCons :: [ConstraintName b],
|
|
|
|
_tdComputedFields :: ComputedFieldDiff b,
|
2021-09-24 01:56:37 +03:00
|
|
|
-- The final list of uniq/primary constraint names
|
|
|
|
-- used for generating types on_conflict clauses
|
|
|
|
-- TODO: this ideally should't be part of TableDiff
|
2022-08-01 12:32:04 +03:00
|
|
|
_tdUniqOrPriCons :: [ConstraintName b],
|
|
|
|
_tdNewDescription :: Maybe PGDescription
|
2020-10-22 23:42:27 +03:00
|
|
|
}
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
getTableDiff ::
|
|
|
|
Backend b =>
|
|
|
|
TableMeta b ->
|
|
|
|
TableMeta b ->
|
|
|
|
TableDiff b
|
2018-06-27 16:11:32 +03:00
|
|
|
getTableDiff oldtm newtm =
|
2021-09-24 01:56:37 +03:00
|
|
|
TableDiff
|
|
|
|
mNewName
|
|
|
|
droppedCols
|
|
|
|
alteredCols
|
|
|
|
droppedFKeyConstraints
|
|
|
|
computedFieldDiff
|
|
|
|
uniqueOrPrimaryCons
|
|
|
|
mNewDesc
|
2018-06-27 16:11:32 +03:00
|
|
|
where
|
|
|
|
mNewName = bool (Just $ tmTable newtm) Nothing $ tmTable oldtm == tmTable newtm
|
2020-12-08 17:22:31 +03:00
|
|
|
oldCols = _ptmiColumns $ tmInfo oldtm
|
|
|
|
newCols = _ptmiColumns $ tmInfo newtm
|
2019-09-17 04:51:11 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
uniqueOrPrimaryCons =
|
|
|
|
map _cName $
|
|
|
|
maybeToList (_pkConstraint <$> _ptmiPrimaryKey (tmInfo newtm))
|
2022-06-08 02:24:42 +03:00
|
|
|
<> (_ucConstraint <$> toList (_ptmiUniqueConstraints (tmInfo newtm)))
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2020-12-08 17:22:31 +03:00
|
|
|
mNewDesc = _ptmiDescription $ tmInfo newtm
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2022-01-19 11:37:50 +03:00
|
|
|
droppedCols = map rciName $ getDifferenceOn rciPosition oldCols newCols
|
|
|
|
existingCols = getOverlapWith rciPosition oldCols newCols
|
2019-12-09 07:18:53 +03:00
|
|
|
alteredCols = filter (uncurry (/=)) existingCols
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2019-06-03 13:21:55 +03:00
|
|
|
-- foreign keys are considered dropped only if their oid
|
|
|
|
-- and (ref-table, column mapping) are changed
|
2021-09-24 01:56:37 +03:00
|
|
|
droppedFKeyConstraints =
|
|
|
|
map (_cName . _fkConstraint) $
|
|
|
|
HS.toList $
|
|
|
|
droppedFKeysWithOid `HS.intersection` droppedFKeysWithUniq
|
2020-12-17 14:37:16 +03:00
|
|
|
tmForeignKeys = fmap unForeignKeyMetadata . toList . _ptmiForeignKeys . tmInfo
|
2021-09-24 01:56:37 +03:00
|
|
|
droppedFKeysWithOid =
|
|
|
|
HS.fromList $
|
|
|
|
(getDifferenceOn (_cOid . _fkConstraint) `on` tmForeignKeys) oldtm newtm
|
|
|
|
droppedFKeysWithUniq =
|
|
|
|
HS.fromList $
|
|
|
|
(getDifferenceOn mkFKeyUniqId `on` tmForeignKeys) oldtm newtm
|
2019-12-09 07:18:53 +03:00
|
|
|
mkFKeyUniqId (ForeignKey _ reftn colMap) = (reftn, colMap)
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2019-10-18 11:29:47 +03:00
|
|
|
-- calculate computed field diff
|
|
|
|
oldComputedFieldMeta = tmComputedFields oldtm
|
|
|
|
newComputedFieldMeta = tmComputedFields newtm
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
droppedComputedFields =
|
|
|
|
map ccmName $
|
|
|
|
getDifferenceOn (fmOid . ccmFunctionMeta) oldComputedFieldMeta newComputedFieldMeta
|
2019-10-18 11:29:47 +03:00
|
|
|
|
|
|
|
alteredComputedFields =
|
2021-05-27 18:06:13 +03:00
|
|
|
getOverlapWith (fmOid . ccmFunctionMeta) oldComputedFieldMeta newComputedFieldMeta
|
2019-10-18 11:29:47 +03:00
|
|
|
|
|
|
|
overloadedComputedFieldFunctions =
|
|
|
|
let getFunction = fmFunction . ccmFunctionMeta
|
|
|
|
getSecondElement (_ NE.:| list) = listToMaybe list
|
2021-09-24 01:56:37 +03:00
|
|
|
in mapMaybe (fmap ((&&&) ccmName getFunction) . getSecondElement) $
|
|
|
|
flip NE.groupBy newComputedFieldMeta $ \l r ->
|
|
|
|
ccmName l == ccmName r && getFunction l == getFunction r
|
|
|
|
|
|
|
|
computedFieldDiff =
|
|
|
|
ComputedFieldDiff
|
|
|
|
droppedComputedFields
|
|
|
|
alteredComputedFields
|
|
|
|
overloadedComputedFieldFunctions
|
|
|
|
|
|
|
|
getTableChangeDeps ::
|
|
|
|
forall b m.
|
|
|
|
(QErrM m, CacheRM m, Backend b) =>
|
|
|
|
SourceName ->
|
|
|
|
TableName b ->
|
|
|
|
TableDiff b ->
|
|
|
|
m [SchemaObjId]
|
2020-12-28 15:56:00 +03:00
|
|
|
getTableChangeDeps source tn tableDiff = do
|
2018-06-27 16:11:32 +03:00
|
|
|
sc <- askSchemaCache
|
|
|
|
-- for all the dropped columns
|
2021-09-24 01:56:37 +03:00
|
|
|
droppedColDeps <- fmap concat $
|
|
|
|
forM droppedCols $ \droppedCol -> do
|
|
|
|
let objId =
|
|
|
|
SOSourceObj source $
|
|
|
|
AB.mkAnyBackend $
|
|
|
|
SOITableObj @b tn $
|
|
|
|
TOCol @b droppedCol
|
|
|
|
return $ getDependentObjs sc objId
|
2018-06-27 16:11:32 +03:00
|
|
|
-- for all dropped constraints
|
2021-09-24 01:56:37 +03:00
|
|
|
droppedConsDeps <- fmap concat $
|
|
|
|
forM droppedFKeyConstraints $ \droppedCons -> do
|
|
|
|
let objId =
|
|
|
|
SOSourceObj source $
|
|
|
|
AB.mkAnyBackend $
|
|
|
|
SOITableObj @b tn $
|
|
|
|
TOForeignKey @b droppedCons
|
|
|
|
return $ getDependentObjs sc objId
|
2019-10-18 11:29:47 +03:00
|
|
|
return $ droppedConsDeps <> droppedColDeps <> droppedComputedFieldDeps
|
2018-06-27 16:11:32 +03:00
|
|
|
where
|
2021-05-27 18:06:13 +03:00
|
|
|
TableDiff _ droppedCols _ droppedFKeyConstraints computedFieldDiff _ _ = tableDiff
|
2021-03-15 16:02:58 +03:00
|
|
|
droppedComputedFieldDeps =
|
|
|
|
map
|
2021-09-24 01:56:37 +03:00
|
|
|
( SOSourceObj source
|
|
|
|
. AB.mkAnyBackend
|
|
|
|
. SOITableObj @b tn
|
|
|
|
. TOComputedField
|
|
|
|
)
|
2021-03-15 16:02:58 +03:00
|
|
|
$ _cfdDropped computedFieldDiff
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2021-10-22 17:49:15 +03:00
|
|
|
data TablesDiff (b :: BackendType) = TablesDiff
|
2022-08-01 12:32:04 +03:00
|
|
|
{ _sdDroppedTables :: [TableName b],
|
|
|
|
_sdAlteredTables :: [(TableName b, TableDiff b)]
|
2020-10-22 23:42:27 +03:00
|
|
|
}
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2021-10-22 17:49:15 +03:00
|
|
|
getTablesDiff ::
|
|
|
|
(Backend b) => [TableMeta b] -> [TableMeta b] -> TablesDiff b
|
|
|
|
getTablesDiff oldMeta newMeta =
|
|
|
|
TablesDiff droppedTables survivingTables
|
2018-06-27 16:11:32 +03:00
|
|
|
where
|
2021-05-27 18:06:13 +03:00
|
|
|
droppedTables = map tmTable $ getDifferenceOn (_ptmiOid . tmInfo) oldMeta newMeta
|
2018-06-27 16:11:32 +03:00
|
|
|
survivingTables =
|
2021-05-27 18:06:13 +03:00
|
|
|
flip map (getOverlapWith (_ptmiOid . tmInfo) oldMeta newMeta) $ \(oldtm, newtm) ->
|
2021-09-24 01:56:37 +03:00
|
|
|
(tmTable oldtm, getTableDiff oldtm newtm)
|
|
|
|
|
2022-04-11 14:24:11 +03:00
|
|
|
getIndirectDependenciesFromTableDiff ::
|
2021-09-24 01:56:37 +03:00
|
|
|
forall b m.
|
|
|
|
(QErrM m, CacheRM m, Backend b) =>
|
|
|
|
SourceName ->
|
2021-10-22 17:49:15 +03:00
|
|
|
TablesDiff b ->
|
2021-09-24 01:56:37 +03:00
|
|
|
m [SchemaObjId]
|
2022-04-11 14:24:11 +03:00
|
|
|
getIndirectDependenciesFromTableDiff source tablesDiff = do
|
2018-06-27 16:11:32 +03:00
|
|
|
sc <- askSchemaCache
|
2021-07-23 02:06:10 +03:00
|
|
|
let tableIds = SOSourceObj source . AB.mkAnyBackend . SOITable @b <$> droppedTables
|
|
|
|
tableDropDeps = concatMap (getDependentObjs sc) tableIds
|
2020-12-28 15:56:00 +03:00
|
|
|
tableModDeps <- concat <$> traverse (uncurry (getTableChangeDeps source)) alteredTables
|
2021-07-23 02:06:10 +03:00
|
|
|
pure $ filter isIndirectDep $ HS.toList $ HS.fromList $ tableDropDeps <> tableModDeps
|
2018-06-27 16:11:32 +03:00
|
|
|
where
|
2021-10-22 17:49:15 +03:00
|
|
|
TablesDiff droppedTables alteredTables = tablesDiff
|
2021-07-23 02:06:10 +03:00
|
|
|
-- we keep all table objects that are not tied to a deleted table
|
|
|
|
isIndirectDep :: SchemaObjId -> Bool
|
|
|
|
isIndirectDep = \case
|
|
|
|
-- table objects in the same source
|
|
|
|
SOSourceObj s obj
|
2021-09-24 01:56:37 +03:00
|
|
|
| s == source,
|
|
|
|
Just (SOITableObj tn _) <- AB.unpackAnyBackend @b obj ->
|
|
|
|
not $ tn `HS.member` HS.fromList droppedTables
|
2021-07-23 02:06:10 +03:00
|
|
|
-- table objects in any other source
|
2021-09-24 01:56:37 +03:00
|
|
|
SOSourceObj _ obj ->
|
|
|
|
AB.runBackend obj \case
|
|
|
|
SOITableObj {} -> True
|
|
|
|
_ -> False
|
2021-07-23 02:06:10 +03:00
|
|
|
-- any other kind of schema object
|
|
|
|
_ -> False
|
2021-05-27 18:06:13 +03:00
|
|
|
|
2021-10-22 17:49:15 +03:00
|
|
|
data FunctionsDiff b = FunctionsDiff
|
2022-08-01 12:32:04 +03:00
|
|
|
{ fdDropped :: [FunctionName b],
|
|
|
|
fdAltered :: [(FunctionName b, FunctionVolatility)]
|
2021-05-27 18:06:13 +03:00
|
|
|
}
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2021-10-22 17:49:15 +03:00
|
|
|
deriving instance (Backend b) => Show (FunctionsDiff b)
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2021-10-22 17:49:15 +03:00
|
|
|
deriving instance (Backend b) => Eq (FunctionsDiff b)
|
2019-02-05 08:57:03 +03:00
|
|
|
|
2021-10-22 17:49:15 +03:00
|
|
|
getFunctionsDiff :: [FunctionMeta b] -> [FunctionMeta b] -> FunctionsDiff b
|
|
|
|
getFunctionsDiff oldMeta newMeta =
|
|
|
|
FunctionsDiff droppedFuncs alteredFuncs
|
2019-02-05 08:57:03 +03:00
|
|
|
where
|
2021-05-27 18:06:13 +03:00
|
|
|
droppedFuncs = map fmFunction $ getDifferenceOn fmOid oldMeta newMeta
|
|
|
|
alteredFuncs = mapMaybe mkAltered $ getOverlapWith fmOid oldMeta newMeta
|
2019-02-05 08:57:03 +03:00
|
|
|
mkAltered (oldfm, newfm) =
|
|
|
|
let isTypeAltered = fmType oldfm /= fmType newfm
|
2019-11-20 21:21:30 +03:00
|
|
|
alteredFunc = (fmFunction oldfm, fmType newfm)
|
2021-09-24 01:56:37 +03:00
|
|
|
in bool Nothing (Just alteredFunc) isTypeAltered
|
2019-02-14 07:05:18 +03:00
|
|
|
|
2021-10-22 17:49:15 +03:00
|
|
|
getOverloadedFunctions ::
|
2021-09-24 01:56:37 +03:00
|
|
|
(Backend b) => [FunctionName b] -> [FunctionMeta b] -> [FunctionName b]
|
2021-10-22 17:49:15 +03:00
|
|
|
getOverloadedFunctions trackedFuncs newFuncMeta =
|
2020-05-27 18:02:58 +03:00
|
|
|
toList $ duplicates $ map fmFunction trackedMeta
|
2019-02-14 07:05:18 +03:00
|
|
|
where
|
|
|
|
trackedMeta = flip filter newFuncMeta $ \fm ->
|
2019-10-18 11:29:47 +03:00
|
|
|
fmFunction fm `elem` trackedFuncs
|
2021-05-27 18:06:13 +03:00
|
|
|
|
2021-10-22 17:49:15 +03:00
|
|
|
processTablesDiff ::
|
2021-09-24 01:56:37 +03:00
|
|
|
forall b m.
|
|
|
|
( MonadError QErr m,
|
|
|
|
CacheRM m,
|
|
|
|
MonadWriter MetadataModifier m,
|
|
|
|
BackendMetadata b
|
|
|
|
) =>
|
|
|
|
SourceName ->
|
|
|
|
TableCache b ->
|
2021-10-22 17:49:15 +03:00
|
|
|
TablesDiff b ->
|
2021-09-24 01:56:37 +03:00
|
|
|
m ()
|
2021-10-22 17:49:15 +03:00
|
|
|
processTablesDiff source preActionTables tablesDiff = do
|
2021-05-27 18:06:13 +03:00
|
|
|
-- Purge the dropped tables
|
|
|
|
dropTablesInMetadata @b source droppedTables
|
|
|
|
|
|
|
|
for_ alteredTables $ \(oldQtn, tableDiff) -> do
|
2021-09-24 01:56:37 +03:00
|
|
|
ti <-
|
|
|
|
onNothing
|
|
|
|
(M.lookup oldQtn preActionTables)
|
2022-10-01 17:47:12 +03:00
|
|
|
(throw500 $ "old table metadata not found in cache: " <>> oldQtn)
|
2021-05-27 18:06:13 +03:00
|
|
|
alterTableInMetadata source (_tiCoreInfo ti) tableDiff
|
|
|
|
where
|
2021-10-22 17:49:15 +03:00
|
|
|
TablesDiff droppedTables alteredTables = tablesDiff
|
2021-05-27 18:06:13 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
alterTableInMetadata ::
|
|
|
|
forall m b.
|
|
|
|
( MonadError QErr m,
|
|
|
|
CacheRM m,
|
|
|
|
MonadWriter MetadataModifier m,
|
|
|
|
BackendMetadata b
|
|
|
|
) =>
|
|
|
|
SourceName ->
|
|
|
|
TableCoreInfo b ->
|
|
|
|
TableDiff b ->
|
|
|
|
m ()
|
2021-05-27 18:06:13 +03:00
|
|
|
alterTableInMetadata source ti tableDiff = do
|
|
|
|
-- If table rename occurs then don't replace constraints and
|
|
|
|
-- process dropped/added columns, because schema reload happens eventually
|
|
|
|
sc <- askSchemaCache
|
|
|
|
let tn = _tciName ti
|
|
|
|
withOldTabName = do
|
|
|
|
alterColumnsInMetadata source alteredCols tableFields sc tn
|
|
|
|
|
|
|
|
withNewTabName :: TableName b -> m ()
|
|
|
|
withNewTabName newTN = do
|
|
|
|
-- check for GraphQL schema conflicts on new name
|
|
|
|
liftEither (tableGraphQLName @b newTN) >>= checkConflictingNode sc . G.unName
|
|
|
|
alterColumnsInMetadata source alteredCols tableFields sc tn
|
|
|
|
-- update new table in metadata
|
|
|
|
renameTableInMetadata @b source newTN tn
|
|
|
|
|
|
|
|
-- Process computed field diff
|
|
|
|
processComputedFieldDiff tn
|
2022-03-09 09:34:47 +03:00
|
|
|
-- Drop custom column names and comments for dropped columns
|
|
|
|
removeDroppedColumnsFromMetadataField source droppedCols ti
|
2021-05-27 18:06:13 +03:00
|
|
|
maybe withOldTabName withNewTabName mNewName
|
|
|
|
where
|
|
|
|
TableDiff mNewName droppedCols alteredCols _ computedFieldDiff _ _ = tableDiff
|
|
|
|
tableFields = _tciFieldInfoMap ti
|
|
|
|
|
|
|
|
processComputedFieldDiff :: TableName b -> m ()
|
2021-09-24 01:56:37 +03:00
|
|
|
processComputedFieldDiff table = do
|
2021-05-27 18:06:13 +03:00
|
|
|
let ComputedFieldDiff _ altered overloaded = computedFieldDiff
|
|
|
|
getFunction = fmFunction . ccmFunctionMeta
|
|
|
|
forM_ overloaded $ \(columnName, function) ->
|
2021-09-24 01:56:37 +03:00
|
|
|
throw400 NotSupported $
|
|
|
|
"The function " <> function
|
|
|
|
<<> " associated with computed field" <> columnName
|
|
|
|
<<> " of table " <> table
|
|
|
|
<<> " is being overloaded"
|
2021-05-27 18:06:13 +03:00
|
|
|
forM_ altered $ \(old, new) ->
|
2021-09-24 01:56:37 +03:00
|
|
|
if
|
|
|
|
| (fmType . ccmFunctionMeta) new == FTVOLATILE ->
|
|
|
|
throw400 NotSupported $
|
|
|
|
"The type of function " <> getFunction old
|
|
|
|
<<> " associated with computed field " <> ccmName old
|
|
|
|
<<> " of table " <> table
|
|
|
|
<<> " is being altered to \"VOLATILE\""
|
|
|
|
| otherwise -> pure ()
|
|
|
|
|
|
|
|
dropTablesInMetadata ::
|
|
|
|
forall b m.
|
|
|
|
( MonadWriter MetadataModifier m,
|
|
|
|
BackendMetadata b
|
|
|
|
) =>
|
|
|
|
SourceName ->
|
|
|
|
[TableName b] ->
|
|
|
|
m ()
|
2021-05-27 18:06:13 +03:00
|
|
|
dropTablesInMetadata source droppedTables =
|
|
|
|
forM_ droppedTables $
|
2021-09-24 01:56:37 +03:00
|
|
|
\tn -> tell $ MetadataModifier $ metaSources . ix source . toSourceMetadata . (smTables @b) %~ OMap.delete tn
|
|
|
|
|
|
|
|
alterColumnsInMetadata ::
|
|
|
|
forall b m.
|
|
|
|
( MonadError QErr m,
|
|
|
|
CacheRM m,
|
|
|
|
MonadWriter MetadataModifier m,
|
|
|
|
BackendMetadata b
|
|
|
|
) =>
|
|
|
|
SourceName ->
|
|
|
|
[(RawColumnInfo b, RawColumnInfo b)] ->
|
|
|
|
FieldInfoMap (FieldInfo b) ->
|
|
|
|
SchemaCache ->
|
|
|
|
TableName b ->
|
|
|
|
m ()
|
|
|
|
alterColumnsInMetadata source alteredCols fields sc tn =
|
|
|
|
for_ alteredCols $
|
2022-01-19 11:37:50 +03:00
|
|
|
\( RawColumnInfo {rciName = oldName, rciType = oldType},
|
|
|
|
RawColumnInfo {rciName = newName, rciType = newType}
|
2021-09-24 01:56:37 +03:00
|
|
|
) -> do
|
|
|
|
if
|
|
|
|
| oldName /= newName ->
|
|
|
|
renameColumnInMetadata oldName newName source tn fields
|
|
|
|
| oldType /= newType -> do
|
|
|
|
let colId =
|
|
|
|
SOSourceObj source $
|
|
|
|
AB.mkAnyBackend $
|
|
|
|
SOITableObj @b tn $
|
|
|
|
TOCol @b oldName
|
|
|
|
typeDepObjs = getDependentObjsWith (== DROnType) sc colId
|
|
|
|
|
|
|
|
unless (null typeDepObjs) $
|
|
|
|
throw400 DependencyError $
|
|
|
|
"cannot change type of column " <> oldName <<> " in table "
|
2022-10-01 17:47:12 +03:00
|
|
|
<> tn <<> " because of the following dependencies: "
|
2021-09-24 01:56:37 +03:00
|
|
|
<> reportSchemaObjs typeDepObjs
|
|
|
|
| otherwise -> pure ()
|
|
|
|
|
2022-03-09 09:34:47 +03:00
|
|
|
removeDroppedColumnsFromMetadataField ::
|
2021-09-24 01:56:37 +03:00
|
|
|
forall b m.
|
|
|
|
(MonadWriter MetadataModifier m, BackendMetadata b) =>
|
|
|
|
SourceName ->
|
|
|
|
[Column b] ->
|
|
|
|
TableCoreInfo b ->
|
|
|
|
m ()
|
2022-03-09 09:34:47 +03:00
|
|
|
removeDroppedColumnsFromMetadataField source droppedCols tableInfo = do
|
|
|
|
when (newColumnConfig /= originalColumnConfig) $
|
2021-09-24 01:56:37 +03:00
|
|
|
tell $
|
|
|
|
MetadataModifier $
|
2022-03-09 09:34:47 +03:00
|
|
|
tableMetadataSetter @b source tableName . tmConfiguration .~ newTableConfig
|
|
|
|
where
|
|
|
|
tableName = _tciName tableInfo
|
|
|
|
originalTableConfig = _tciCustomConfig tableInfo
|
|
|
|
originalColumnConfig = _tcColumnConfig originalTableConfig
|
|
|
|
newColumnConfig = foldl' (flip M.delete) originalColumnConfig droppedCols
|
|
|
|
newTableConfig = originalTableConfig & tcColumnConfig .~ newColumnConfig
|