-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check the migrations files in the current repository and ensure that they all have a `safe` property defined. While we can think of easy ways to break this implementation, for all the use-cases I've had this covers it quite nicely so I think it's worth merging even if its rough. Tim Schilling put an initial sketch of this in #39 and I was able to modify it for use in a precommit hook without difficulty. The modifications were made to tailor it to the environment of pre-commit, where file names are passed to the command as arguments. This allows pre-commit to only need to run the checks incrementally for the affected files for new commits. Co-authored-by: Tim Schilling <schillingt@better-simple.com>
- Loading branch information
1 parent
e4e5300
commit 4e76e8c
Showing
4 changed files
with
103 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
- id: check | ||
name: Check migrations safety | ||
description: Ensure that all local migrations have been marked for safety. | ||
entry: safemigrate-check | ||
language: python | ||
files: migrations/\d{4}_.+\.py$ | ||
types: [file, python, text] | ||
stages: [pre-commit, pre-merge-commit, pre-push, manual] | ||
pass_filenames: true |
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,56 @@ | ||
""" | ||
Ensure migrations are using django_safemigrate. | ||
This is fairly rudimentary and won't work if the class doesn't | ||
explicitly inherit from ``Migration``. | ||
""" | ||
import re | ||
import sys | ||
|
||
MIGRATION_PATTERN = re.compile(r"class\s+(?P<MigrationClass>\w+)\s?\(.*Migration\):") | ||
|
||
MISSING_SAFE_MESSAGE = ( | ||
"{file_path}: {migration_class} is missing the 'safe' attribute.\n" | ||
) | ||
FAILURE_MESSAGE = ( | ||
"\n" | ||
"Add the following to the migration class:\n" | ||
"\n" | ||
"from django_safemigrate import Safe\n" | ||
"class Migration(migrations.Migration):\n" | ||
" safe = Safe.before_deploy\n" | ||
"\n" | ||
"You can also use the following:\n" | ||
" safe = Safe.always\n" | ||
" safe = Safe.after_deploy\n" | ||
"\n" | ||
) | ||
|
||
|
||
def validate_migrations(files): | ||
success = True | ||
for file_path in files: | ||
with open(file_path) as f: | ||
content = f.read() | ||
|
||
match = MIGRATION_PATTERN.search(content) | ||
if match: | ||
migration_class = match.group("MigrationClass") | ||
if "safe = Safe." not in content: | ||
success = False | ||
sys.stdout.write( | ||
MISSING_SAFE_MESSAGE.format( | ||
file_path=file_path, migration_class=migration_class | ||
) | ||
) | ||
if not success: | ||
sys.stdout.write(FAILURE_MESSAGE) | ||
return success | ||
|
||
|
||
def main(): # pragma: no cover | ||
sys.exit(0 if validate_migrations(sys.argv[1:]) else 1) | ||
|
||
|
||
if __name__ == "__main__": # pragma: no cover | ||
main() |
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