{"record":{"id":"1f0416f9b535279f","repo":"siyuan-note/siyuan","slug":"read-encrypted-index-setting-s-w","errorCode":null,"errorMessage":"read encrypted index setting %s: %w","messagePattern":"read encrypted index setting (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/util/encrypted_index.go","lineNumber":21,"sourceCode":"// SPDX-License-Identifier: AGPL-3.0-or-later\n\npackage util\n\nimport (\n\t\"database/sql\"\n\t\"encoding/json\"\n\t\"errors\"\n\t\"fmt\"\n)\n\n// CheckEncryptedIndexCompatibility 将索引版本保存在受 SQLCipher 认证的表中，拒绝复用缺少版本或参数不匹配的旧索引。\n// schema 由各索引维护，修改表结构时递增；索引重建由已认证源文档的调用方负责。\nfunc CheckEncryptedIndexCompatibility(db *sql.DB, kind string, schema int) error {\n\tsettings := map[string]string{}\n\tfor _, name := range []string{\"cipher_version\", \"cipher_page_size\", \"kdf_iter\", \"cipher_hmac_algorithm\", \"cipher_kdf_algorithm\", \"cipher_use_hmac\"} {\n\t\tvar value string\n\t\tif err := db.QueryRow(\"PRAGMA \" + name).Scan(&value); err != nil {\n\t\t\treturn fmt.Errorf(\"read encrypted index setting %s: %w\", name, err)\n\t\t}\n\t\tif value == \"\" {\n\t\t\treturn fmt.Errorf(\"missing encrypted index setting %s\", name)\n\t\t}\n\t\tsettings[name] = value\n\t}\n\tencoded, err := json.Marshal(settings)\n\tif err != nil {\n\t\treturn err\n\t}\n\tvar metadataTables int\n\tif err = db.QueryRow(\"SELECT count(*) FROM sqlite_master WHERE type = 'table' AND name = 'encrypted_index_meta'\").Scan(&metadataTables); err != nil {\n\t\treturn err\n\t}\n\tif metadataTables == 0 {\n\t\tvar tables int\n\t\tif err = db.QueryRow(\"SELECT count(*) FROM sqlite_master WHERE type = 'table'\").Scan(&tables); err != nil {\n\t\t\treturn err","sourceCodeStart":3,"sourceCodeEnd":39,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/util/encrypted_index.go#L3-L39","documentation":"CheckEncryptedIndexCompatibility verifies that an encrypted SQLite index (SQLCipher-backed) exposes its cipher PRAGMA settings so the kernel can compare them against recorded compatibility metadata. This error wraps the underlying driver failure when reading one of the required PRAGMA values (cipher_version, cipher_page_size, kdf_iter, etc.). It means the connection could not answer the cipher introspection query, typically because the database is not opened with the SQLCipher driver or the key is wrong.","triggerScenarios":"Calling OpenEncryptedDB or OpenEncryptedBlockTreeDB with a database not actually encrypted with SQLCipher (the plain SQLite driver fails on PRAGMA cipher_*), or with an incorrect/wrong-format key so the cipher page cannot be read.","commonSituations":"Mixing plain mattn/go-sqlite3 with encrypted notebooks; corrupted database file after an interrupted write; opening a database created by a different SQLCipher version whose PRAGMAs are unsupported; wrong passphrase supplied at open time.","solutions":["Verify the database is opened via OpenEncryptedDB/OpenEncryptedBlockTreeDB with the correct SQLCipher key, not a plain sqlite handle","Check kernel logs for the wrapped driver error to distinguish wrong key vs wrong driver","Confirm the database file is not corrupted (restore from sync/history rather than repairing by hand)","Ensure the bundled SQLCipher fork (go-sqlite3 replace directive) is intact and the binary was rebuilt after dependency changes"],"exampleFix":"// before\ndb, err := sql.Open(\"sqlite3\", path)\n// after\ndb, err := util.OpenEncryptedDB(path, key)","handlingStrategy":"try-catch","validationCode":"if db == nil { return errors.New(\"db not initialized\") }\n// ensure the open path used the SQLCipher driver with a key before compatibility checks","typeGuard":null,"tryCatchPattern":"if err := util.CheckEncryptedIndexCompatibility(db, kind, schema); err != nil {\n    log.Printf(\"encrypted index unusable, rebuilding from source docs: %v\", err)\n    // fall back to the authenticated rebuild path\n}","preventionTips":["Always open encrypted indexes through OpenEncryptedDB/OpenEncryptedBlockTreeDB","Keep the bundled SQLCipher fork and its replace directive intact","Never open the same workspace DB with two kernel processes","Restore corrupted indexes from sync/history instead of repairing by hand"],"tags":["database","encryption","sqlcipher","pragma"],"backgroundTag":"database-query-failed","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}