def test_success(self) -> None: self.assertFalse(self.conn.in_transaction) with dbver.begin_pool(self.pool, dbver.LockMode.DEFERRED) as conn: self.assertTrue(self.conn.in_transaction) conn.cursor().execute("insert into x (x) values (1)") self.assertFalse(self.conn.in_transaction) self.assertEqual( self.conn.cursor().execute("select * from x").fetchall(), [(1, )])
def test_fail(self) -> None: self.assertFalse(self.conn.in_transaction) with self.assertRaises(DummyException): with dbver.begin_pool(self.pool, dbver.LockMode.DEFERRED) as conn: self.assertTrue(self.conn.in_transaction) conn.cursor().execute("insert into x (x) values (1)") raise DummyException() self.assertFalse(self.conn.in_transaction) self.assertEqual( self.conn.cursor().execute("select * from x").fetchall(), [])
def _apply(self, jobs: Iterable[Job]) -> None: with dbver.begin_pool(self._pool, dbver.IMMEDIATE) as conn: dbver.semver_check_breaking(LATEST, upgrade(conn)) for job in jobs: try: job(conn) except apsw.Error: _LOG.exception( "dropped resume data update %r", job, )
def _user_write(pool: dbver.Pool) -> Iterator[Tuple[sqlite3.Connection, int]]: with dbver.begin_pool(pool, dbver.LockMode.IMMEDIATE) as conn: version = user_db.upgrade(conn) dbver.semver_check_breaking(version, _USER_VERSION_SUPPORTED) yield (conn, version)
def _meta_read(pool: dbver.Pool) -> Iterator[Tuple[sqlite3.Connection, int]]: with dbver.begin_pool(pool, dbver.LockMode.DEFERRED) as conn: version = metadata_db.get_version(conn) dbver.semver_check_breaking(version, _META_VERSION_SUPPORTED) yield (conn, version)
def iter_atps() -> Iterator[lt.add_torrent_params]: with dbver.begin_pool(self._pool, dbver.LockMode.DEFERRED) as conn: yield from iter_resume_data_from_db(conn)
def write_metadata_db() -> Iterator[Tuple[sqlite3.Connection, int]]: # TODO: should we set WAL? where? with dbver.begin_pool(metadata_db_pool, dbver.LockMode.IMMEDIATE) as conn: version = metadata_db.upgrade(conn) dbver.semver_check_breaking(version, METADATA_DB_VERSION_SUPPORTED) yield (conn, version)