From b50a8e25a4f109d18786b286ca3e124bc4ab046b Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Tue, 28 May 2024 09:58:03 +0100 Subject: [PATCH 1/4] Change allow_unsafe_locale to also apply on new databases --- docs/postgres.md | 11 +++++------ synapse/storage/engines/postgres.py | 10 ++++++++-- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/docs/postgres.md b/docs/postgres.md index ae34f7689b..6bc390db1b 100644 --- a/docs/postgres.md +++ b/docs/postgres.md @@ -242,12 +242,11 @@ host all all ::1/128 ident ### Fixing incorrect `COLLATE` or `CTYPE` -Synapse will refuse to set up a new database if it has the wrong values of -`COLLATE` and `CTYPE` set. Synapse will also refuse to start an existing database with incorrect values -of `COLLATE` and `CTYPE` unless the config flag `allow_unsafe_locale`, found in the -`database` section of the config, is set to true. Using different locales can cause issues if the locale library is updated from -underneath the database, or if a different version of the locale is used on any -replicas. +Synapse will also refuse to start when using a database with incorrect values of +`COLLATE` and `CTYPE` unless the config flag `allow_unsafe_locale`, found in the +`database` section of the config, is set to true. Using different locales can +cause issues if the locale library is updated from underneath the database, or +if a different version of the locale is used on any replicas. If you have a database with an unsafe locale, the safest way to fix the issue is to dump the database and recreate it with the correct locale parameter (as shown above). It is also possible to change the diff --git a/synapse/storage/engines/postgres.py b/synapse/storage/engines/postgres.py index b9168ee074..edd1f6d8be 100644 --- a/synapse/storage/engines/postgres.py +++ b/synapse/storage/engines/postgres.py @@ -142,6 +142,10 @@ def check_new_database(self, txn: Cursor) -> None: apply stricter checks on new databases versus existing database. """ + allow_unsafe_locale = self.config.get("allow_unsafe_locale", False) + if allow_unsafe_locale: + return + collation, ctype = self.get_db_locale(txn) errors = [] @@ -154,8 +158,10 @@ def check_new_database(self, txn: Cursor) -> None: if errors: raise IncorrectDatabaseSetup( - "Database is incorrectly configured:\n\n%s\n\n" - "See docs/postgres.md for more information." % ("\n".join(errors)) + "Database has incorrect ctype of %r. Should be 'C'\n" + "See docs/postgres.md for more information. You can override this check by" + "setting 'allow_unsafe_locale' to true in the database config.", + ctype, ) def convert_param_style(self, sql: str) -> str: From 11f6e79e9e910c38a3d109cc75b2a364a6354f88 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Tue, 28 May 2024 09:59:24 +0100 Subject: [PATCH 2/4] Newsfile --- changelog.d/17238.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/17238.misc diff --git a/changelog.d/17238.misc b/changelog.d/17238.misc new file mode 100644 index 0000000000..261467e55c --- /dev/null +++ b/changelog.d/17238.misc @@ -0,0 +1 @@ +Change the `allow_unsafe_locale` config option to also apply when setting up new databases. From fea697303465164a725a345572b170221990599e Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Wed, 29 May 2024 10:40:48 +0100 Subject: [PATCH 3/4] Update docs/postgres.md Co-authored-by: Till <2353100+S7evinK@users.noreply.github.com> --- docs/postgres.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/postgres.md b/docs/postgres.md index 6bc390db1b..4b2ba38275 100644 --- a/docs/postgres.md +++ b/docs/postgres.md @@ -242,7 +242,7 @@ host all all ::1/128 ident ### Fixing incorrect `COLLATE` or `CTYPE` -Synapse will also refuse to start when using a database with incorrect values of +Synapse will refuse to start when using a database with incorrect values of `COLLATE` and `CTYPE` unless the config flag `allow_unsafe_locale`, found in the `database` section of the config, is set to true. Using different locales can cause issues if the locale library is updated from underneath the database, or From 2bf7a89fef35d5e6bc26c03ab70d9a4278bfbdc7 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Wed, 29 May 2024 10:43:29 +0100 Subject: [PATCH 4/4] Better error message --- synapse/storage/engines/postgres.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/synapse/storage/engines/postgres.py b/synapse/storage/engines/postgres.py index edd1f6d8be..90641d5a18 100644 --- a/synapse/storage/engines/postgres.py +++ b/synapse/storage/engines/postgres.py @@ -158,10 +158,10 @@ def check_new_database(self, txn: Cursor) -> None: if errors: raise IncorrectDatabaseSetup( - "Database has incorrect ctype of %r. Should be 'C'\n" + "Database is incorrectly configured:\n\n%s\n\n" "See docs/postgres.md for more information. You can override this check by" "setting 'allow_unsafe_locale' to true in the database config.", - ctype, + "\n".join(errors), ) def convert_param_style(self, sql: str) -> str: