Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dbrestore of sqlite database to like newlines #258

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 1 addition & 0 deletions dbbackup/tests/test_connectors/test_postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.test import TestCase
from django.utils.six import BytesIO


from dbbackup.db.postgresql import (PgDumpConnector, PgDumpGisConnector,
PgDumpBinaryConnector)

Expand Down
8 changes: 7 additions & 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 All @@ -29,6 +29,12 @@ def test_create_dump_with_unicode(self):
dump = connector.create_dump()
self.assertTrue(dump.read())

def test_create_dump_with_newline(self):
TextModel.objects.create(field='foo\nbar')
connector = SqliteConnector()
dump = connector.create_dump()
self.assertTrue(dump.read())

def test_restore_dump(self):
connector = SqliteConnector()
dump = connector.create_dump()
Expand Down
23 changes: 17 additions & 6 deletions dbbackup/tests/testapp/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,52 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.5 on 2017-10-17 07:15
from __future__ import unicode_literals

from django.db import models, migrations
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='CharModel',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('field', models.CharField(max_length=10)),
],
),
migrations.CreateModel(
name='FileModel',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('field', models.FileField(upload_to='.')),
],
),
migrations.CreateModel(
name='ForeignKeyModel',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('field', models.ForeignKey(to='testapp.CharModel')),
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('field', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='testapp.CharModel')),
],
),
migrations.CreateModel(
name='ManyToManyModel',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('field', models.ManyToManyField(to='testapp.CharModel')),
],
),
migrations.CreateModel(
name='TextModel',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('field', models.TextField()),
],
),
]
4 changes: 4 additions & 0 deletions dbbackup/tests/testapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
class CharModel(models.Model):
field = models.CharField(max_length=10)


class TextModel(models.Model):
field = models.TextField()


class ForeignKeyModel(models.Model):
field = models.ForeignKey(CharModel, on_delete=models.CASCADE)

Expand Down