这是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
10 changes: 10 additions & 0 deletions server/src-lib/Hasura/RQL/DDL/Schema/Function.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{- |
Description: Create/delete SQL functions to/from Hasura metadata.
-}

module Hasura.RQL.DDL.Schema.Function where

import Hasura.EncJSON
Expand Down Expand Up @@ -108,13 +112,19 @@ newtype TrackFunction
{ tfName :: QualifiedFunction}
deriving (Show, Eq, FromJSON, ToJSON, Lift)

-- | Track function, Phase 1:
-- Validate function tracking operation. Fails if function is already being
-- tracked, or if a table with the same name is being tracked.
trackFunctionP1
:: (CacheRM m, UserInfoM m, QErrM m) => TrackFunction -> m ()
trackFunctionP1 (TrackFunction qf) = do
adminOnly
rawSchemaCache <- askSchemaCache
when (M.member qf $ scFunctions rawSchemaCache) $
throw400 AlreadyTracked $ "function already tracked : " <>> qf
let qt = fmap (TableName . getFunctionTxt) qf
when (M.member qt $ scTables rawSchemaCache) $
throw400 NotSupported $ "table with name " <> qf <<> " already exists"

trackFunctionP2Setup :: (QErrM m, CacheRWM m, MonadTx m)
=> QualifiedFunction -> RawFuncInfo -> m ()
Expand Down
10 changes: 10 additions & 0 deletions server/src-lib/Hasura/RQL/DDL/Schema/Table.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{- |
Description: Create/delete SQL tables to/from Hasura metadata.
-}

{-# LANGUAGE TypeApplications #-}

module Hasura.RQL.DDL.Schema.Table where
Expand Down Expand Up @@ -56,13 +60,19 @@ newtype TrackTable
{ tName :: QualifiedTable }
deriving (Show, Eq, FromJSON, ToJSON, Lift)

-- | Track table/view, Phase 1:
-- Validate table tracking operation. Fails if table is already being tracked,
-- or if a function with the same name is being tracked.
trackExistingTableOrViewP1
:: (CacheRM m, UserInfoM m, QErrM m) => TrackTable -> m ()
trackExistingTableOrViewP1 (TrackTable vn) = do
adminOnly
rawSchemaCache <- askSchemaCache
when (M.member vn $ scTables rawSchemaCache) $
throw400 AlreadyTracked $ "view/table already tracked : " <>> vn
let qf = fmap (FunctionName . getTableTxt) vn
when (M.member qf $ scFunctions rawSchemaCache) $
throw400 NotSupported $ "function with name " <> vn <<> " already exists"

trackExistingTableOrViewP2
:: (QErrM m, CacheRWM m, MonadTx m)
Expand Down
2 changes: 1 addition & 1 deletion server/src-lib/Hasura/SQL/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ data QualifiedObject a
= QualifiedObject
{ qSchema :: !SchemaName
, qName :: !a
} deriving (Show, Eq, Ord, Generic, Lift)
} deriving (Show, Eq, Functor, Ord, Generic, Lift)

instance (FromJSON a) => FromJSON (QualifiedObject a) where
parseJSON v@(String _) =
Expand Down
48 changes: 31 additions & 17 deletions server/tests-py/queries/v1/track_table/setup.yaml
Original file line number Diff line number Diff line change
@@ -1,30 +1,44 @@
type: bulk
args:

#Author table
- type: run_sql
args:
sql: |
create table author(
id serial primary key,
name text unique,
remarks text
CREATE TABLE author (
id SERIAL PRIMARY KEY,
name TEXT UNIQUE,
remarks TEXT
);
create table article(
id serial primary key,
title text,
author_id integer references author(id)

CREATE TABLE article (
id SERIAL PRIMARY KEY,
title TEXT,
author_id INTEGER REFERENCES author(id)
);

CREATE TABLE test1 (
id SERIAL PRIMARY KEY
);

CREATE TABLE test2 (
id SERIAL PRIMARY KEY
);

CREATE TABLE hge_tests.resident (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
age INTEGER NOT NULL
);
insert into author (name)
values
('Author 1'),
('Author 2');
insert into hge_tests.resident (name,age)
values
('Resident 1', 23),
('Resident 2', 31);

INSERT INTO author (name) VALUES
('Author 1'),
('Author 2');

INSERT INTO hge_tests.resident (name, age) VALUES
('Resident 1', 23),
('Resident 2', 31);

CREATE OR REPLACE FUNCTION test1()
RETURNS SETOF test2 AS $$
SELECT * FROM test2
$$ LANGUAGE sql STABLE;
9 changes: 6 additions & 3 deletions server/tests-py/queries/v1/track_table/teardown.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ args:
- type: run_sql
args:
sql: |
drop table hge_tests.resident;
drop table article;
drop table author;
DROP TABLE hge_tests.resident;
DROP TABLE article;
DROP TABLE author;
DROP TABLE test1;
DROP TABLE test2 CASCADE;
cascade: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Track function and table with same name: error

# Track "test2" table in order to track the return type of the "test1" function
- description: Track table
url: /v1/query
status: 200
response:
message: success
query:
type: track_table
args:
name: test2

# Track the "test1" function
- description: Track function
url: /v1/query
status: 200
response:
message: success
query:
type: track_function
args:
name: test1

# Track the "test1" table - this fails because a function with the same name is already being tracked
- description: Track table
url: /v1/query
status: 400
response:
code: not-supported
error: function with name "test1" already exists
path: $.args
query:
type: track_table
args:
name: test1
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Track table and function with same name: error
- description: Track table
url: /v1/query
status: 200
response:
message: success
query:
type: track_table
args:
name: test1

- description: Track function
url: /v1/query
status: 400
response:
code: not-supported
error: table with name "test1" already exists
path: $.args
query:
type: track_function
args:
name: test1
6 changes: 6 additions & 0 deletions server/tests-py/test_v1_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,12 @@ def dir(cls):

class TestTrackTables(DefaultTestQueries):

def test_track_table_function_same_name(self, hge_ctx):
check_query_f(hge_ctx, self.dir() + '/track_table_function_same_name.yaml')

def test_track_function_table_same_name(self, hge_ctx):
check_query_f(hge_ctx, self.dir() + '/track_function_table_same_name.yaml')

def test_track_untrack_table(self, hge_ctx):
check_query_f(hge_ctx, self.dir() + '/track_untrack_table.yaml')
hge_ctx.may_skip_test_teardown = True
Expand Down