@@ -1089,41 +1089,44 @@ def _connect_and_init(self) -> None:
10891089
10901090 # Detect an existing schema version *before* creating any tables so
10911091 # a structural migration (added columns / renamed indexes) can be
1092- # handled by dropping the old tables outright.
1092+ # handled by dropping the old tables outright. Two cases yield
1093+ # "version unknown": schema_meta is missing entirely, or schema_meta
1094+ # exists but has no ``schema_version`` row. Both mean we can't trust
1095+ # any pre-existing ``entries`` layout -- CREATE TABLE IF NOT EXISTS
1096+ # would keep it and later reads/writes would fail with
1097+ # OperationalError instead of recovering to an empty cache.
10931098 existing_version = None
1094- schema_meta_missing = False
1099+ schema_version_known = False
10951100 try :
10961101 row = self ._conn .execute (
10971102 "SELECT value FROM schema_meta WHERE key = ?" ,
10981103 ("schema_version" ,),
10991104 ).fetchone ()
11001105 if row is not None :
11011106 existing_version = row [0 ]
1107+ schema_version_known = True
11021108 except self ._sqlite3 .OperationalError :
11031109 # schema_meta doesn't exist.
1104- schema_meta_missing = True
1110+ pass
11051111
1106- # If schema_meta is absent but an ``entries`` table is already
1107- # present (e.g. the file was produced by a different tool or a
1108- # pre-schema_meta version of this cache), treat the DB as
1109- # incompatible: CREATE TABLE IF NOT EXISTS would keep the old
1110- # ``entries`` layout and later reads/writes would fail with
1111- # OperationalError instead of recovering to an empty cache.
1112- entries_present_without_schema_meta = False
1113- if schema_meta_missing :
1112+ # If the schema_version isn't known but an ``entries`` table exists,
1113+ # the DB came from a different tool or a pre-schema_meta revision;
1114+ # drop and rebuild. If both are present and match, leave in place.
1115+ entries_present_with_unknown_version = False
1116+ if not schema_version_known :
11141117 row = self ._conn .execute (
11151118 "SELECT name FROM sqlite_master WHERE type='table' AND name='entries'" ,
11161119 ).fetchone ()
1117- entries_present_without_schema_meta = row is not None
1120+ entries_present_with_unknown_version = row is not None
11181121
11191122 needs_drop = (
1120- (existing_version is not None and existing_version != _SQLITE_SCHEMA_VERSION )
1121- or entries_present_without_schema_meta
1123+ (schema_version_known and existing_version != _SQLITE_SCHEMA_VERSION )
1124+ or entries_present_with_unknown_version
11221125 )
11231126 if needs_drop :
11241127 # Drop all cache tables -- ensures structural mismatch (whether
1125- # a version bump or a foreign ``entries`` layout) can't leave a
1126- # legacy table in place.
1128+ # a version bump or a foreign/pre-schema_meta ``entries``
1129+ # layout) can't leave a legacy table in place.
11271130 self ._conn .execute ("DROP TABLE IF EXISTS entries" )
11281131 self ._conn .execute ("DROP TABLE IF EXISTS schema_meta" )
11291132
0 commit comments