summaryrefslogtreecommitdiff
path: root/lorrycontroller/statedb.py
diff options
context:
space:
mode:
Diffstat (limited to 'lorrycontroller/statedb.py')
-rw-r--r--lorrycontroller/statedb.py61
1 files changed, 43 insertions, 18 deletions
diff --git a/lorrycontroller/statedb.py b/lorrycontroller/statedb.py
index 1b885d9..17b31dd 100644
--- a/lorrycontroller/statedb.py
+++ b/lorrycontroller/statedb.py
@@ -1,4 +1,4 @@
-# Copyright (C) 2014-2016 Codethink Limited
+# Copyright (C) 2014-2017 Codethink Limited
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@@ -56,8 +56,7 @@ class StateDB(object):
self._conn = None
self._transaction_started = None
- def _open(self):
- self.lorries_fields = [
+ self.initial_lorries_fields = [
('path', 'TEXT PRIMARY KEY'),
('text', 'TEXT'),
('from_trovehost', 'TEXT'),
@@ -68,28 +67,44 @@ class StateDB(object):
('lorry_timeout', 'INT'),
('disk_usage', 'INT'),
]
+ self.lorries_fields = list(self.initial_lorries_fields)
+ self.lorries_fields.extend([
+ ('last_run_exit', 'TEXT'),
+ ('last_run_error', 'TEXT'),
+ ])
self.lorries_booleans = [
]
+ def _open(self):
if self._conn is None:
- existed = os.path.exists(self._filename)
- logging.debug(
- 'Connecting to %r (existed=%r)', self._filename, existed)
- self._conn = sqlite3.connect(
- self._filename,
- timeout=100000,
- isolation_level="IMMEDIATE")
- logging.debug('New connection is %r', self._conn)
- if not existed:
- self._initialise_tables()
-
- self.perform_any_migrations()
-
- def perform_any_migrations(self):
+ db_exists = os.path.exists(self._filename)
+ assert db_exists
+ self._create_or_connect_to_db()
+
+ def _create_or_connect_to_db(self):
+ logging.debug(
+ 'Connecting to %r', self._filename)
+ self._conn = sqlite3.connect(
+ self._filename,
+ timeout=100000,
+ isolation_level="IMMEDIATE")
+ logging.debug('New connection is %r', self._conn)
+
+ def initialise_db(self):
+ db_exists = os.path.exists(self._filename)
+ if self._conn is None:
+ self._create_or_connect_to_db()
+ if not db_exists:
+ self._initialise_tables()
+ self._perform_any_migrations()
+
+ def _perform_any_migrations(self):
+ logging.debug('Performing database migrations needed')
backend = yoyo.get_backend('sqlite:///' + self._filename)
migrations_dir = os.path.join(os.path.dirname(__file__), 'migrations')
migrations = yoyo.read_migrations(migrations_dir)
backend.apply_migrations(backend.to_apply(migrations))
+ logging.debug('Database migrated')
def _initialise_tables(self):
logging.debug('Initialising tables in database')
@@ -118,7 +133,7 @@ class StateDB(object):
# Table for all the known lorries (the "run queue").
fields_sql = ', '.join(
- '%s %s' % (name, info) for name, info in self.lorries_fields
+ '%s %s' % (name, info) for name, info in self.initial_lorries_fields
)
c.execute('CREATE TABLE lorries (%s)' % fields_sql)
@@ -435,6 +450,16 @@ class StateDB(object):
'UPDATE lorries SET last_run=? WHERE path=?',
(last_run, path))
+ def set_lorry_last_run_exit_and_output(self, path, exit, output):
+ logging.debug(
+ 'StateDB.set_lorry_last_run_exit_and_output(%r, %r, %r) called',
+ path, exit, output)
+ assert self.in_transaction
+ c = self.get_cursor()
+ c.execute(
+ 'UPDATE lorries SET last_run_exit=?, last_run_error=? WHERE path=?',
+ (exit, output, path))
+
def set_lorry_disk_usage(self, path, disk_usage):
logging.debug(
'StateDB.set_lorry_disk_usage(%r, %r) called', path, disk_usage)