Skip to content

[IMP] _change_foreign_key_refs: Add extra_where params#246

Merged
StefanRijnhart merged 1 commit intoOCA:masterfrom
Tecnativa:_change_foreign_key_refs-extra_where
Oct 6, 2021
Merged

[IMP] _change_foreign_key_refs: Add extra_where params#246
StefanRijnhart merged 1 commit intoOCA:masterfrom
Tecnativa:_change_foreign_key_refs-extra_where

Conversation

@pedrobaeza
Copy link
Member

Adding the new argument extra_where, we are able to change a FK reference depending on an extra condition, so not all ocurrences are replaced.

This is useful for example when we have a unique record for all companies, and on new version, there should be one record per company. We perform the split, and we need to replace references for only a specific company (extra_where="AND company_id = X").

Retro-compatibility is kept as the argument is optional, and a safeguard has been introduced for skipping models where the condition can't be fulfilled if the field doesn't exist (so we are not able to say if the reference should be replaced or not).

@Tecnativa TT28115

@pedrobaeza
Copy link
Member Author

Being used in OCA/OpenUpgrade#2379

@pedrobaeza

This comment has been minimized.

@pedrobaeza pedrobaeza force-pushed the _change_foreign_key_refs-extra_where branch 2 times, most recently from daba54f to b2712d3 Compare September 29, 2021 22:19
Copy link
Contributor

@MiquelRForgeFlow MiquelRForgeFlow left a comment

Choose a reason for hiding this comment

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

LGTM

@pedrobaeza
Copy link
Member Author

pedrobaeza commented Sep 30, 2021

@StefanRijnhart do you oppose to this change, as you were the original author of the method?

env.cr.execute('ROLLBACK TO SAVEPOINT sp1')
if error.pgcode != UNIQUE_VIOLATION:
raise
elif error.pgcode == UNDEFINED_COLUMN:
Copy link
Member

@StefanRijnhart StefanRijnhart Oct 5, 2021

Choose a reason for hiding this comment

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

Isn't the exception raised already at this point?
Also, I would prefer that you check if extra_where is actually set before you assume it's causing the error here.

Copy link
Member Author

@pedrobaeza pedrobaeza Oct 5, 2021

Choose a reason for hiding this comment

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

I don't get your first sentence, but yes, we can check if extra_where is set. Does this looks good to you?:

Suggested change
elif error.pgcode == UNDEFINED_COLUMN:
elif error.pgcode == UNDEFINED_COLUMN:
if not extra_where:
raise

Copy link
Member

Choose a reason for hiding this comment

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

Yes, agree with that.

What I mean with my first sentence, is that if error.pgcode == UNDEFINED_COLUMN is true, then error.pgcode != UNIQUE_VIOLATION is also true in which case line 60 raises before your code can take effect.

Copy link
Member Author

Choose a reason for hiding this comment

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

I get you. What about now?

Copy link
Member

Choose a reason for hiding this comment

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

Good now, thanks!

Adding the new argument `extra_where`, we are able to change a FK
reference depending on an extra condition, so not all ocurrences are
replaced.

This is useful for example when we have a unique record for all
companies, and on new version, there should be one record per company.
We perform the split, and we need to replace references for only
a specific company (extra_where="AND company_id = X").

Retro-compatibility is kept as the argument is optional, and a safeguard
has been introduced for skipping models where the condition can't be
fulfilled if the field doesn't exist (so we are not able to say if
the reference should be replaced or not).

TT28115
@pedrobaeza pedrobaeza force-pushed the _change_foreign_key_refs-extra_where branch from b2712d3 to 86e621f Compare October 5, 2021 16:58
@OCA-git-bot
Copy link
Contributor

This PR has the approved label and has been created more than 5 days ago. It should therefore be ready to merge by a maintainer (or a PSC member if the concerned addon has no declared maintainer). 🤖

@StefanRijnhart StefanRijnhart merged commit 6d97dfc into OCA:master Oct 6, 2021
@pedrobaeza pedrobaeza deleted the _change_foreign_key_refs-extra_where branch October 6, 2021 08:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants