- sh.call("mysqldump", "--compress", "-r", outfile, *get_mysql_args(deployment.dsn))
- sh.call("gzip", "--best", outfile)
- except shell.CallError as e:
- shutil.rmtree(outdir)
- raise BackupFailure(e.stderr)
-
-def restore_database(backup_dir, deployment):
- """
- Generic database restoration function for MySQL.
- """
- # XXX: see backup_database
- if deployment.application.database == "mysql":
- return restore_mysql_database(backup_dir, deployment)
- else:
- raise NotImplementedError
-
-def restore_mysql_database(backup_dir, deployment):
- """
- Database restoration for MySQL by piping SQL commands into :command:`mysql`.
- """
- sh = shell.Shell()
- if not os.path.exists(backup_dir):
- raise RestoreFailure("Backup %s doesn't exist", backup_dir.rpartition("/")[2])
- sql = open(os.path.join(backup_dir, "db.sql"), 'w+')
- sh.call("gunzip", "-c", os.path.join(backup_dir, "db.sql.gz"), stdout=sql)
- sql.seek(0)
- sh.call("mysql", *get_mysql_args(deployment.dsn), stdin=sql)
- sql.close()
-
-def remove_database(deployment):
- """
- Generic database removal function. Actually, not so generic because we
- go and check if we're on scripts and if we are run a different command.
- """
- sh = shell.Shell()
- if deployment.dsn.host == "sql.mit.edu":
- try:
- sh.call("/mit/scripts/sql/bin/drop-database", deployment.dsn.database)
- return
- except shell.CallError:
- pass
- engine = sqlalchemy.create_engine(deployment.dsn)
- engine.execute("DROP DATABASE `%s`" % deployment.dsn.database)
-
-def get_mysql_args(dsn):
- """
- Extracts arguments that would be passed to the command line mysql utility
- from a deployment.
- """
- args = []
- if dsn.host:
- args += ["-h", dsn.host]
- if dsn.username:
- args += ["-u", dsn.username]
- if dsn.password:
- args += ["-p" + dsn.password]
- args += [dsn.database]
- return args
+ return f(self, *args, **kwargs)
+ except sql.BackupDatabaseError:
+ raise BackupFailure(traceback.format_exc())
+ except sql.RestoreDatabaseError:
+ raise RestoreFailure(traceback.format_exc())
+ except sql.RemoveDatabaseError:
+ raise RemoveFailure(traceback.format_exc())