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: old unique constraint remnance #9882

Closed

Conversation

mistercrunch
Copy link
Member

SUMMARY

closes #8871

When constraints names aren't defined in sqlalchemy/alembic, they are non-deterministic, and it makes it hard/impossible to delete them in subsequent migrations. Here a bad constraint was created originally without a name, and some other migration tries to re-create it, and may fail on a subset of database engines. Also I think sqlite doesn't support constraint making the whole thing more of a headache.

I decided to go back to the first migration and prevent the constraint to be created in the first place. Try to delete the constraint in the subsequent migrations, and finally create the appropriate one.

TODO: We should squash [at least a subset of] migrations eventually.

@codecov-commenter
Copy link

codecov-commenter commented May 22, 2020

Codecov Report

Merging #9882 into master will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #9882   +/-   ##
=======================================
  Coverage   71.24%   71.24%           
=======================================
  Files         584      584           
  Lines       30789    30789           
  Branches     3234     3238    +4     
=======================================
+ Hits        21936    21937    +1     
+ Misses       8744     8742    -2     
- Partials      109      110    +1     
Flag Coverage Δ
#cypress 53.84% <ø> (+0.01%) ⬆️
#javascript 59.44% <ø> (ø)
#python 71.39% <100.00%> (ø)
Impacted Files Coverage Δ
superset/connectors/sqla/models.py 88.60% <100.00%> (ø)
...rontend/src/explore/components/PropertiesModal.tsx 16.66% <0.00%> (ø)
.../src/dashboard/components/gridComponents/Chart.jsx 87.64% <0.00%> (+1.12%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ee99196...69833db. Read the comment docs.

Copy link
Member

@villebro villebro left a comment

Choose a reason for hiding this comment

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

I agree with the general direction here, but would prefer to hear @john-bodley 's comments prior to proceeding. In general I think we should squash all migrations for the 1.0 release and start from a clean slate, with proper constraint naming conventions and setting the bar much higher with up/down migration testing. Doing so should make the migrations much more robust, and should make it possible to use pretty much any proper SqlAlchemy supported RDBMS as a backend database going forward.

Comment on lines +376 to +380
__table_args__ = (
UniqueConstraint(
"database_id", "schema", "table_name", name="_customer_location_uc",
),
)
Copy link
Member

Choose a reason for hiding this comment

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

Ping @john-bodley . I've been meaning to bring this up for discussion, as I also feel schema should be included in the constraint. However, I remember you had done work in this area before, and there was some reason why it wasn't possible to include schema in the unique constraint.

Copy link
Member

Choose a reason for hiding this comment

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

I don't think this is a viable solution based on how different engine treat uniqueness for nullable columns (schema in this case). This is documented in #5449 which is an open PR from over two years ago but was updated last year to remedy the nullable issue. Though it's not perfect, i.e., ideally the constraint should reside within the database model I sense it's the only viable solution unless we special case the various engines.

Copy link
Member Author

Choose a reason for hiding this comment

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

hey @john-bodley @villebro , sorry to bring this back, but how about we just get rid of the constraint altogether? You want 2 datasets pointing to the same table, why not?

At least we should alter the old migration that creates the old/bad constraint we then cannot delete successfuly

Copy link
Member

Choose a reason for hiding this comment

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

Having multiple Superset datasources with the same schema and table name for a given database does not sense to me as it is not easily apparent from the current UI which datasource is being referenced as the name if the primary indicator throughout the application.

@villebro villebro requested a review from john-bodley May 25, 2020 06:31
@john-bodley
Copy link
Member

@villebro regarding the the naming conventions I agree we should explicitly define constraints (per the naming conventions) when we define the models to ensure consistency. Note there are a number of migrations (example) which leverage this logic. Note we also have utility functions (generic_find_fk_constraint_name, generic_find_uq_constraint_name, etc.) to obtain the constraints.

Copy link
Member

@john-bodley john-bodley left a comment

Choose a reason for hiding this comment

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

@rvanaalderen
Copy link

Question: Will this PR fix bugreport: #8871?
If yes, when will this PR be merged?

@mistercrunch
Copy link
Member Author

mistercrunch commented Oct 3, 2020

@rvanaalderen: maybe. Though one solution is to just go and DROP the bad constraint in your database. It's a one time fix to any environment. Of course we should fix it so people don't run into it anymore, but as highlighted here, it's not trivial

@MaryamJFard
Copy link

@mistercrunch Hi there, what do you mean by "manually delete the constraint using DML against your database"? How am I going to do it in superset?

@FishermanZzhang FishermanZzhang mentioned this pull request Jul 22, 2021
8 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Integrity error, probably unique constraint (two mysql databases with same tables)
6 participants