-
Notifications
You must be signed in to change notification settings - Fork 37
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
Add schema version #253
Merged
Merged
Add schema version #253
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
2e418c0
Add schema version validation.
bdice 3c9ec56
Add test and use Version class.
bdice 58fcc27
Update changelog.
bdice ef616bc
Store version as string in configuration.
csadorf afe6f83
Major revision to the schema check and migration approach.
csadorf 9929308
Use 'signac migrate' CLI tool as primary way to migrate projects.
csadorf b162938
Update signac/__main__.py
csadorf a7b3cdb
Change config key 'schema_version' default value to '1'.
csadorf 9dfa36f
Disable 'migrate' CLI command for now.
csadorf 3aaae1f
Use try: ... finally: ... for lock-file removal.
csadorf c91145b
Update signac/contrib/errors.py
csadorf 5b5a1d7
Import migration lock file constant in __main__.
csadorf 04a13e5
Update signac/contrib/project.py
csadorf 12eb2f4
Remove redundant doc-string comment.
csadorf 62d0927
Check for correct exception type in unit test.
csadorf d9408e8
Rename the contrib.migrations module to contrib.migration.
csadorf 5f43426
Move locking for migration into migration module.
csadorf a0e3a39
Add unit test for migration.
csadorf 39e2ea9
Remove 'contrib.migration.MIGRATION' from public API.
csadorf e8e1329
Replace migration class with migration function.
csadorf 8d6644b
Convert 'migration' module to package.
csadorf cda3136
Add 'packaging' to requirements.
csadorf de09bd9
Minor refactorization of the migrate/__init__.py module.
csadorf 3ac966d
Comment out migration related code.
csadorf 43a3392
Streamline migration unit tests.
csadorf a15f078
Make the apply_migrations() function a generator.
csadorf c84c7cf
Add 'filelock' and 'packaging' to requirements.txt.
csadorf c04d471
Use integer strings instead of full semantic versions for consistency.
bdice 9a56741
Merge branch 'master' into feature/schema-version
bdice 15ce09d
Merge branch 'master' into feature/schema-version
bdice File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
deprecation>=2 | ||
filelock~=3.0 | ||
packaging>=15.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
# Copyright (c) 2019 The Regents of the University of Michigan | ||
# All rights reserved. | ||
# This software is licensed under the BSD 3-Clause License. | ||
import os | ||
import sys | ||
from packaging import version | ||
from contextlib import contextmanager | ||
|
||
from filelock import FileLock | ||
|
||
from ...common.config import get_config | ||
from ...version import __version__, SCHEMA_VERSION | ||
|
||
|
||
from .v0_to_v1 import migrate_v0_to_v1 | ||
|
||
|
||
FN_MIGRATION_LOCKFILE = '.SIGNAC_PROJECT_MIGRATION_LOCK' | ||
|
||
|
||
MIGRATIONS = { | ||
('0', '1'): migrate_v0_to_v1, | ||
} | ||
|
||
|
||
def _reload_project_config(project): | ||
project_reloaded = project.get_project( | ||
root=project._rd, search=False, _ignore_schema_version=True) | ||
project._config = project_reloaded._config | ||
|
||
|
||
def _update_project_config(project, **kwargs): | ||
"Update the project configuration." | ||
for fn in ('signac.rc', '.signacrc'): | ||
config = get_config(project.fn(fn)) | ||
if 'project' in config: | ||
break | ||
else: | ||
raise RuntimeError("Unable to determine project configuration file.") | ||
config.update(kwargs) | ||
config.write() | ||
_reload_project_config(project) | ||
|
||
|
||
@contextmanager | ||
def _lock_for_migration(project): | ||
lock = FileLock(project.fn(FN_MIGRATION_LOCKFILE)) | ||
try: | ||
with lock: | ||
yield | ||
finally: | ||
try: | ||
os.unlink(lock.lock_file) | ||
except FileNotFoundError: | ||
pass | ||
|
||
|
||
def _collect_migrations(project): | ||
schema_version = version.parse(SCHEMA_VERSION) | ||
|
||
def config_schema_version(): | ||
return version.parse(project._config['schema_version']) | ||
|
||
if config_schema_version() > schema_version: | ||
# Project config schema version is newer and therefore not supported. | ||
raise RuntimeError( | ||
"The signac schema version used by this project is {}, but signac {} " | ||
"only supports up to schema version {}. Try updating signac.".format( | ||
config_schema_version, __version__, SCHEMA_VERSION)) | ||
|
||
while config_schema_version() < schema_version: | ||
for (origin, destination), migration in MIGRATIONS.items(): | ||
if version.parse(origin) == config_schema_version(): | ||
yield (origin, destination), migration | ||
break | ||
else: | ||
raise RuntimeError( | ||
"The signac schema version used by this project is {}, but signac {} " | ||
"uses schema version {} and does not know how to migrate.".format( | ||
config_schema_version(), __version__, schema_version)) | ||
|
||
|
||
def apply_migrations(project): | ||
with _lock_for_migration(project): | ||
for (origin, destination), migrate in _collect_migrations(project): | ||
try: | ||
print("Applying migration for " | ||
"version {} to {}... ".format(origin, destination), end='', | ||
file=sys.stderr) | ||
migrate(project) | ||
except Exception as e: | ||
raise RuntimeError( | ||
"Failed to apply migration {}.".format(destination)) from e | ||
else: | ||
_update_project_config(project, schema_version=destination) | ||
print("OK", file=sys.stderr) | ||
yield origin, destination | ||
|
||
|
||
__all__ = [ | ||
'apply_migrations', | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Copyright (c) 2019 The Regents of the University of Michigan | ||
# All rights reserved. | ||
# This software is licensed under the BSD 3-Clause License. | ||
"""Migrate from schema version 0 to version 1. | ||
|
||
This migration is a null-migration that serves as a template | ||
for future migrations and testing purposes. | ||
""" | ||
|
||
|
||
def migrate_v0_to_v1(project): | ||
"Migrate from schema version 0 to version 1." | ||
pass # nothing to do here, serves purely as example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be a proper semantic version string?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comment below.