File tree Expand file tree Collapse file tree 7 files changed +63
-18
lines changed Expand file tree Collapse file tree 7 files changed +63
-18
lines changed Original file line number Diff line number Diff line change @@ -71,11 +71,23 @@ def restore_dump(self, dump):
71
71
if not self .connection .is_usable ():
72
72
self .connection .connect ()
73
73
cursor = self .connection .cursor ()
74
+ sql_command = b""
75
+ sql_is_complete = True
74
76
for line in dump .readlines ():
75
- try :
76
- cursor .execute (line .decode ("UTF-8" ))
77
- except (OperationalError , IntegrityError ) as err :
78
- warnings .warn (f"Error in db restore: { err } " )
77
+ sql_command = sql_command + line
78
+ line_str = line .decode ("UTF-8" )
79
+ if line_str .startswith ("INSERT" ) and not line_str .endswith (");\n " ):
80
+ sql_is_complete = False
81
+ continue
82
+ if not sql_is_complete and line_str .endswith (");\n " ):
83
+ sql_is_complete = True
84
+
85
+ if sql_is_complete :
86
+ try :
87
+ cursor .execute (sql_command .decode ("UTF-8" ))
88
+ except (OperationalError , IntegrityError ) as err :
89
+ warnings .warn (f"Error in db restore: { err } " )
90
+ sql_command = b""
79
91
80
92
81
93
class SqliteCPConnector (BaseDBConnector ):
Original file line number Diff line number Diff line change 29
29
"dbbackup" ,
30
30
"dbbackup.tests.testapp" ,
31
31
)
32
+ DEFAULT_AUTO_FIELD = "django.db.models.AutoField"
32
33
33
34
DATABASES = {
34
35
"default" : {
Original file line number Diff line number Diff line change 5
5
from django .test import TestCase
6
6
7
7
from dbbackup .db .sqlite import SqliteConnector , SqliteCPConnector
8
- from dbbackup .tests .testapp .models import CharModel
8
+ from dbbackup .tests .testapp .models import CharModel , TextModel
9
9
10
10
11
11
class SqliteConnectorTest (TestCase ):
@@ -28,7 +28,17 @@ def test_create_dump_with_unicode(self):
28
28
dump = connector .create_dump ()
29
29
self .assertTrue (dump .read ())
30
30
31
+ def test_create_dump_with_newline (self ):
32
+ TextModel .objects .create (
33
+ field = f'INSERT ({ "foo" * 5000 } \n bar\n WHERE \n baz IS\n "great" );\n '
34
+ )
35
+
36
+ connector = SqliteConnector ()
37
+ dump = connector .create_dump ()
38
+ self .assertTrue (dump .read ())
39
+
31
40
def test_restore_dump (self ):
41
+ TextModel .objects .create (field = "T\n f\n w\n nl" )
32
42
connector = SqliteConnector ()
33
43
dump = connector .create_dump ()
34
44
connector .restore_dump (dump )
Original file line number Diff line number Diff line change 3
3
4
4
class Migration (migrations .Migration ):
5
5
6
+ initial = True
6
7
dependencies = []
7
8
8
9
operations = [
Original file line number Diff line number Diff line change
1
+ # Generated by Django 4.0.1 on 2022-04-27 22:36
2
+
3
+ from django .db import migrations , models
4
+
5
+
6
+ class Migration (migrations .Migration ):
7
+
8
+ dependencies = [
9
+ ("testapp" , "0001_initial" ),
10
+ ]
11
+
12
+ operations = [
13
+ migrations .CreateModel (
14
+ name = "TextModel" ,
15
+ fields = [
16
+ (
17
+ "id" ,
18
+ models .AutoField (
19
+ auto_created = True ,
20
+ primary_key = True ,
21
+ serialize = False ,
22
+ verbose_name = "ID" ,
23
+ ),
24
+ ),
25
+ ("field" , models .TextField ()),
26
+ ],
27
+ ),
28
+ ]
Original file line number Diff line number Diff line change 1
1
from django .db import models
2
2
3
- ___all__ = (
4
- "CharModel" ,
5
- "IntegerModel" ,
6
- "TextModel" ,
7
- "BooleanModel" "DateModel" ,
8
- "DateTimeModel" ,
9
- "ForeignKeyModel" ,
10
- "ManyToManyModel" ,
11
- "FileModel" ,
12
- "TestModel" ,
13
- )
14
-
15
3
16
4
class CharModel (models .Model ):
17
5
field = models .CharField (max_length = 10 )
18
6
19
7
8
+ class TextModel (models .Model ):
9
+ field = models .TextField ()
10
+
11
+
20
12
class ForeignKeyModel (models .Model ):
21
13
field = models .ForeignKey (CharModel , on_delete = models .CASCADE )
22
14
Original file line number Diff line number Diff line change @@ -4,7 +4,8 @@ Changelog
4
4
Unreleased
5
5
----------
6
6
7
- * Add `--no-drop ` option to `dbrestore ` command to prevent dropping tables before restoring data.
7
+ * Add --no-drop option to dbrestore command to prevent dropping tables before restoring data.
8
+ * Fix bug where sqlite dbrestore would fail if field data contains the line break character.
8
9
9
10
4.2.0 (2024-08-22)
10
11
------------------
You can’t perform that action at this time.
0 commit comments