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

[migration] add unique constraint on dashboard_slices table #7880

Conversation

graceguo-supercat
Copy link

@graceguo-supercat graceguo-supercat commented Jul 16, 2019

CATEGORY

Choose one

  • Bug Fix
  • Enhancement (new features, refinement)
  • Refactor
  • Add tests
  • Build / Development Environment
  • Documentation

SUMMARY

Superset didn't have unique constraint on dashboard_sliced table. In dashboard front-end code and when saving dashboard data to db, we added logic to prevent same slice id added into dashboard, but still found some duplicated entries in the dashboard_slices table.

When user tried to remove the duplicated slice_id from dashboard, they will see following error, and they cannot remove that duplicated slice:
Screen Shot 2018-11-20 at 1 19 54 PM (5)

Solution:

  1. remove duplicated entries in many-to-many relation tbl dashboard_slices
  2. add unique constraint
  3. update the dashboard_slices model to include the uniqueness constraint

TEST PLAN

run following query from database, should get 0 row:

SELECT dashboard_id, slice_id, count(*)
from dashboard_slices
group by dashboard_id, slice_id
having count(*) > 1
mysql> SHOW CREATE TABLE dashboard_slices;

| dashboard_slices | CREATE TABLE `dashboard_slices` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `dashboard_id` int(11) DEFAULT NULL,
  `slice_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uq_dashboard_slice` (`dashboard_id`,`slice_id`),
  KEY `dashboard_id` (`dashboard_id`),
  KEY `slice_id` (`slice_id`),
  CONSTRAINT `dashboard_slices_ibfk_1` FOREIGN KEY (`dashboard_id`) REFERENCES `dashboards` (`id`),
  CONSTRAINT `dashboard_slices_ibfk_2` FOREIGN KEY (`slice_id`) REFERENCES `slices` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=180250 DEFAULT CHARSET=latin1 |

ADDITIONAL INFORMATION

  • Has associated issue:
  • Changes UI
  • Requires DB Migration.
  • Confirm DB Migration upgrade and downgrade tested.
  • Introduces new feature or API
  • Removes existing feature or API

REVIEWERS

@mistercrunch @john-bodley @michellethomas @etr2460

@graceguo-supercat graceguo-supercat force-pushed the gg-AddUniqueConstraintDashboardSlicesTable branch from 3044d05 to 0e1f0b7 Compare July 16, 2019 19:57
@codecov-io
Copy link

codecov-io commented Jul 16, 2019

Codecov Report

Merging #7880 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #7880   +/-   ##
=======================================
  Coverage   65.83%   65.83%           
=======================================
  Files         461      461           
  Lines       22210    22210           
  Branches     2425     2425           
=======================================
  Hits        14621    14621           
  Misses       7468     7468           
  Partials      121      121
Impacted Files Coverage Δ
superset/models/core.py 82.95% <ø> (ø) ⬆️

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 92eed8a...5357679. Read the comment docs.

"uq_dashboard_slice", "dashboard_slices", ["dashboard_id", "slice_id"]
)
except Exception:
pass
Copy link
Member

Choose a reason for hiding this comment

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

Maybe a logging.error / logging.exception(e)?

Copy link
Author

Choose a reason for hiding this comment

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

fixed.

@graceguo-supercat graceguo-supercat force-pushed the gg-AddUniqueConstraintDashboardSlicesTable branch 2 times, most recently from a9f343e to dfc95ef Compare July 16, 2019 21:15
@john-bodley
Copy link
Member

@graceguo-supercat you'll have to update the model to include the uniqueness constraint as well.

@graceguo-supercat graceguo-supercat force-pushed the gg-AddUniqueConstraintDashboardSlicesTable branch 3 times, most recently from 0b3eff4 to 5d5f49f Compare July 17, 2019 01:01
@pull-request-size pull-request-size bot added size/L and removed size/M labels Jul 17, 2019
@graceguo-supercat graceguo-supercat force-pushed the gg-AddUniqueConstraintDashboardSlicesTable branch from 5d5f49f to 6dd187a Compare July 17, 2019 21:22
@graceguo-supercat
Copy link
Author

graceguo-supercat commented Jul 17, 2019

@mistercrunch This PR always failed at a postgres unit test, even after i rebased onto master branch. But i am not sure this is related to my code change. Do you have a clue why it is failing? thanks!

======================================================================
FAIL: test_export_2_dashboards (tests.import_export_tests.ImportExportTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/travis/build/apache/incubator-superset/tests/import_export_tests.py", line 259, in test_export_2_dashboards
    self.assert_dash_equals(world_health_dash, exported_dashboards[1])
  File "/home/travis/build/apache/incubator-superset/tests/import_export_tests.py", line 151, in assert_dash_equals
    self.assert_slice_equals(e_slc, a_slc)
  File "/home/travis/build/apache/incubator-superset/tests/import_export_tests.py", line 190, in assert_slice_equals
    self.assertEquals(expected_slc.viz_type, actual_slc.viz_type)
AssertionError: 'big_number' != 'filter_box'
- big_number
+ filter_box
    """Fail immediately, with the given message."""
>>  raise self.failureException("'big_number' != 'filter_box'\n- big_number\n+ filter_box\n")

-------------------- >> begin captured logging << --------------------

@graceguo-supercat
Copy link
Author

graceguo-supercat commented Jul 18, 2019

trying to fix flaky unit test in #7898.

@graceguo-supercat graceguo-supercat force-pushed the gg-AddUniqueConstraintDashboardSlicesTable branch from 6dd187a to 3d4eda2 Compare July 19, 2019 22:39
@graceguo-supercat
Copy link
Author

graceguo-supercat commented Jul 19, 2019

after rebased onto #7898, no flaky failure any more!
ping @john-bodley


def downgrade():
try:
op.drop_constraint(u"uq_dashboard_slice", "dashboard_slices", type_="unique")
Copy link
Member

Choose a reason for hiding this comment

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

Nit. You don't need the u prefix here.

Copy link
Member

Choose a reason for hiding this comment

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

See the note above.

)

# add unique constraint
try:
Copy link
Member

Choose a reason for hiding this comment

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

@graceguo-supercat if you use the batch_alter_table context manager (see here for an example) you shouldn't need to special case the the SQLite logic.

Copy link
Author

Choose a reason for hiding this comment

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

fix as suggested.

@graceguo-supercat graceguo-supercat force-pushed the gg-AddUniqueConstraintDashboardSlicesTable branch from 3d4eda2 to d24b59f Compare July 22, 2019 22:32
1. remove duplicated entries in many-to-many relation tbl dashboard_slices
2. add unique constraint on tbl
3. update the model to include the uniqueness constraint
@graceguo-supercat graceguo-supercat force-pushed the gg-AddUniqueConstraintDashboardSlicesTable branch from d24b59f to 5357679 Compare July 22, 2019 22:41
@graceguo-supercat graceguo-supercat merged commit 9dd6a38 into apache:master Jul 22, 2019
alex-mark pushed a commit to alex-mark/incubator-superset that referenced this pull request Jul 29, 2019
)

1. remove duplicated entries in many-to-many relation tbl dashboard_slices
2. add unique constraint on tbl
3. update the model to include the uniqueness constraint
@graceguo-supercat graceguo-supercat deleted the gg-AddUniqueConstraintDashboardSlicesTable branch August 8, 2019 20:38
@mistercrunch mistercrunch added 🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels 🚢 0.34.0 labels Feb 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels size/L 🚢 0.34.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants