New column for encrypted data in preparation for full text search

This commit is contained in:
Magnus Åhall 2023-07-19 08:52:32 +02:00
parent b7c5a91c06
commit 57180e986e
3 changed files with 56 additions and 19 deletions

View File

@ -22,7 +22,7 @@ import (
const VERSION = "v8"; const VERSION = "v8";
const LISTEN_HOST = "0.0.0.0"; const LISTEN_HOST = "0.0.0.0";
const DB_SCHEMA = 10 const DB_SCHEMA = 11
var ( var (
flagPort int flagPort int

68
node.go
View File

@ -21,6 +21,8 @@ type Node struct {
Files []File Files []File
Complete bool Complete bool
Level int Level int
ContentEncrypted string `db:"content_encrypted" json:"-"`
} }
func (session Session) NodeTree(startNodeID int) (nodes []Node, err error) {// {{{ func (session Session) NodeTree(startNodeID int) (nodes []Node, err error) {// {{{
@ -142,6 +144,7 @@ func (session Session) Node(nodeID int) (node Node, err error) {// {{{
COALESCE(crypto_key_id, 0) AS crypto_key_id, COALESCE(crypto_key_id, 0) AS crypto_key_id,
name, name,
content, content,
content_encrypted,
0 AS level 0 AS level
FROM node FROM node
WHERE WHERE
@ -157,6 +160,7 @@ func (session Session) Node(nodeID int) (node Node, err error) {// {{{
COALESCE(n.crypto_key_id, 0) AS crypto_key_id, COALESCE(n.crypto_key_id, 0) AS crypto_key_id,
n.name, n.name,
'' AS content, '' AS content,
'' AS content_encrypted,
r.level + 1 AS level r.level + 1 AS level
FROM node n FROM node n
INNER JOIN recurse r ON n.parent_id = r.id AND r.level = 0 INNER JOIN recurse r ON n.parent_id = r.id AND r.level = 0
@ -193,8 +197,13 @@ func (session Session) Node(nodeID int) (node Node, err error) {// {{{
node.ParentID = row.ParentID node.ParentID = row.ParentID
node.CryptoKeyID = row.CryptoKeyID node.CryptoKeyID = row.CryptoKeyID
node.Name = row.Name node.Name = row.Name
node.Content = row.Content
node.Complete = true node.Complete = true
if node.CryptoKeyID > 0 {
node.Content = row.ContentEncrypted
} else {
node.Content = row.Content
}
} }
if row.Level == 1 { if row.Level == 1 {
@ -287,23 +296,46 @@ func (session Session) CreateNode(parentID int, name string) (node Node, err err
return return
}// }}} }// }}}
func (session Session) UpdateNode(nodeID int, content string, cryptoKeyID int) (err error) {// {{{ func (session Session) UpdateNode(nodeID int, content string, cryptoKeyID int) (err error) {// {{{
_, err = db.Exec(` if cryptoKeyID > 0 {
UPDATE node _, err = db.Exec(`
SET UPDATE node
content = $1, SET
crypto_key_id = CASE $2::int content = '',
WHEN 0 THEN NULL content_encrypted = $1,
ELSE $2 crypto_key_id = CASE $2::int
END WHEN 0 THEN NULL
WHERE ELSE $2
id = $3 AND END
user_id = $4 WHERE
`, id = $3 AND
content, user_id = $4
cryptoKeyID, `,
nodeID, content,
session.UserID, cryptoKeyID,
) nodeID,
session.UserID,
)
} else {
_, err = db.Exec(`
UPDATE node
SET
content = $1,
content_encrypted = '',
crypto_key_id = CASE $2::int
WHEN 0 THEN NULL
ELSE $2
END
WHERE
id = $3 AND
user_id = $4
`,
content,
cryptoKeyID,
nodeID,
session.UserID,
)
}
return return
}// }}} }// }}}
func (session Session) RenameNode(nodeID int, name string) (err error) {// {{{ func (session Session) RenameNode(nodeID int, name string) (err error) {// {{{

5
sql/0011.sql Normal file
View File

@ -0,0 +1,5 @@
ALTER TABLE node ADD COLUMN content_encrypted TEXT NOT NULL DEFAULT '';
UPDATE node SET content_encrypted = content, content = '' WHERE crypto_key_id IS NOT NULL;
CREATE EXTENSION pg_trgm;
CREATE INDEX node_content_index ON node USING gin (content gin_trgm_ops);