diff options
-rw-r--r-- | CHANGES.rst | 8 | ||||
-rw-r--r-- | coverage/sqldata.py | 29 |
2 files changed, 29 insertions, 8 deletions
diff --git a/CHANGES.rst b/CHANGES.rst index 7438771d..1fff59ea 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -37,6 +37,14 @@ Unreleased - SQLite data storage is now faster. There's no longer a reason to keep the JSON data file code, so it has been removed. +- Changes to the `CoverageData` interface: + + - The new :meth:`CoverageData.dumps` method serializes the data to a string, + and a corresponding :meth:`CoverageData.loads` method reconstitutes ths + data. The format of the data string is subject to change at any time, and + so should only be used between two installations of the same version of + coverage.py. + - Added the classmethod :meth:`Coverage.current` to get the latest started `Coverage` instance. diff --git a/coverage/sqldata.py b/coverage/sqldata.py index 998abd64..a9ba0d41 100644 --- a/coverage/sqldata.py +++ b/coverage/sqldata.py @@ -188,12 +188,9 @@ class CoverageData(SimpleReprMixin): def _create_db(self): if self._debug.should('dataio'): self._debug.write("Creating data file {!r}".format(self._filename)) - self._dbs[get_thread_id()] = SqliteDb(self._filename, self._debug) - with self._dbs[get_thread_id()] as db: - for stmt in SCHEMA.split(';'): - stmt = stmt.strip() - if stmt: - db.execute(stmt) + self._dbs[get_thread_id()] = db = SqliteDb(self._filename, self._debug) + with db: + db.executescript(SCHEMA) db.execute("insert into coverage_schema (version) values (?)", (SCHEMA_VERSION,)) db.execute( "insert into meta (has_lines, has_arcs, sys_argv) values (?, ?, ?)", @@ -203,8 +200,8 @@ class CoverageData(SimpleReprMixin): def _open_db(self): if self._debug.should('dataio'): self._debug.write("Opening data file {!r}".format(self._filename)) - self._dbs[get_thread_id()] = SqliteDb(self._filename, self._debug) - with self._dbs[get_thread_id()] as db: + self._dbs[get_thread_id()] = db = SqliteDb(self._filename, self._debug) + with db: try: schema_version, = db.execute("select version from coverage_schema").fetchone() except Exception as exc: @@ -254,6 +251,17 @@ class CoverageData(SimpleReprMixin): with self._connect() as con: self._debug.write(con.dump()) + def dumps(self): + with self._connect() as con: + return con.dump() + + def loads(self, data): + if self._debug.should('dataio'): + self._debug.write("Loading data into data file {!r}".format(self._filename)) + self._dbs[get_thread_id()] = db = SqliteDb(self._filename, self._debug) + with db: + db.executescript(data) + def _file_id(self, filename, add=False): """Get the file id for `filename`. @@ -800,6 +808,11 @@ class SqliteDb(SimpleReprMixin): self.debug.write("Executing many {!r} with {} rows".format(sql, len(data))) return self.con.executemany(sql, data) + def executescript(self, script): + if self.debug: + self.debug.write("Executing script with {} chars".format(len(script))) + self.con.executescript(script) + def dump(self): # pragma: debugging """Return a multi-line string, the dump of the database.""" return "\n".join(self.con.iterdump()) |