10000 fix: always persist all indexes added via db.add_index (backport #31177) by mergify[bot] · Pull Request #31712 · frappe/frappe · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix: always persist all indexes added via db.add_index (backport #31177) #31712

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions frappe/commands/site.py
8000
Original file line number Diff line number Diff line change
Expand Up @@ -550,23 +550,12 @@ def format_app(app):
@pass_context
def add_db_index(context, doctype, column):
"Adds a new DB index and creates a property setter to persist it."
from frappe.custom.doctype.property_setter.property_setter import make_property_setter

columns = column # correct naming
for site in context.sites:
frappe.init(site=site)
frappe.connect()
try:
frappe.db.add_index(doctype, columns)
if len(columns) == 1:
make_property_setter(
doctype,
columns[0],
property="search_index",
value="1",
property_type="Check",
for_doctype=False, # Applied on docfield
)
frappe.db.commit()
finally:
frappe.destroy()
Expand Down
8 changes: 0 additions & 8 deletions frappe/core/doctype/recorder/recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,6 @@ def add_indexes(indexes):
def _add_index(table, column):
doctype = get_doctype_name(table)
frappe.db.add_index(doctype, [column])
make_property_setter(
doctype,
column,
property="search_index",
value="1",
property_type="Check",
for_doctype=False, # Applied on docfield
)
frappe.msgprint(
_("Index created successfully on column {0} of doctype {1}").format(column, doctype),
alert=True,
Expand Down
15 changes: 14 additions & 1 deletion frappe/database/mariadb/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,14 +404,27 @@ def get_column_index(self, table_name: str, fieldname: str, unique: bool = False
def add_index(self, doctype: str, fields: list, index_name: str | None = None):
"""Creates an index with given fields if not already created.
Index name will be `fieldname1_fieldname2_index`"""
from frappe.custom.doctype.property_setter.property_setter import make_property_setter

index_name = index_name or self.get_index_name(fields)
table_name = get_table_name(doctype)
if not self.has_index(table_name, index_name):
self.commit()
self.sql(
"""ALTER TABLE `{}`
ADD INDEX `{}`({})""".format(table_name, index_name, ", ".join(fields))
ADD INDEX IF NOT EXISTS `{}`({})""".format(table_name, index_name, ", ".join(fields))
)
# Ensure that DB migration doesn't clear this index, assuming this is manually added
# via code or console.
if len(fields) == 1 and not (frappe.flags.in_install or frappe.flags.in_migrate):
make_property_setter(
doctype,
fields[0],
property="search_index",
value="1",
property_type="Check",
for_doctype=False, # Applied on docfield
)

def add_unique(self, doctype, fields, constraint_name=None):
if isinstance(fields, str):
Expand Down
4 changes: 4 additions & 0 deletions frappe/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,10 @@ def test_build_assets_size_check(self):


class TestDBUtils(BaseTestCommands):
@skipIf(
not (frappe.conf.db_type == "mariadb"),
"Only for MariaDB",
)
def test_db_add_index(self):
field = "reset_password_key"
self.execute("bench --site {site} add-database-index --doctype User --column " + field, {})
Expand Down
Loading
0