Skip to content

Commit 3ccc518

Browse files
authored
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.
1 parent 07e7980 commit 3ccc518

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

changelog.d/19020.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix CI linter for schema delta files to correctly handle all types of `CREATE TABLE` syntax.

scripts-dev/check_schema_delta.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@
1111
import git
1212

1313
SCHEMA_FILE_REGEX = re.compile(r"^synapse/storage/schema/(.*)/delta/(.*)/(.*)$")
14-
INDEX_CREATION_REGEX = re.compile(r"CREATE .*INDEX .*ON ([a-z_]+)", flags=re.IGNORECASE)
15-
INDEX_DELETION_REGEX = re.compile(r"DROP .*INDEX ([a-z_]+)", flags=re.IGNORECASE)
16-
TABLE_CREATION_REGEX = re.compile(r"CREATE .*TABLE ([a-z_]+)", flags=re.IGNORECASE)
14+
INDEX_CREATION_REGEX = re.compile(
15+
r"CREATE .*INDEX .*ON ([a-z_0-9]+)", flags=re.IGNORECASE
16+
)
17+
INDEX_DELETION_REGEX = re.compile(r"DROP .*INDEX ([a-z_0-9]+)", flags=re.IGNORECASE)
18+
TABLE_CREATION_REGEX = re.compile(
19+
r"CREATE .*TABLE.* ([a-z_0-9]+)\s*\(", flags=re.IGNORECASE
20+
)
1721

1822
# The base branch we want to check against. We use the main development branch
1923
# on the assumption that is what we are developing against.
@@ -173,11 +177,14 @@ def main(force_colors: bool) -> None:
173177
clause = match.group()
174178

175179
click.secho(
176-
f"Found delta with index deletion: '{clause}' in {delta_file}\nThese should be in background updates.",
180+
f"Found delta with index deletion: '{clause}' in {delta_file}",
177181
fg="red",
178182
bold=True,
179183
color=force_colors,
180184
)
185+
click.secho(
186+
" ↪ These should be in background updates.",
187+
)
181188
return_code = 1
182189

183190
# Check for index creation, which is only allowed for tables we've
@@ -188,11 +195,14 @@ def main(force_colors: bool) -> None:
188195
table_name = match.group(1)
189196
if table_name not in created_tables:
190197
click.secho(
191-
f"Found delta with index creation: '{clause}' in {delta_file}\nThese should be in background updates.",
198+
f"Found delta with index creation for existing table: '{clause}' in {delta_file}",
192199
fg="red",
193200
bold=True,
194201
color=force_colors,
195202
)
203+
click.secho(
204+
" ↪ These should be in background updates (or the table should be created in the same delta).",
205+
)
196206
return_code = 1
197207

198208
click.get_current_context().exit(return_code)

0 commit comments

Comments
 (0)