Changeset - c1ecc4829722
[Not reviewed]
0 1 0
Brett Smith - 4 years ago 2020-05-13 14:48:09
brettcsmith@brettcsmith.org
rtutil: Avoid loading from a URI in RTLinkCache.setup.

See comments for rationale. RT#10543.
1 file changed with 6 insertions and 1 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/rtutil.py
Show inline comments
...
 
@@ -95,50 +95,55 @@ class RTLinkCache(_LinkCache):
 
            return None
 
        try:
 
            # There shouldn't be any records where url is NULL, so running this
 
            # DELETE pulls double duty for us: it tells us whether or not we
 
            # can write to the database and it enforces database integrity.
 
            cursor.execute('DELETE FROM RTLinkCache WHERE url IS NULL')
 
        except sqlite3.OperationalError:
 
            cls.logger.debug("setup: error writing %s", cache_path, exc_info=True)
 
            can_write = False
 
        else:
 
            can_write = True
 
        if not (can_write or have_data):
 
            # If there's nothing to read and no way to write, sqlite provides
 
            # no benefit.
 
            cls.logger.debug("setup: not using %s: nothing to read or write", cache_path)
 
            return None
 
        elif not can_write:
 
            # Set up an in-memory database that we can write to, seeded with
 
            # the data available to read.
 
            try:
 
                cursor.close()
 
                db.close()
 
                db = sqlite3.connect(':memory:', isolation_level=None)
 
                cursor = db.cursor()
 
                # It would better to use
 
                #   '{}?mode=ro'.format(cache_path.as_uri())
 
                # as the argument here, but that doesn't work on SUSE 15,
 
                # possibly because its sqlite doesn't recognize query
 
                # arguments (added to upstream sqlite in late 2016).
 
                cursor.execute('ATTACH DATABASE ? AS readsource',
 
                               ('{}?mode=ro'.format(cache_path.as_uri()),))
 
                               (os.fspath(cache_path),))
 
                cursor.execute(cls.CREATE_TABLE_SQL)
 
                cursor.execute('INSERT INTO RTLinkCache SELECT * FROM readsource.RTLinkCache')
 
                cursor.execute('DETACH DATABASE readsource')
 
            except sqlite3.OperationalError as error:
 
                # We're back to the case of having nothing to read and no way
 
                # to write.
 
                cls.logger.debug("setup: error loading %s into memory", cache_path, exc_info=True)
 
                return None
 
            else:
 
                cls.logger.debug("setup: loaded %s into memory", cache_path)
 
        else:
 
            cls.logger.debug("setup: caching at %s", cache_path)
 
        cursor.close()
 
        db.commit()
 
        return db
 

	
 
    def __init__(self, cache_db: sqlite3.Connection) -> None:
 
        self._db = cache_db
 
        self._nourls: Set[TicketAttachmentIds] = set()
 

	
 
    def __iter__(self) -> Iterator[TicketAttachmentIds]:
 
        yield from self._db.execute('SELECT ticket_id, attachment_id FROM RTLinkCache')
 
        yield from self._nourls
 

	
0 comments (0 inline, 0 general)