summaryrefslogtreecommitdiff
path: root/lorrycontroller/statedb.py
diff options
context:
space:
mode:
Diffstat (limited to 'lorrycontroller/statedb.py')
-rw-r--r--lorrycontroller/statedb.py40
1 files changed, 25 insertions, 15 deletions
diff --git a/lorrycontroller/statedb.py b/lorrycontroller/statedb.py
index 99ea7fc..17b31dd 100644
--- a/lorrycontroller/statedb.py
+++ b/lorrycontroller/statedb.py
@@ -56,7 +56,6 @@ class StateDB(object):
self._conn = None
self._transaction_started = None
- def _open(self):
self.initial_lorries_fields = [
('path', 'TEXT PRIMARY KEY'),
('text', 'TEXT'),
@@ -76,25 +75,36 @@ class StateDB(object):
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')