Skip to content

Fix interactions unicity by adding wiki to the UNIQUE index#300

Open
alterphp wants to merge 1 commit into
masterfrom
fix/interactions-unicity
Open

Fix interactions unicity by adding wiki to the UNIQUE index#300
alterphp wants to merge 1 commit into
masterfrom
fix/interactions-unicity

Conversation

@alterphp
Copy link
Copy Markdown
Collaborator

@alterphp alterphp commented Dec 7, 2025

Summary by CodeRabbit

  • Bug Fixes
    • Fixed user interaction tracking to properly account for wiki-specific contexts. Updated the uniqueness constraint for page interactions to include wiki identifier, ensuring accurate deduplication and follow behavior across different wiki instances.

✏️ Tip: You can customize this high-level summary in your review settings.

@alterphp alterphp requested a review from Copilot December 7, 2025 22:27
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Dec 7, 2025

Walkthrough

This PR updates the user-page interaction uniqueness constraint to include the wiki field. The console command's upsert operation is modified to use (user_id, page_id, wiki) as the unique key, and a corresponding database migration alters the interactions table's unique index to match this new composite key.

Changes

Cohort / File(s) Summary
Upsert operation update
app/Console/Commands/UsersSubscribeCharacteristicsTags.php
Modified the unique key in the upsert operation from (user_id, page_id) to (user_id, page_id, wiki), changing how interactions are deduplicated and identified per wiki context.
Database schema migration
database/migrations/2025_12_07_222422_fix_interactions_unicity.php
New migration that drops the existing unique_user_page index and creates a new unique index on (user_id, page_id, wiki) on the interactions table.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10–15 minutes

  • Verify consistency between the upsert unique key and the database index definition
  • Check for data migration considerations or conflicts with existing records under the old uniqueness constraint
  • Confirm that wiki-specific deduplication is the intended behavior change

Possibly related PRs

Suggested reviewers

  • bertrandgorge

Poem

🐰 A wiki-aware follow, now properly tracked,
Each user, each page, each wiki stacked,
No duplicates haunt the index anew,
The interactions bloom in their rightful brew! 🌱✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ 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 'Fix interactions unicity by adding wiki to the UNIQUE index' directly reflects the main changes in the PR, which involve adding the wiki column to a unique index on the interactions table and updating a related upsert operation.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/interactions-unicity

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.

Copy link
Copy Markdown

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

This PR fixes a uniqueness constraint issue in the interactions table by adding the wiki column to the UNIQUE index. Previously, the unique constraint only considered user_id and page_id, which could allow duplicate interactions for the same user and page across different wikis.

Key Changes:

  • Updated the database UNIQUE index on the interactions table to include the wiki column
  • Updated the upsert operation to match the new unique constraint by including wiki in the conflict resolution keys

Reviewed changes

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

File Description
database/migrations/2025_12_07_222422_fix_interactions_unicity.php Creates a migration to modify the UNIQUE index on interactions table to include wiki column
app/Console/Commands/UsersSubscribeCharacteristicsTags.php Updates the upsert unique key array to include wiki, aligning with the new database constraint

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

*/
public function down(): void
{
//
Copy link

Copilot AI Dec 7, 2025

Choose a reason for hiding this comment

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

The down() method is empty and should implement the rollback logic to restore the original UNIQUE index constraint with only user_id and page_id.

Suggested change
//
// Restaure l'index UNIQUE original sur (user_id, page_id)
DB::statement(<<<SQL
ALTER TABLE `insights`.`interactions`
DROP INDEX `unique_user_page`,
ADD UNIQUE `unique_user_page` (`user_id`, `page_id`) USING BTREE;
SQL);

Copilot uses AI. Check for mistakes.
{
// Modifie l'index UNIQUE sur les interactions => (user_id, page_id, wiki)
DB::statement(<<<SQL
ALTER TABLE `insights`.`interactions`
Copy link

Copilot AI Dec 7, 2025

Choose a reason for hiding this comment

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

The database name 'insights' is hardcoded in the migration. Consider using the configured database name or removing the database prefix to make this migration more portable across different environments.

Suggested change
ALTER TABLE `insights`.`interactions`
ALTER TABLE `interactions`

Copilot uses AI. Check for mistakes.
*/
public function up(): void
{
// Modifie l'index UNIQUE sur les interactions => (user_id, page_id, wiki)
Copy link

Copilot AI Dec 7, 2025

Choose a reason for hiding this comment

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

[nitpick] The comment is in French while the codebase appears to use English conventions. Consider translating to English: '// Modifies the UNIQUE index on interactions => (user_id, page_id, wiki)'.

Suggested change
// Modifie l'index UNIQUE sur les interactions => (user_id, page_id, wiki)
// Modifies the UNIQUE index on interactions => (user_id, page_id, wiki)

Copilot uses AI. Check for mistakes.
Comment on lines +13 to +21
public function up(): void
{
// Modifie l'index UNIQUE sur les interactions => (user_id, page_id, wiki)
DB::statement(<<<SQL
ALTER TABLE `insights`.`interactions`
DROP INDEX `unique_user_page`,
ADD UNIQUE `unique_user_page` (`user_id`, `page_id`, `wiki`) USING BTREE;
SQL);
}
Copy link

Copilot AI Dec 7, 2025

Choose a reason for hiding this comment

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

The migration drops and recreates the UNIQUE index without handling potential duplicate data. If duplicate rows exist (same user_id, page_id, wiki combination), this migration will fail. Consider adding logic to identify and resolve duplicates before modifying the index.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor

@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: 0

🧹 Nitpick comments (2)
database/migrations/2025_12_07_222422_fix_interactions_unicity.php (2)

13-21: Prefer Schema builder over raw SQL for the index change (portability/maintainability).

The ALTER TABLE is correct for MySQL, but using DB::statement with a hard‑coded insights schema makes this migration less portable (e.g., different DB names, test environments, or future engine changes). You already import Schema and Blueprint, so you could rely on the schema builder instead:

-        DB::statement(<<<SQL
-            ALTER TABLE `insights`.`interactions`
-                DROP INDEX `unique_user_page`,
-                ADD UNIQUE `unique_user_page` (`user_id`, `page_id`, `wiki`) USING BTREE;
-        SQL);
+        Schema::table('interactions', function (Blueprint $table) {
+            $table->dropUnique('unique_user_page');
+            $table->unique(['user_id', 'page_id', 'wiki'], 'unique_user_page');
+        });

This keeps the migration aligned with Laravel conventions and avoids coupling to a specific schema name.


26-29: Implement a reversible down() to restore the previous unique key on rollback.

down() is currently a no‑op, so rolling back this migration will leave the (user_id, page_id, wiki) index in place instead of restoring the old (user_id, page_id) constraint. If you rely on rollbacks (local/dev, CI, or feature branches), consider restoring the prior definition:

     public function down(): void
     {
-        //
+        Schema::table('interactions', function (Blueprint $table) {
+            $table->dropUnique('unique_user_page');
+            $table->unique(['user_id', 'page_id'], 'unique_user_page');
+        });
     }

Adjust the old index definition if it differed, but having a real rollback path will make schema changes safer.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6f49c41 and 94b252f.

📒 Files selected for processing (2)
  • app/Console/Commands/UsersSubscribeCharacteristicsTags.php (1 hunks)
  • database/migrations/2025_12_07_222422_fix_interactions_unicity.php (1 hunks)
🔇 Additional comments (1)
app/Console/Commands/UsersSubscribeCharacteristicsTags.php (1)

72-78: Upsert conflict key now matches the new (user_id, page_id, wiki) unique index—LGTM.

Including wiki in the upsert’s unique key aligns the command’s behavior with the new DB constraint and correctly scopes follows per wiki; just ensure this code is deployed together with the migration so older schemas (without the wiki column/index) don’t run this command.

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.

2 participants