117 lines
4.7 KiB
Go
117 lines
4.7 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"math"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// All native Path writers acquire the same namespace lock as metadata saves
|
|
// and governance changes, before reading the authorized snapshot. Do not hold
|
|
// this transaction while doing payload I/O.
|
|
func (s *Store) beginPathMutation(domain string) (*sql.Tx, context.CancelFunc, error) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
if s.ReadOnly {
|
|
cancel()
|
|
return nil, func() {}, ErrReadOnly
|
|
}
|
|
tx, err := s.DB.BeginTx(ctx, nil)
|
|
if err == nil && s.sqlBackend == storePostgreSQL {
|
|
_, err = tx.ExecContext(ctx, `SELECT pg_advisory_xact_lock(1885431653,1853058931)`)
|
|
if err == nil {
|
|
var local bool
|
|
err = tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM domains d JOIN campuses c ON c.uuid=d.campus_uuid WHERE d.uuid=$1 AND d.enabled=1 AND c.enabled=1 AND d.placement='local')`, domain).Scan(&local)
|
|
if err == nil && !local {
|
|
err = ErrPageNotFound
|
|
}
|
|
}
|
|
}
|
|
if err != nil {
|
|
if tx != nil {
|
|
tx.Rollback()
|
|
}
|
|
cancel()
|
|
return nil, func() {}, s.sqlError("path mutation begin", err)
|
|
}
|
|
return tx, cancel, nil
|
|
}
|
|
|
|
func (s *Store) cloneLatestRevisionFilenames(tx *sql.Tx, domain, object, oldName, newName string) error {
|
|
if s.sqlBackend == storeSQLite {
|
|
return cloneLatestRevisionFilenames(tx, object, oldName, newName)
|
|
}
|
|
rows, err := tx.Query(`SELECT uuid,document_hash,lang,content_type,filename,created_at_ms FROM (
|
|
SELECT uuid,document_hash,lang,content_type,filename,created_at_ms,
|
|
ROW_NUMBER() OVER(PARTITION BY lang ORDER BY created_at_ms DESC,uuid DESC) AS rn
|
|
FROM revisions WHERE object_uuid=$1) heads WHERE rn=1 ORDER BY lang`, object)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
type head struct {
|
|
id, hash, lang, contentType, filename string
|
|
ms int64
|
|
}
|
|
var heads []head
|
|
for rows.Next() {
|
|
var h head
|
|
if err := rows.Scan(&h.id, &h.hash, &h.lang, &h.contentType, &h.filename, &h.ms); err != nil {
|
|
rows.Close()
|
|
return err
|
|
}
|
|
if h.filename == "" || h.filename == oldName {
|
|
heads = append(heads, h)
|
|
}
|
|
}
|
|
err = rows.Err()
|
|
rows.Close()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, h := range heads {
|
|
if h.ms == math.MaxInt64 {
|
|
return ErrRevisionConflict
|
|
}
|
|
ms := time.Now().UnixMilli()
|
|
if ms <= h.ms {
|
|
ms = h.ms + 1
|
|
}
|
|
id := uuid.NewString()
|
|
if _, err := tx.Exec(`INSERT INTO revisions(uuid,object_uuid,body,document_hash,lang,content_type,filename,message,created_at_ms,created_at) VALUES($1,$2,'',$3,$4,$5,$6,$7,$8,$9)`, id, object, h.hash, h.lang, h.contentType, newName, "rename filename: "+oldName+" -> "+newName, ms, ms/1000); err != nil {
|
|
return err
|
|
}
|
|
if _, err := tx.Exec(`INSERT INTO revision_document_refs(revision_uuid,document_hash) SELECT $1,document_hash FROM revision_document_refs WHERE revision_uuid=$2`, id, h.id); err != nil {
|
|
return err
|
|
}
|
|
payload, _ := json.Marshal(map[string]string{"revision_uuid": id, "object_uuid": object, "document_hash": h.hash, "content_type": h.contentType, "filename": newName})
|
|
if err := s.appendActivityOutboxIfAvailable(tx, ActivityOutboxEvent{DomainUUID: domain, EventType: "revision.created", TargetKind: "revision", TargetUUID: id, Payload: string(payload), IdempotencyKey: "revision.created:" + id}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Store) copyNativeRevisionsTx(tx *sql.Tx, domain, source, destination, path string) error {
|
|
if _, err := tx.Exec(`INSERT INTO object_origins(object_uuid,domain_uuid,created_at) VALUES($1,$2,$3)`, destination, domain, time.Now().Unix()); err != nil {
|
|
return err
|
|
}
|
|
// One materialized UUID map is shared by both inserts. Do not copy bodies
|
|
// or re-resolve mutable Asset names; copy the immutable reference graph.
|
|
_, err := tx.Exec(`WITH mapping AS MATERIALIZED (
|
|
SELECT uuid AS old_id,gen_random_uuid()::text AS new_id FROM revisions WHERE object_uuid=$1
|
|
), copied AS (
|
|
INSERT INTO revisions(uuid,object_uuid,body,document_hash,lang,content_type,filename,message,created_at_ms,created_at)
|
|
SELECT m.new_id,$2,'',r.document_hash,r.lang,r.content_type,r.filename,r.message,r.created_at_ms,r.created_at
|
|
FROM revisions r JOIN mapping m ON m.old_id=r.uuid RETURNING uuid
|
|
) INSERT INTO revision_document_refs(revision_uuid,document_hash)
|
|
SELECT m.new_id,ref.document_hash FROM revision_document_refs ref JOIN mapping m ON m.old_id=ref.revision_uuid JOIN copied c ON c.uuid=m.new_id`, source, destination)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
payload, _ := json.Marshal(map[string]string{"path": path, "object_uuid": destination})
|
|
return s.appendActivityOutboxIfAvailable(tx, ActivityOutboxEvent{DomainUUID: domain, EventType: "page.created", TargetKind: "page", TargetUUID: destination, Payload: string(payload), IdempotencyKey: "page.created:" + destination})
|
|
}
|