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

PsqlDosMigrator: Remove hardcoding of table name in database reset #5781

Merged

Conversation

sphuber
Copy link
Contributor

@sphuber sphuber commented Nov 18, 2022

Fixes #5769

The PsqlDosMigrator.reset_database and PsqlDosBackend._clear methods
both deleted (almost) all database tables. The tables to delete were
hard-coded in a list by each method.

The delete_tables method is added to the PsqlDosMigrator class which
will delete all tables of the schema which are determined dynamically by
reflecting the metadata schema. The exclude_tables argument can be
used to define table names to exclude.

The new method is used by both classes in order to remove the
hard-coding of table names.

@sphuber sphuber force-pushed the fix/5769/psql-dos-clear-table-name-reflection branch 5 times, most recently from e90fb59 to 5c31ef4 Compare November 19, 2022 17:56
@sphuber sphuber requested a review from ltalirz November 19, 2022 20:56
@sphuber
Copy link
Contributor Author

sphuber commented Nov 19, 2022

@ltalirz fixed the problem that was bugging the reflection keeping open connections. This problem is addressed in #5783 and needs to be reviewed first. This one then builds on top of that.

@sphuber sphuber force-pushed the fix/5769/psql-dos-clear-table-name-reflection branch 2 times, most recently from 44217d3 to 28e60df Compare November 22, 2022 13:45
@sphuber
Copy link
Contributor Author

sphuber commented Nov 23, 2022

@ltalirz this is ready for review

Copy link
Member

@ltalirz ltalirz left a comment

Choose a reason for hiding this comment

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

thank you @sphuber !

just a few final questions

aiida/storage/psql_dos/migrator.py Outdated Show resolved Hide resolved
aiida/storage/psql_dos/migrator.py Outdated Show resolved Hide resolved
aiida/storage/psql_dos/migrator.py Outdated Show resolved Hide resolved
with self.transaction() as session:

# Close the session otherwise the ``delete_tables`` call will hang as there will be an open connection
# to the PostgreSQL server and it will block the deletion and the command will hang.
Copy link
Member

@ltalirz ltalirz Nov 23, 2022

Choose a reason for hiding this comment

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

I understand this measure prevents connections opened by this particular process from interfering, but I guess there could be other connections open to the DB (daemon / REST API / verdi shell / ...)?

Is there a way we could detect this and exit with an error message instead of hanging?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure to be honest. Not sure if sqlalchemy provides an API to get all open connection to a database, even those that are managed by it. I doubt it. We would probably have to go straight to psycopg and directly execute a postgres command. Anyway, for now, this method is only being called during unit testing, so it is not that likely.

The `PsqlDosMigrator.reset_database` and `PsqlDosBackend._clear` methods
both deleted (almost) all database tables. The tables to delete were
hard-coded in a list by each method.

The `delete_tables` method is added to the `PsqlDosMigrator` class which
will delete all tables of the schema which are determined dynamically by
reflecting the metadata schema. The `exclude_tables` argument can be
used to define table names to exclude.

The new method is used by both classes in order to remove the
hard-coding of table names.
@sphuber sphuber force-pushed the fix/5769/psql-dos-clear-table-name-reflection branch from 28e60df to ad1250c Compare November 24, 2022 09:04
@sphuber sphuber requested a review from ltalirz November 24, 2022 09:09
Copy link
Member

@ltalirz ltalirz left a comment

Choose a reason for hiding this comment

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

thanks, all good!

@sphuber sphuber merged commit 3ab392a into aiidateam:main Nov 24, 2022
@sphuber sphuber deleted the fix/5769/psql-dos-clear-table-name-reflection branch November 24, 2022 14:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remove hard-coding of table names in the PsqlDosBackend._clear
2 participants