server: rename source

GitOrigin-RevId: 5f4a17941bfd447deb9663a4c250f149238c5f53
This commit is contained in:
Vladimir Ciobanu 2021-05-24 16:13:08 +03:00 committed by hasura-bot
parent 31465c5bba
commit 0f8f4764c7
8 changed files with 144 additions and 1 deletions

View File

@ -10,6 +10,7 @@
### Bug fixes and improvements
- server: add `rename_source` metadata API (fix #6681)
- server: fix subscriptions with session argument in user-defined function (fix #6657)
- server: MSSQL: Support ORDER BY for text/ntext types.
- server: MSSQL: Support _lt, _eq, etc. for text/ntext types.

View File

@ -131,6 +131,52 @@ Args syntax
- Boolean
- When set to ``true``, the effect (if possible) is cascaded to any metadata dependent objects (relationships, permissions etc.) from other sources (default: ``false``)
.. _rename_source:
rename_source
-------------
``rename_source`` is used to rename an existing source.
Given there already exists a database with name ``pg1``, we can rename it to ``pg2`` using:
.. code-block:: http
POST /v1/metadata HTTP/1.1
Content-Type: application/json
X-Hasura-Role: admin
{
"type": "rename_source",
"args": {
"name": "pg1",
"new_name": "pg2"
}
}
Note that all settings are kept, only the name is changed.
.. _rename_source_syntax:
Args syntax
^^^^^^^^^^^
.. list-table::
:header-rows: 1
* - Key
- Required
- Schema
- Description
* - name
- true
- :ref:`SourceName <SourceName>`
- Name of the database
* - new_name
- true
- :ref:`SourceName <SourceName>`
- Name of the database
mssql_add_source
----------------

View File

@ -57,6 +57,45 @@ runAddSource (AddSource name sourceConfig replaceConfiguration) = do
buildSchemaCacheFor (MOSource name) metadataModifier
pure successMsg
runRenameSource
:: forall m
. (MonadError QErr m, CacheRWM m, MetadataM m)
=> RenameSource
-> m EncJSON
runRenameSource RenameSource {..} = do
sources <- scSources <$> askSchemaCache
unless (HM.member _rmName sources) $
throw400 NotExists $ "Could not find source with name " <>> _rmName
when (HM.member _rmNewName sources) $
throw400 AlreadyExists $ "Source with name " <> _rmNewName <<> " already exists"
let metadataModifier =
MetadataModifier
$ metaSources %~ renameBackendSourceMetadata _rmName _rmNewName
buildSchemaCacheFor (MOSource _rmNewName) metadataModifier
pure successMsg
where
renameBackendSourceMetadata
:: SourceName
-> SourceName
-> OMap.InsOrdHashMap SourceName BackendSourceMetadata
-> OMap.InsOrdHashMap SourceName BackendSourceMetadata
renameBackendSourceMetadata oldKey newKey m =
case OMap.lookup oldKey m of
Just val ->
OMap.insert
newKey
(AB.mapBackend val (renameSource newKey))
. OMap.delete oldKey
$ m
Nothing -> m
renameSource :: forall b. SourceName -> SourceMetadata b -> SourceMetadata b
renameSource newName metadata = metadata { _smName = newName }
runDropSource
:: forall m. (MonadError QErr m, CacheRWM m, MonadIO m, MonadBaseControl IO m, MetadataM m)
=> DropSource -> m EncJSON

View File

@ -121,6 +121,19 @@ instance (Backend b) => FromJSON (AddSource b) where
<*> o .: "configuration"
<*> o .:? "replace_configuration" .!= False
data RenameSource
= RenameSource
{ _rmName :: !SourceName
, _rmNewName :: !SourceName
} deriving stock (Generic, Show, Eq)
instance ToJSON RenameSource where
toJSON = genericToJSON hasuraJSON
instance FromJSON RenameSource where
parseJSON = withObject "Object" $ \o ->
RenameSource <$> o .: "name" <*> o .: "new_name"
data DropSource
= DropSource
{ _dsName :: !SourceName

View File

@ -172,6 +172,8 @@ data RQLMetadataV1
| RMBigqueryDropDeletePermission !(DropPerm 'BigQuery (DelPerm 'BigQuery))
| RMBigquerySetPermissionComment !(SetPermComment 'BigQuery)
| RMRenameSource !RenameSource
-- Inconsistent metadata
| RMGetInconsistentMetadata !GetInconsistentMetadata
| RMDropInconsistentMetadata !DropInconsistentMetadata
@ -412,6 +414,7 @@ runMetadataQueryV1M
-> m EncJSON
runMetadataQueryV1M env currentResourceVersion = \case
RMPgAddSource q -> runAddSource q
RMRenameSource q -> runRenameSource q
RMPgDropSource q -> runDropSource q
RMPgTrackTable q -> runTrackTableV2Q q

View File

@ -56,4 +56,4 @@
query:
type: pg_drop_source
args:
name: pg1
name: pg1

View File

@ -0,0 +1,38 @@
- description: PG add source
url: /v1/metadata
status: 200
response:
message: success
query:
type: pg_add_source
args:
name: pg1
configuration:
connection_info:
database_url:
from_env: HASURA_GRAPHQL_PG_SOURCE_URL_1
pool_settings:
max_connections: 50
idle_timeout: 180
retries: 1
- description: PG rename source
url: /v1/metadata
status: 200
response:
message: success
query:
type: rename_source
args:
name: pg1
new_name: pg_renamed_1
- description: PG Drop Source
url: /v1/metadata
status: 200
response:
message: success
query:
type: pg_drop_source
args:
name: pg_renamed_1

View File

@ -49,6 +49,9 @@ class TestMetadata:
def test_pg_track_table_source(self, hge_ctx):
check_query_f(hge_ctx, self.dir() + '/pg_track_table_source.yaml')
def test_rename_source(self, hge_ctx):
check_query_f(hge_ctx, self.dir() + '/rename_source.yaml')
def test_pg_multisource_query(self, hge_ctx):
check_query_f(hge_ctx, self.dir() + '/pg_multisource_query.yaml')