From 8f11ef80c5458f0d12eefa1c6fbba81af8bb3042 Mon Sep 17 00:00:00 2001
From: Tomas Vondra <tomas.vondra@postgresql.org>
Date: Mon, 16 Dec 2024 16:20:04 +0100
Subject: [PATCH] psql: Tab completion for ALTER TYPE ... CASCADE/RESTRICT

Updates table completion for ALTER TYPE to offer CASCADE/RESTRICT for a
number of actions on attributes:

    ALTER TYPE ... ADD/DROP/RENAME ATTRIBUTE ... [CASCADE|RESTRICT]
    ALTER TYPE ... TYPE ... [CASCADE|RESTRICT]

Author: Kirill Reshke
Reviewed-By: Karina Litskevich
Discussion: https://postgr.es/m/CALdSSPhVELkvutquqrDB=Ujfq_Pjz=6jn-kzh+291KPNViLTfw@mail.gmail.com
---
 src/bin/psql/tab-complete.in.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/src/bin/psql/tab-complete.in.c b/src/bin/psql/tab-complete.in.c
index d2c7f94000..79b0204717 100644
--- a/src/bin/psql/tab-complete.in.c
+++ b/src/bin/psql/tab-complete.in.c
@@ -2992,6 +2992,9 @@ match_previous_words(int pattern_id,
 	/* ALTER TYPE xxx RENAME (ATTRIBUTE|VALUE) yyy */
 	else if (Matches("ALTER", "TYPE", MatchAny, "RENAME", "ATTRIBUTE|VALUE", MatchAny))
 		COMPLETE_WITH("TO");
+	/* ALTER TYPE xxx RENAME ATTRIBUTE yyy TO zzz */
+	else if (Matches("ALTER", "TYPE", MatchAny, "RENAME", "ATTRIBUTE", MatchAny, "TO", MatchAny))
+		COMPLETE_WITH("CASCADE", "RESTRICT");
 
 	/*
 	 * If we have ALTER TYPE <sth> ALTER/DROP/RENAME ATTRIBUTE, provide list
@@ -3002,9 +3005,18 @@ match_previous_words(int pattern_id,
 	/* complete ALTER TYPE ADD ATTRIBUTE <foo> with list of types */
 	else if (Matches("ALTER", "TYPE", MatchAny, "ADD", "ATTRIBUTE", MatchAny))
 		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
+	/* complete ALTER TYPE ADD ATTRIBUTE <foo> <footype> with CASCADE/RESTRICT */
+	else if (Matches("ALTER", "TYPE", MatchAny, "ADD", "ATTRIBUTE", MatchAny, MatchAny))
+		COMPLETE_WITH("CASCADE", "RESTRICT");
+	/* complete ALTER TYPE DROP ATTRIBUTE <foo> with CASCADE/RESTRICT */
+	else if (Matches("ALTER", "TYPE", MatchAny, "DROP", "ATTRIBUTE", MatchAny))
+		COMPLETE_WITH("CASCADE", "RESTRICT");
 	/* ALTER TYPE ALTER ATTRIBUTE <foo> */
 	else if (Matches("ALTER", "TYPE", MatchAny, "ALTER", "ATTRIBUTE", MatchAny))
 		COMPLETE_WITH("TYPE");
+	/* ALTER TYPE ALTER ATTRIBUTE <foo> TYPE <footype> */
+	else if (Matches("ALTER", "TYPE", MatchAny, "ALTER", "ATTRIBUTE", MatchAny, "TYPE", MatchAny))
+		COMPLETE_WITH("CASCADE", "RESTRICT");
 	/* complete ALTER TYPE <sth> RENAME VALUE with list of enum values */
 	else if (Matches("ALTER", "TYPE", MatchAny, "RENAME", "VALUE"))
 		COMPLETE_WITH_ENUM_VALUE(prev3_wd);
-- 
2.39.5

