Repository: rotki/rotki

Issue:
rotki 1.26.0 + windows: Unable to open database
## Problem Definition

Premium windows users started getting this after upgrading to 1.26.0.

It's probably related to the changes I did here: https://github.com/rotki/rotki/pull/5043 but manifests only in Windows as none else saw it when using/testing in other OSes.

## Logs

```
[28/10/2022 18:14:05 W. Europe Daylight Time] ERROR rotkehlchen.greenlets Greenlet with id 3094640951536: Upload data to server died with exception: unable to open database: C:\Users\isido\AppData\Local\Temp\tmpp2jyr7sq.db.
Exception Name: <class 'pysqlcipher3.dbapi2.OperationalError'>
Exception Info: unable to open database: C:\Users\isido\AppData\Local\Temp\tmpp2jyr7sq.db
Traceback:
   File "src\\gevent\\greenlet.py", line 908, in gevent._gevent_cgreenlet.Greenlet.run
  File "rotkehlchen\premium\sync.py", line 143, in maybe_upload_data_to_server
  File "rotkehlchen\data_handler.py", line 198, in compress_and_encrypt_db
  File "rotkehlchen\db\dbhandler.py", line 483, in export_unencrypted
  File "rotkehlchen\db\drivers\gevent.py", line 264, in executescript

[28/10/2022 18:14:05 W. Europe Daylight Time] ERROR rotkehlchen.user_messages Greenlet with id 3094640951536: Upload data to server died with exception: unable to open database: C:\Users\isido\AppData\Local\Temp\tmpp2jyr7sq.db. Check the logs for more details
```



### System Description


Operating system: Windows
Rotki version: 1.26.0



Discussion:
Hello darkness, my old friend: https://docs.python.org/3/library/tempfile.html#tempfile.NamedTemporaryFile

> Whether the name can be used to open the file a second time, while the named temporary file is still open, varies across platforms (it can be so used on Unix; it cannot on Windows).

Resolving patch:
diff --git a/rotkehlchen/data_handler.py b/rotkehlchen/data_handler.py
--- a/rotkehlchen/data_handler.py
+++ b/rotkehlchen/data_handler.py
@@ -193,12 +193,13 @@ def compress_and_encrypt_db(self, password: str) -> Tuple[B64EncodedBytes, str]:
         Returns a b64 encoded binary blob"""
         log.info('Compress and encrypt DB')
         compressor = zlib.compressobj(level=9)
-        with tempfile.NamedTemporaryFile(delete=True, suffix='.db') as tempdbfile:
-            tempdb = Path(tempdbfile.name)
-            self.db.export_unencrypted(tempdb)
+        with tempfile.NamedTemporaryFile(delete=False, suffix='.db') as tempdbfile:
+            tempdbpath = Path(tempdbfile.name)
+            tempdbfile.close()  # close the file to allow re-opening by export_unencrypted in windows https://github.com/rotki/rotki/issues/5051  # noqa: E501
+            self.db.export_unencrypted(tempdbpath)
             source_data = bytearray()
             compressed_data = bytearray()
-            with open(tempdb, 'rb') as src_f:
+            with open(tempdbpath, 'rb') as src_f:
                 block = src_f.read(BUFFERSIZE)
                 while block:
                     source_data += block
@@ -211,6 +212,8 @@ def compress_and_encrypt_db(self, password: str) -> Tuple[B64EncodedBytes, str]:
             hashlib.sha256(source_data).digest(),
         ).decode()
         encrypted_data = encrypt(password.encode(), bytes(compressed_data))
+        # cleanup temp file to avoid windows problem (https://github.com/rotki/rotki/issues/5051)
+        tempdbpath.unlink()
         return B64EncodedBytes(encrypted_data.encode()), original_data_hash
 
     def decompress_and_decrypt_db(self, password: str, encrypted_data: B64EncodedString) -> None:


Test patch: