forked from DMOJ/online-judge
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(judge): add FULLTEXT index and tests for Problem model
Adds a FULLTEXT index to the `Problem` model as needed by the builtin search. Includes migration and tests (skipped for non-MySQL databases). see DMOJ/docs#100
- Loading branch information
1 parent
e381e1f
commit 5cb9223
Showing
2 changed files
with
160 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Generated by Django 3.2.25 on 2024-10-01 06:08 | ||
|
||
from django.db import migrations | ||
|
||
|
||
def execute_mysql_command(apps, schema_editor, sql, error_msg, success_msg): | ||
if schema_editor.connection.vendor != 'mysql': | ||
return | ||
|
||
Problem = apps.get_model('judge', 'Problem') | ||
formatted_sql = sql.format(Problem._meta.db_table) | ||
|
||
with schema_editor.connection.cursor() as cursor: | ||
try: | ||
cursor.execute(formatted_sql) | ||
print(success_msg) | ||
except Exception as e: | ||
if error_msg in str(e): | ||
print(f'Info: {error_msg}') | ||
else: | ||
raise | ||
|
||
|
||
def add_fulltext_index(apps, schema_editor): | ||
execute_mysql_command( | ||
apps, | ||
schema_editor, | ||
'ALTER TABLE {} ADD FULLTEXT(code, name, description)', | ||
'Duplicate key name', | ||
'FULLTEXT index added successfully.', | ||
) | ||
|
||
|
||
def remove_fulltext_index(apps, schema_editor): | ||
execute_mysql_command( | ||
apps, | ||
schema_editor, | ||
'ALTER TABLE {} DROP INDEX code', | ||
'check that column/key exists', | ||
'FULLTEXT index removed successfully.', | ||
) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
('judge', '0147_judge_add_tiers'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(add_fulltext_index, remove_fulltext_index), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters