Add option to ignore migrations containing a certain string
This commit is contained in:
1 parent
79e2ef054d
commit
5cbd22f6da
2 files changed
+13
-7
No files matched your search
@@ -4,10 +4,11 @@ Detect backward incompatible migrations for your django projects. All in one fil
|
|||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
`python migration_checker.py DJANGO_PROJECT_FOLDER [GIT_COMMIT_ID]`
|
`python migration_checker.py DJANGO_PROJECT_FOLDER [GIT_COMMIT_ID] [--ignore-name-contains=IGNORE_NAME_CONTAINS]`
|
||||||
|
|
||||||
* `DJANGO_PROJECT_FOLDER` - an absolute or relative path to the django project.
|
* `DJANGO_PROJECT_FOLDER` - an absolute or relative path to the django project.
|
||||||
* `GIT_COMMIT_ID` - if specified, only migrations since this commit will be taken into account. If not specified, the initial repo commit will be used.
|
* `GIT_COMMIT_ID` - if specified, only migrations since this commit will be taken into account. If not specified, the initial repo commit will be used.
|
||||||
|
* `--ignore-name-contains IGNORE_NAME_CONTAINS`- ignore migrations containing this name
|
||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
|
|
||||||
@@ -28,7 +29,6 @@ More test are to be written.
|
|||||||
## Improvements
|
## Improvements
|
||||||
|
|
||||||
* More tests (always!)
|
* More tests (always!)
|
||||||
* Possibility to ignore certain migrations
|
|
||||||
* Possibility to only check certain apps within the project (and/or exclude certain ones)
|
* Possibility to only check certain apps within the project (and/or exclude certain ones)
|
||||||
* Detect data migration (not only scheme migrations)
|
* Detect data migration (not only scheme migrations)
|
||||||
* Detection dependent on specified database (mysql, postgresql, ...)
|
* Detection dependent on specified database (mysql, postgresql, ...)
|
||||||
|
|||||||
+11
-5
@@ -21,9 +21,10 @@ class MigrationChecker:
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
def __init__(self, django_folder, commit_id=None):
|
def __init__(self, django_folder, commit_id=None, **kwargs):
|
||||||
self.location = django_folder
|
self.location = django_folder
|
||||||
self.commit_id = commit_id
|
self.commit_id = commit_id
|
||||||
|
self.ignore_name_contains = kwargs.get('ignore_name_contains', None)
|
||||||
self.changed_migration_files = []
|
self.changed_migration_files = []
|
||||||
self._gather_migrations()
|
self._gather_migrations()
|
||||||
|
|
||||||
@@ -51,11 +52,15 @@ class MigrationChecker:
|
|||||||
def check_migrations(self):
|
def check_migrations(self):
|
||||||
nb_valid = 0
|
nb_valid = 0
|
||||||
nb_erroneous = 0
|
nb_erroneous = 0
|
||||||
|
nb_ignore = 0
|
||||||
for migration in self.changed_migration_files:
|
for migration in self.changed_migration_files:
|
||||||
app_name, migration_name = self._split_migration_path(migration)
|
app_name, migration_name = self._split_migration_path(migration)
|
||||||
sql_statements = self.django_sqlmigrate(app_name, migration_name)
|
|
||||||
print('{0}... '.format(migration), end='')
|
print('{0}... '.format(migration), end='')
|
||||||
|
if self.ignore_name_contains and self.ignore_name_contains in migration_name:
|
||||||
|
print('IGNORE')
|
||||||
|
nb_ignore += 1
|
||||||
|
else:
|
||||||
|
sql_statements = self.django_sqlmigrate(app_name, migration_name)
|
||||||
errors = set()
|
errors = set()
|
||||||
for statement in sql_statements:
|
for statement in sql_statements:
|
||||||
is_valid, err_msg = self._test_sql_statement_for_backward_incompatibility(statement)
|
is_valid, err_msg = self._test_sql_statement_for_backward_incompatibility(statement)
|
||||||
@@ -70,7 +75,7 @@ class MigrationChecker:
|
|||||||
for err in errors:
|
for err in errors:
|
||||||
print('\t' + err)
|
print('\t' + err)
|
||||||
print('*** Summary:')
|
print('*** Summary:')
|
||||||
print('Valid migrations: {0}/{1} - erroneous migrations: {2}/{1}'.format(nb_valid, len(self.changed_migration_files), nb_erroneous))
|
print('Valid migrations: {0}/{1} - erroneous migrations: {2}/{1} - ignored migrations: {3}/{1}'.format(nb_valid, len(self.changed_migration_files), nb_erroneous, nb_ignore))
|
||||||
|
|
||||||
def _split_migration_path(self, migration_path):
|
def _split_migration_path(self, migration_path):
|
||||||
decomposed_path = split_path(migration_path)
|
decomposed_path = split_path(migration_path)
|
||||||
@@ -123,9 +128,10 @@ if __name__ == '__main__':
|
|||||||
parser = argparse.ArgumentParser(description='Detect backward incompatible django migrations.')
|
parser = argparse.ArgumentParser(description='Detect backward incompatible django migrations.')
|
||||||
parser.add_argument('django_folder', metavar='DJANGO_FOLDER', type=str, nargs=1, help='the path to the django project')
|
parser.add_argument('django_folder', metavar='DJANGO_FOLDER', type=str, nargs=1, help='the path to the django project')
|
||||||
parser.add_argument('commit_id', metavar='GIT_COMMIT_ID', type=str, nargs='?', help='if specified, only migrations since this commit will be taken into account. If not specified, the initial repo commit will be used')
|
parser.add_argument('commit_id', metavar='GIT_COMMIT_ID', type=str, nargs='?', help='if specified, only migrations since this commit will be taken into account. If not specified, the initial repo commit will be used')
|
||||||
|
parser.add_argument('--ignore-name-contains', type=str, nargs='?', help='ignore migrations containing this name')
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
folder_name = args.django_folder[0]
|
folder_name = args.django_folder[0]
|
||||||
if valid_folder(folder_name):
|
if valid_folder(folder_name):
|
||||||
checker = MigrationChecker(folder_name, args.commit_id)
|
checker = MigrationChecker(folder_name, args.commit_id, ignore_name_contains=args.ignore_name_contains)
|
||||||
checker.check_migrations()
|
checker.check_migrations()
|
||||||
Reference in new issue
Block a user