-
Notifications
You must be signed in to change notification settings - Fork 1.1k
refactor: change primary key of verifiable and recovery addresses #4138
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| // Copyright © 2024 Ory Corp | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package gomigrations | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "path/filepath" | ||
| "runtime" | ||
|
|
||
| "github.com/gobuffalo/pop/v6" | ||
| "github.com/pkg/errors" | ||
|
|
||
| "github.com/ory/x/popx" | ||
| ) | ||
|
|
||
| func path() string { | ||
| _, file, line, _ := runtime.Caller(1) | ||
| return fmt.Sprintf("%s:%d", filepath.Base(file), line) | ||
| } | ||
|
|
||
| var ChangeAddressesPK = []popx.Migration{ | ||
| { | ||
| Version: "20241001000000000000", | ||
| Path: path(), | ||
| Name: "Change primary key for identity_verifiable_addresses", | ||
| Direction: "up", | ||
| Type: "go", | ||
| DBType: "cockroach", | ||
| RunnerNoTx: func(m popx.Migration, c *pop.Connection) error { | ||
| _, err := c.Store.Exec("ALTER TABLE identity_verifiable_addresses ALTER PRIMARY KEY USING COLUMNS (identity_id,id)") | ||
| return errors.WithStack(err) | ||
| }, | ||
| }, | ||
| { | ||
| Version: "20241001000000000000", | ||
| Path: path(), | ||
| Name: "Revert primary key for identity_verifiable_addresses", | ||
| Direction: "down", | ||
| Type: "go", | ||
| DBType: "cockroach", | ||
| RunnerNoTx: func(m popx.Migration, c *pop.Connection) error { | ||
| _, err := c.Store.Exec("ALTER TABLE identity_verifiable_addresses ALTER PRIMARY KEY USING COLUMNS (id)") | ||
| return errors.WithStack(err) | ||
| }, | ||
| }, | ||
| { | ||
| Version: "20241001000000000001", | ||
| Path: path(), | ||
| Name: "Change primary key for identity_recovery_addresses", | ||
| Direction: "up", | ||
| Type: "go", | ||
| DBType: "cockroach", | ||
| RunnerNoTx: func(m popx.Migration, c *pop.Connection) error { | ||
| _, err := c.Store.Exec("ALTER TABLE identity_recovery_addresses ALTER PRIMARY KEY USING COLUMNS (identity_id,id)") | ||
| return errors.WithStack(err) | ||
| }, | ||
| }, | ||
| { | ||
| Version: "20241001000000000001", | ||
| Path: path(), | ||
| Name: "Revert primary key for identity_recovery_addresses", | ||
| Direction: "down", | ||
| Type: "go", | ||
| DBType: "cockroach", | ||
| RunnerNoTx: func(m popx.Migration, c *pop.Connection) error { | ||
| _, err := c.Store.Exec("ALTER TABLE identity_recovery_addresses ALTER PRIMARY KEY USING COLUMNS (id)") | ||
| return errors.WithStack(err) | ||
| }, | ||
| }, | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| ALTER TABLE identity_verifiable_addresses | ||
| DROP FOREIGN KEY identity_verifiable_addresses_ibfk_1; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess we need this because the primary key won't be deleted if there is still a foreign key using it, correct? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exactly. |
||
|
|
||
| ALTER TABLE identity_verifiable_addresses | ||
| DROP PRIMARY KEY, | ||
| ADD PRIMARY KEY (id), | ||
| ADD CONSTRAINT identity_verifiable_addresses_ibfk_1 FOREIGN KEY (identity_id) REFERENCES identities(id) ON DELETE CASCADE; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| ALTER TABLE identity_verifiable_addresses | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this missing a unique index for id? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure, can you explain why we need that unique index? If there is any FK to this table, that should always use the PK and not just the |
||
| DROP PRIMARY KEY, | ||
| ADD PRIMARY KEY (identity_id, id), | ||
| ADD UNIQUE KEY identity_verifiable_addresses_id_uq_idx (id); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ALTER TABLE identity_verifiable_addresses | ||
| DROP CONSTRAINT identity_verifiable_addresses_pkey, | ||
| ADD PRIMARY KEY (id); |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,17 @@ | ||||||||||||||||||||||
| CREATE UNIQUE INDEX identity_verifiable_addresses_id_uq_idx ON identity_verifiable_addresses (id); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ALTER TABLE identity_verification_codes | ||||||||||||||||||||||
| DROP CONSTRAINT identity_verification_codes_identity_verifiable_addresses_id_fk; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ALTER TABLE identity_verification_tokens | ||||||||||||||||||||||
| DROP CONSTRAINT identity_verification_tokens_identity_verifiable_address_i_fkey; | ||||||||||||||||||||||
|
Comment on lines
+3
to
+7
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do these need to be dropped and recreated? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes: |
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ALTER TABLE identity_verifiable_addresses | ||||||||||||||||||||||
| DROP CONSTRAINT identity_verifiable_addresses_pkey, | ||||||||||||||||||||||
| ADD PRIMARY KEY (identity_id, id); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ALTER TABLE identity_verification_codes | ||||||||||||||||||||||
| ADD CONSTRAINT identity_verification_codes_identity_verifiable_addresses_id_fk FOREIGN KEY (identity_verifiable_address_id) REFERENCES identity_verifiable_addresses(id) ON DELETE CASCADE; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ALTER TABLE identity_verification_tokens | ||||||||||||||||||||||
| ADD CONSTRAINT identity_verification_tokens_identity_verifiable_address_i_fkey FOREIGN KEY (identity_verifiable_address_id) REFERENCES identity_verifiable_addresses(id) ON DELETE CASCADE; | ||||||||||||||||||||||
|
Comment on lines
+13
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As the PK changed, the foreign key should also change.
Suggested change
|
||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| CREATE TABLE IF NOT EXISTS "_identity_verifiable_addresses_tmp" ( | ||
| "id" TEXT PRIMARY KEY, | ||
| "status" TEXT NOT NULL, | ||
| "via" TEXT NOT NULL, | ||
| "verified" bool NOT NULL, | ||
| "value" TEXT NOT NULL, | ||
| "verified_at" DATETIME, | ||
| "identity_id" TEXT NOT NULL, | ||
| "created_at" DATETIME NOT NULL, | ||
| "updated_at" DATETIME NOT NULL, | ||
| "nid" TEXT NOT NULL, | ||
| FOREIGN KEY ("identity_id") REFERENCES "identities" ("id") ON UPDATE RESTRICT ON DELETE CASCADE, | ||
| FOREIGN KEY ("nid") REFERENCES "networks" ("id") ON UPDATE RESTRICT ON DELETE CASCADE | ||
| ); | ||
|
|
||
| INSERT INTO "_identity_verifiable_addresses_tmp" | ||
| ("id", "status", "via", "verified", "value", "verified_at", "identity_id", "created_at", "updated_at", "nid") | ||
| SELECT | ||
| "id", "status", "via", "verified", "value", "verified_at", "identity_id", "created_at", "updated_at", "nid" | ||
| FROM "identity_verifiable_addresses"; | ||
|
|
||
| DROP TABLE "identity_verifiable_addresses"; | ||
| ALTER TABLE "_identity_verifiable_addresses_tmp" RENAME TO "identity_verifiable_addresses"; | ||
|
|
||
| CREATE UNIQUE INDEX IF NOT EXISTS "identity_verifiable_addresses_status_via_uq_idx" ON "identity_verifiable_addresses" (nid, via, value); | ||
| CREATE INDEX IF NOT EXISTS "identity_verifiable_addresses_status_via_idx" ON "identity_verifiable_addresses" (nid, via, value); | ||
| CREATE INDEX IF NOT EXISTS identity_recovery_addresses_nid_id_idx ON identity_recovery_addresses (nid, id); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| CREATE TABLE IF NOT EXISTS "_identity_verifiable_addresses_tmp" ( | ||
| "id" TEXT NOT NULL, | ||
| "status" TEXT NOT NULL, | ||
| "via" TEXT NOT NULL, | ||
| "verified" bool NOT NULL, | ||
| "value" TEXT NOT NULL, | ||
| "verified_at" DATETIME, | ||
| "identity_id" TEXT NOT NULL, | ||
| "created_at" DATETIME NOT NULL, | ||
| "updated_at" DATETIME NOT NULL, | ||
| "nid" TEXT NOT NULL, | ||
| PRIMARY KEY ("identity_id","id"), | ||
| FOREIGN KEY ("identity_id") REFERENCES "identities" ("id") ON UPDATE RESTRICT ON DELETE CASCADE, | ||
| FOREIGN KEY ("nid") REFERENCES "networks" ("id") ON UPDATE RESTRICT ON DELETE CASCADE | ||
| ); | ||
|
|
||
| INSERT INTO "_identity_verifiable_addresses_tmp" | ||
| ("id", "status", "via", "verified", "value", "verified_at", "identity_id", "created_at", "updated_at", "nid") | ||
| SELECT | ||
| "id", "status", "via", "verified", "value", "verified_at", "identity_id", "created_at", "updated_at", "nid" | ||
| FROM "identity_verifiable_addresses"; | ||
|
|
||
| DROP TABLE "identity_verifiable_addresses"; | ||
| ALTER TABLE "_identity_verifiable_addresses_tmp" RENAME TO "identity_verifiable_addresses"; | ||
|
|
||
| CREATE UNIQUE INDEX "identity_verifiable_addresses_status_via_uq_idx" ON "identity_verifiable_addresses" (nid, via, value); | ||
| CREATE UNIQUE INDEX "identity_verifiable_addresses_id_uq_idx" ON "identity_verifiable_addresses" (id); | ||
| CREATE INDEX "identity_verifiable_addresses_status_via_idx" ON "identity_verifiable_addresses" (nid, via, value); | ||
| CREATE INDEX identity_verifiable_addresses_nid_id_idx ON identity_recovery_addresses (nid, id); | ||
| CREATE INDEX identity_verifiable_addresses_id_nid_idx ON identity_recovery_addresses (id, nid); |
Uh oh!
There was an error while loading. Please reload this page.