Skip to content

Commit

Permalink
#57: Improved error message when migration command fails.
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermechapiewski committed Oct 5, 2009
1 parent 5fa25f5 commit d9195cd
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Makefile
Expand Up @@ -14,8 +14,9 @@ help:

clean:
@echo "Cleaning..."
@git clean -df > /dev/null
@rm -rf build dist src/simple_db_migrate.egg-info simple_db_migrate.egg-info *.pyc **/*.pyc *~ *.migration *.foo
# removing test temp files
@rm -rf `date +%Y`*

compile: clean
@echo "Compiling source code..."
Expand Down
File renamed without changes.
13 changes: 13 additions & 0 deletions src/core/exceptions.py
@@ -0,0 +1,13 @@
class MigrationException(Exception):
def __init__(self, msg=None, sql=None):
self.msg = msg
if not msg:
self.msg = 'error executing migration'
self.sql = sql

def __str__(self):
if self.sql:
self.details = '[ERROR DETAILS] SQL command was:\n%s' % self.sql
return '%s\n\n%s' % (self.msg, self.details)

return self.msg
7 changes: 5 additions & 2 deletions src/mysql.py
Expand Up @@ -3,6 +3,7 @@

import MySQLdb

from core.exceptions import MigrationException
from helpers import Utils

class MySQL(object):
Expand Down Expand Up @@ -32,20 +33,22 @@ def __mysql_connect(self, connect_using_db_name=True):
conn.select_db(self.__mysql_db)
return conn
except Exception, e:
raise Exception("could not connect to database (%s)" % e)
raise Exception("could not connect to database: %s" % e)

def __execute(self, sql):
db = self.__mysql_connect()
cursor = db.cursor()
cursor._defer_warnings = True
curr_statement = None
try:
for statement in self._parse_sql_statements(sql):
curr_statement = statement
cursor.execute(statement.encode("utf-8"))
cursor.close()
db.commit()
db.close()
except Exception, e:
raise Exception("error executing migration (%s)" % e)
raise MigrationException("error executing migration: %s" % e, curr_statement)

def __change_db_version(self, version, up=True):
if up:
Expand Down
File renamed without changes.

0 comments on commit d9195cd

Please sign in to comment.