Skip to content

Commit

Permalink
Query sqlite_master in ensure_insert_time_column()
Browse files Browse the repository at this point in the history
  • Loading branch information
my8100 committed Jul 3, 2019
1 parent 631fd44 commit 0343475
Showing 1 changed file with 7 additions and 16 deletions.
23 changes: 7 additions & 16 deletions scrapyd/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,22 +148,13 @@ def clear(self):
self.update_project_priority_map()

def ensure_insert_time_column(self):
q = "SELECT insert_time FROM %s LIMIT 1" % self.table
try:
self.conn.execute(q).fetchone()
except sqlite3.OperationalError as err:
# sqlite3.OperationalError: no such column: insert_time
if 'insert_time' in str(err):
# Cannot append 'DEFAULT CURRENT_TIMESTAMP' when ALTER TABLE, otherwise,
# would raise sqlite3.OperationalError: Cannot add a column with non-constant default
q = "ALTER TABLE %s ADD COLUMN insert_time TIMESTAMP" % self.table
self.conn.execute(q)
# Fill in the new added column for exsiting pending jobs
q = "UPDATE %s SET insert_time=CURRENT_TIMESTAMP" % self.table
self.conn.execute(q)
self.conn.commit()
else:
raise
q = "SELECT sql FROM sqlite_master WHERE type='table' AND name='%s'" % self.table
if 'insert_time TIMESTAMP' not in self.conn.execute(q).fetchone()[0]:
q = "ALTER TABLE %s ADD COLUMN insert_time TIMESTAMP" % self.table
self.conn.execute(q)
q = "UPDATE %s SET insert_time=CURRENT_TIMESTAMP" % self.table
self.conn.execute(q)
self.conn.commit()

def update_project_priority_map(self):
q = "select priority, strftime('%%s', insert_time) from %s order by priority desc limit 1" \
Expand Down

0 comments on commit 0343475

Please sign in to comment.