From 3ccc5184e0fe1f00dec69293c097d513da54a410 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Fri, 31 Oct 2025 13:16:47 +0000 Subject: [PATCH] Fix schema lint script to understand `CREATE TABLE IF NOT EXISTS` (#19020) The schema lint tries to make sure we don't add or remove indices in schema files (rather than as background updates), *unless* the table was created in the same schema file. The regex to pull out the `CREATE TABLE` SQL incorrectly didn't recognise `IF NOT EXISTS`. There is a test delta file that shows that we accept different types of `CREATE TABLE` and `CREATE INDEX` statements, as well as an index creation that doesn't have a matching create table (to show that we do still catch it). The test delta should be removed before merge. --- changelog.d/19020.misc | 1 + scripts-dev/check_schema_delta.py | 20 +++++++++++++++----- 2 files changed, 16 insertions(+), 5 deletions(-) create mode 100644 changelog.d/19020.misc diff --git a/changelog.d/19020.misc b/changelog.d/19020.misc new file mode 100644 index 0000000000..f5775ff194 --- /dev/null +++ b/changelog.d/19020.misc @@ -0,0 +1 @@ +Fix CI linter for schema delta files to correctly handle all types of `CREATE TABLE` syntax. diff --git a/scripts-dev/check_schema_delta.py b/scripts-dev/check_schema_delta.py index 7b2dec25d4..dd96c904bb 100755 --- a/scripts-dev/check_schema_delta.py +++ b/scripts-dev/check_schema_delta.py @@ -11,9 +11,13 @@ import click import git SCHEMA_FILE_REGEX = re.compile(r"^synapse/storage/schema/(.*)/delta/(.*)/(.*)$") -INDEX_CREATION_REGEX = re.compile(r"CREATE .*INDEX .*ON ([a-z_]+)", flags=re.IGNORECASE) -INDEX_DELETION_REGEX = re.compile(r"DROP .*INDEX ([a-z_]+)", flags=re.IGNORECASE) -TABLE_CREATION_REGEX = re.compile(r"CREATE .*TABLE ([a-z_]+)", flags=re.IGNORECASE) +INDEX_CREATION_REGEX = re.compile( + r"CREATE .*INDEX .*ON ([a-z_0-9]+)", flags=re.IGNORECASE +) +INDEX_DELETION_REGEX = re.compile(r"DROP .*INDEX ([a-z_0-9]+)", flags=re.IGNORECASE) +TABLE_CREATION_REGEX = re.compile( + r"CREATE .*TABLE.* ([a-z_0-9]+)\s*\(", flags=re.IGNORECASE +) # The base branch we want to check against. We use the main development branch # on the assumption that is what we are developing against. @@ -173,11 +177,14 @@ def main(force_colors: bool) -> None: clause = match.group() click.secho( - f"Found delta with index deletion: '{clause}' in {delta_file}\nThese should be in background updates.", + f"Found delta with index deletion: '{clause}' in {delta_file}", fg="red", bold=True, color=force_colors, ) + click.secho( + " ↪ These should be in background updates.", + ) return_code = 1 # Check for index creation, which is only allowed for tables we've @@ -188,11 +195,14 @@ def main(force_colors: bool) -> None: table_name = match.group(1) if table_name not in created_tables: click.secho( - f"Found delta with index creation: '{clause}' in {delta_file}\nThese should be in background updates.", + f"Found delta with index creation for existing table: '{clause}' in {delta_file}", fg="red", bold=True, color=force_colors, ) + click.secho( + " ↪ These should be in background updates (or the table should be created in the same delta).", + ) return_code = 1 click.get_current_context().exit(return_code)