Skip to content
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: Make admin recovery to work without emails #1419 #1750

Merged
merged 24 commits into from
Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
a750020
Make admin recovery to work without emails, #1419
abador Sep 14, 2021
c6f3d98
CR fixes, #1419
abador Sep 15, 2021
a6be25c
Docs, #1419
abador Sep 15, 2021
3492ca5
Update persistence/sql/persister_recovery.go
abador Sep 16, 2021
45ec59b
Migration with foreign key for sqlite, #1419
abador Sep 22, 2021
e8c943f
Merge branch 'master' into issue-1419-admin-recovery-without-email
abador Sep 29, 2021
142d4dd
Merge branch 'master' into issue-1419-admin-recovery-without-email
aeneasr Oct 11, 2021
db4b63f
chore: code review
aeneasr Oct 12, 2021
823050a
Merge branch 'master' into issue-1419-admin-recovery-without-email
abador Oct 21, 2021
f57f1bb
fix tests
abador Oct 21, 2021
99441b1
fix imports
abador Oct 21, 2021
58f696d
fix tests
abador Oct 25, 2021
5272633
fix tests
abador Oct 25, 2021
5eaedc4
Merge branch 'master' into issue-1419-admin-recovery-without-email
abador Oct 25, 2021
1fc3eb4
try to fix tests
abador Oct 25, 2021
48bcb38
try to fix tests
abador Oct 25, 2021
d19eede
try to fix tests
abador Oct 26, 2021
08a6d1f
try to fix tests
abador Oct 26, 2021
462df89
Merge branch 'master' into issue-1419-admin-recovery-without-email
abador Oct 26, 2021
6810666
try to fix tests
abador Oct 26, 2021
053da6b
Merge remote-tracking branch 'origin/issue-1419-admin-recovery-withou…
abador Oct 26, 2021
0c72018
Cleanup tests
abador Oct 26, 2021
b3ff59f
Merge branch 'master' into issue-1419-admin-recovery-without-email
abador Oct 28, 2021
9bfe61e
Merge branch 'master' into issue-1419-admin-recovery-without-email
abador Oct 28, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/docs/.static/api.json
Original file line number Diff line number Diff line change
Expand Up @@ -2258,7 +2258,7 @@
},
"/recovery/link": {
"post": {
"description": "This endpoint creates a recovery link which should be given to the user in order for them to recover\n(or activate) their account.",
"description": "This endpoint creates a recovery link which should be given to the user in order for them to recover\n(or activate) their account. It's possible to generate a link for an account without a recovery address.",
"operationId": "adminCreateSelfServiceRecoveryLink",
"requestBody": {
"content": {
Expand Down
5 changes: 4 additions & 1 deletion docs/docs/admin/managing-users-identities.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ to use for this user. Similarly, the trait key/values depend on your schema as
well. The command shown does not create a password for the identity or any other
type of credential. Instead, we will use another REST call to create a recovery
link (here "invite link" is probably more appropriate, but the flow uses an
account recovery link).
account recovery link). It's possible to generate a link for an account without
a recovery address via the admin API, but if the recovery link expires the user
won't be able to reinitiate the flow by himself as long as the recovery address
has been added.

To create the account recovery link, use:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"id": "1b667e6d-8fda-4194-a765-08185185d7e4",
"recovery_address": null,
"expires_at": "2013-10-07T08:23:19Z",
"issued_at": "2013-10-07T08:23:19Z"
"issued_at": "2013-10-07T08:23:19Z",
"identity_id": "a251ebc2-880c-4f76-a8f3-38e6940eab0e"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"id": "5529d454-2946-404e-b681-d950f8657fd0",
"recovery_address": null,
"expires_at": "2000-01-01T00:00:00Z",
"issued_at": "2000-01-01T00:00:00Z"
"issued_at": "2000-01-01T00:00:00Z",
"identity_id": "a251ebc2-880c-4f76-a8f3-38e6940eab0e"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"id": "77ca3f5c-cd39-488b-9f1d-cc7166d14bdc",
"recovery_address": null,
"expires_at": "2000-01-01T00:00:00Z",
"issued_at": "2000-01-01T00:00:00Z"
"issued_at": "2000-01-01T00:00:00Z",
"identity_id": "a251ebc2-880c-4f76-a8f3-38e6940eab0e"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
UPDATE identity_recovery_tokens SET identity_id = 'a251ebc2-880c-4f76-a8f3-38e6940eab0e' WHERE id = '5529d454-2946-404e-b681-d950f8657fd0';
UPDATE identity_recovery_tokens SET identity_id = 'a251ebc2-880c-4f76-a8f3-38e6940eab0e' WHERE id = '77ca3f5c-cd39-488b-9f1d-cc7166d14bdc';
UPDATE identity_recovery_tokens SET identity_id = 'a251ebc2-880c-4f76-a8f3-38e6940eab0e' WHERE id = '1b667e6d-8fda-4194-a765-08185185d7e4';

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "identity_recovery_tokens" DROP COLUMN "identity_id";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "identity_recovery_tokens" DROP CONSTRAINT "identity_recovery_tokens_identity_recovery_addresses_id_fk";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE `identity_recovery_tokens` DROP COLUMN `identity_id`;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE `identity_recovery_tokens` MODIFY `identity_recovery_address_id` char(36);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "identity_recovery_tokens" DROP COLUMN "identity_id";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "identity_recovery_tokens" ALTER COLUMN "identity_recovery_address_id" TYPE UUID, ALTER COLUMN "identity_recovery_address_id" DROP NOT NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "_identity_recovery_tokens_tmp" RENAME TO "identity_recovery_tokens";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP INDEX IF EXISTS "identity_recovery_tokens_nid_idx";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "identity_recovery_tokens" DROP CONSTRAINT "identity_recovery_tokens_identity_id_fk_idx";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "identity_recovery_tokens" RENAME COLUMN "identity_recovery_address_id" TO "_identity_recovery_address_id_tmp";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE `identity_recovery_tokens` DROP FOREIGN KEY `identity_recovery_tokens_identity_id_fk_idx`;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE `identity_recovery_tokens` ADD COLUMN `identity_id` char(36);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "identity_recovery_tokens" DROP CONSTRAINT "identity_recovery_tokens_identity_id_fk_idx";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "identity_recovery_tokens" ADD COLUMN "identity_id" UUID;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

DROP TABLE "identity_recovery_tokens";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP INDEX IF EXISTS "identity_recovery_addresses_code_idx";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "identity_recovery_tokens" ADD CONSTRAINT "identity_recovery_tokens_identity_recovery_addresses_id_fk" FOREIGN KEY ("identity_recovery_address_id") REFERENCES "identity_recovery_addresses" ("id") ON UPDATE NO ACTION ON DELETE CASCADE;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "identity_recovery_tokens" ADD COLUMN "identity_recovery_address_id" UUID;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE `identity_recovery_tokens` MODIFY `identity_recovery_address_id` char(36) NOT NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE identity_recovery_tokens SET identity_id=(SELECT identity_id FROM identity_recovery_addresses WHERE id=identity_recovery_address_id) WHERE identity_id = '' OR identity_id IS NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "identity_recovery_tokens" ALTER COLUMN "identity_recovery_address_id" TYPE UUID, ALTER COLUMN "identity_recovery_address_id" SET NOT NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE identity_recovery_tokens SET identity_id=(SELECT identity_id FROM identity_recovery_addresses WHERE id=identity_recovery_address_id) WHERE identity_id = '00000000-0000-0000-0000-000000000000' OR identity_id IS NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INSERT INTO "_identity_recovery_tokens_tmp" (id, token, used, used_at, identity_recovery_address_id, selfservice_recovery_flow_id, created_at, updated_at, expires_at, issued_at, nid) SELECT id, token, used, used_at, identity_recovery_address_id, selfservice_recovery_flow_id, created_at, updated_at, expires_at, issued_at, nid FROM "identity_recovery_tokens";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP INDEX IF EXISTS "identity_recovery_addresses_code_uq_idx";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "identity_recovery_tokens" DROP COLUMN "_identity_recovery_address_id_tmp";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE "identity_recovery_tokens" SET "identity_recovery_address_id" = "_identity_recovery_address_id_tmp";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DELETE FROM identity_recovery_tokens WHERE identity_recovery_address_id IS NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE `identity_recovery_tokens` MODIFY `identity_id` char(36) NOT NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DELETE FROM identity_recovery_tokens WHERE identity_recovery_address_id IS NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "identity_recovery_tokens" ALTER COLUMN "identity_id" TYPE UUID, ALTER COLUMN "identity_id" SET NOT NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE INDEX "identity_recovery_tokens_nid_idx" ON "_identity_recovery_tokens_tmp" (id, nid);
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CREATE TABLE "_identity_recovery_tokens_tmp" (
"id" TEXT PRIMARY KEY,
"token" TEXT NOT NULL,
"used" bool NOT NULL DEFAULT 'false',
"used_at" DATETIME,
"identity_recovery_address_id" char(36),
"selfservice_recovery_flow_id" char(36),
"created_at" DATETIME NOT NULL,
"updated_at" DATETIME NOT NULL,
"expires_at" DATETIME NOT NULL DEFAULT '2000-01-01 00:00:00',
"issued_at" DATETIME NOT NULL DEFAULT '2000-01-01 00:00:00',
"nid" char(36),
FOREIGN KEY (selfservice_recovery_flow_id) REFERENCES selfservice_recovery_flows (id) ON UPDATE NO ACTION ON DELETE CASCADE,
FOREIGN KEY (identity_recovery_address_id) REFERENCES identity_recovery_addresses (id) ON UPDATE NO ACTION ON DELETE CASCADE
);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "identity_recovery_tokens" ALTER COLUMN "identity_recovery_address_id" SET NOT NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "identity_recovery_tokens" DROP COLUMN "_identity_recovery_address_id_tmp";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE `identity_recovery_tokens` ADD CONSTRAINT `identity_recovery_tokens_identity_id_fk_idx` FOREIGN KEY (`identity_id`) REFERENCES `identities` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "identity_recovery_tokens" ADD CONSTRAINT "identity_recovery_tokens_identity_id_fk_idx" FOREIGN KEY ("identity_id") REFERENCES "identities" ("id") ON UPDATE RESTRICT ON DELETE CASCADE;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE INDEX "identity_recovery_addresses_code_idx" ON "_identity_recovery_tokens_tmp" (token);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE INDEX "identity_recovery_tokens_nid_idx" ON "_identity_recovery_tokens_tmp" (id, nid);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE "identity_recovery_tokens" SET "identity_recovery_address_id" = "_identity_recovery_address_id_tmp";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "identity_recovery_tokens" ADD CONSTRAINT "identity_recovery_tokens_identity_recovery_addresses_id_fk" FOREIGN KEY ("identity_recovery_address_id") REFERENCES "identity_recovery_addresses" ("id") ON UPDATE NO ACTION ON DELETE CASCADE;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE UNIQUE INDEX "identity_recovery_addresses_code_uq_idx" ON "_identity_recovery_tokens_tmp" (token);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE INDEX "identity_recovery_addresses_code_idx" ON "_identity_recovery_tokens_tmp" (token);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "identity_recovery_tokens" ADD COLUMN "identity_recovery_address_id" UUID;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "identity_recovery_tokens" ADD COLUMN "identity_id" UUID;
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CREATE TABLE "_identity_recovery_tokens_tmp" (
"id" TEXT PRIMARY KEY,
"token" TEXT NOT NULL,
"used" bool NOT NULL DEFAULT 'false',
"used_at" DATETIME,
"identity_recovery_address_id" char(36) NOT NULL,
"selfservice_recovery_flow_id" char(36),
"created_at" DATETIME NOT NULL,
"updated_at" DATETIME NOT NULL,
"expires_at" DATETIME NOT NULL DEFAULT '2000-01-01 00:00:00',
"issued_at" DATETIME NOT NULL DEFAULT '2000-01-01 00:00:00',
"nid" char(36),
FOREIGN KEY (identity_recovery_address_id) REFERENCES identity_recovery_addresses (id) ON UPDATE NO ACTION ON DELETE CASCADE,
FOREIGN KEY (selfservice_recovery_flow_id) REFERENCES selfservice_recovery_flows (id) ON UPDATE NO ACTION ON DELETE CASCADE
);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE UNIQUE INDEX "identity_recovery_addresses_code_uq_idx" ON "_identity_recovery_tokens_tmp" (token);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "identity_recovery_tokens" RENAME COLUMN "identity_recovery_address_id" TO "_identity_recovery_address_id_tmp";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE identity_recovery_tokens SET identity_id=(SELECT identity_id FROM identity_recovery_addresses WHERE id=identity_recovery_address_id) WHERE identity_id = '00000000-0000-0000-0000-000000000000' OR identity_id IS NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP INDEX IF EXISTS "identity_recovery_tokens_nid_idx";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INSERT INTO "_identity_recovery_tokens_tmp" (id, token, used, used_at, identity_recovery_address_id, selfservice_recovery_flow_id, created_at, updated_at, expires_at, issued_at, nid) SELECT id, token, used, used_at, identity_recovery_address_id, selfservice_recovery_flow_id, created_at, updated_at, expires_at, issued_at, nid FROM "identity_recovery_tokens";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "identity_recovery_tokens" DROP CONSTRAINT "identity_recovery_tokens_identity_recovery_addresses_id_fk";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "identity_recovery_tokens" RENAME COLUMN "identity_id" TO "_identity_id_tmp";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP INDEX IF EXISTS "identity_recovery_addresses_code_idx";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE "identity_recovery_tokens";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DELETE FROM identity_recovery_tokens WHERE identity_recovery_address_id IS NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "identity_recovery_tokens" ADD COLUMN "identity_id" UUID;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP INDEX IF EXISTS "identity_recovery_addresses_code_uq_idx";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "_identity_recovery_tokens_tmp" RENAME TO "identity_recovery_tokens";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE "identity_recovery_tokens" SET "identity_id" = "_identity_id_tmp";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "_identity_recovery_tokens_tmp" RENAME TO "identity_recovery_tokens";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE identity_recovery_tokens ADD COLUMN identity_id CHAR(36) NULL REFERENCES identities(id) ON DELETE CASCADE ON UPDATE RESTRICT;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "identity_recovery_tokens" ALTER COLUMN "identity_id" SET NOT NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE "identity_recovery_tokens";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE identity_recovery_tokens SET identity_id=(SELECT identity_id FROM identity_recovery_addresses WHERE id=identity_recovery_address_id) WHERE identity_id = '' OR identity_id IS NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "identity_recovery_tokens" DROP COLUMN "_identity_id_tmp";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INSERT INTO "_identity_recovery_tokens_tmp" (id, token, used, used_at, identity_recovery_address_id, selfservice_recovery_flow_id, created_at, updated_at, expires_at, issued_at, nid, identity_id) SELECT id, token, used, used_at, identity_recovery_address_id, selfservice_recovery_flow_id, created_at, updated_at, expires_at, issued_at, nid, identity_id FROM "identity_recovery_tokens";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DELETE FROM identity_recovery_tokens WHERE identity_recovery_address_id IS NULL AND identity_id = '';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "identity_recovery_tokens" ADD CONSTRAINT "identity_recovery_tokens_identity_id_fk_idx" FOREIGN KEY ("identity_id") REFERENCES "identities" ("id") ON UPDATE RESTRICT ON DELETE CASCADE;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE INDEX "identity_recovery_tokens_nid_idx" ON "_identity_recovery_tokens_tmp" (id, nid);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE INDEX "identity_recovery_addresses_code_idx" ON "_identity_recovery_tokens_tmp" (token);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE UNIQUE INDEX "identity_recovery_addresses_code_uq_idx" ON "_identity_recovery_tokens_tmp" (token);
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
CREATE TABLE "_identity_recovery_tokens_tmp" (
"id" TEXT PRIMARY KEY,
"token" TEXT NOT NULL,
"used" bool NOT NULL DEFAULT 'false',
"used_at" DATETIME,
"identity_recovery_address_id" char(36) NOT NULL,
"selfservice_recovery_flow_id" char(36),
"created_at" DATETIME NOT NULL,
"updated_at" DATETIME NOT NULL,
"expires_at" DATETIME NOT NULL DEFAULT '2000-01-01 00:00:00',
"issued_at" DATETIME NOT NULL DEFAULT '2000-01-01 00:00:00',
"nid" char(36),
"identity_id" CHAR(36),
FOREIGN KEY (identity_recovery_address_id) REFERENCES identity_recovery_addresses (id) ON UPDATE NO ACTION ON DELETE CASCADE,
FOREIGN KEY (selfservice_recovery_flow_id) REFERENCES selfservice_recovery_flows (id) ON UPDATE NO ACTION ON DELETE CASCADE,
FOREIGN KEY (identity_id) REFERENCES identities (id) ON UPDATE RESTRICT ON DELETE CASCADE
);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP INDEX IF EXISTS "identity_recovery_tokens_nid_idx";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP INDEX IF EXISTS "identity_recovery_addresses_code_idx";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP INDEX IF EXISTS "identity_recovery_addresses_code_uq_idx";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DELETE FROM identity_recovery_tokens WHERE identity_recovery_address_id IS NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
sql("DELETE FROM identity_recovery_tokens WHERE identity_recovery_address_id IS NULL")
change_column("identity_recovery_tokens", "identity_recovery_address_id", "uuid", {"size": 36})
aeneasr marked this conversation as resolved.
Show resolved Hide resolved
{{ if not .IsSQLite }}
drop_foreign_key("identity_recovery_tokens", "identity_recovery_tokens_identity_id_fk_idx")
{{ end }}
drop_column("identity_recovery_tokens", "identity_id")
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
change_column("identity_recovery_tokens", "identity_recovery_address_id", "uuid", {"size": 36,"null": true})

{{ if .IsSQLite }}
sql("ALTER TABLE identity_recovery_tokens ADD COLUMN identity_id CHAR(36) NULL REFERENCES identities(id) ON DELETE CASCADE ON UPDATE RESTRICT")
{{ else }}
add_column("identity_recovery_tokens", "identity_id", "uuid", {"size": 36,"null": true})
{{ end }}
{{ if or .IsPostgreSQL .IsCockroach }}
sql("UPDATE identity_recovery_tokens SET identity_id=(SELECT identity_id FROM identity_recovery_addresses WHERE id=identity_recovery_address_id) WHERE identity_id = '00000000-0000-0000-0000-000000000000'")
{{ else }}
sql("UPDATE identity_recovery_tokens SET identity_id=(SELECT identity_id FROM identity_recovery_addresses WHERE id=identity_recovery_address_id) WHERE identity_id = ''")
{{ end }}
{{ if not .IsSQLite }}
change_column("identity_recovery_tokens", "identity_id", "uuid", {"size": 36})
add_foreign_key("identity_recovery_tokens", "identity_id", {"identities": ["id"]}, {
"name": "identity_recovery_tokens_identity_id_fk_idx",
"on_delete": "CASCADE",
"on_update": "RESTRICT",
})
{{ end }}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can add foreign keys in sqlite like so:

sql("ALTER TABLE selfservice_login_flows ADD COLUMN nid CHAR(36) NULL REFERENCES networks(id) ON DELETE CASCADE ON UPDATE RESTRICT")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry missed that comment. I'll try to fix it today

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aeneasr if I add a column this way the migrations don't generate properly. The identity_id column is added i one of the first migrations and then is being removed after some temp table is generated and data is copied there. This causes tests to fail since the identity_id column doesn't exist
"sqlite create: table identity_recovery_tokens has no column named identity_id"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you share the full up/down migration?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did it help? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry @aeneasr no it didn't. I still get "sqlite create: table identity_recovery_tokens has no column named identity_id" . The only change that wasn't in the pr is in this file: 20210410175418000063_network.sqlite3.down.sql, but it doesn't matter.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'll take a look

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just checked out your branch, ran rm -rf persistence/sql/migrations/sql/* && make migrations-render-replace and it worked. Not sure what's going on on your end?

Assuming tests pass, is this PR complete in your opinion?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just checked out your branch, ran rm -rf persistence/sql/migrations/sql/* && make migrations-render-replace and it worked. Not sure what's going on on your end?

Assuming tests pass, is this PR complete in your opinion?

make migrations-render-replace works, but tests fail after that the same way. After regenerating migrations the only difference between my pr and local changes is the file I mentioned in the previous comment.

As for the PR I would say it's finished.

13 changes: 6 additions & 7 deletions persistence/sql/persister_recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ import (
"fmt"
"time"

"github.com/ory/kratos/corp"
"github.com/ory/kratos/identity"

"github.com/gobuffalo/pop/v5"
"github.com/gofrs/uuid"

"github.com/ory/x/sqlcon"

"github.com/ory/kratos/corp"
abador marked this conversation as resolved.
Show resolved Hide resolved
"github.com/ory/kratos/identity"
"github.com/ory/kratos/selfservice/flow/recovery"
"github.com/ory/kratos/selfservice/strategy/link"
"github.com/ory/x/sqlcon"
)

var _ recovery.FlowPersister = new(Persister)
Expand Down Expand Up @@ -76,9 +74,10 @@ func (p *Persister) UseRecoveryToken(ctx context.Context, token string) (*link.R

var ra identity.RecoveryAddress
if err := tx.Where("id = ? AND nid = ?", rt.RecoveryAddressID, nid).First(&ra); err != nil {
return sqlcon.HandleError(err)
if !errors.Is(sqlcon.HandleError(err), sqlcon.ErrNoRows) {
return err
}
}

rt.RecoveryAddress = &ra

/* #nosec G201 TableName is static */
Expand Down
Loading