-
Notifications
You must be signed in to change notification settings - Fork 974
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: change primary key on identity_verifiable_addresses and identit…
…y_recovery_addresses
- Loading branch information
Showing
19 changed files
with
343 additions
and
58 deletions.
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
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
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
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
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
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,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) | ||
}, | ||
}, | ||
} |
7 changes: 7 additions & 0 deletions
7
...e/sql/migrations/sql/20241001000000000000_identity_verifiable_addresses_pk.mysql.down.sql
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,7 @@ | ||
ALTER TABLE identity_verifiable_addresses | ||
DROP FOREIGN KEY identity_verifiable_addresses_ibfk_1; | ||
|
||
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; |
4 changes: 4 additions & 0 deletions
4
...nce/sql/migrations/sql/20241001000000000000_identity_verifiable_addresses_pk.mysql.up.sql
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,4 @@ | ||
ALTER TABLE identity_verifiable_addresses | ||
DROP PRIMARY KEY, | ||
ADD PRIMARY KEY (identity_id, id), | ||
ADD UNIQUE KEY identity_verifiable_addresses_id_uq_idx (id); |
3 changes: 3 additions & 0 deletions
3
...ql/migrations/sql/20241001000000000000_identity_verifiable_addresses_pk.postgres.down.sql
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,3 @@ | ||
ALTER TABLE identity_verifiable_addresses | ||
DROP CONSTRAINT identity_verifiable_addresses_pkey, | ||
ADD PRIMARY KEY (id); |
17 changes: 17 additions & 0 deletions
17
.../sql/migrations/sql/20241001000000000000_identity_verifiable_addresses_pk.postgres.up.sql
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,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; | ||
|
||
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; |
27 changes: 27 additions & 0 deletions
27
.../sql/migrations/sql/20241001000000000000_identity_verifiable_addresses_pk.sqlite.down.sql
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,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); |
30 changes: 30 additions & 0 deletions
30
...ce/sql/migrations/sql/20241001000000000000_identity_verifiable_addresses_pk.sqlite.up.sql
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,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); |
Oops, something went wrong.