- 
                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?
Conversation
          Codecov ReportAttention: Patch coverage is  
 
 Additional details and impacted files@@            Coverage Diff             @@
##           master    #4138      +/-   ##
==========================================
- Coverage   78.61%   78.58%   -0.04%     
==========================================
  Files         378      380       +2     
  Lines       26936    27013      +77     
==========================================
+ Hits        21177    21228      +51     
- Misses       4147     4172      +25     
- Partials     1612     1613       +1     ☔ View full report in Codecov by Sentry.  | 
    
        
          
                persistence/sql/migrations/sql/20210817181232000000_unique_credentials.sqlite3.down.sql
          
            Show resolved
            Hide resolved
        
      | @@ -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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
exactly.
| 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; | 
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.
Do these need to be dropped and recreated?
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.
Yes:
ERROR: cannot drop constraint identity_recovery_addresses_pkey on table identity_recovery_addresses because other objects depend on it (SQLSTATE 2BP01)
| @@ -0,0 +1,3 @@ | |||
| ALTER TABLE identity_verifiable_addresses | |||
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.
Isn't this missing a unique index for id?
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.
added!
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.
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 id column.
| ALTER TABLE identity_recovery_codes | ||
| DROP FOREIGN KEY identity_recovery_codes_identity_recovery_addresses_id_fk, | ||
| DROP FOREIGN KEY identity_recovery_codes_identity_id_fk; | ||
| 
               | 
          ||
| ALTER TABLE identity_recovery_tokens | ||
| DROP FOREIGN KEY identity_recovery_tokens_identity_id_fk, | ||
| DROP FOREIGN KEY identity_recovery_tokens_identity_recovery_addresses_id_fk; | 
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.
Do we need to remove/recreate the child's foreign key if we change a constraint in the other table?
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.
This dance is only necessary on MySQL.
In this particular case, yes.
803208b    to
    ca35cfb      
    Compare
  
    …y_recovery_addresses
ca35cfb    to
    792cee3      
    Compare
  
    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.
I think that the FKs should also become composite, so we don't need an additional unique index that serves no real purpose.
| @@ -0,0 +1,3 @@ | |||
| ALTER TABLE identity_verifiable_addresses | |||
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.
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 id column.
| 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; | 
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.
As the PK changed, the foreign key should also change.
| 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; | |
| 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(identity_id, 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(identity_id, id) ON DELETE CASCADE; | 
This changes improves lookup performance for verifiable and recovery addresses.
Please be aware that this primary key change may require exclusive locks in your database. Test your upgrade before applying it in production.