Skip to content

feat(core,schemas): add user_social_identity_ids table #7459

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

simeng-li
Copy link
Contributor

Summary

Add user_social_identity_ids index table for social identity lookup.

  • Created a new table: user_social_identity_ids
  • This table automatically syncs user social identity records from the users.identities JSONB column on INSERT or UPDATE

Why

Our existing implementation stores social identity information as a JSONB object in the users table. While this design centralizes identity data, it presents several limitations:

  • Social identity fields (e.g., target + identityId) are not individually indexable
  • Queries filtering users by social identity (e.g., find by Google ID) are inefficient
  • Social identity values cannot be used as foreign keys for other resources

To address these issues, we’ve introduced a dedicated index table to enable fast and reliable lookups and joins.

Design Notes

Testing

  • This new table only stores key identity metadata (e.g., user_id, target, identity_id)
  • It does not replace the users.identities field — that remains the single source of truth (SSOT) for social identity details
  • A PostgreSQL trigger ensures that data in this table stays in sync with users.identities after INSERT or UPDATE operations
  • The table is read-only by design and should never be mutated directly by application code

Checklist

  • .changeset
  • unit tests
  • integration tests
  • necessary TSDoc comments

@simeng-li simeng-li requested a review from Copilot June 16, 2025 10:33
@github-actions github-actions bot added the feature Cool stuff label Jun 16, 2025
Copy link

github-actions bot commented Jun 16, 2025

COMPARE TO master

Total Size Diff ⚠️ 📈 +10.91 KB

Diff by File
Name Diff
packages/core/src/queries/user-social-identity-relations.ts 📈 +1.17 KB
packages/core/src/routes/admin-user/social.ts 📈 +811 Bytes
packages/core/src/routes/swagger/utils/general.ts 📈 +311 Bytes
packages/core/src/tenants/Queries.ts 📈 +190 Bytes
packages/integration-tests/src/api/admin-user.ts 📈 +216 Bytes
packages/integration-tests/src/tests/api/admin-user.identities.test.ts 📈 +5.49 KB
packages/integration-tests/src/tests/api/admin-user.test.ts 📈 +3.45 KB
packages/schemas/alterations/next-1750063177-add-user-social-identity-relations-table.ts 📈 +3.71 KB
packages/schemas/tables/user_social_identity_relations.sql 📈 +2.51 KB

Copy link
Contributor

@Copilot 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 adds a dedicated index table (user_social_identity_ids) for fast lookup of social identity data and synchronizes its content with the users.identities JSONB column via a database trigger.

  • Introduces a new SQL migration file and associated trigger function for syncing social identity records.
  • Provides an alteration script written in TypeScript to manage the table creation, trigger setup, and rollback.
  • Updates integration tests and core API queries to expose the new user social identity IDs.

Reviewed Changes

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

File Description
packages/schemas/tables/user-social-identitiy-ids.sql Creates the user_social_identity_ids table and trigger function for syncing.
packages/schemas/alterations/next-1750063177-add-user-social-identity-ids-table.ts Adds the alteration script for table creation and trigger integration.
packages/integration-tests (various files) Updates tests to cover creation, update, query, and deletion workflows for social identities.
packages/core (various files) Integrates the new table with queries and exposes API routes for identity IDs.

@simeng-li simeng-li force-pushed the simeng-log-11694-schemas-create-social-identities-indexing-table branch from 9c2813e to 3c8a695 Compare June 17, 2025 02:53
Comment on lines 72 to 83
"api/users/:userId/identity-ids": {
"get": {
"tags": ["Dev feature"],
"parameters": [],
"responses": {
"200": {
"description": "A list of social identity IDs of the user."
}
},
"summary": "Get social identity IDs of user",
"description": "Get all social identity IDs of the user."
}
Copy link
Member

Choose a reason for hiding this comment

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

it may be not practical to only fetch IDs? how about api/users/:userId/identities and return identity objects?

Copy link
Contributor Author

@simeng-li simeng-li Jun 17, 2025

Choose a reason for hiding this comment

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

Removed this API doc. This API is temporarily used for integration testing only since we have no place to check the synced user identity relation status.
This API is guarded by the isIntegrationTest guard.

@@ -0,0 +1,69 @@
/* init_order = 2 */

create table user_social_identity_ids (
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
create table user_social_identity_ids (
create table user_social_identity_relations (

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

unique (tenant_id, target, identity_id)
);

create function sync_user_social_identity_ids()
Copy link
Member

Choose a reason for hiding this comment

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

better add some comments to explain the purpose

Copy link
Contributor Author

Choose a reason for hiding this comment

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

comments added

add user_social_identity_relations table
@simeng-li simeng-li force-pushed the simeng-log-11694-schemas-create-social-identities-indexing-table branch from 059590f to 078d83c Compare June 17, 2025 07:04
Comment on lines +13 to +14
* Important: This table should only be queried for reading data. Any write operations
* should be performed on the `Users` table instead, as the trigger will handle the synchronization.
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: we can limit update/delete operation for this table when we apply RLS policy?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

3 participants