mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Fix dump and restore on memory limited systems
This commit is contained in:
parent
00bd5a658e
commit
18e3f9464b
@ -982,26 +982,28 @@ class DB(object):
|
|||||||
self.conn
|
self.conn
|
||||||
|
|
||||||
def dump_and_restore(self, callback=None, sql=None):
|
def dump_and_restore(self, callback=None, sql=None):
|
||||||
from io import StringIO
|
import codecs
|
||||||
|
from calibre.utils.apsw_shell import Shell
|
||||||
from contextlib import closing
|
from contextlib import closing
|
||||||
if callback is None:
|
if callback is None:
|
||||||
callback = lambda x: x
|
callback = lambda x: x
|
||||||
uv = int(self.user_version)
|
uv = int(self.user_version)
|
||||||
|
|
||||||
|
with TemporaryFile(suffix='.sql') as fname:
|
||||||
if sql is None:
|
if sql is None:
|
||||||
from calibre.utils.apsw_shell import Shell
|
|
||||||
callback(_('Dumping database to SQL') + '...')
|
callback(_('Dumping database to SQL') + '...')
|
||||||
buf = StringIO()
|
with codecs.open(fname, 'wb', encoding='utf-8') as buf:
|
||||||
shell = Shell(db=self.conn, stdout=buf)
|
shell = Shell(db=self.conn, stdout=buf)
|
||||||
shell.process_command('.dump')
|
shell.process_command('.dump')
|
||||||
sql = buf.getvalue()
|
else:
|
||||||
del shell
|
with open(fname, 'wb') as buf:
|
||||||
del buf
|
buf.write(sql if isinstance(sql, bytes) else sql.encode('utf-8'))
|
||||||
|
|
||||||
with TemporaryFile(suffix='_tmpdb.db', dir=os.path.dirname(self.dbpath)) as tmpdb:
|
with TemporaryFile(suffix='_tmpdb.db', dir=os.path.dirname(self.dbpath)) as tmpdb:
|
||||||
callback(_('Restoring database from SQL') + '...')
|
callback(_('Restoring database from SQL') + '...')
|
||||||
with closing(Connection(tmpdb)) as conn:
|
with closing(Connection(tmpdb)) as conn:
|
||||||
conn.execute(sql)
|
shell = Shell(db=conn, encoding='utf-8')
|
||||||
|
shell.process_command('.read ' + fname)
|
||||||
conn.execute('PRAGMA user_version=%d;'%uv)
|
conn.execute('PRAGMA user_version=%d;'%uv)
|
||||||
|
|
||||||
self.close()
|
self.close()
|
||||||
|
@ -606,11 +606,13 @@ class WritingTest(BaseTest):
|
|||||||
def test_dump_and_restore(self): # {{{
|
def test_dump_and_restore(self): # {{{
|
||||||
' Test roundtripping the db through SQL '
|
' Test roundtripping the db through SQL '
|
||||||
cache = self.init_cache()
|
cache = self.init_cache()
|
||||||
|
uv = int(cache.backend.user_version)
|
||||||
all_ids = cache.all_book_ids()
|
all_ids = cache.all_book_ids()
|
||||||
cache.dump_and_restore()
|
cache.dump_and_restore()
|
||||||
self.assertEqual(cache.set_field('title', {1:'nt'}), set([1]), 'database connection broken')
|
self.assertEqual(cache.set_field('title', {1:'nt'}), set([1]), 'database connection broken')
|
||||||
cache = self.init_cache()
|
cache = self.init_cache()
|
||||||
self.assertEqual(cache.all_book_ids(), all_ids, 'dump and restore broke database')
|
self.assertEqual(cache.all_book_ids(), all_ids, 'dump and restore broke database')
|
||||||
|
self.assertEqual(int(cache.backend.user_version), uv)
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
def test_set_author_data(self): # {{{
|
def test_set_author_data(self): # {{{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user