fix: user email is set to a unique index (#7917)

This commit is contained in:
Riqwan Thamir authored and GitHub committed 2024-07-03 12:32:30 +02:00
1 parent a7844efd09
commit 00ba74c71f
3 files changed
+27 -8

No files matched your search

@@ -126,7 +126,7 @@
"composite": false, "composite": false,
"primary": false, "primary": false,
"unique": false, "unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_invite_token\" ON \"invite\" (deleted_at) WHERE deleted_at IS NOT NULL" "expression": "CREATE INDEX IF NOT EXISTS \"IDX_invite_deleted_at\" ON \"invite\" (deleted_at) WHERE deleted_at IS NOT NULL"
}, },
{ {
"keyName": "invite_pkey", "keyName": "invite_pkey",
@@ -241,7 +241,7 @@
"composite": false, "composite": false,
"primary": false, "primary": false,
"unique": false, "unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_user_email\" ON \"user\" (email) WHERE deleted_at IS NULL" "expression": "CREATE UNIQUE INDEX IF NOT EXISTS \"IDX_user_email\" ON \"user\" (email) WHERE deleted_at IS NULL"
}, },
{ {
"keyName": "IDX_user_deleted_at", "keyName": "IDX_user_deleted_at",
@@ -0,0 +1,21 @@
import { Migration } from "@mikro-orm/migrations"
export class Migration20240703095850 extends Migration {
async up(): Promise<void> {
this.addSql('drop index if exists "IDX_user_email";')
this.addSql(
'CREATE UNIQUE INDEX "IDX_user_email" ON "user" (email) WHERE deleted_at IS NULL;'
)
// Adding this log here as the point of failure is not in this function, but bundled up when running all pending migration
console.warn(
`Note: If the index "IDX_user_email" fails to create, then delete any existing users with duplicate emails before retrying the migration.`
)
}
async down(): Promise<void> {
this.addSql(
'CREATE INDEX IF NOT EXISTS "IDX_user_email" ON "user" (email) WHERE deleted_at IS NULL;'
)
}
}
+4 -6
View File
@@ -11,8 +11,8 @@ import {
import { DAL } from "@medusajs/types" import { DAL } from "@medusajs/types"
import { import {
DALUtils,
createPsqlIndexStatementHelper, createPsqlIndexStatementHelper,
DALUtils,
generateEntityId, generateEntityId,
Searchable, Searchable,
} from "@medusajs/utils" } from "@medusajs/utils"
@@ -20,10 +20,11 @@ import {
const userEmailIndexName = "IDX_user_email" const userEmailIndexName = "IDX_user_email"
const userEmailIndexStatement = createPsqlIndexStatementHelper({ const userEmailIndexStatement = createPsqlIndexStatementHelper({
name: userEmailIndexName, name: userEmailIndexName,
unique: true,
tableName: "user", tableName: "user",
columns: "email", columns: "email",
where: "deleted_at IS NULL", where: "deleted_at IS NULL",
}).expression })
const userDeletedAtIndexName = "IDX_user_deleted_at" const userDeletedAtIndexName = "IDX_user_deleted_at"
const userDeletedAtIndexStatement = createPsqlIndexStatementHelper({ const userDeletedAtIndexStatement = createPsqlIndexStatementHelper({
@@ -56,10 +57,7 @@ export default class User {
@Property({ columnType: "text", nullable: true }) @Property({ columnType: "text", nullable: true })
last_name: string | null = null last_name: string | null = null
@Index({ @userEmailIndexStatement.MikroORMIndex()
name: userEmailIndexName,
expression: userEmailIndexStatement,
})
@Searchable() @Searchable()
@Property({ columnType: "text" }) @Property({ columnType: "text" })
email: string email: string