Fix missing .hex() when inserting SHA256

This commit is contained in:
Jacob Hinkle 2022-10-11 12:49:11 -04:00
parent 8e69ca3390
commit 7a0fea9a80
3 changed files with 24 additions and 31 deletions

View File

@ -37,7 +37,7 @@ deps =
pytest-cov pytest-cov
coverage coverage
commands = commands =
pytest --cov {envsitepackagesdir}/nancy pytest --cov {envsitepackagesdir}/nancy {posargs}
[testenv:mypy] [testenv:mypy]
deps = deps =

View File

@ -89,9 +89,9 @@ class FSEntryVersion:
"INSERT INTO filedir_version VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", "INSERT INTO filedir_version VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
( (
self.uuid, self.uuid,
self.filedir.sha256, self.filedir.sha256.hex(),
datetime.now().timestamp(), datetime.now().timestamp(),
self.filetype, str(self.filetype),
False, False,
self.perms, self.perms,
self.symlink_target, self.symlink_target,
@ -286,22 +286,16 @@ class FSEntry:
) -> None: ) -> None:
# Find entries with this name and parent # Find entries with this name and parent
cur.execute( cur.execute(
"SELECT sha256 FROM filedir WHERE store = 1 AND name = ? AND parent = ? LIMIT 1", "INSERT OR IGNORE INTO filedir VALUES (?, ?, ?, ?)",
(self.filename, None if self.parent is None else self.parent.sha256),
)
res = cur.fetchall()
if len(res) == 0:
# create filedir entry and get its id
cur.execute(
"INSERT INTO filedir VALUES (?, ?, ?, ?)",
( (
self.sha256, self.sha256.hex(),
None if self.store is None else self.store.uuid, None if self.store is None else self.store.uuid,
self.filename, self.filename,
parent_key, parent_key,
), ),
) )
self.versions[-1].persist(cur=cur, source_task=source_task) for v in self.versions:
v.persist(cur=cur, source_task=source_task)
# descend into children and record all of them anew as well # descend into children and record all of them anew as well
for c in self.children: for c in self.children:

View File

@ -30,6 +30,7 @@ class Store:
self, self,
directory: fs.PathStr, directory: fs.PathStr,
conn: Optional[sqlite3.Connection] = None, conn: Optional[sqlite3.Connection] = None,
uuid: Optional[str] = None,
): ):
self.path = Path(directory) self.path = Path(directory)
self.db_path = self.path / "nancy.db" self.db_path = self.path / "nancy.db"
@ -39,7 +40,7 @@ class Store:
else: else:
self.conn = conn self.conn = conn
self.uuid = self.find_store_uuid() self.uuid = self.find_store_uuid() if uuid is None else uuid
def copy(self: _StoreT, store_path: fs.PathStr) -> _StoreT: def copy(self: _StoreT, store_path: fs.PathStr) -> _StoreT:
"""Copy this store to a new store path""" """Copy this store to a new store path"""
@ -94,21 +95,19 @@ class Store:
conn.commit() conn.commit()
cur.close() cur.close()
new_store = cls(directory=directory, conn=conn) new_uuid = str(uuid.uuid4())
new_store = cls(directory=directory, conn=conn, uuid=new_uuid)
with new_store.new_program("INIT", message) as p:
# set the timing to the actual times it took to initialize the db
p.start_time = start_time
# generate a new UUID for this store # generate a new UUID for this store
assert new_store.uuid is None
new_store.uuid = str(uuid.uuid4())
with new_store.committing() as cur: with new_store.committing() as cur:
cur.execute( cur.execute(
'INSERT INTO local_metadata VALUES ("store_uuid", ?)', 'INSERT INTO local_metadata VALUES ("store_uuid", ?)',
(new_store.uuid,), (new_store.uuid,),
) )
with new_store.new_program("INIT", message) as p:
# set the timing to the actual times it took to initialize the db
p.start_time = start_time
return new_store return new_store
def filedir_root_key(self, cur: Optional[sqlite3.Cursor] = None) -> Optional[str]: def filedir_root_key(self, cur: Optional[sqlite3.Cursor] = None) -> Optional[str]:
@ -116,7 +115,7 @@ class Store:
if cur is None: if cur is None:
assert self.conn is not None assert self.conn is not None
cur = self.conn.cursor() cur = self.conn.cursor()
cur.execute("SELECT uuid FROM filedir WHERE store=1 AND parent is NULL") cur.execute("SELECT sha256 FROM filedir WHERE store=1 AND parent is NULL")
row = cur.fetchone() row = cur.fetchone()
if row is None: if row is None:
return None return None
@ -147,7 +146,7 @@ class Store:
for p in Path(rel).parts: # Path.parts splits a path reliably for p in Path(rel).parts: # Path.parts splits a path reliably
# get child with that name # get child with that name
cur.execute( cur.execute(
"SELECT uuid, filetype FROM filedir WHERE filename=? AND parent=? LIMIT 1", "SELECT sha256, filetype FROM filedir WHERE filename=? AND parent=? LIMIT 1",
(p, fd_key), (p, fd_key),
) )
row = cur.fetchone() row = cur.fetchone()