Skip to content

Commit

Permalink
Fix dbrestore of sqlite database to like newlines
Browse files Browse the repository at this point in the history
On Textfields that have line breaks, sqlite reports "unrecognized token"
because it doesn't keep the previous line in memory to be ammended by
the current line.

This pull requests implements a very simple logic to only send SQL
INSERT commands when the line finished with ");".

Close jazzband#238
  • Loading branch information
rgaiacs committed Oct 17, 2017
1 parent 7f3cbb2 commit a06c428
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
24 changes: 18 additions & 6 deletions dbbackup/db/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,25 @@ def restore_dump(self, dump):
if not self.connection.is_usable():
self.connection.connect()
cursor = self.connection.cursor()
sql_command = b""
sql_is_complete = True
for line in dump.readlines():
try:
cursor.execute(line.decode('UTF-8'))
except OperationalError as err:
warnings.warn("Error in db restore: {}".format(err))
except IntegrityError as err:
warnings.warn("Error in db restore: {}".format(err))
sql_command = sql_command + line
line_str = line.decode('UTF-8')
if line_str.startswith("INSERT") and not line_str.endswith(");\n"):
sql_is_complete = False
continue
if not sql_is_complete and line_str.endswith(");\n"):
sql_is_complete = True

if sql_is_complete:
try:
cursor.execute(sql_command.decode('UTF-8'))
except OperationalError as err:
warnings.warn("Error in db restore: {}".format(err))
except IntegrityError as err:
warnings.warn("Error in db restore: {}".format(err))
sql_command = b""


class SqliteCPConnector(BaseDBConnector):
Expand Down
2 changes: 1 addition & 1 deletion dbbackup/tests/test_connectors/test_sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.utils.six import BytesIO

from dbbackup.db.sqlite import SqliteConnector, SqliteCPConnector
from dbbackup.tests.testapp.models import CharModel
from dbbackup.tests.testapp.models import CharModel, TextModel


class SqliteConnectorTest(TestCase):
Expand Down

0 comments on commit a06c428

Please sign in to comment.