def restore_database(config, dump_dir, dump_name=None): dump_name = dump_name or config.type dump_name = "{}.sql".format(dump_name) dump_path = os.path.join(dump_dir, dump_name) if config.type == 'mysql': restore_cmd = "mysql {config.name}" elif config.type == 'postgresql': restore_cmd = "psql {config.name}" else: raise ValueError("Invalid db type {}".format(config.type)) with open(dump_path, "r") as dump_file: cmd = command(restore_cmd, stdin=dump_file) return cmd
def dump_database(config, dump_dir, dump_name=None): dump_name = config.type if not dump_name else dump_name dump_name = "{}.sql".format(dump_name) dump_path = os.path.join(dump_dir, dump_name) if config.type == 'mysql': dump_cmd = "mysqldump {config.name} --add-drop-database "\ " --single-transaction -r {dump_path}"\ .format(config=config, dump_path=dump_path) elif config.type == 'postgresql': dump_cmd = "pg_dump -C {config.name} -f {dump_path}"\ .format(config=config, dump_path=dump_path) else: raise ValueError("Invalid db type {}".format(config.type)) return command(dump_cmd, on_init=True)