Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e983457

Browse files
committed
Wipe out the database without os.remove
To avoid Windows locking problems
1 parent 7adc111 commit e983457

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

lib/matplotlib/dviread.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,17 +1055,26 @@ def __init__(self, filename=None):
10551055
else:
10561056
_log.warn("%s created with a newer version (%d>%d), deleting",
10571057
filename, version, self.schema_version)
1058-
self.connection.close()
1059-
os.remove(filename)
1058+
self._wipeout()
10601059

10611060
while version < self.schema_version:
10621061
version += 1
10631062
self._migrate(version)
10641063

1065-
def close(self):
1066-
"""Close the database connection, making the database unusable.
1067-
Useful mainly in tests."""
1068-
pass
1064+
def _wipeout(self):
1065+
"""Wipe out the schema."""
1066+
with self.connection as conn:
1067+
conn.executescript(
1068+
"""
1069+
PRAGMA writable_schema=1;
1070+
DELETE FROM sqlite_master WHERE type = 'table';
1071+
DELETE FROM sqlite_master WHERE type = 'view';
1072+
DELETE FROM sqlite_master WHERE type = 'index';
1073+
DELETE FROM sqlite_master WHERE type = 'trigger';
1074+
PRAGMA writable_schema=0;
1075+
VACUUM;
1076+
PRAGMA user_version=0;
1077+
""")
10691078

10701079
def _migrate(self, version):
10711080
"""Update the database schema to the given schema version from

lib/matplotlib/tests/test_dviread.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,36 @@ def test_tex_support_cache_versioning(tmpdir):
9898

9999
conn = sqlite3.connect(dbfile, isolation_level="DEFERRED")
100100
with conn:
101-
conn.execute("PRAGMA user_version=1000000000;")
101+
conn.executescript(
102+
"""
103+
CREATE TABLE foo (a, b);
104+
CREATE TABLE bar (c, d);
105+
CREATE INDEX foo_a ON foo (a);
106+
CREATE VIEW foo_a0 AS SELECT a FROM foo WHERE a>0;
107+
CREATE TRIGGER trig INSERT ON foo WHEN b<0
108+
BEGIN INSERT INTO bar VALUES (1,2); END;
109+
PRAGMA user_version=1000000000;
110+
""")
102111
conn.close()
103112

104113
# now reopening the cache should wipe it out
105114
cache2 = dr._tex_support_cache(dbfile)
106115
assert cache2.get_pathnames(['foo']) == {}
107116

117+
# there should be no extra objects in the schema
118+
conn = sqlite3.connect(dbfile, isolation_level="DEFERRED")
119+
with conn:
120+
rows = conn.execute(
121+
"""SELECT name FROM sqlite_master
122+
WHERE type IN ('table', 'index', 'trigger', 'view')
123+
""").fetchall()
124+
assert ('foo',) not in rows
125+
assert ('bar',) not in rows
126+
assert ('foo_a',) not in rows
127+
assert ('foo_a0',) not in rows
128+
assert ('trig',) not in rows
129+
assert ('file_path',) in rows
130+
108131

109132
def test_find_tex_files(tmpdir):
110133
with mock.patch('matplotlib.dviread.subprocess.Popen') as mock_popen:

0 commit comments

Comments
 (0)