Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8ec0e42

Browse files
committed
try to atomically create DB
1 parent 745fc89 commit 8ec0e42

1 file changed

Lines changed: 18 additions & 12 deletions

File tree

IPython/core/history.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -242,18 +242,24 @@ def init_db(self):
242242
kwargs = dict(detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES)
243243
kwargs.update(self.connection_options)
244244
self.db = sqlite3.connect(str(self.hist_file), **kwargs)
245-
self.db.execute("""CREATE TABLE IF NOT EXISTS sessions (session integer
246-
primary key autoincrement, start timestamp,
247-
end timestamp, num_cmds integer, remark text)""")
248-
self.db.execute("""CREATE TABLE IF NOT EXISTS history
249-
(session integer, line integer, source text, source_raw text,
250-
PRIMARY KEY (session, line))""")
251-
# Output history is optional, but ensure the table's there so it can be
252-
# enabled later.
253-
self.db.execute("""CREATE TABLE IF NOT EXISTS output_history
254-
(session integer, line integer, output text,
255-
PRIMARY KEY (session, line))""")
256-
self.db.commit()
245+
with self.db:
246+
self.db.execute(
247+
"""CREATE TABLE IF NOT EXISTS sessions (session integer
248+
primary key autoincrement, start timestamp,
249+
end timestamp, num_cmds integer, remark text)"""
250+
)
251+
self.db.execute(
252+
"""CREATE TABLE IF NOT EXISTS history
253+
(session integer, line integer, source text, source_raw text,
254+
PRIMARY KEY (session, line))"""
255+
)
256+
# Output history is optional, but ensure the table's there so it can be
257+
# enabled later.
258+
self.db.execute(
259+
"""CREATE TABLE IF NOT EXISTS output_history
260+
(session integer, line integer, output text,
261+
PRIMARY KEY (session, line))"""
262+
)
257263
# success! reset corrupt db count
258264
self._corrupt_db_counter = 0
259265

0 commit comments

Comments
 (0)