def empty_db(self):
cmd = [
"mysqldump",
"-u%(user)s" % self.db_config,
"-h%(host)s" % self.db_config,
"--add_drop-table",
"--no-data",
"%(name)s" % self.db_config,
]
tmphandle, tmppath = tempfile.mkstemp(text=True)
tmpfile = os.fdopen(tmphandle, "w")
sql_data = subprocess.check_output(cmd, stderr=None).split('\n')
tmpfile.write("SET FOREIGN_KEY_CHECKS = 0;\n")
tmpfile.write("use %(name)s;\n" % self.db_config)
for line in sql_data:
if line.startswith("DROP"):
tmpfile.write(line + '\n')
tmpfile.close()
self._run_mysql_cmd("source %s" % tmppath)
os.remove(tmppath)
评论列表
文章目录