Skip to content

feat: add accounts indexes (email + password)#3897

Open
slawkens wants to merge 5 commits intoopentibiabr:mainfrom
slawkens:feature/accounts-email-password-indexes
Open

feat: add accounts indexes (email + password)#3897
slawkens wants to merge 5 commits intoopentibiabr:mainfrom
slawkens:feature/accounts-email-password-indexes

Conversation

@slawkens
Copy link
Contributor

@slawkens slawkens commented Mar 13, 2026

Description

Added indexes into accounts table - for email and password. This gives 10x performance boost when searching for those columns. I tested on db with 3k accounts.

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Tested execution of migration + schema.sql in the phpmyadmin

Test Configuration:

  • MySQL Version: 11.2.0-MariaDB
  • Server Version: Canary latest
  • Operating System: Windows

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I checked the PR checks reports
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works

Summary by CodeRabbit

  • Chores
    • Database upgraded to version 56 (server config updated).
    • Account password field now required and limited to 255 characters.
    • New indexes on account email and password to improve lookup and authentication performance.
    • Migration runs with guarded logging to report and surface any upgrade failures.

Copilot AI review requested due to automatic review settings March 13, 2026 18:46
@coderabbitai
Copy link

coderabbitai bot commented Mar 13, 2026

📝 Walkthrough

Walkthrough

Adds a migration (v56) and schema updates: makes accounts.password NOT NULL (VARCHAR(255)), sets server_config.db_version to 56, and adds non-unique indexes on accounts.email and accounts.password. A new Lua migration runs the SQL statements and logs failures.

Changes

Cohort / File(s) Summary
Migration script
data-otservbr-global/migrations/56.lua
Adds onUpdateDatabase() which logs the upgrade and runs three db.query calls to alter accounts.password to NOT NULL and create indexes accounts_email and accounts_password; logs on query failures.
Schema / seed
schema.sql
Updates server_config insert to set db_version = '56'; changes accounts.password column to VARCHAR(255) NOT NULL; declares indexes accounts_email (email) and accounts_password (password).

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested reviewers

  • dudantas
  • majestyotbr

Poem

🐰
I hop through tables with a cheerful twitch,
I add an index, tidy every stitch.
Passwords and emails now sorted with care,
Migration fifty-six — a little rabbit's flair. ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding indexes on the accounts table for email and password columns to improve query performance.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Tip

Migrating from UI to YAML configuration.

Use the @coderabbitai configuration command in a PR comment to get a dump of all your UI settings in YAML format. You can then edit this YAML file and upload it to the root of your repository to configure CodeRabbit programmatically.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds database indexes intended to speed up account lookups by email (and password) in the accounts table.

Changes:

  • Adds accounts_email and accounts_password indexes to accounts in schema.sql.
  • Introduces migration 56.lua to create the same indexes on existing databases.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
schema.sql Adds new indexes to the accounts table definition.
data-otservbr-global/migrations/56.lua Adds a migration that creates the new indexes for existing DBs.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

logger.info("Updating database to version 56 (feat: accounts email + password indexes)")

db.query("CREATE INDEX `accounts_email` ON `accounts` (`email`);")
db.query("CREATE INDEX `accounts_password` ON `accounts` (`password`);")
Comment on lines +4 to +5
db.query("CREATE INDEX `accounts_email` ON `accounts` (`email`);")
db.query("CREATE INDEX `accounts_password` ON `accounts` (`password`);")
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@data-otservbr-global/migrations/56.lua`:
- Around line 4-5: The migration currently ignores return values from
db.query("CREATE INDEX `accounts_email`...") and db.query("CREATE INDEX
`accounts_password`..."); update the migration (in the code that runs these
statements) to check each db.query call's boolean result and fail fast by
logging an error and returning false (or otherwise signalling failure from
onUpdateDatabase) when a query returns false so the migration system does not
advance db_version on failed DDL; ensure both db.query calls are wrapped with
this check and a clear error message referencing the failed SQL and preserve
existing logging conventions.

In `@schema.sql`:
- Around line 29-31: The seed in schema.sql sets server_config.db_version to 54
but the schema already includes migration-56 changes (indexes `accounts_email`
and `accounts_password`), causing fresh installs to re-run migration 56 and
error; fix by updating the seed Insert into `server_config` to set `db_version`
to '56' (i.e., update the values inserted for `db_version`) so schema and seed
match, or alternatively modify migration 56 to be idempotent by checking for the
existence of indexes `accounts_email` and `accounts_password` on the `accounts`
table before attempting to create them.
- Line 31: The index declaration INDEX `accounts_password` on the TEXT column
`password` is invalid; update the schema so the indexed column is indexable by
either changing `password` from TEXT to a bounded type (e.g., VARCHAR with an
appropriate length) or by adding a prefix length to the index (e.g., INDEX
`accounts_password` (`password`(N))); locate the `password` column definition
(currently TEXT) and the INDEX `accounts_password` entry and apply one of these
fixes, ensuring the chosen prefix length N or VARCHAR length is sufficient for
your use case and MySQL-compatible.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: efd1adfc-1f97-4be7-a392-ae6497779f75

📥 Commits

Reviewing files that changed from the base of the PR and between 55711ef and ff013fe.

📒 Files selected for processing (2)
  • data-otservbr-global/migrations/56.lua
  • schema.sql

Comment on lines +4 to +5
db.query("CREATE INDEX `accounts_email` ON `accounts` (`email`);")
db.query("CREATE INDEX `accounts_password` ON `accounts` (`password`);")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Fail fast on DDL errors to avoid false-success migration state.

db.query(...) results are ignored on Line [4]-Line [5]. Per src/database/database.cpp Line [224]-Line [242], failures return false (not exceptions), and src/database/databasemanager.cpp Line [135] still advances db_version after onUpdateDatabase() returns. A failed index creation can therefore still mark version 56 as applied.

Suggested fix
 function onUpdateDatabase()
 	logger.info("Updating database to version 56 (feat: accounts email + password indexes)")
 
-	db.query("CREATE INDEX `accounts_email` ON `accounts` (`email`);")
-	db.query("CREATE INDEX `accounts_password` ON `accounts` (`password`);")
+	local ok = db.query("CREATE INDEX `accounts_email` ON `accounts` (`email`);")
+	if not ok then
+		error("Migration 56 failed creating index `accounts_email`.")
+	end
+
+	ok = db.query("CREATE INDEX `accounts_password` ON `accounts` (`password`(191));")
+	if not ok then
+		error("Migration 56 failed creating index `accounts_password`.")
+	end
 end
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@data-otservbr-global/migrations/56.lua` around lines 4 - 5, The migration
currently ignores return values from db.query("CREATE INDEX
`accounts_email`...") and db.query("CREATE INDEX `accounts_password`...");
update the migration (in the code that runs these statements) to check each
db.query call's boolean result and fail fast by logging an error and returning
false (or otherwise signalling failure from onUpdateDatabase) when a query
returns false so the migration system does not advance db_version on failed DDL;
ensure both db.query calls are wrapped with this check and a clear error message
referencing the failed SQL and preserve existing logging conventions.

@sonarqubecloud
Copy link

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@data-otservbr-global/migrations/56.lua`:
- Around line 12-14: The logger.error call in the migration rollback block is
missing a closing parenthesis which breaks parsing; update the logger.error
invocation (the call that follows db.query("CREATE INDEX `accounts_password` ON
`accounts` (`password`);")) to include the missing closing parenthesis so the
call is syntactically correct and the migration file parses cleanly.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 7f8d10ad-a9aa-4a37-904f-133117cb3569

📥 Commits

Reviewing files that changed from the base of the PR and between ff013fe and 651aaab.

📒 Files selected for processing (2)
  • data-otservbr-global/migrations/56.lua
  • schema.sql
🚧 Files skipped from review as they are similar to previous changes (1)
  • schema.sql

@majestyotbr majestyotbr changed the title Add accounts indexes (email + password) feat: add accounts indexes (email + password) Mar 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants