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: pass if table is already removed on upgrade #30017

Merged

Conversation

sadpandajoe
Copy link
Member

@sadpandajoe sadpandajoe commented Aug 26, 2024

SUMMARY

Looks like if you try to run the migrations but the sl_ tables don't already exist the migrations will fail. This will just pass the upgrade check if the table doesn't already exist.

  • Added a has_table function to return a boolean if the table exist or not
  • Updated drop_fks_for_table to handle sqlite in the function

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

TESTING INSTRUCTIONS

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

@github-actions github-actions bot added the risk:db-migration PRs that require a DB migration label Aug 26, 2024
@dosubot dosubot bot added change:backend Requires changing the backend risk:breaking-change Issues or PRs that will introduce breaking changes labels Aug 26, 2024
Copy link

codecov bot commented Aug 26, 2024

Codecov Report

Attention: Patch coverage is 8.33333% with 11 lines in your changes missing coverage. Please review.

Project coverage is 83.62%. Comparing base (76d897e) to head (4061377).
Report is 659 commits behind head on master.

Files with missing lines Patch % Lines
superset/migrations/shared/constraints.py 0.00% 8 Missing ⚠️
superset/migrations/shared/utils.py 25.00% 3 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           master   #30017       +/-   ##
===========================================
+ Coverage   60.48%   83.62%   +23.13%     
===========================================
  Files        1931      529     -1402     
  Lines       76236    38277    -37959     
  Branches     8568        0     -8568     
===========================================
- Hits        46114    32010    -14104     
+ Misses      28017     6267    -21750     
+ Partials     2105        0     -2105     
Flag Coverage Δ
hive 48.90% <8.33%> (-0.27%) ⬇️
javascript ?
mysql 76.72% <8.33%> (?)
postgres 76.79% <8.33%> (?)
presto 53.48% <8.33%> (-0.32%) ⬇️
python 83.62% <8.33%> (+20.13%) ⬆️
sqlite 76.26% <8.33%> (?)
unit 60.24% <8.33%> (+2.62%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

if connection.dialect.name != "sqlite":
drop_fks_for_table("sl_dataset_users")
op.drop_table("sl_dataset_users")
except sa.exc.NoSuchTableError:
Copy link
Member

Choose a reason for hiding this comment

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

@sadpandajoe Would you mind adding a method called has_table to https://github.com/apache/superset/blob/master/superset/migrations/shared/utils.py to be reused in these and future migrations?

def has_table(table_name: str) -> bool:
    """
    Check if a table exists in the database.

    :param table_name: The table name
    :returns: True iff the table exists
    """

    insp = inspect(op.get_context().bind)
    return insp.has_table(table_name)

Copy link
Member

Choose a reason for hiding this comment

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

yes, and/or change drop_fks_for_table to receive a fail_when_missing=False

Copy link
Member Author

Choose a reason for hiding this comment

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

Even if we updated drop_fks_for_table we'd have to write something to drop table too, else it'll fail won't it?

Copy link
Member

Choose a reason for hiding this comment

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

Whatever makes sense, maybe drop_table(table_name, including_fks=[], only_if_it_exists=True) or maybe it's for another PR, but a common issue is a migration that fails at step 2 and when you rerun it it fails at step one since step one can only run once (say a create table). Personally think we could use more of the "drop-table-but-only-if-it-exists" and "create-table-but-drop-it-first-if-it-exists" type utils to avoid getting people tangled up. We've had many people open issue with the message of "step 1 one of the migration fails, here's the stacktrace", only to realize that the issue was with a subsequent step.

op.drop_table("sl_columns")

try:
if connection.dialect.name != "sqlite":
Copy link
Member

Choose a reason for hiding this comment

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

also seems like the special handling for sqlite could be handled within drop_fks_for_table. More generally would be great to have migrations that are simpler / self-healing / free-of-engine-specific-logic through better utils.

insp = inspect(op.get_context().bind)
table_exists = insp.has_table(table_name)

print(f"Table {table_name} exist: {table_exists}")
Copy link
Member

Choose a reason for hiding this comment

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

Maybe delete this print? If not, then:

Suggested change
print(f"Table {table_name} exist: {table_exists}")
print(f"Table {table_name} exists: {table_exists}")

if connection.dialect.name != "sqlite":
drop_fks_for_table("sl_table_columns")
op.drop_table("sl_table_columns")
table_name = "sl_table_columns"
Copy link
Member

@michael-s-molina michael-s-molina Aug 27, 2024

Choose a reason for hiding this comment

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

Can you move this outside the upgrade method (below down_revision variable) and reuse it on downgrade?

Copy link
Member

Choose a reason for hiding this comment

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

Please do this for all changed migrations.

Copy link
Member Author

Choose a reason for hiding this comment

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

done

@pull-request-size pull-request-size bot added size/L and removed size/M labels Sep 3, 2024
@sadpandajoe sadpandajoe added the v4.1 Label added by the release manager to track PRs to be included in the 4.1 branch label Sep 3, 2024
@sadpandajoe sadpandajoe merged commit c929f5e into apache:master Sep 3, 2024
40 of 41 checks passed
@sadpandajoe sadpandajoe deleted the joe/check-for-table-before-dropping branch September 3, 2024 22:58
sadpandajoe added a commit that referenced this pull request Sep 3, 2024
@sadpandajoe sadpandajoe removed the risk:breaking-change Issues or PRs that will introduce breaking changes label Sep 6, 2024
@sadpandajoe
Copy link
Member Author

Not a breaking change since this handles the case where if migration has already been run gracefully.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
change:backend Requires changing the backend risk:db-migration PRs that require a DB migration size/L v4.1 Label added by the release manager to track PRs to be included in the 4.1 branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants