feat(migration): create unique index on folders for parent_uuid and plain_name#882
feat(migration): create unique index on folders for parent_uuid and plain_name#882
Conversation
0ae29f3 to
6c4180d
Compare
|
This PR is stale because it has been open for more than 15 days with no activity. |
6c4180d to
e85fbd6
Compare
|
This PR has conflicts with master @jzunigax2, let's reconcile it (waiting this before #883) |
e85fbd6 to
20491aa
Compare
|
This PR is stale because it has been open for more than 15 days with no activity. |
20491aa to
fe9b26f
Compare
fe9b26f to
865c602
Compare
865c602 to
65bfe8d
Compare
| await queryInterface.sequelize.query(` | ||
| CREATE UNIQUE INDEX CONCURRENTLY folders_parentuuid_plainname_unique | ||
| ON folders (parent_uuid, plain_name) | ||
| WHERE deleted = false; |
There was a problem hiding this comment.
This index should include and removed = false also. Otherwise, permanently removed folders inside the a parent could prevent the same folder (=same name) creation inside. This could seem tricky but this could be a common case on a desktop sync if a rename happens, for instance.
There was a problem hiding this comment.
hmm it does seem tricky, this was meant to replace this other index which also only has a where deleted = false
by adding removed = false to the where wouldn't that prevent creaing a new folder with the same name as a trashed but not deleted one in the same parent? or am I missing something?
… and create unique index on parent_uuid and plain_name
65bfe8d to
7b062dd
Compare
|



What
Create a new unique index
folders_parentuuid_plainname_uniqueon the folders table using (parent_uuid,plain_name) to replace the legacy
folders_plainname_parentid_keyindex.Why
1. Legacy index uses deprecated column
The current unique index folders_plainname_parentid_key uses parent_id (integer FK), while we have kept
migrating towards using parent_uuid (UUID FK) for all folder relationship operations.
-- Current (legacy)
CREATE UNIQUE INDEX folders_plainname_parentid_key
ON folders (plain_name, parent_id)
WHERE deleted = false;
-- New (aligned with application code)
CREATE UNIQUE INDEX folders_parentuuid_plainname_unique
ON folders (parent_uuid, plain_name)
WHERE deleted = false;
2. Suboptimal column order causing inefficient uniqueness checks
The legacy index has plain_name as the first column, which has low cardinality. This forces the db to possibly scan through entries before narrowing by parent.
The new index places parent_uuid first (high cardinality).
3. Application code already uses parent_uuid:
How
Migration uses CREATE INDEX CONCURRENTLY to avoid table locks: