def _diff_current_schema_vs_saved_schema(schema_path): saved_schema = file_reader.read(schema_path) current_schema = db_instance.get_schema() if _schemas_equal(current_schema, saved_schema): return '' with tempfile.NamedTemporaryFile('wt', encoding='utf8') as schema_file: schema_file.write(current_schema) schema_file.flush() try: return subprocess.check_output([ 'git', '--no-pager', 'diff', '--color', '--ignore-space-at-eol', schema_path, schema_file.name ]).stdout.decode('utf8') # `git diff` has a non-zero exit code when there is a diff, seemingly by # default, at least for me -- even though the docs make it sound like # you need to specify `--exit-code` for this behavior. except subprocess.CalledProcessError as err: return err.output.decode('utf8')
def main(argv): schema_path = None for arg in sys.argv[1:]: matches = re.search(r'^--(db_config|patches|schema)\=(.*)$', arg) if matches: if matches.group(1) == 'db_config': set_config_file(matches.group(2)) elif matches.group(1) == 'patches': set_patch_dir(matches.group(2)) elif matches.group(1) == 'schema': schema_path = matches.group(2) else: raise Exception('Unknown param "{}"'.format(matches.group(1))) command = _get_command_from_args(argv) force = _has_arg(argv, '-y') dev_mode = _has_arg(argv, '--dev_mode') db_instance.set_dev_mode(dev_mode) dev_mode_allowed = _has_arg(argv, '--WARNING__permit_data_loss') if dev_mode and not command == 'init': if not dev_mode_allowed: raise Exception( 'In order to use dev_mode, you must pass the parameter ' '--WARNING__permit_data_loss.') db_instance.rewind_invalid_patches() if command == 'init': if schema_path is None: raise Exception('Schema path was not provided.') _init(schema_path, force) elif command == 'upgrade': if schema_path is None: raise Exception('Schema path was not provided.') upgrade(schema_path, force) elif command == 'show_upgrade': next_upgrade = _get_next_upgrade() if next_upgrade: if isinstance(next_upgrade, _SqlUpgrade): upgrade_contents = 'SQL: ' + upgrade.query elif isinstance(next_upgrade, _PythonUpgrade): upgrade_contents = 'Python: ' + next_upgrade.script else: assert isinstance(next_upgrade, _ShellUpgrade) upgrade_contents = 'Shell Script: ' + next_upgrade.script print(upgrade_contents) elif command == 'show_current_schema': print(db_instance.get_schema()) elif command == 'save_current_schema': if schema_path is None: raise Exception('Schema path was not provided.') _save_current_db_schema(schema_path) elif command == 'verify': if schema_path is None: raise Exception('Schema path was not provided.') _verify_db_up_to_date(schema_path) elif command == 'database_name': print(db_instance.get_db_name()) elif command == 'connect': db_instance.connect_repl() elif command == 'query': print(db_instance.query(sys.stdin.read())) elif command == 'query_file': print(db_instance.query_file(argv[-1])) else: raise Exception('Invalid command: {}\n{}'.format( command, 'Expected one of: verify, upgrade, show_upgrade, ' 'show_current_schema, save_current_schema, database_name, ' 'connect, query, query_file'))
def _save_current_db_schema(schema_path): if not _should_continue( 'This will override changes to {}, continue?'.format(schema_path)): return with open(schema_path, 'wt', encoding='utf8') as schema_file: schema_file.write(db_instance.get_schema())