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

Create backup of configuration file before migrating #3568

Merged
merged 1 commit into from
Nov 26, 2019

Conversation

sphuber
Copy link
Contributor

@sphuber sphuber commented Nov 23, 2019

Fixes #3510

This allows users to revert in case of problems with the migration or if
the migration happens by accident. Since the migration is done
automatically without prompt and is not reversible, having a backup is
crucial if users checkout a version of the code by accident.

P.S.: tests not included.

@sphuber
Copy link
Contributor Author

sphuber commented Nov 25, 2019

@giovannipizzi Wanted to include this in a v1.0.0 release with everything else that is currently on develop and then merge PR #3566 . Anything else you think should be included?

@giovannipizzi
Copy link
Member

I guess you meant v1.0.1, right?
I'm not sure anymore, didn't we say to ask explicitly to migrate instead of doing it silently? Maybe I'm wrong.
Probably OK to do v1.0.1 (@asle85 could you confirm that you don't need at this moment additional changes to the REST API?), in case we can do a v1.0.2 later (even if a bit more complex because we have to backport to py2).

@sphuber
Copy link
Contributor Author

sphuber commented Nov 26, 2019

Yes, I meant v1.0.1. I think the whole point of making a separate backup, with timestamp in case it is performed in quick succession, is to prevent having to add the complexity of a prompt. In most cases one actually does just want to migrate it. This is mostly for developers who are switching version often. @ltalirz what do you think?

@sphuber sphuber force-pushed the fix_3510_config_migration_backup branch from 7345a6f to 275aaa8 Compare November 26, 2019 09:11
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 @sphuber - I would keep it without prompt.
Two comments

version = get_current_version(config_dictionary)
filepath_backup = '{}.v{}.{}'.format(filepath, version, timezone.now().strftime('%Y%m%d-%H%M%S'))

echo.echo_warning('current configuration file `{}` is outdated and will be migrated'.format(filepath))
Copy link
Member

Choose a reason for hiding this comment

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

To me, these two echo statements don't belong in this function - the function should just create the backup, right?
I would move them to where you do the migration

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have looked at the code a bit better and think I have found a solution that is a lot better and aligns with your suggestion. Construction of Config from file is now done through Config.from_file class method which is now responsible for migrating and creating of backup. This bundles these two operations more closely, and for the better I feel.

from aiida.cmdline.utils import echo

version = get_current_version(config_dictionary)
filepath_backup = '{}.v{}.{}'.format(filepath, version, timezone.now().strftime('%Y%m%d-%H%M%S'))
Copy link
Member

@ltalirz ltalirz Nov 26, 2019

Choose a reason for hiding this comment

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

Maybe switch the order of version and date so that the files will be ordered by date.

Also, we already have a backup function that creates backups of the config file ending in .json~

def _backup(self, filepath=None):

Perhaps we should be reusing this function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can't do because that is a member function of Config which in order to be constructed requires a fully migrated config dictionary, so at that point creating a backup will be too late. It would be possible if we move the logic that determines whether migration is necessary in the constructor of the Config class. I am not sure if that is better. The code there is already a bit complicated and might make it even more tightly coupled. On the other hand maybe it makes it more self-contained and therefore better. I will take a look what that would look like.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also re the ordering, with the current order they will still be ordered by data, within each version group, which should also be ordered, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Decided to omit the version as it is in the files themselves.

@sphuber sphuber force-pushed the fix_3510_config_migration_backup branch from 9675592 to 1a8b4f1 Compare November 26, 2019 13:44
This allows users to revert in case of problems with the migration or if
the migration happens by accident. Since the migration is done
automatically without prompt and is not reversible, having a backup is
crucial if users checkout a version of the code by accident.

To accomplish this the configuration instantiation and migration is also
refactored moving the responsibility of migrating a configuration file to
the `Config` class itself. Constructing a `Config` instance from an
existing configuration file should now be done through the `from_file`
class method. This will backup the file if it determines that it is
outdatedand then construct a `Config` instance from the migrated contents.
@sphuber sphuber force-pushed the fix_3510_config_migration_backup branch from 1a8b4f1 to 55861f5 Compare November 26, 2019 14: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 @sphuber , looks good to me.

now we can even do concurrent backups in the same microsecond ;-)

@sphuber
Copy link
Contributor Author

sphuber commented Nov 26, 2019

thanks @sphuber , looks good to me.

now we can even do concurrent backups in the same microsecond ;-)

It is actually necessary without concurrent backups. If you look in the constructor there are multiple checks for inconsistencies and for each one it will create a backup. These can follow one another pretty quickly so having a granularity of seconds ran the risk of the first backup with the original file being overridden.

@sphuber sphuber merged commit 003fd1c into aiidateam:develop Nov 26, 2019
@sphuber sphuber deleted the fix_3510_config_migration_backup branch November 26, 2019 15:19
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.

Warn when configuration is migrated and create unique backup
3 participants