Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

chore(user): add partial index for email,org_id (#10828)

Vikrant Guptaa71006b662ed
diff --git a/pkg/modules/user/impluser/store.go b/pkg/modules/user/impluser/store.go
index 20b382af51..bdda343402 100644
--- a/pkg/modules/user/impluser/store.go
+++ b/pkg/modules/user/impluser/store.go
@@ -279,7 +279,6 @@ func (store *store) SoftDeleteUser(ctx context.Context, orgID string, id string)
_, err = tx.NewUpdate().
Model(new(types.User)).
Set("status = ?", types.UserStatusDeleted).
- Set("deleted_at = ?", now).
Set("updated_at = ?", now).
Where("org_id = ?", orgID).
Where("id = ?", id).
diff --git a/pkg/signoz/provider.go b/pkg/signoz/provider.go
index e5a3bdd969..0c924f6e0a 100644
--- a/pkg/signoz/provider.go
+++ b/pkg/signoz/provider.go
@@ -194,6 +194,7 @@ func NewSQLMigrationProviderFactories(
sqlmigration.NewAddServiceAccountFactory(sqlstore, sqlschema),
sqlmigration.NewDeprecateAPIKeyFactory(sqlstore, sqlschema),
sqlmigration.NewServiceAccountAuthzactory(sqlstore),
+ sqlmigration.NewDropUserDeletedAtFactory(sqlstore, sqlschema),
)
}
diff --git a/pkg/sqlmigration/076_drop_user_deleted_at.go b/pkg/sqlmigration/076_drop_user_deleted_at.go
new file mode 100644
index 0000000000..c79cb5b9bf
--- /dev/null
+++ b/pkg/sqlmigration/076_drop_user_deleted_at.go
@@ -0,0 +1,84 @@
+package sqlmigration
+
+import (
+ "context"
+
+ "github.com/SigNoz/signoz/pkg/factory"
+ "github.com/SigNoz/signoz/pkg/sqlschema"
+ "github.com/SigNoz/signoz/pkg/sqlstore"
+ "github.com/uptrace/bun"
+ "github.com/uptrace/bun/migrate"
+)
+
+type dropUserDeletedAt struct {
+ sqlstore sqlstore.SQLStore
+ sqlschema sqlschema.SQLSchema
+}
+
+func NewDropUserDeletedAtFactory(sqlstore sqlstore.SQLStore, sqlschema sqlschema.SQLSchema) factory.ProviderFactory[SQLMigration, Config] {
+ return factory.NewProviderFactory(factory.MustNewName("drop_user_deleted_at"), func(ctx context.Context, ps factory.ProviderSettings, c Config) (SQLMigration, error) {
+ return &dropUserDeletedAt{sqlstore: sqlstore, sqlschema: sqlschema}, nil
+ })
+}
+
+func (migration *dropUserDeletedAt) Register(migrations *migrate.Migrations) error {
+ if err := migrations.Register(migration.Up, migration.Down); err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func (migration *dropUserDeletedAt) Up(ctx context.Context, db *bun.DB) error {
+ tx, err := db.BeginTx(ctx, nil)
+ if err != nil {
+ return err
+ }
+
+ defer func() {
+ _ = tx.Rollback()
+ }()
+
+ table, _, err := migration.sqlschema.GetTable(ctx, sqlschema.TableName("users"))
+ if err != nil {
+ return err
+ }
+
+ deletedAtColumn := &sqlschema.Column{
+ Name: sqlschema.ColumnName("deleted_at"),
+ DataType: sqlschema.DataTypeTimestamp,
+ Nullable: false,
+ }
+
+ sqls := [][]byte{}
+
+ dropIndexSQLs := migration.sqlschema.Operator().DropIndex(&sqlschema.UniqueIndex{TableName: "users", ColumnNames: []sqlschema.ColumnName{"org_id", "email", "deleted_at"}})
+ sqls = append(sqls, dropIndexSQLs...)
+
+ dropSQLs := migration.sqlschema.Operator().DropColumn(table, deletedAtColumn)
+ sqls = append(sqls, dropSQLs...)
+
+ indexSQLs := migration.sqlschema.Operator().CreateIndex(
+ &sqlschema.PartialUniqueIndex{
+ TableName: "users",
+ ColumnNames: []sqlschema.ColumnName{"email", "org_id"},
+ Where: "status != 'deleted'",
+ })
+ sqls = append(sqls, indexSQLs...)
+
+ for _, sql := range sqls {
+ if _, err := tx.ExecContext(ctx, string(sql)); err != nil {
+ return err
+ }
+ }
+
+ if err := tx.Commit(); err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func (migration *dropUserDeletedAt) Down(context.Context, *bun.DB) error {
+ return nil
+}
diff --git a/pkg/types/user.go b/pkg/types/user.go
index 582319580a..df7b0d1126 100644
--- a/pkg/types/user.go
+++ b/pkg/types/user.go
@@ -42,7 +42,6 @@ type User struct {
OrgID valuer.UUID `bun:"org_id" json:"orgId"`
IsRoot bool `bun:"is_root" json:"isRoot"`
Status valuer.String `bun:"status" json:"status"`
- DeletedAt time.Time `bun:"deleted_at" json:"-"`
TimeAuditable
}
@@ -136,7 +135,6 @@ func NewUserFromDeprecatedUser(deprecatedUser *DeprecatedUser) *User {
OrgID: deprecatedUser.OrgID,
IsRoot: deprecatedUser.IsRoot,
Status: deprecatedUser.Status,
- DeletedAt: deprecatedUser.DeletedAt,
TimeAuditable: deprecatedUser.TimeAuditable,
}
}
diff --git a/tests/integration/src/passwordauthn/07_user_unique_index.py b/tests/integration/src/passwordauthn/07_user_unique_index.py
index ae334d6a58..18ea47cb1b 100644
--- a/tests/integration/src/passwordauthn/07_user_unique_index.py
+++ b/tests/integration/src/passwordauthn/07_user_unique_index.py
@@ -18,13 +18,13 @@ def test_unique_index_allows_multiple_deleted_rows(
get_token: Callable[[str, str], str],
):
"""
- Verify that the composite unique index on (org_id, email, deleted_at) allows multiple
- deleted rows for the same (org_id, email) while still enforcing uniqueness among
- non-deleted rows.
+ Verify that the partial unique index on (email, org_id) WHERE status != 'deleted'
+ allows multiple deleted rows for the same (org_id, email) while still enforcing
+ uniqueness among non-deleted rows.
- Non-deleted users share deleted_at=zero-time, so the unique index prevents duplicates.
- Soft-deleted users each have a distinct deleted_at timestamp, so the index allows
- multiple deleted rows for the same (org_id, email).
+ The partial unique index only covers rows where status != 'deleted', so active
+ users cannot share the same (org_id, email). Deleted users are excluded from
+ the index, allowing multiple deleted rows for the same (org_id, email).
Steps:
1. Invite and soft-delete a user via the API (first deleted row).
@@ -32,9 +32,9 @@ def test_unique_index_allows_multiple_deleted_rows(
3. Assert via SQL that exactly two deleted rows exist for the email.
4. Assert via SQL that inserting one active row succeeds (no conflict — only
deleted rows exist), then inserting a second active row for the same
- (org_id, email) fails with a unique constraint error (both have deleted_at=zero-time).
+ (org_id, email) fails with a unique constraint error.
5. Assert via SQL that inserting a third deleted row for the same (org_id, email)
- with a unique deleted_at succeeds — confirming the index does not cover deleted rows.
+ succeeds — confirming the index does not cover deleted rows.
6. Assert via SQL that the final count of deleted rows is 3.
"""
admin_token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)
@@ -86,10 +86,9 @@ def test_unique_index_allows_multiple_deleted_rows(
with signoz.sqlstore.conn.connect() as conn:
result = conn.execute(
sql.text(
- "SELECT id, deleted_at FROM users"
- " WHERE email = :email AND deleted_at != :zero_time"
+ "SELECT id FROM users WHERE email = :email AND status = 'deleted'"
),
- {"email": UNIQUE_INDEX_USER_EMAIL, "zero_time": "0001-01-01 00:00:00"},
+ {"email": UNIQUE_INDEX_USER_EMAIL},
)
deleted_rows = result.fetchall()
@@ -109,24 +108,24 @@ def test_unique_index_allows_multiple_deleted_rows(
org_id = result.fetchone()[0]
# Step 4: the unique index must still block a duplicate non-deleted row.
- # Both active rows have deleted_at=zero-time, so they share the same (org_id, email, zero-time)
- # tuple. First insert must succeed (only deleted rows exist so far).
- # Second insert for the same (org_id, email) with deleted_at=zero-time must fail.
+ # The partial unique index covers rows WHERE status != 'deleted', so two active
+ # rows with the same (org_id, email) must conflict.
+ # First insert must succeed (only deleted rows exist so far).
+ # Second insert for the same (org_id, email) with status='active' must fail.
active_id = str(uuid.uuid4())
with signoz.sqlstore.conn.connect() as conn:
conn.execute(
sql.text(
"INSERT INTO users"
- " (id, display_name, email, org_id, is_root, status, created_at, updated_at, deleted_at)"
+ " (id, display_name, email, org_id, is_root, status, created_at, updated_at)"
" VALUES (:id, :display_name, :email, :org_id,"
- " false, 'active', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, :zero_time)"
+ " false, 'active', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)"
),
{
"id": active_id,
"display_name": "first active row",
"email": UNIQUE_INDEX_USER_EMAIL,
"org_id": org_id,
- "zero_time": "0001-01-01 00:00:00",
},
)
conn.commit()
@@ -136,27 +135,26 @@ def test_unique_index_allows_multiple_deleted_rows(
conn.execute(
sql.text(
"INSERT INTO users"
- " (id, display_name, email, org_id, is_root, status, created_at, updated_at, deleted_at)"
+ " (id, display_name, email, org_id, is_root, status, created_at, updated_at)"
" VALUES (:id, :display_name, :email, :org_id,"
- " false, 'active', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, :zero_time)"
+ " false, 'active', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)"
),
{
"id": str(uuid.uuid4()),
"display_name": "should violate index",
"email": UNIQUE_INDEX_USER_EMAIL,
"org_id": org_id,
- "zero_time": "0001-01-01 00:00:00",
},
)
- # Step 5: a third deleted row with a unique deleted_at must be accepted
+ # Step 5: a third deleted row must be accepted (excluded from partial index)
with signoz.sqlstore.conn.connect() as conn:
conn.execute(
sql.text(
"INSERT INTO users"
- " (id, display_name, email, org_id, is_root, status, created_at, updated_at, deleted_at)"
+ " (id, display_name, email, org_id, is_root, status, created_at, updated_at)"
" VALUES (:id, :display_name, :email, :org_id,"
- " false, 'deleted', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)"
+ " false, 'deleted', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)"
),
{
"id": str(uuid.uuid4()),
@@ -172,9 +170,9 @@ def test_unique_index_allows_multiple_deleted_rows(
result = conn.execute(
sql.text(
"SELECT COUNT(*) FROM users"
- " WHERE email = :email AND deleted_at != :zero_time"
+ " WHERE email = :email AND status = 'deleted'"
),
- {"email": UNIQUE_INDEX_USER_EMAIL, "zero_time": "0001-01-01 00:00:00"},
+ {"email": UNIQUE_INDEX_USER_EMAIL},
)
count = result.fetchone()[0]