def execute_sql_file(filename):
""" Since mysql can't rollback any CREATE/ALTER/DROP instruction,
do a full backup before starting the migration and if it fails, and reapply
it if it fails
"""
with database.Database() as db, open(filename) as fd:
if not db.transaction():
print("Failed to start the migration")
sys.exit(1)
cursor = QtSql.QSqlQuery(db)
for statement in sqlparse.split(fd.read()):
if not statement:
continue
if not cursor.exec_(statement):
db.rollback()
print(cursor.lastError().text())
break
else:
db.commit()
评论列表
文章目录