Skip to content

Commit

Permalink
New method to parse SQL statements.
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermechapiewski committed Aug 10, 2009
1 parent 78483b8 commit 4d30766
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/mysql.py
Expand Up @@ -36,16 +36,18 @@ def __execute(self, sql):
cursor = db.cursor()
cursor._defer_warnings = True
try:
sql_statements = sql.split(";")
sql_statements = [s.strip() for s in sql_statements if s.strip() != ""]
for statement in sql_statements:
for statement in self._parse_sql_statements(sql):
cursor.execute(statement.encode("utf-8"))
cursor.close()
db.commit()
db.close()
except Exception, e:
raise Exception("error executing migration (%s)" % e)


def _parse_sql_statements(self, migration_sql):
all_statements = migration_sql.split(';')
return [s.strip() for s in all_statements if s.strip() != ""]

def _drop_database(self):
db = self.__mysql_connect(False)
try:
Expand Down
17 changes: 16 additions & 1 deletion tests/mysql_test.py
Expand Up @@ -144,6 +144,21 @@ def test_it_should_get_all_schema_versions(self):
self.assertEquals(len(expected_versions), len(schema_versions))
for version in schema_versions:
self.assertTrue(version in expected_versions)


def test_it_should_parse_sql_statements(self):
mysql_driver_mock = Mock()
db_mock = Mock()
cursor_mock = Mock()
self.__mock_db_init(mysql_driver_mock, db_mock, cursor_mock)
mysql = MySQL(self.__config, mysql_driver_mock)

sql = 'create table eggs; drop table spam; ; ;'
statements = mysql._parse_sql_statements(sql)

assert len(statements) == 2
assert statements[0] == 'create table eggs'
assert statements[1] == 'drop table spam'


if __name__ == "__main__":
unittest.main()

0 comments on commit 4d30766

Please sign in to comment.