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 16, 2017
1 parent 7f3cbb2 commit 841d9cd
Showing 1 changed file with 18 additions and 6 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

0 comments on commit 841d9cd

Please sign in to comment.