-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
feat(auth): Migrate auth module to DML #10387
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
packages/modules/auth/src/migrations/Migration20241202100304.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Migration } from "@mikro-orm/migrations" | ||
|
||
export class Migration20241202100304 extends Migration { | ||
async up(): Promise<void> { | ||
this.addSql( | ||
'alter table if exists "auth_identity" add column if not exists "deleted_at" timestamptz null;' | ||
) | ||
this.addSql( | ||
'CREATE INDEX IF NOT EXISTS "IDX_auth_identity_deleted_at" ON "auth_identity" (deleted_at) WHERE deleted_at IS NULL;' | ||
) | ||
|
||
this.addSql( | ||
'alter table if exists "provider_identity" add column if not exists "deleted_at" timestamptz null;' | ||
) | ||
this.addSql( | ||
'CREATE INDEX IF NOT EXISTS "IDX_provider_identity_deleted_at" ON "provider_identity" (deleted_at) WHERE deleted_at IS NULL;' | ||
) | ||
} | ||
|
||
async down(): Promise<void> { | ||
this.addSql('drop index if exists "IDX_auth_identity_deleted_at";') | ||
this.addSql( | ||
'alter table if exists "auth_identity" drop column if exists "deleted_at";' | ||
) | ||
|
||
this.addSql('drop index if exists "IDX_provider_identity_deleted_at";') | ||
this.addSql( | ||
'alter table if exists "provider_identity" drop column if exists "deleted_at";' | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,14 @@ | ||
import { | ||
BeforeCreate, | ||
Collection, | ||
Entity, | ||
OneToMany, | ||
OnInit, | ||
PrimaryKey, | ||
Property, | ||
} from "@mikro-orm/core" | ||
|
||
import { generateEntityId } from "@medusajs/framework/utils" | ||
import ProviderIdentity from "./provider-identity" | ||
|
||
@Entity() | ||
export default class AuthIdentity { | ||
@PrimaryKey({ columnType: "text" }) | ||
id!: string | ||
|
||
@OneToMany(() => ProviderIdentity, (o) => o.auth_identity) | ||
provider_identities = new Collection<ProviderIdentity>(this) | ||
|
||
@Property({ columnType: "jsonb", nullable: true }) | ||
app_metadata: Record<string, unknown> | null | ||
|
||
@Property({ | ||
onCreate: () => new Date(), | ||
columnType: "timestamptz", | ||
defaultRaw: "now()", | ||
import { model } from "@medusajs/framework/utils" | ||
import { ProviderIdentity } from "./provider-identity" | ||
|
||
export const AuthIdentity = model | ||
.define("auth_identity", { | ||
id: model.id({ prefix: "authid" }).primaryKey(), | ||
provider_identities: model.hasMany(() => ProviderIdentity, { | ||
mappedBy: "auth_identity", | ||
}), | ||
app_metadata: model.json().nullable(), | ||
}) | ||
created_at: Date | ||
|
||
@Property({ | ||
onCreate: () => new Date(), | ||
onUpdate: () => new Date(), | ||
columnType: "timestamptz", | ||
defaultRaw: "now()", | ||
.cascades({ | ||
delete: ["provider_identities"], | ||
}) | ||
updated_at: Date | ||
|
||
@BeforeCreate() | ||
@OnInit() | ||
onCreate() { | ||
this.id = generateEntityId(this.id, "authid") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export { default as AuthIdentity } from "./auth-identity" | ||
export { default as ProviderIdentity } from "./provider-identity" | ||
export { AuthIdentity } from "./auth-identity" | ||
export { ProviderIdentity } from "./provider-identity" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,21 @@ | ||
import { | ||
BeforeCreate, | ||
Entity, | ||
ManyToOne, | ||
OnInit, | ||
PrimaryKey, | ||
Property, | ||
Rel, | ||
} from "@mikro-orm/core" | ||
|
||
import { | ||
createPsqlIndexStatementHelper, | ||
generateEntityId, | ||
} from "@medusajs/framework/utils" | ||
import AuthIdentity from "./auth-identity" | ||
|
||
const providerEntityIdIndexName = "IDX_provider_identity_provider_entity_id" | ||
const providerEntityIdIndexStatement = createPsqlIndexStatementHelper({ | ||
name: providerEntityIdIndexName, | ||
tableName: "provider_identity", | ||
columns: ["entity_id", "provider"], | ||
unique: true, | ||
}) | ||
|
||
const authIdentityIndexName = "IDX_provider_identity_auth_identity_id" | ||
const authIdentityIndexStatement = createPsqlIndexStatementHelper({ | ||
name: authIdentityIndexName, | ||
tableName: "provider_identity", | ||
columns: ["auth_identity_id"], | ||
}) | ||
|
||
@Entity() | ||
@providerEntityIdIndexStatement.MikroORMIndex() | ||
@authIdentityIndexStatement.MikroORMIndex() | ||
export default class ProviderIdentity { | ||
@PrimaryKey({ columnType: "text" }) | ||
id!: string | ||
|
||
@Property({ columnType: "text" }) | ||
entity_id: string | ||
|
||
@Property({ columnType: "text" }) | ||
provider: string | ||
|
||
@ManyToOne(() => AuthIdentity, { | ||
columnType: "text", | ||
fieldName: "auth_identity_id", | ||
mapToPk: true, | ||
onDelete: "cascade", | ||
}) | ||
auth_identity_id: string | ||
|
||
@ManyToOne(() => AuthIdentity, { | ||
persist: false, | ||
}) | ||
auth_identity: Rel<AuthIdentity> | ||
|
||
@Property({ columnType: "jsonb", nullable: true }) | ||
user_metadata: Record<string, unknown> | null | ||
|
||
@Property({ columnType: "jsonb", nullable: true }) | ||
provider_metadata: Record<string, unknown> | null = null | ||
|
||
@Property({ | ||
onCreate: () => new Date(), | ||
columnType: "timestamptz", | ||
defaultRaw: "now()", | ||
}) | ||
created_at: Date | ||
|
||
@Property({ | ||
onCreate: () => new Date(), | ||
onUpdate: () => new Date(), | ||
columnType: "timestamptz", | ||
defaultRaw: "now()", | ||
import { model } from "@medusajs/framework/utils" | ||
import { AuthIdentity } from "./auth-identity" | ||
|
||
export const ProviderIdentity = model | ||
.define("provider_identity", { | ||
id: model.id().primaryKey(), | ||
entity_id: model.text(), | ||
provider: model.text(), | ||
auth_identity: model.belongsTo(() => AuthIdentity, { | ||
mappedBy: "provider_identities", | ||
}), | ||
user_metadata: model.json().nullable(), | ||
provider_metadata: model.json().nullable(), | ||
}) | ||
updated_at: Date | ||
|
||
@BeforeCreate() | ||
@OnInit() | ||
onCreate() { | ||
this.id = generateEntityId(this.id, "provid") | ||
this.auth_identity_id ??= this.auth_identity?.id ?? null | ||
} | ||
} | ||
.indexes([ | ||
{ | ||
name: "IDX_provider_identity_provider_entity_id", | ||
on: ["entity_id", "provider"], | ||
unique: true, | ||
}, | ||
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NOTE: The entities were not soft deletable and are now by default, this is why there is this migration.