diff --git a/README.md b/README.md index 63c9c97..4ec66bc 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ Detect backward incompatible migrations for your django project. All in one file * `--include-apps INCLUDE_APPS [INCLUDE_APPS ...]` - check only migrations that are in the specified django apps * `--exclude-apps EXCLUDE_APPS [EXCLUDE_APPS ...]` - ignore migrations that are in the specified django apps * `--verbose` or `-v` - print more information during execution +* `--database DATABASE` - specify the database for which to generate the SQL. Defaults to *default* ## Requirements diff --git a/migration_linter.py b/migration_linter.py index 2e7f7f4..7ae5db9 100644 --- a/migration_linter.py +++ b/migration_linter.py @@ -49,6 +49,7 @@ class MigrationLinter: self.ignore_name_contains = kwargs.get('ignore_name_contains', None) self.include_apps = kwargs.get('include_apps', None) self.exclude_apps = kwargs.get('exclude_apps', None) + self.database = kwargs.get('database', None) or 'default' self.changed_migration_files = [] self._gather_migrations() @@ -118,7 +119,7 @@ class MigrationLinter: or (self.ignore_name_contains and self.ignore_name_contains in migration_name) def django_sqlmigrate(self, app_name, migration_name): - git_diff_command = 'cd {0} && python manage.py sqlmigrate {1} {2}'.format(self.location, app_name, migration_name) + git_diff_command = 'cd {0} && python manage.py sqlmigrate {1} {2} --database {3}'.format(self.location, app_name, migration_name, self.database) log.info('Executing {0}'.format(git_diff_command)) diff_process = Popen(git_diff_command, shell=True, stdout=PIPE, stderr=PIPE) sql_statements = [] @@ -126,6 +127,8 @@ class MigrationLinter: if not line.startswith('--'): # Do not take sql comments into account sql_statements.append(line.strip()) diff_process.wait() + if diff_process.returncode != 0: + raise RuntimeError('sqlmigrate command failed') log.info('Found {0} sql migration lines'.format(len(sql_statements))) return sql_statements @@ -168,6 +171,7 @@ if __name__ == '__main__': 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') parser.add_argument('--verbose', '-v', action='store_true', help='print more information during execution') + parser.add_argument('--database', type=str, nargs='?', help='specify the database for which to generate the SQL. Defaults to default') incl_excl_group = parser.add_mutually_exclusive_group(required=False) incl_excl_group.add_argument('--include-apps', type=str, nargs='*', help='check only migrations that are in the specified django apps') incl_excl_group.add_argument('--exclude-apps', type=str, nargs='*', help='ignore migrations that are in the specified django apps') @@ -180,7 +184,7 @@ if __name__ == '__main__': folder_name = args.django_folder[0] if valid_folder(folder_name): - linter = MigrationLinter(folder_name, args.commit_id, ignore_name_contains=args.ignore_name_contains, include_apps=args.include_apps, exclude_apps=args.exclude_apps) + linter = MigrationLinter(folder_name, args.commit_id, ignore_name_contains=args.ignore_name_contains, include_apps=args.include_apps, exclude_apps=args.exclude_apps, database=args.database) has_errors = linter.lint_migrations() if has_errors: sys.exit(1)