Skip to content

armgit / zulip

Public Imported from GitHub
0

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.md
index 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 actions
in the service's web UI.
diff --git a/scripts/setup/create-db.sql b/scripts/setup/create-db.sql
index 0d8a26dccbb..73cd675b527 100644
--- a/scripts/setup/create-db.sql
+++ b/scripts/setup/create-db.sql
@@ -11,7 +11,6 @@ EXCEPTION WHEN duplicate_object THEN
RAISE NOTICE 'user already exists';
END$$, :'dbuser') AS code \gset
DO :'code';
-ALTER ROLE :"dbuser" SET search_path TO zulip,public;
CREATE DATABASE :"dbname"
OWNER=:dbuser
ENCODING=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-db
index c3500da1c6d..c715da6675a 100755
--- a/tools/setup/postgresql-init-dev-db
+++ b/tools/setup/postgresql-init-dev-db
@@ -45,7 +45,6 @@ fi
VAGRANTUSERNAME=$(whoami)
-SEARCH_PATH=zulip,public
PASSWORD=$("$(dirname "$0")/../../scripts/get-django-setting" LOCAL_DATABASE_PASSWORD)
DBNAME_BASE=${DBNAME}_base
@@ -72,7 +71,6 @@ EXCEPTION WHEN duplicate_object THEN
END\$\$;
ALTER USER $USERNAME PASSWORD '$PASSWORD';
ALTER USER $USERNAME CREATEDB;
-ALTER ROLE $USERNAME SET search_path TO $SEARCH_PATH;
DO \$\$BEGIN
CREATE USER "$VAGRANTUSERNAME";
@@ -80,7 +78,7 @@ EXCEPTION WHEN duplicate_object THEN
RAISE 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;
EOF
umask go-rw
@@ -102,10 +100,6 @@ DROP DATABASE IF EXISTS $DBNAME_BASE;
CREATE DATABASE $DBNAME_BASE
EOF
-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" <<EOF
CREATE EXTENSION pgroonga;
diff --git a/zerver/apps.py b/zerver/apps.py
index 1ca1d55afce..4fc22bfe2a6 100644
--- a/zerver/apps.py
+++ b/zerver/apps.py
@@ -6,7 +6,8 @@ from django.apps import AppConfig
from django.conf import settings
from django.core.cache import cache
from 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_migrate
from typing_extensions import override
from 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.py
index 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.py
index 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=[