这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions server/src-lib/Hasura/RQL/DDL/RemoteRelationship.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ module Hasura.RQL.DDL.RemoteRelationship
( runCreateRemoteRelationship
, runDeleteRemoteRelationship
, runUpdateRemoteRelationship
, updateRemoteRelInCatalog
, persistRemoteRelationship
, resolveRemoteRelationship
, delRemoteRelFromCatalog
, getRemoteRelDefFromCatalog
) where

import Hasura.EncJSON
Expand Down Expand Up @@ -115,3 +117,13 @@ delRemoteRelFromCatalog (QualifiedObject sn tn) (RemoteRelationshipName relName)
AND table_name = $2
AND remote_relationship_name = $3
|] (sn, tn, relName) True

getRemoteRelDefFromCatalog :: RemoteRelationshipName -> QualifiedTable -> Q.TxE QErr RemoteRelationshipDef
getRemoteRelDefFromCatalog relName (QualifiedObject schemaName tableName) = do
(Q.AltJ defn) <- (runIdentity . Q.getRow) <$> Q.withQE defaultTxErrorHandler
[Q.sql|
SELECT definition::json
FROM hdb_catalog.hdb_remote_relationship
WHERE remote_relationship_name = $1 and table_schema = $2 and table_name = $3
|] (relName,schemaName,tableName) False
return defn
60 changes: 56 additions & 4 deletions server/src-lib/Hasura/RQL/DDL/Schema/Rename.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{-# LANGUAGE ViewPatterns #-}

-- | Functions for mutating the catalog (with integrity checking) to incorporate schema changes
-- discovered after applying a user-supplied SQL query. None of these functions modify the schema
-- cache, so it must be reloaded after the catalog is updated.
Expand All @@ -20,10 +22,13 @@ import Hasura.Session
import Hasura.SQL.Types

import qualified Hasura.RQL.DDL.EventTrigger as DS
import qualified Hasura.RQL.DDL.RemoteRelationship as RR

import qualified Data.HashMap.Strict as M
import qualified Database.PG.Query as Q

import qualified Data.Set as Set
import qualified Data.List.NonEmpty as NE
import qualified Language.GraphQL.Draft.Syntax as G
import Data.Aeson

data RenameItem a
Expand Down Expand Up @@ -57,6 +62,7 @@ renameTableInCatalog
renameTableInCatalog newQT oldQT = do
sc <- askSchemaCache
let allDeps = getDependentObjs sc $ SOTable oldQT

-- update all dependant schema objects
forM_ allDeps $ \case
SOTableObj refQT (TORel rn) ->
Expand All @@ -65,9 +71,13 @@ renameTableInCatalog newQT oldQT = do
updatePermFlds refQT rn pt $ RTable (oldQT, newQT)
-- A trigger's definition is not dependent on the table directly
SOTableObj _ (TOTrigger _) -> return ()
-- A remote relationship's definition is not dependent on the table directly
SOTableObj _ (TORemoteRel _) -> return ()

d -> otherDeps errMsg d
-- -- Update table name in hdb_catalog
-- Update table name in hdb_catalog
liftTx $ Q.catchE defaultTxErrorHandler updateTableInCatalog

where
QualifiedObject nsn ntn = newQT
QualifiedObject osn otn = oldQT
Expand Down Expand Up @@ -97,6 +107,8 @@ renameColInCatalog oCol nCol qt fieldInfo = do
updateColInRel refQT rn $ RenameItem qt oCol nCol
SOTableObj _ (TOTrigger triggerName) ->
updateColInEventTriggerDef triggerName $ RenameItem qt oCol nCol
SOTableObj _ (TORemoteRel remoteRelName) ->
updateColInRemoteRelationship remoteRelName $ RenameItem qt oCol nCol
d -> otherDeps errMsg d
-- Update custom column names
possiblyUpdateCustomColumnNames qt oCol nCol
Expand Down Expand Up @@ -135,7 +147,6 @@ renameRelInCatalog qt oldRN newRN = do
AND rel_name = $4
|] (newRN, sn, tn, oldRN) True


-- update table names in relationship definition
updateRelDefs
:: (MonadTx m, CacheRM m)
Expand All @@ -147,7 +158,6 @@ updateRelDefs qt rn renameTable = do
ObjRel -> updateObjRelDef qt rn renameTable
ArrRel -> updateArrRelDef qt rn renameTable


updateObjRelDef
:: (MonadTx m)
=> QualifiedTable -> RelName -> RenameTable -> m ()
Expand Down Expand Up @@ -348,6 +358,48 @@ updateColInRel fromQT rn rnCol = do
updateColInArrRel fromQT toQT rnCol <$> decodeValue oldDefV
liftTx $ updateRel fromQT rn newDefV

updateColInRemoteRelationship
:: (MonadTx m)
=> RemoteRelationshipName -> RenameCol -> m ()
updateColInRemoteRelationship remoteRelationshipName renameCol = do
let (RenameItem qt oldCol newCol) = renameCol
(RemoteRelationshipDef remoteSchemaName hasuraFlds remoteFields) <-
liftTx $ RR.getRemoteRelDefFromCatalog remoteRelationshipName qt
let oldColPGTxt = getPGColTxt oldCol
newColPGTxt = getPGColTxt newCol
oldColFieldName = FieldName $ oldColPGTxt
newColFieldName = FieldName $ newColPGTxt
modifiedHasuraFlds = Set.insert newColFieldName $ Set.delete oldColFieldName hasuraFlds
fieldCalls = unRemoteFields remoteFields
oldColName = G.Name $ oldColPGTxt
newColName = G.Name $ newColPGTxt
modifiedFieldCalls = NE.map (\(FieldCall name args) ->
let remoteArgs = getRemoteArguments args
in FieldCall name $ RemoteArguments $
map (\(G.ObjectFieldG key val) ->
G.ObjectFieldG key $ replaceVariableName oldColName newColName val
) $ remoteArgs
) $ fieldCalls
liftTx $ RR.updateRemoteRelInCatalog (RemoteRelationship remoteRelationshipName qt modifiedHasuraFlds remoteSchemaName (RemoteFields modifiedFieldCalls))
where
replaceVariableName :: G.Name -> G.Name -> G.Value -> G.Value
replaceVariableName oldColName newColName = \case
G.VVariable (G.Variable oldColName') ->
G.VVariable $
if oldColName == oldColName'
then (G.Variable newColName)
else (G.Variable oldColName')
G.VList (G.unListValue -> values) -> G.VList $ G.ListValueG $ map (replaceVariableName oldColName newColName) values
G.VObject (G.unObjectValue -> values) ->
G.VObject $ G.ObjectValueG $
map (\(G.ObjectFieldG key val) -> G.ObjectFieldG key $ replaceVariableName oldColName newColName val) values
G.VInt i -> G.VInt i
G.VFloat f -> G.VFloat f
G.VBoolean b -> G.VBoolean b
G.VNull -> G.VNull
G.VString s -> G.VString s
G.VEnum e -> G.VEnum e

-- rename columns in relationship definitions
updateColInEventTriggerDef
:: (MonadTx m)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
- description: Rename the column of a table which is used in creating the remote relationship
url: /v1/query
status: 200
query:
type: run_sql
args:
sql: ALTER TABLE profiles rename column id to profile_id

- description: Check if the remote relationship works after renaming the column
url: /v1/graphql
status: 200
response:
data:
profiles:
- profile_id: 1
messagesNestedArgs:
- id: 1
msg: You win!
- profile_id: 2
messagesNestedArgs: []
- profile_id: 3
messagesNestedArgs:
- id: 3
msg: Another alice
query:
query: |
query {
profiles {
profile_id
messagesNestedArgs(where: { name: { eq: "alice" } }) {
id
msg
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
- description: Rename the table which is used in creating the remote relationship
url: /v1/query
status: 200
query:
type: run_sql
args:
sql: ALTER TABLE profiles rename to user_profiles

- description: Check if the remote relationship works after renaming the table
url: /v1/graphql
status: 200
response:
data:
user_profiles:
- id: 1
messagesNestedArgs:
- id: 1
msg: You win!
- id: 2
messagesNestedArgs: []
- id: 3
messagesNestedArgs:
- id: 3
msg: Another alice
query:
query: |
query {
user_profiles {
id
messagesNestedArgs(where: { name: { eq: "alice" } }) {
id
msg
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ args:
- type: run_sql
args:
sql: |
drop table profiles
drop table if exists profiles

- type: run_sql
args:
sql: |
drop table if exists user_profiles

# also drops remote relationship as direct dep
- type: remove_remote_schema
args:
Expand Down
10 changes: 10 additions & 0 deletions server/tests-py/test_remote_relationships.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,16 @@ def test_with_scalar_relationship(self, hge_ctx):
assert st_code == 200, resp
check_query_f(hge_ctx, self.dir() + 'query_with_scalar_rel.yaml')

def test_renaming_column_with_remote_relationship_dependency(self, hge_ctx):
st_code, resp = hge_ctx.v1q_f(self.dir() + 'setup_remote_rel_nested_args.yaml')
assert st_code == 200, resp
check_query_f(hge_ctx, self.dir() + 'rename_col_with_remote_rel_dependency.yaml')

def test_renaming_table_with_remote_relationship_dependency(self, hge_ctx):
st_code, resp = hge_ctx.v1q_f(self.dir() + 'setup_remote_rel_nested_args.yaml')
assert st_code == 200, resp
check_query_f(hge_ctx, self.dir() + 'rename_table_with_remote_rel_dependency.yaml')


class TestDeepExecution:

Expand Down