Repository: simphony/simphony-remote

Issue:
Sqlite database file created if it does not exist
Similar to #113, when `remoteappmanager` is started with its default accounting setting and that the default sqlite database does not exist, an empty file `remoteappmanager.db` would be created.


Discussion:

Patch:
diff --git a/remoteappmanager/db/orm.py b/remoteappmanager/db/orm.py
index 7111d0b..f877d5a 100644
--- a/remoteappmanager/db/orm.py
+++ b/remoteappmanager/db/orm.py
@@ -1,5 +1,6 @@
 import contextlib
 import hashlib
+import os
 
 from sqlalchemy import (
     Column, Integer, Boolean, Unicode, ForeignKey, create_engine, Enum,
@@ -157,6 +158,21 @@ class AppAccounting(ABCAccounting):
 
     def __init__(self, url, **kwargs):
         self.db = Database(url, **kwargs)
+        self.check_database_readable()
+
+    def check_database_readable(self):
+        ''' Raise IOError if the database url points to a sqlite database
+        that is not readable
+
+        TODO: may extend for validating databases in other dialects?
+        '''
+        db_url = self.db.url
+
+        if db_url.startswith('sqlite:///'):
+            file_path = os.path.abspath(db_url[10:])
+            if not os.access(file_path, os.R_OK):
+                raise IOError(
+                    'Sqlite database {} is not readable'.format(file_path))
 
     def get_user_by_name(self, user_name):
         """ Return an orm.User given a user name.  Return None


Test patch:
diff --git a/tests/db/test_interfaces.py b/tests/db/test_interfaces.py
index 02ced52..4ed20d0 100644
--- a/tests/db/test_interfaces.py
+++ b/tests/db/test_interfaces.py
@@ -8,7 +8,7 @@ from collections import namedtuple
 
 from remoteappmanager.db.interfaces import (
     ABCApplication, ABCApplicationPolicy, ABCAccounting)
-from .abc_test_interfaces import ABCTestDatabaseInterface
+from tests.db.abc_test_interfaces import ABCTestDatabaseInterface
 
 User = namedtuple('User', ('name',))
 
diff --git a/tests/db/test_orm.py b/tests/db/test_orm.py
index 2869b94..6c1052c 100644
--- a/tests/db/test_orm.py
+++ b/tests/db/test_orm.py
@@ -199,3 +199,12 @@ class TestOrmAppAccounting(TempMixin, ABCTestDatabaseInterface,
 
         self.assertEqual(actual_app, expected_config[0][0])
         self.assertEqual(actual_policy, expected_config[0][1])
+
+    def test_no_file_creation_if_sqlite_database_not_exist(self):
+        temp_file_path = os.path.join(self.tempdir, 'some.db')
+
+        with self.assertRaises(IOError):
+            AppAccounting(
+                url="sqlite:///"+temp_file_path)
+
+        self.assertFalse(os.path.exists(temp_file_path))