Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

test: add 11 new SQLCipher tests aligning with official test suite

LearningCircuit50232b563be0
diff --git a/tests/database/test_sqlcipher_integration.py b/tests/database/test_sqlcipher_integration.py
index 2fdb335f1e..c67aab27c6 100644
--- a/tests/database/test_sqlcipher_integration.py
+++ b/tests/database/test_sqlcipher_integration.py
@@ -2396,3 +2396,706 @@ class TestSQLCipherIntegration:
), f"Expected meaningful error, got: {e}"
finally:
conn.close()
+
+ # =========================================================================
+ # ADDITIONAL TESTS: Gap Coverage (Official SQLCipher Test Suite Alignment)
+ # =========================================================================
+
+ def test_multiple_key_pragma_calls_safe(self, tmp_path):
+ """Verify setting wrong key then correct key works (official test: multiple-key-calls-safe).
+
+ This test ensures that if a user attempts to open with the wrong key and then
+ provides the correct key, the database will authenticate correctly without
+ corruption. This is important for applications that may retry key entry.
+ """
+ from local_deep_research.database.sqlcipher_compat import (
+ get_sqlcipher_module,
+ )
+
+ pysqlcipher3 = get_sqlcipher_module()
+ db_path = tmp_path / "multi_key_test.db"
+ correct_password = "correct_password_123"
+ wrong_password = "wrong_password_456"
+
+ # Create encrypted database
+ conn = pysqlcipher3.connect(str(db_path))
+ cursor = conn.cursor()
+ cursor.execute(f"PRAGMA key = '{correct_password}'")
+ cursor.execute("CREATE TABLE test (id INTEGER, value TEXT)")
+ cursor.execute("INSERT INTO test VALUES (1, 'secret_data')")
+ conn.commit()
+ conn.close()
+
+ # Reopen - first with wrong key (should fail silently until first op)
+ conn = pysqlcipher3.connect(str(db_path))
+ cursor = conn.cursor()
+ cursor.execute(f"PRAGMA key = '{wrong_password}'")
+
+ # Try an operation - this should fail
+ try:
+ cursor.execute("SELECT * FROM test")
+ cursor.fetchall()
+ wrong_key_succeeded = True
+ except Exception:
+ wrong_key_succeeded = False
+
+ assert not wrong_key_succeeded, "Operation with wrong key should fail"
+ conn.close()
+
+ # Now set the correct key on a fresh connection
+ conn = pysqlcipher3.connect(str(db_path))
+ cursor = conn.cursor()
+ cursor.execute(f"PRAGMA key = '{correct_password}'")
+
+ # This should now work
+ cursor.execute("SELECT value FROM test WHERE id = 1")
+ result = cursor.fetchone()
+ assert result is not None, "Should be able to read with correct key"
+ assert result[0] == "secret_data", "Data should be intact"
+ conn.close()
+
+ def test_rekey_in_wal_mode(self, tmp_path):
+ """Verify rekey works correctly with WAL journal mode.
+
+ WAL mode + rekey is a known problem area in SQLCipher. This test ensures
+ that rekeying a database operating in WAL mode works correctly and that
+ WAL files (if present) are properly re-encrypted.
+ """
+ from local_deep_research.database.sqlcipher_compat import (
+ get_sqlcipher_module,
+ )
+
+ pysqlcipher3 = get_sqlcipher_module()
+ db_path = tmp_path / "wal_rekey_test.db"
+ original_password = "original_pass_123"
+ new_password = "new_pass_456"
+
+ # Create database with WAL mode
+ conn = pysqlcipher3.connect(str(db_path))
+ cursor = conn.cursor()
+ cursor.execute(f"PRAGMA key = '{original_password}'")
+ cursor.execute("PRAGMA journal_mode = WAL")
+
+ # Verify WAL mode is active
+ cursor.execute("PRAGMA journal_mode")
+ mode = cursor.fetchone()[0]
+ if mode.lower() != "wal":
+ conn.close()
+ pytest.skip("WAL mode not available in this configuration")
+
+ # Create table and insert data
+ cursor.execute("CREATE TABLE wal_test (id INTEGER, value TEXT)")
+ cursor.execute(
+ "INSERT INTO wal_test VALUES (1, 'wal_data_before_rekey')"
+ )
+ cursor.execute("INSERT INTO wal_test VALUES (2, 'more_wal_data')")
+ conn.commit()
+
+ # Perform rekey while in WAL mode
+ cursor.execute(f"PRAGMA rekey = '{new_password}'")
+ conn.commit()
+ conn.close()
+
+ # Verify WAL file is properly handled (may or may not exist)
+ wal_path = tmp_path / "wal_rekey_test.db-wal"
+
+ # If WAL file exists, verify it doesn't contain plaintext data
+ if wal_path.exists():
+ with open(wal_path, "rb") as f:
+ wal_content = f.read()
+ assert b"wal_data_before_rekey" not in wal_content, (
+ "WAL file should not contain plaintext data"
+ )
+
+ # Reopen with new password and verify data
+ conn = pysqlcipher3.connect(str(db_path))
+ cursor = conn.cursor()
+ cursor.execute(f"PRAGMA key = '{new_password}'")
+
+ cursor.execute("SELECT COUNT(*) FROM wal_test")
+ count = cursor.fetchone()[0]
+ assert count == 2, f"Expected 2 rows, got {count}"
+
+ cursor.execute("SELECT value FROM wal_test WHERE id = 1")
+ result = cursor.fetchone()
+ assert result[0] == "wal_data_before_rekey", (
+ "Data should persist after rekey"
+ )
+ conn.close()
+
+ # Verify old password no longer works
+ conn = pysqlcipher3.connect(str(db_path))
+ cursor = conn.cursor()
+ cursor.execute(f"PRAGMA key = '{original_password}'")
+ try:
+ cursor.execute("SELECT * FROM wal_test")
+ pytest.fail("Old password should no longer work")
+ except Exception as e:
+ assert "not a database" in str(e).lower()
+ finally:
+ conn.close()
+
+ def test_kdf_iterations_mismatch_fails(self, tmp_path):
+ """Verify mismatched KDF iterations causes 'file is not a database'.
+
+ This is a common migration failure cause. When a database is created with
+ specific KDF iteration count, it must be opened with the same count.
+ """
+ from local_deep_research.database.sqlcipher_compat import (
+ get_sqlcipher_module,
+ )
+
+ pysqlcipher3 = get_sqlcipher_module()
+ db_path = tmp_path / "kdf_mismatch_test.db"
+ password = "test_password_123"
+
+ # Create database with high iteration count (SQLCipher 4 default: 256000)
+ conn = pysqlcipher3.connect(str(db_path))
+ cursor = conn.cursor()
+ cursor.execute(f"PRAGMA key = '{password}'")
+ # Uses the default high iteration count (256000 for SQLCipher 4)
+ cursor.execute("CREATE TABLE test (id INTEGER)")
+ cursor.execute("INSERT INTO test VALUES (1)")
+ conn.commit()
+ conn.close()
+
+ # Try to open with different (lower) iteration count
+ conn = pysqlcipher3.connect(str(db_path))
+ cursor = conn.cursor()
+ # Set a different KDF iteration count before key
+ cursor.execute("PRAGMA cipher_default_kdf_iter = 1000")
+ cursor.execute(f"PRAGMA key = '{password}'")
+
+ # This should fail because KDF iterations don't match
+ try:
+ cursor.execute("SELECT * FROM test")
+ cursor.fetchall()
+ # If it succeeds, check if kdf_iter was ignored
+ cursor.execute("PRAGMA kdf_iter")
+ actual_kdf = cursor.fetchone()[0]
+ if int(actual_kdf) != 1000:
+ # cipher_default_kdf_iter was likely ignored, which is acceptable
+ conn.close()
+ pytest.skip(
+ "cipher_default_kdf_iter not supported in this version"
+ )
+ pytest.fail("Should fail with mismatched KDF iterations")
+ except Exception as e:
+ error_msg = str(e).lower()
+ assert "not a database" in error_msg or "error" in error_msg, (
+ f"Expected 'file is not a database' error, got: {e}"
+ )
+ finally:
+ conn.close()
+
+ def test_rekey_on_empty_database(self, tmp_path):
+ """Verify rekey behavior on brand new empty database.
+
+ Official test case: rekey-as-first-op-on-empty. Tests what happens when
+ rekey is called on a database that has been keyed but has no tables.
+ """
+ from local_deep_research.database.sqlcipher_compat import (
+ get_sqlcipher_module,
+ )
+
+ pysqlcipher3 = get_sqlcipher_module()
+ db_path = tmp_path / "empty_rekey_test.db"
+ original_password = "original_pass"
+ new_password = "new_pass"
+
+ # Create new database, set key but don't create any tables
+ conn = pysqlcipher3.connect(str(db_path))
+ cursor = conn.cursor()
+ cursor.execute(f"PRAGMA key = '{original_password}'")
+
+ # Immediately try to rekey (no tables created yet)
+ # SQLCipher should handle this gracefully
+ try:
+ cursor.execute(f"PRAGMA rekey = '{new_password}'")
+ conn.commit()
+ rekey_succeeded = True
+ except Exception:
+ rekey_succeeded = False
+
+ conn.close()
+
+ if rekey_succeeded:
+ # If rekey succeeded, verify new password works
+ conn = pysqlcipher3.connect(str(db_path))
+ cursor = conn.cursor()
+ cursor.execute(f"PRAGMA key = '{new_password}'")
+
+ # Should be able to create tables with new key
+ cursor.execute("CREATE TABLE test (id INTEGER)")
+ cursor.execute("INSERT INTO test VALUES (1)")
+ conn.commit()
+
+ cursor.execute("SELECT * FROM test")
+ result = cursor.fetchall()
+ assert len(result) == 1, (
+ "Should be able to use rekeyed empty database"
+ )
+ conn.close()
+ else:
+ # If rekey failed, verify original password still works
+ conn = pysqlcipher3.connect(str(db_path))
+ cursor = conn.cursor()
+ cursor.execute(f"PRAGMA key = '{original_password}'")
+
+ # Should still be usable
+ cursor.execute("CREATE TABLE test (id INTEGER)")
+ cursor.execute("INSERT INTO test VALUES (1)")
+ conn.commit()
+ conn.close()
+
+ def test_rekey_with_same_password(self, tmp_path):
+ """Verify rekeying with identical password doesn't corrupt database.
+
+ Edge case: what happens when you rekey a database with the same password?
+ This should not corrupt the database.
+ """
+ from local_deep_research.database.sqlcipher_compat import (
+ get_sqlcipher_module,
+ )
+
+ pysqlcipher3 = get_sqlcipher_module()
+ db_path = tmp_path / "same_pass_rekey_test.db"
+ password = "same_password_123"
+
+ # Create database with data
+ conn = pysqlcipher3.connect(str(db_path))
+ cursor = conn.cursor()
+ cursor.execute(f"PRAGMA key = '{password}'")
+ cursor.execute("CREATE TABLE test (id INTEGER, value TEXT)")
+ cursor.execute("INSERT INTO test VALUES (1, 'original_data')")
+ cursor.execute("INSERT INTO test VALUES (2, 'more_data')")
+ conn.commit()
+
+ # Rekey with the SAME password
+ cursor.execute(f"PRAGMA rekey = '{password}'")
+ conn.commit()
+ conn.close()
+
+ # Verify database is not corrupted
+ conn = pysqlcipher3.connect(str(db_path))
+ cursor = conn.cursor()
+ cursor.execute(f"PRAGMA key = '{password}'")
+
+ cursor.execute("SELECT COUNT(*) FROM test")
+ count = cursor.fetchone()[0]
+ assert count == 2, (
+ f"Expected 2 rows after same-password rekey, got {count}"
+ )
+
+ cursor.execute("SELECT value FROM test WHERE id = 1")
+ result = cursor.fetchone()
+ assert result[0] == "original_data", "Data should be intact"
+
+ # Run integrity check
+ cursor.execute("PRAGMA integrity_check")
+ integrity = cursor.fetchone()[0]
+ assert integrity.lower() == "ok", f"Integrity check failed: {integrity}"
+ conn.close()
+
+ def test_attach_database_inherits_default_key(self, tmp_path):
+ """Verify ATTACH without KEY uses main database key.
+
+ Official test: attach-database-with-default-key. When attaching a database
+ without specifying a KEY parameter, it should use the main database's key.
+ """
+ from local_deep_research.database.sqlcipher_compat import (
+ get_sqlcipher_module,
+ )
+
+ pysqlcipher3 = get_sqlcipher_module()
+ shared_password = "shared_key_123"
+
+ # Create main database
+ main_db_path = tmp_path / "main_default_key.db"
+ conn = pysqlcipher3.connect(str(main_db_path))
+ cursor = conn.cursor()
+ cursor.execute(f"PRAGMA key = '{shared_password}'")
+ cursor.execute("CREATE TABLE main_data (id INTEGER, value TEXT)")
+ cursor.execute("INSERT INTO main_data VALUES (1, 'main_value')")
+ conn.commit()
+ conn.close()
+
+ # Create second database with the SAME password
+ second_db_path = tmp_path / "second_default_key.db"
+ conn = pysqlcipher3.connect(str(second_db_path))
+ cursor = conn.cursor()
+ cursor.execute(f"PRAGMA key = '{shared_password}'")
+ cursor.execute("CREATE TABLE second_data (id INTEGER, value TEXT)")
+ cursor.execute("INSERT INTO second_data VALUES (1, 'second_value')")
+ conn.commit()
+ conn.close()
+
+ # Open main and attach second WITHOUT specifying KEY
+ conn = pysqlcipher3.connect(str(main_db_path))
+ cursor = conn.cursor()
+ cursor.execute(f"PRAGMA key = '{shared_password}'")
+
+ # Attach without KEY parameter - should use main database key
+ # Note: This behavior may vary by SQLCipher version
+ try:
+ cursor.execute(f"ATTACH DATABASE '{second_db_path}' AS second")
+
+ # Try to access the attached database
+ cursor.execute("SELECT value FROM second.second_data WHERE id = 1")
+ result = cursor.fetchone()
+ assert result[0] == "second_value", (
+ "Should access attached DB with inherited key"
+ )
+
+ # Verify main database still works
+ cursor.execute("SELECT value FROM main_data WHERE id = 1")
+ result = cursor.fetchone()
+ assert result[0] == "main_value", "Main DB should still work"
+
+ except Exception as e:
+ error_msg = str(e).lower()
+ # If ATTACH without key doesn't inherit key, it may fail
+ # This is acceptable - we're documenting the behavior
+ if "not a database" in error_msg or "unable to open" in error_msg:
+ pytest.skip(
+ "This SQLCipher version requires explicit KEY for ATTACH"
+ )
+ raise
+ finally:
+ conn.close()
+
+ def test_schema_alteration_persists(self, isolated_db_manager):
+ """Verify ALTER TABLE changes persist through database reopen.
+
+ Official test: alter-schema. Tests that schema modifications like
+ ALTER TABLE ADD COLUMN are properly persisted in encrypted databases.
+ """
+ username = "testuser"
+ password = "SecurePassword123!"
+
+ isolated_db_manager.create_user_database(username, password)
+
+ from sqlalchemy import text
+
+ engine = isolated_db_manager.connections[username]
+
+ # Create initial table
+ with engine.connect() as conn:
+ conn.execute(
+ text("CREATE TABLE alter_test (id INTEGER PRIMARY KEY)")
+ )
+ conn.execute(text("INSERT INTO alter_test VALUES (1)"))
+ conn.commit()
+
+ # Alter table - add new column
+ with engine.connect() as conn:
+ conn.execute(text("ALTER TABLE alter_test ADD COLUMN new_col TEXT"))
+ conn.execute(
+ text(
+ "UPDATE alter_test SET new_col = 'added_value' WHERE id = 1"
+ )
+ )
+ conn.commit()
+
+ # Verify change before close
+ with engine.connect() as conn:
+ result = conn.execute(
+ text("SELECT new_col FROM alter_test WHERE id = 1")
+ )
+ assert result.fetchone()[0] == "added_value"
+
+ # Close and reopen database
+ isolated_db_manager.close_user_database(username)
+ engine = isolated_db_manager.open_user_database(username, password)
+ assert engine is not None, "Should reopen database"
+
+ # Verify schema change persisted
+ with engine.connect() as conn:
+ # Check column exists
+ result = conn.execute(text("PRAGMA table_info(alter_test)"))
+ columns = [row[1] for row in result.fetchall()]
+ assert "new_col" in columns, "Added column should persist"
+
+ # Check data persisted
+ result = conn.execute(
+ text("SELECT new_col FROM alter_test WHERE id = 1")
+ )
+ assert result.fetchone()[0] == "added_value", (
+ "Data in new column should persist"
+ )
+
+ def test_cipher_default_compatibility_pragma(self, tmp_path):
+ """Verify cipher_default_compatibility sets process-wide default.
+
+ Tests that cipher_default_compatibility pragma works for setting
+ a process-wide default compatibility mode for all new connections.
+ """
+ from local_deep_research.database.sqlcipher_compat import (
+ get_sqlcipher_module,
+ )
+
+ pysqlcipher3 = get_sqlcipher_module()
+ db_path = tmp_path / "default_compat_test.db"
+
+ # Create in-memory connection to set default compatibility
+ setup_conn = pysqlcipher3.connect(":memory:")
+ setup_cursor = setup_conn.cursor()
+
+ try:
+ # Set process-wide default compatibility to SQLCipher 3
+ setup_cursor.execute("PRAGMA cipher_default_compatibility = 3")
+ except Exception as e:
+ setup_conn.close()
+ if "error" in str(e).lower() or "not an error" in str(e).lower():
+ pytest.skip("cipher_default_compatibility not supported")
+ raise
+
+ setup_conn.close()
+
+ # Now create a new database - it should use SQLCipher 3 settings
+ conn = pysqlcipher3.connect(str(db_path))
+ cursor = conn.cursor()
+ cursor.execute("PRAGMA key = 'test_password'")
+
+ # Verify compatibility settings are applied (query should not error)
+ # Note: cipher_default_compatibility may not persist to individual DBs
+ # The important thing is the pragma doesn't error
+ try:
+ cursor.execute("PRAGMA cipher_compatibility")
+ cursor.fetchone() # Just verify the query succeeds
+ except Exception:
+ # Some versions may not support querying compatibility
+ pass
+
+ # Create and verify database works
+ cursor.execute("CREATE TABLE test (id INTEGER)")
+ cursor.execute("INSERT INTO test VALUES (1)")
+ conn.commit()
+
+ cursor.execute("SELECT * FROM test")
+ result = cursor.fetchall()
+ assert len(result) == 1, (
+ "Database should work after setting default compat"
+ )
+ conn.close()
+
+ # Reset to default (SQLCipher 4 compatibility)
+ reset_conn = pysqlcipher3.connect(":memory:")
+ reset_cursor = reset_conn.cursor()
+ try:
+ reset_cursor.execute("PRAGMA cipher_default_compatibility = 4")
+ except Exception:
+ pass # May not be supported
+ reset_conn.close()
+
+ def test_uri_key_parameter(self, tmp_path):
+ """Verify encryption key can be provided via URI parameter.
+
+ Tests opening a database with the key specified in the URI
+ connection string rather than via PRAGMA key.
+ """
+ from local_deep_research.database.sqlcipher_compat import (
+ get_sqlcipher_module,
+ )
+
+ pysqlcipher3 = get_sqlcipher_module()
+ db_path = tmp_path / "uri_key_test.db"
+ password = "uri_test_password"
+
+ # First, create the database traditionally
+ conn = pysqlcipher3.connect(str(db_path))
+ cursor = conn.cursor()
+ cursor.execute(f"PRAGMA key = '{password}'")
+ cursor.execute("CREATE TABLE test (id INTEGER, value TEXT)")
+ cursor.execute("INSERT INTO test VALUES (1, 'uri_data')")
+ conn.commit()
+ conn.close()
+
+ # Now try to open using URI with key parameter
+ # URI format: file:path?key=password
+ uri = f"file:{db_path}?key={password}"
+
+ try:
+ # Try connecting with URI - requires uri=True parameter
+ conn = pysqlcipher3.connect(uri, uri=True)
+ cursor = conn.cursor()
+
+ # Key should already be set from URI
+ cursor.execute("SELECT value FROM test WHERE id = 1")
+ result = cursor.fetchone()
+ assert result[0] == "uri_data", "Should read data via URI key"
+ conn.close()
+
+ except TypeError:
+ # If uri parameter not supported, try without it
+ try:
+ conn = pysqlcipher3.connect(uri)
+ cursor = conn.cursor()
+ cursor.execute("SELECT value FROM test WHERE id = 1")
+ result = cursor.fetchone()
+ assert result[0] == "uri_data", "Should read data via URI key"
+ conn.close()
+ except Exception as e:
+ if (
+ "uri" in str(e).lower()
+ or "not a database" in str(e).lower()
+ ):
+ pytest.skip(
+ "URI key parameter not supported in this version"
+ )
+ raise
+
+ except Exception as e:
+ error_msg = str(e).lower()
+ if "not a database" in error_msg or "unable to open" in error_msg:
+ pytest.skip("URI key parameter not supported in this version")
+ raise
+
+ def test_memory_database_integrity_check_behavior(self, tmp_path):
+ """Verify cipher_integrity_check behavior on :memory: database.
+
+ Official test: memory-integrity-check-should-fail. Tests what happens
+ when cipher_integrity_check is called on an in-memory database
+ (which has no file to verify).
+ """
+ from local_deep_research.database.sqlcipher_compat import (
+ get_sqlcipher_module,
+ )
+
+ pysqlcipher3 = get_sqlcipher_module()
+
+ # Open :memory: database with key
+ conn = pysqlcipher3.connect(":memory:")
+ cursor = conn.cursor()
+ cursor.execute("PRAGMA key = 'test_password'")
+
+ # Create some data
+ cursor.execute("CREATE TABLE test (id INTEGER)")
+ cursor.execute("INSERT INTO test VALUES (1)")
+ conn.commit()
+
+ # Call cipher_integrity_check on memory database
+ try:
+ cursor.execute("PRAGMA cipher_integrity_check")
+ cursor.fetchone()
+ # Memory DB integrity check behavior varies by version
+ # It may return an error message, empty result, or skip
+ # Memory databases don't have files, so integrity check
+ # should either fail, return error, or return empty
+ # All of these behaviors are acceptable
+
+ except Exception as e:
+ # It's acceptable for cipher_integrity_check to raise an error
+ # on memory databases since there's no file to check
+ error_msg = str(e).lower()
+ # Verify it's a reasonable error about the memory database
+ assert (
+ "memory" in error_msg
+ or "undefined" in error_msg
+ or "error" in error_msg
+ or "not" in error_msg
+ ), f"Unexpected error for memory DB integrity check: {e}"
+
+ finally:
+ conn.close()
+
+ def test_rekey_attached_database_by_name(self, tmp_path):
+ """Verify rekey can target attached database by schema name.
+
+ Official test: rekey-database-by-name. Tests that PRAGMA schema.rekey
+ can be used to rekey an attached database without affecting the main database.
+ """
+ from local_deep_research.database.sqlcipher_compat import (
+ get_sqlcipher_module,
+ )
+
+ pysqlcipher3 = get_sqlcipher_module()
+
+ main_password = "main_pass_123"
+ attached_original_password = "attached_pass_123"
+ attached_new_password = "attached_new_pass_456"
+
+ # Create main database
+ main_db_path = tmp_path / "main_rekey_by_name.db"
+ conn = pysqlcipher3.connect(str(main_db_path))
+ cursor = conn.cursor()
+ cursor.execute(f"PRAGMA key = '{main_password}'")
+ cursor.execute("CREATE TABLE main_data (id INTEGER, value TEXT)")
+ cursor.execute("INSERT INTO main_data VALUES (1, 'main_value')")
+ conn.commit()
+ conn.close()
+
+ # Create attached database
+ attached_db_path = tmp_path / "attached_rekey_by_name.db"
+ conn = pysqlcipher3.connect(str(attached_db_path))
+ cursor = conn.cursor()
+ cursor.execute(f"PRAGMA key = '{attached_original_password}'")
+ cursor.execute("CREATE TABLE attached_data (id INTEGER, value TEXT)")
+ cursor.execute("INSERT INTO attached_data VALUES (1, 'attached_value')")
+ conn.commit()
+ conn.close()
+
+ # Open main and attach second database
+ conn = pysqlcipher3.connect(str(main_db_path))
+ cursor = conn.cursor()
+ cursor.execute(f"PRAGMA key = '{main_password}'")
+ cursor.execute(
+ f"ATTACH DATABASE '{attached_db_path}' AS other KEY '{attached_original_password}'"
+ )
+
+ # Verify both databases accessible
+ cursor.execute("SELECT value FROM main_data WHERE id = 1")
+ assert cursor.fetchone()[0] == "main_value"
+ cursor.execute("SELECT value FROM other.attached_data WHERE id = 1")
+ assert cursor.fetchone()[0] == "attached_value"
+
+ # Rekey ONLY the attached database using schema qualifier
+ try:
+ cursor.execute(f"PRAGMA other.rekey = '{attached_new_password}'")
+ rekey_worked = True
+ except Exception as e:
+ rekey_worked = False
+ rekey_error = str(e)
+ # Schema-qualified rekey may not be supported in all versions
+ if "error" in rekey_error.lower():
+ conn.close()
+ pytest.skip("Schema-qualified PRAGMA rekey not supported")
+ raise
+
+ conn.commit()
+ conn.close()
+
+ if rekey_worked:
+ # Verify main database still uses original password
+ conn = pysqlcipher3.connect(str(main_db_path))
+ cursor = conn.cursor()
+ cursor.execute(f"PRAGMA key = '{main_password}'")
+ cursor.execute("SELECT value FROM main_data WHERE id = 1")
+ assert cursor.fetchone()[0] == "main_value", (
+ "Main DB should still use original password"
+ )
+ conn.close()
+
+ # Verify attached database now uses new password
+ conn = pysqlcipher3.connect(str(attached_db_path))
+ cursor = conn.cursor()
+ cursor.execute(f"PRAGMA key = '{attached_new_password}'")
+ cursor.execute("SELECT value FROM attached_data WHERE id = 1")
+ assert cursor.fetchone()[0] == "attached_value", (
+ "Attached DB should use new password"
+ )
+ conn.close()
+
+ # Verify attached database's original password no longer works
+ conn = pysqlcipher3.connect(str(attached_db_path))
+ cursor = conn.cursor()
+ cursor.execute(f"PRAGMA key = '{attached_original_password}'")
+ try:
+ cursor.execute("SELECT * FROM attached_data")
+ pytest.fail(
+ "Original password should no longer work on attached DB"
+ )
+ except Exception as e:
+ assert "not a database" in str(e).lower()
+ finally:
+ conn.close()