def getNewConnection(self, database: SqliteDatabase, *,
                      writeable: bool) -> SqliteDatabase:
     connection = SqliteDatabase.connect(filename=database.filename,
                                         writeable=writeable)
     return SqliteDatabase.fromConnection(origin=database.origin,
                                          connection=connection,
                                          writeable=writeable)
Exemple #2
0
 def getNewConnection(self, database: SqliteDatabase, *,
                      writeable: bool) -> SqliteDatabase:
     engine = SqliteDatabase.makeEngine(filename=database.filename,
                                        writeable=writeable)
     return SqliteDatabase.fromEngine(origin=database.origin,
                                      engine=engine,
                                      writeable=writeable)
Exemple #3
0
def isEmptyDatabaseActuallyWriteable(database: SqliteDatabase) -> bool:
    """Check whether we really can modify a database.

    This intentionally allows any exception to be raised (not just
    `ReadOnlyDatabaseError`) to deal with cases where the file is read-only
    but the Database was initialized (incorrectly) with writeable=True.
    """
    try:
        with database.declareStaticTables(create=True) as context:
            table = context.addTable(
                "a",
                ddl.TableSpec(fields=[ddl.FieldSpec("b", dtype=sqlalchemy.Integer, primaryKey=True)])
            )
        # Drop created table so that schema remains empty.
        database._metadata.drop_all(database._connection, tables=[table])
        return True
    except Exception:
        return False
Exemple #4
0
    def testConnection(self):
        """Test that different ways of connecting to a SQLite database
        are equivalent.
        """
        _, filename = tempfile.mkstemp(dir=self.root, suffix=".sqlite3")
        # Create a read-write database by passing in the filename.
        rwFromFilename = SqliteDatabase.fromEngine(
            SqliteDatabase.makeEngine(filename=filename), origin=0)
        self.assertEqual(rwFromFilename.filename, filename)
        self.assertEqual(rwFromFilename.origin, 0)
        self.assertTrue(rwFromFilename.isWriteable())
        self.assertTrue(isEmptyDatabaseActuallyWriteable(rwFromFilename))
        # Create a read-write database via a URI.
        rwFromUri = SqliteDatabase.fromUri(f"sqlite:///{filename}", origin=0)
        self.assertEqual(rwFromUri.filename, filename)
        self.assertEqual(rwFromUri.origin, 0)
        self.assertTrue(rwFromUri.isWriteable())
        self.assertTrue(isEmptyDatabaseActuallyWriteable(rwFromUri))
        # We don't support SQLite URIs inside SQLAlchemy URIs.
        with self.assertRaises(NotImplementedError):
            SqliteDatabase.makeEngine(
                uri=f"sqlite:///file:{filename}?uri=true")

        # Test read-only connections against a read-only file.
        with removeWritePermission(filename):
            # Create a read-only database by passing in the filename.
            roFromFilename = SqliteDatabase.fromEngine(
                SqliteDatabase.makeEngine(filename=filename),
                origin=0,
                writeable=False)
            self.assertEqual(roFromFilename.filename, filename)
            self.assertEqual(roFromFilename.origin, 0)
            self.assertFalse(roFromFilename.isWriteable())
            self.assertFalse(isEmptyDatabaseActuallyWriteable(roFromFilename))
            # Create a read-write database via a URI.
            roFromUri = SqliteDatabase.fromUri(f"sqlite:///{filename}",
                                               origin=0,
                                               writeable=False)
            self.assertEqual(roFromUri.filename, filename)
            self.assertEqual(roFromUri.origin, 0)
            self.assertFalse(roFromUri.isWriteable())
            self.assertFalse(isEmptyDatabaseActuallyWriteable(roFromUri))
Exemple #5
0
 def testConnection(self):
     """Test that different ways of connecting to a SQLite database
     are equivalent.
     """
     # Create an in-memory database by passing filename=None.
     memFromFilename = SqliteDatabase.fromConnection(SqliteDatabase.connect(filename=None), origin=0)
     self.assertIsNone(memFromFilename.filename)
     self.assertEqual(memFromFilename.origin, 0)
     self.assertTrue(memFromFilename.isWriteable())
     self.assertTrue(isEmptyDatabaseActuallyWriteable(memFromFilename))
     # Create an in-memory database via a URI.
     memFromUri = SqliteDatabase.fromUri("sqlite://", origin=0)
     self.assertIsNone(memFromUri.filename)
     self.assertEqual(memFromUri.origin, 0)
     self.assertTrue(memFromUri.isWriteable())
     self.assertTrue(isEmptyDatabaseActuallyWriteable(memFromUri))
     # We don't support SQLite URIs inside SQLAlchemy URIs.
     with self.assertRaises(NotImplementedError):
         SqliteDatabase.connect(uri="sqlite:///:memory:?uri=true")
     # We don't support read-only in-memory databases.
     with self.assertRaises(NotImplementedError):
         SqliteDatabase.connect(filename=None, writeable=False)
Exemple #6
0
 def makeEmptyDatabase(self, origin: int = 0) -> SqliteDatabase:
     _, filename = tempfile.mkstemp(dir=self.root, suffix=".sqlite3")
     connection = SqliteDatabase.connect(filename=filename)
     return SqliteDatabase.fromConnection(connection=connection, origin=origin)
Exemple #7
0
 def getNewConnection(self, database: SqliteDatabase, *, writeable: bool) -> SqliteDatabase:
     return SqliteDatabase.fromConnection(origin=database.origin, connection=database._connection,
                                          writeable=writeable)
Exemple #8
0
 def makeEmptyDatabase(self, origin: int = 0) -> SqliteDatabase:
     connection = SqliteDatabase.connect(filename=None)
     return SqliteDatabase.fromConnection(connection=connection, origin=origin)
Exemple #9
0
 def makeEmptyDatabase(self, origin: int = 0) -> SqliteDatabase:
     _, filename = tempfile.mkstemp(dir=self.root, suffix=".sqlite3")
     engine = SqliteDatabase.makeEngine(filename=filename)
     return SqliteDatabase.fromEngine(engine=engine, origin=origin)
Exemple #10
0
 def getNewConnection(self, database: SqliteDatabase, *, writeable: bool) -> SqliteDatabase:
     return SqliteDatabase.fromEngine(origin=database.origin, engine=database._engine,
                                      writeable=writeable)
Exemple #11
0
 def makeEmptyDatabase(self, origin: int = 0) -> SqliteDatabase:
     engine = SqliteDatabase.makeEngine(filename=None)
     return SqliteDatabase.fromEngine(engine=engine, origin=origin)