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 LISTEN_HOST = "0.0.0.0";
const DB_SCHEMA = 10
const DB_SCHEMA = 11
var (
flagPort int

68
node.go
View File

@ -21,6 +21,8 @@ type Node struct {
Files []File
Complete bool
Level int
ContentEncrypted string `db:"content_encrypted" json:"-"`
}
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,
name,
content,
content_encrypted,
0 AS level
FROM node
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,
n.name,
'' AS content,
'' AS content_encrypted,
r.level + 1 AS level
FROM node n
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.CryptoKeyID = row.CryptoKeyID
node.Name = row.Name
node.Content = row.Content
node.Complete = true
if node.CryptoKeyID > 0 {
node.Content = row.ContentEncrypted
} else {
node.Content = row.Content
}
}
if row.Level == 1 {
@ -287,23 +296,46 @@ func (session Session) CreateNode(parentID int, name string) (node Node, err err
return
}// }}}
func (session Session) UpdateNode(nodeID int, content string, cryptoKeyID int) (err error) {// {{{
_, err = db.Exec(`
UPDATE node
SET
content = $1,
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,
)
if cryptoKeyID > 0 {
_, err = db.Exec(`
UPDATE node
SET
content = '',
content_encrypted = $1,
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,
)
} 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
}// }}}
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);