mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Allow --reinitialize-db to use an SQL dump from elsewhere
This commit is contained in:
parent
63f02aa91b
commit
d225fd9ff5
@ -36,13 +36,17 @@ Run an embedded python interpreter.
|
|||||||
'plugin code.')
|
'plugin code.')
|
||||||
parser.add_option('--reinitialize-db', default=None,
|
parser.add_option('--reinitialize-db', default=None,
|
||||||
help='Re-initialize the sqlite calibre database at the '
|
help='Re-initialize the sqlite calibre database at the '
|
||||||
'specified path. Useful to recover from db corruption.')
|
'specified path. Useful to recover from db corruption.'
|
||||||
|
' You can also specify the path to an SQL dump which '
|
||||||
|
'will be used instead of trying to dump the database.'
|
||||||
|
' This can be useful when dumping fails, but dumping '
|
||||||
|
'with sqlite3 works.')
|
||||||
parser.add_option('-p', '--py-console', help='Run python console',
|
parser.add_option('-p', '--py-console', help='Run python console',
|
||||||
default=False, action='store_true')
|
default=False, action='store_true')
|
||||||
|
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
def reinit_db(dbpath, callback=None):
|
def reinit_db(dbpath, callback=None, sql_dump=None):
|
||||||
if not os.path.exists(dbpath):
|
if not os.path.exists(dbpath):
|
||||||
raise ValueError(dbpath + ' does not exist')
|
raise ValueError(dbpath + ' does not exist')
|
||||||
from calibre.library.sqlite import connect
|
from calibre.library.sqlite import connect
|
||||||
@ -52,26 +56,32 @@ def reinit_db(dbpath, callback=None):
|
|||||||
uv = conn.get('PRAGMA user_version;', all=False)
|
uv = conn.get('PRAGMA user_version;', all=False)
|
||||||
conn.execute('PRAGMA writable_schema=ON')
|
conn.execute('PRAGMA writable_schema=ON')
|
||||||
conn.commit()
|
conn.commit()
|
||||||
sql_lines = conn.dump()
|
if sql_dump is None:
|
||||||
|
sql_lines = conn.dump()
|
||||||
|
else:
|
||||||
|
sql_lines = open(sql_dump, 'rb').read()
|
||||||
conn.close()
|
conn.close()
|
||||||
dest = dbpath + '.tmp'
|
dest = dbpath + '.tmp'
|
||||||
try:
|
try:
|
||||||
with closing(connect(dest, False)) as nconn:
|
with closing(connect(dest, False)) as nconn:
|
||||||
nconn.execute('create temporary table temp_sequence(id INTEGER PRIMARY KEY AUTOINCREMENT)')
|
nconn.execute('create temporary table temp_sequence(id INTEGER PRIMARY KEY AUTOINCREMENT)')
|
||||||
nconn.commit()
|
nconn.commit()
|
||||||
if callable(callback):
|
if sql_dump is None:
|
||||||
callback(len(sql_lines), True)
|
if callable(callback):
|
||||||
for i, line in enumerate(sql_lines):
|
callback(len(sql_lines), True)
|
||||||
try:
|
for i, line in enumerate(sql_lines):
|
||||||
nconn.execute(line)
|
try:
|
||||||
except:
|
nconn.execute(line)
|
||||||
import traceback
|
except:
|
||||||
prints('SQL line %r failed with error:'%line)
|
import traceback
|
||||||
prints(traceback.format_exc())
|
prints('SQL line %r failed with error:'%line)
|
||||||
continue
|
prints(traceback.format_exc())
|
||||||
finally:
|
continue
|
||||||
if callable(callback):
|
finally:
|
||||||
callback(i, False)
|
if callable(callback):
|
||||||
|
callback(i, False)
|
||||||
|
else:
|
||||||
|
nconn.executescript(sql_lines)
|
||||||
nconn.execute('pragma user_version=%d'%int(uv))
|
nconn.execute('pragma user_version=%d'%int(uv))
|
||||||
nconn.commit()
|
nconn.commit()
|
||||||
os.remove(dbpath)
|
os.remove(dbpath)
|
||||||
@ -170,7 +180,10 @@ def main(args=sys.argv):
|
|||||||
prints('CALIBRE_EXTENSIONS_PATH='+sys.extensions_location)
|
prints('CALIBRE_EXTENSIONS_PATH='+sys.extensions_location)
|
||||||
prints('CALIBRE_PYTHON_PATH='+os.pathsep.join(sys.path))
|
prints('CALIBRE_PYTHON_PATH='+os.pathsep.join(sys.path))
|
||||||
elif opts.reinitialize_db is not None:
|
elif opts.reinitialize_db is not None:
|
||||||
reinit_db(opts.reinitialize_db)
|
sql_dump = None
|
||||||
|
if len(args) > 1 and os.access(args[-1], os.R_OK):
|
||||||
|
sql_dump = args[-1]
|
||||||
|
reinit_db(opts.reinitialize_db, sql_dump=sql_dump)
|
||||||
else:
|
else:
|
||||||
from calibre import ipython
|
from calibre import ipython
|
||||||
ipython()
|
ipython()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user