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

Fix ResponseType data migration #381

Merged
merged 1 commit into from Dec 14, 2023

Conversation

Urth
Copy link
Contributor

@Urth Urth commented Oct 21, 2020

If oidc provider is used in a multi database setup it may not be used on
the default database alone. And when used in a database mirror setup the
migration could be executed on a different db alias/transaction.

This can cause migration failures because the ReponseType table is
created on the database passed to the migrate command while the data is
inserted in the database returned from the database router.
Depending on the configuration the ResponseType table may not exist for
that database yet or the ResponseType data was already migrated
resulting in a DatabaseError and IntegrityError respectively.

@nicwolff
Copy link

nicwolff commented Dec 9, 2021

@juanifioren Please merge this! It's preventing us from upgrading Django.

@Urth
Copy link
Contributor Author

Urth commented Dec 9, 2021

If you can upgrade to Django 3.2 this problem is mitigated by https://code.djangoproject.com/ticket/29052

@nicwolff
Copy link

This migration still fails in Django 3.2, because the two ORM calls use different connections – note the different PIDs on these log lines:

2022-01-12 21:00:22.670 UTC [37] LOG:  statement: BEGIN
2022-01-12 21:00:22.671 UTC [37] LOG:  statement: CREATE TABLE "oidc_provider_responsetype" ("id" serial NOT NULL PRIMARY KEY, "value" varchar(30) NOT NULL UNIQUE, "description" varchar(50) NOT NULL)
2022-01-12 21:00:22.870 UTC [37] LOG:  statement: CREATE TABLE "oidc_provider_client_response_types" ("id" serial NOT NULL PRIMARY KEY, "client_id" integer NOT NULL, "responsetype_id" integer NOT NULL)
2022-01-12 21:00:22.980 UTC [37] LOG:  statement: INSERT INTO "oidc_provider_responsetype" ("value", "description") VALUES ('code', 'code (Authorization Code Flow)') RETURNING "oidc_provider_responsetype"."id"
2022-01-12 21:00:22.983 UTC [37] LOG:  statement: INSERT INTO "oidc_provider_responsetype" ("value", "description") VALUES ('id_token', 'id_token (Implicit Flow)') RETURNING "oidc_provider_responsetype"."id"
2022-01-12 21:00:22.985 UTC [37] LOG:  statement: INSERT INTO "oidc_provider_responsetype" ("value", "description") VALUES ('id_token token', 'id_token token (Implicit Flow)') RETURNING "oidc_provider_responsetype"."id"
2022-01-12 21:00:22.987 UTC [37] LOG:  statement: INSERT INTO "oidc_provider_responsetype" ("value", "description") VALUES ('code token', 'code token (Hybrid Flow)') RETURNING "oidc_provider_responsetype"."id"
2022-01-12 21:00:22.988 UTC [37] LOG:  statement: INSERT INTO "oidc_provider_responsetype" ("value", "description") VALUES ('code id_token', 'code id_token (Hybrid Flow)') RETURNING "oidc_provider_responsetype"."id"
2022-01-12 21:00:22.990 UTC [37] LOG:  statement: INSERT INTO "oidc_provider_responsetype" ("value", "description") VALUES ('code id_token token', 'code id_token token (Hybrid Flow)') RETURNING "oidc_provider_responsetype"."id"
2022-01-12 21:00:22.999 UTC [38] LOG:  statement: SET TIME ZONE 'UTC'
2022-01-12 21:00:23.001 UTC [38] LOG:  statement: SELECT "oidc_provider_client"."id", "oidc_provider_client"."name", "oidc_provider_client"."client_id", "oidc_provider_client"."client_secret", "oidc_provider_client"."response_type", "oidc_provider_client"."_redirect_uris", "oidc_provider_client"."date_created", "oidc_provider_client"."client_type", "oidc_provider_client"."jwt_alg", "oidc_provider_client"."contact_email", "oidc_provider_client"."logo", "oidc_provider_client"."terms_url", "oidc_provider_client"."website_url", "oidc_provider_client"."_post_logout_redirect_uris", "oidc_provider_client"."require_consent", "oidc_provider_client"."reuse_consent", "oidc_provider_client"."owner_id", "oidc_provider_client"."_scope" FROM "oidc_provider_client"
2022-01-12 21:00:23.005 UTC [38] LOG:  statement: SELECT "oidc_provider_responsetype"."id", "oidc_provider_responsetype"."value", "oidc_provider_responsetype"."description" FROM "oidc_provider_responsetype" WHERE "oidc_provider_responsetype"."value" = '' LIMIT 21
2022-01-12 21:00:23.005 UTC [38] ERROR:  relation "oidc_provider_responsetype" does not exist at character 129
2022-01-12 21:00:23.005 UTC [38] STATEMENT:  SELECT "oidc_provider_responsetype"."id", "oidc_provider_responsetype"."value", "oidc_provider_responsetype"."description" FROM "oidc_provider_responsetype" WHERE "oidc_provider_responsetype"."value" = '' LIMIT 21
2022-01-12 21:00:23.006 UTC [37] LOG:  statement: ROLLBACK

Please merge this!

If oidc provider is used in a multi database setup it may not be used on
the default database alone. And when used in a database mirror setup the
migration could be executed on a different db alias/transaction.

This can cause migration failures because the ReponseType table is
created on the database passed to the migrate command while the data is
inserted in the database returned from the database router.
Depending on the configuration the ResponseType table may not exist for
that database yet or the ResponseType data was already migrated
resulting in a DatabaseError and IntegrityError respectively.
@juanifioren juanifioren self-assigned this Dec 14, 2023
Copy link
Owner

@juanifioren juanifioren left a comment

Choose a reason for hiding this comment

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

LGTM! thanks for the contribution

@juanifioren juanifioren merged commit fe9b031 into juanifioren:develop Dec 14, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants