Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
migrations: Move schema creation and search_path into pre_migrate hook.
Alex Vandiver13243be3e8f8
diff --git a/docs/production/postgresql.md b/docs/production/postgresql.mdindex 2d86b034145..981e1f01b41 100644--- a/docs/production/postgresql.md+++ b/docs/production/postgresql.md@@ -47,9 +47,6 @@ run the commands in `scripts/setup/create-db.sql` to:- Create a database called `zulip` with `C.UTF-8` collation.- Create a user called `zulip` with full rights on that database.-- Log in with the `zulip` user to create a schema called `zulip` in the `zulip`- database. You might have to grant `create` privileges first for the `zulip`- user to do this.If you cannot run that SQL directly, you should perform the equivalent actionsin the service's web UI.diff --git a/scripts/setup/create-db.sql b/scripts/setup/create-db.sqlindex 0d8a26dccbb..73cd675b527 100644--- a/scripts/setup/create-db.sql+++ b/scripts/setup/create-db.sql@@ -11,7 +11,6 @@ EXCEPTION WHEN duplicate_object THENRAISE NOTICE 'user already exists';END$$, :'dbuser') AS code \gsetDO :'code';-ALTER ROLE :"dbuser" SET search_path TO zulip,public;CREATE DATABASE :"dbname"OWNER=:dbuserENCODING=UTF8@@ -19,4 +18,3 @@ CREATE DATABASE :"dbname"LC_CTYPE='C.UTF-8'TEMPLATE=template0;\connect :"dbname"-CREATE SCHEMA zulip AUTHORIZATION :"dbuser";diff --git a/tools/setup/postgresql-init-dev-db b/tools/setup/postgresql-init-dev-dbindex c3500da1c6d..c715da6675a 100755--- a/tools/setup/postgresql-init-dev-db+++ b/tools/setup/postgresql-init-dev-db@@ -45,7 +45,6 @@ fiVAGRANTUSERNAME=$(whoami)-SEARCH_PATH=zulip,publicPASSWORD=$("$(dirname "$0")/../../scripts/get-django-setting" LOCAL_DATABASE_PASSWORD)DBNAME_BASE=${DBNAME}_base@@ -72,7 +71,6 @@ EXCEPTION WHEN duplicate_object THENEND\$\$;ALTER USER $USERNAME PASSWORD '$PASSWORD';ALTER USER $USERNAME CREATEDB;-ALTER ROLE $USERNAME SET search_path TO $SEARCH_PATH;DO \$\$BEGINCREATE USER "$VAGRANTUSERNAME";@@ -80,7 +78,7 @@ EXCEPTION WHEN duplicate_object THENRAISE NOTICE '$VAGRANTUSERNAME user already exists';END\$\$;GRANT $USERNAME TO "$VAGRANTUSERNAME";-ALTER ROLE "$VAGRANTUSERNAME" SET search_path TO $SEARCH_PATH;+ALTER ROLE "$VAGRANTUSERNAME" SET search_path TO zulip,public;EOFumask go-rw@@ -102,10 +100,6 @@ DROP DATABASE IF EXISTS $DBNAME_BASE;CREATE DATABASE $DBNAME_BASEEOF-psql -v ON_ERROR_STOP=1 -e -h localhost "$DBNAME_BASE" "$USERNAME" <<EOF-CREATE SCHEMA zulip;-EOF-"${ROOT_POSTGRES[@]}" -v ON_ERROR_STOP=1 -e "$DBNAME_BASE" <<EOFCREATE EXTENSION pgroonga;diff --git a/zerver/apps.py b/zerver/apps.pyindex 1ca1d55afce..4fc22bfe2a6 100644--- a/zerver/apps.py+++ b/zerver/apps.py@@ -6,7 +6,8 @@ from django.apps import AppConfigfrom django.conf import settingsfrom django.core.cache import cachefrom django.core.checks import register-from django.db.models.signals import post_migrate+from django.db import connection+from django.db.models.signals import post_migrate, pre_migratefrom typing_extensions import overridefrom zerver.checks import check_auth_settings, check_external_host_setting, check_required_settings@@ -17,6 +18,17 @@ def flush_cache(sender: AppConfig | None, **kwargs: Any) -> None:cache.clear()+def create_zulip_schema(sender: AppConfig, **kwargs: Any) -> None: # nocoverage+ # We do this here, and not in a zerver migration, because we+ # cannot be sure that zerver migrations run _first_, of all of the+ # Django apps. Updating `SET search_path` is just for the current+ # connection; the user's default search_path is set in the+ # zerver/0001 migrations.+ with connection.cursor() as cursor:+ cursor.execute("CREATE SCHEMA IF NOT EXISTS zulip")+ cursor.execute("SET search_path = zulip,public")++class ZerverConfig(AppConfig):name: str = "zerver"@@ -45,3 +57,5 @@ class ZerverConfig(AppConfig):if settings.POST_MIGRATION_CACHE_FLUSHING:post_migrate.connect(flush_cache, sender=self)++ pre_migrate.connect(create_zulip_schema)diff --git a/zerver/migrations/0001_initial.py b/zerver/migrations/0001_initial.pyindex e6c5009b467..b00a3a7b697 100644--- a/zerver/migrations/0001_initial.py+++ b/zerver/migrations/0001_initial.py@@ -86,6 +86,7 @@ class Migration(migrations.Migration):]operations = [+ migrations.RunSQL(sql="ALTER ROLE CURRENT_USER SET search_path TO zulip,public"),migrations.CreateModel(name="Recipient",fields=[diff --git a/zerver/migrations/0001_squashed_0569.py b/zerver/migrations/0001_squashed_0569.pyindex 08ec3233065..a87eae71ab9 100644--- a/zerver/migrations/0001_squashed_0569.py+++ b/zerver/migrations/0001_squashed_0569.py@@ -678,6 +678,7 @@ class Migration(migrations.Migration):]operations = [+ migrations.RunSQL(sql="ALTER ROLE CURRENT_USER SET search_path TO zulip,public"),migrations.CreateModel(name="ArchiveTransaction",fields=[