Skip to content

Commit f8b07cc

Browse files
committed
Support "DROP FUNCTION" in migrations
1 parent 3ad5686 commit f8b07cc

File tree

7 files changed

+20
-21
lines changed

7 files changed

+20
-21
lines changed

IHP/IDE/CodeGen/MigrationGenerator.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ diffSchemas targetSchema' actualSchema' = (drop <> create)
146146
Just constraintName -> Just DropConstraint { tableName, constraintName }
147147
Nothing -> Nothing
148148
toDropStatement CreatePolicy { tableName, name } = Just DropPolicy { tableName, policyName = name }
149+
toDropStatement CreateFunction { functionName } = Just DropFunction { functionName }
149150
toDropStatement otherwise = Nothing
150151

151152
removeNoise = filter \case

IHP/IDE/SchemaDesigner/Compiler.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ compileStatement AddValueToEnumType { enumName, newValue } = "ALTER TYPE " <> co
5050
compileStatement CreateTrigger { name, eventWhen, event, tableName, for, whenCondition, functionName, arguments } = "CREATE TRIGGER " <> compileIdentifier name <> " " <> compileTriggerEventWhen eventWhen <> " " <> compileTriggerEvent event <> " ON " <> compileIdentifier tableName <> " " <> compileTriggerFor for <> " EXECUTE FUNCTION " <> compileExpression (CallExpression functionName arguments) <> ";"
5151
compileStatement Begin = "BEGIN;"
5252
compileStatement Commit = "COMMIT;"
53+
compileStatement DropFunction { functionName } = "DROP FUNCTION " <> compileIdentifier functionName <> ";"
5354
compileStatement UnknownStatement { raw } = raw <> ";"
5455

5556
-- | Emit a PRIMARY KEY constraint when there are multiple primary key columns

IHP/IDE/SchemaDesigner/Parser.hs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ statement = do
6767
let alter = do
6868
lexeme "ALTER"
6969
alterTable <|> alterType <|> alterSequence
70-
s <- setStatement <|> create <|> alter <|> selectStatement <|> try dropTable <|> try dropIndex <|> try dropPolicy <|> dropType <|> commentStatement <|> comment <|> begin <|> commit
70+
s <- setStatement <|> create <|> alter <|> selectStatement <|> try dropTable <|> try dropIndex <|> try dropPolicy <|> try dropFunction <|> dropType <|> commentStatement <|> comment <|> begin <|> commit
7171
space
7272
pure s
7373

@@ -826,6 +826,13 @@ dropType = do
826826
char ';'
827827
pure DropEnumType { name }
828828

829+
dropFunction = do
830+
lexeme "DROP"
831+
lexeme "FUNCTION"
832+
functionName <- qualifiedIdentifier
833+
char ';'
834+
pure DropFunction { functionName }
835+
829836
dropIndex = do
830837
lexeme "DROP"
831838
lexeme "INDEX"

IHP/IDE/SchemaDesigner/Types.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ data Statement
6868
| Begin
6969
-- | COMMIT;
7070
| Commit
71+
| DropFunction { functionName :: !Text }
7172
deriving (Eq, Show)
7273

7374
data DeferrableType

Test/IDE/CodeGeneration/MigrationGenerator.hs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -530,15 +530,6 @@ tests = do
530530
COMMENT ON EXTENSION "uuid-ossp" IS 'generate universally unique identifiers (UUIDs)';
531531

532532

533-
--
534-
-- Name: ihp_user_id(); Type: FUNCTION; Schema: public; Owner: -
535-
--
536-
537-
CREATE FUNCTION public.ihp_user_id() RETURNS uuid
538-
LANGUAGE sql
539-
AS $$ SELECT current_setting('rls.ihp_user_id')::uuid; $$;
540-
541-
542533
SET default_tablespace = '';
543534

544535
SET default_table_access_method = heap;
@@ -631,16 +622,6 @@ tests = do
631622

632623
COMMENT ON EXTENSION "uuid-ossp" IS 'generate universally unique identifiers (UUIDs)';
633624

634-
635-
--
636-
-- Name: ihp_user_id(); Type: FUNCTION; Schema: public; Owner: -
637-
--
638-
639-
CREATE FUNCTION public.ihp_user_id() RETURNS uuid
640-
LANGUAGE sql
641-
AS $$ SELECT current_setting('rls.ihp_user_id')::uuid; $$;
642-
643-
644625
SET default_tablespace = '';
645626

646627
SET default_table_access_method = heap;
@@ -932,7 +913,7 @@ tests = do
932913
NEW.updated_at = NOW();
933914
RETURN NEW;
934915
END;
935-
$$$$ language PLPGSQL;
916+
$$$$ language plpgsql;
936917
|]
937918
let actualSchema = sql [trimming|
938919
--

Test/IDE/SchemaDesigner/CompilerSpec.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,3 +1017,7 @@ tests = do
10171017
}
10181018
]
10191019
compileSql statements `shouldBe` sql
1020+
it "should compile 'DROP FUNCTION ..;' statements" do
1021+
let sql = "DROP FUNCTION my_function;\n"
1022+
let statements = [ DropFunction { functionName = "my_function" } ]
1023+
compileSql statements `shouldBe` sql

Test/IDE/SchemaDesigner/ParserSpec.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,6 +1097,10 @@ COMMENT ON EXTENSION "uuid-ossp" IS 'generate universally unique identifiers (UU
10971097
it "should parse 'COMMIT' statements" do
10981098
let sql = cs [plain|COMMIT;|]
10991099
parseSql sql `shouldBe` Commit
1100+
1101+
it "should parse 'DROP FUNCTION ..' statements" do
1102+
let sql = cs [plain|DROP FUNCTION my_function;|]
1103+
parseSql sql `shouldBe` DropFunction { functionName = "my_function" }
11001104

11011105
col :: Column
11021106
col = Column

0 commit comments

Comments
 (0)