diff --git a/scripts/lib/upgrade-zulip-stage-2 b/scripts/lib/upgrade-zulip-stage-2 index 7969e54852..91903e6bd8 100755 --- a/scripts/lib/upgrade-zulip-stage-2 +++ b/scripts/lib/upgrade-zulip-stage-2 @@ -32,6 +32,7 @@ from scripts.lib.zulip_tools import ( get_config_file, get_tornado_ports, parse_os_release, + run_psql_as_postgres, su_to_zulip, ) @@ -137,13 +138,8 @@ if os.path.exists("/etc/init.d/postgresql"): if glob.glob("/usr/share/postgresql/*/extension/tsearch_extras.control"): # Remove legacy tsearch_extras package references - subprocess.check_call( - [ - "su", - "postgres", - "-c", - 'psql -v ON_ERROR_STOP=1 zulip -c "DROP EXTENSION IF EXISTS tsearch_extras;"', - ] + run_psql_as_postgres( + sql_query="DROP EXTENSION IF EXISTS tsearch_extras;", ) subprocess.check_call(["apt-get", "remove", "-y", "postgresql-*-tsearch-extras"]) diff --git a/scripts/lib/zulip_tools.py b/scripts/lib/zulip_tools.py index b096b91b3d..9cb75886b4 100755 --- a/scripts/lib/zulip_tools.py +++ b/scripts/lib/zulip_tools.py @@ -580,6 +580,26 @@ def get_deploy_options(config_file: configparser.RawConfigParser) -> List[str]: return get_config(config_file, "deployment", "deploy_options", "").strip().split() +def run_psql_as_postgres( + sql_query: str, +) -> None: + subcmd = " ".join( + map( + shlex.quote, + [ + "psql", + "-v", + "ON_ERROR_STOP=1", + # TODO: Stop hardcoding the database name. + "zulip", + "-c", + sql_query, + ], + ) + ) + subprocess.check_call(["su", "postgres", "-c", subcmd]) + + def get_tornado_ports(config_file: configparser.RawConfigParser) -> List[int]: ports = [] if config_file.has_section("tornado_sharding"):