feat(storage): add native PostgreSQL runtime and verified offline application import
Build and publish container images / images (push) Failing after 12m29s
Build and publish container images / images (push) Failing after 12m29s
This commit is contained in:
1 parent
96065d1e89
commit
0ba1dcfef0
243 files changed
+21441
-3090
No files matched your search
@@ -0,0 +1,156 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"pageswiki/internal/db"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
)
|
||||
|
||||
type storeSQLBackend uint8
|
||||
|
||||
const (
|
||||
storeSQLite storeSQLBackend = iota
|
||||
storePostgreSQL
|
||||
)
|
||||
|
||||
var ErrPostgreSQLComponentNotReady = errors.New("PostgreSQL component dependencies are not ported")
|
||||
|
||||
type namespaceValidationError string
|
||||
|
||||
func (e namespaceValidationError) Error() string { return string(e) }
|
||||
|
||||
// SQL placeholders are generated while composing owned queries. Do not rebind
|
||||
// arbitrary SQL strings or translate SQLite syntax at the driver boundary.
|
||||
func (s *Store) sqlParam(n int) string {
|
||||
if s.sqlBackend == storePostgreSQL {
|
||||
return fmt.Sprintf("$%d", n)
|
||||
}
|
||||
return "?"
|
||||
}
|
||||
func (s *Store) sqlValues(n int) string {
|
||||
parts := make([]string, n)
|
||||
for i := range parts {
|
||||
parts[i] = s.sqlParam(i + 1)
|
||||
}
|
||||
return strings.Join(parts, ",")
|
||||
}
|
||||
func (s *Store) sqlHasDot(expression string) string {
|
||||
if s.sqlBackend == storePostgreSQL {
|
||||
return "strpos(" + expression + ", '.') > 0"
|
||||
}
|
||||
return "instr(" + expression + ", '.') > 0"
|
||||
}
|
||||
|
||||
// Owned expressions only. C/BINARY ordering makes '%' and '_' literal and
|
||||
// keeps segment ranges case-sensitive, independent of database locale.
|
||||
func (s *Store) pathScopeSQL(parent, child string) string {
|
||||
if s.sqlBackend == storeSQLite {
|
||||
return cityPathScopeSQL(parent, child)
|
||||
}
|
||||
return `(` + parent + `!='' AND (` + child + ` COLLATE "C" = ` + parent + ` OR (` + child + ` COLLATE "C" >= rtrim(` + parent + `,'/')||'/' AND ` + child + ` COLLATE "C" < rtrim(` + parent + `,'/')||'0')))`
|
||||
}
|
||||
|
||||
func (s *Store) pathOverlapSQL(left, right string) string {
|
||||
return `(` + s.pathScopeSQL(left, right) + ` OR ` + s.pathScopeSQL(right, left) + `)`
|
||||
}
|
||||
|
||||
// Both operands are owned SQL expressions/literals, never request input.
|
||||
func (s *Store) sqlStringPosition(expression, literal string) string {
|
||||
if s.sqlBackend == storePostgreSQL {
|
||||
return "strpos(" + expression + ", '" + literal + "')"
|
||||
}
|
||||
return "instr(" + expression + ", '" + literal + "')"
|
||||
}
|
||||
|
||||
func (s *Store) sqlError(operation string, err error) error {
|
||||
if s.sqlBackend != storePostgreSQL || err == nil {
|
||||
return err
|
||||
}
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return sql.ErrNoRows
|
||||
}
|
||||
if errors.Is(err, context.Canceled) {
|
||||
return context.Canceled
|
||||
}
|
||||
if errors.Is(err, context.DeadlineExceeded) {
|
||||
return context.DeadlineExceeded
|
||||
}
|
||||
var already *db.PostgreSQLError
|
||||
if errors.As(err, &already) {
|
||||
return err
|
||||
}
|
||||
var validation namespaceValidationError
|
||||
for _, sentinel := range []error{ErrEventWorker, ErrEventClaim, ErrEventQueueFull, ErrEventCompletionLog, ErrEventJobFilter, ErrPeriodicalConflict, ErrPeriodicalDisabled, ErrEmbeddedAssetUnavailable, ErrContactUIDConflict, ErrContactSyncToken, ErrTaskNoteExists, ErrTaskCaptureMissing, ErrNotificationInput, ErrTaskInvalid, ErrTaskConflict, ErrTaskDeleted, ErrTaskHasChildren, ErrTaskWorking, ErrTaskDAVLimit, ErrTaskDAVIdentity, ErrTaskDAVRelocated, ErrTaskDAVCollectionExists, ErrDocumentHashMismatch, ErrObjectListBudget} {
|
||||
if errors.Is(err, sentinel) {
|
||||
return sentinel
|
||||
}
|
||||
}
|
||||
for _, sentinel := range []error{ErrBackendFeatureUnavailable, ErrCLIPending, ErrCLISlowDown, ErrCLIExpired, ErrCLILimit, ErrReadOnly,
|
||||
ErrGovernanceInvalid, ErrGovernanceConflict, ErrGovernanceCeiling, ErrGovernanceConfirm,
|
||||
ErrLocalityAccessAlias, ErrLocalityAccessEnforcing, ErrPublicationImpact, ErrNavigationBudget, ErrApprovalTransition,
|
||||
ErrManifestCapability, ErrPeriodicalInvalid, ErrContactPhone, ErrRevisionCursor, ErrRevisionConflict, ErrInvalidRevisionLanguage, ErrPageNotFound, ErrRevisionDomainRequired, ErrDocumentStoreRequired} {
|
||||
if errors.Is(err, sentinel) {
|
||||
return sentinel
|
||||
}
|
||||
}
|
||||
if errors.As(err, &validation) || errors.Is(err, ErrPostgreSQLComponentNotReady) || errors.Is(err, ErrAccessDenied) {
|
||||
return err
|
||||
}
|
||||
for _, sentinel := range []error{ErrPathNotFound, ErrPathConflict, ErrPathSnapshotChanged, ErrPathMutationTooLarge, ErrCityChanged, ErrCityMapBudget, ErrCityNameAmbiguous, ErrAssetConflict, ErrAssetNotFound, ErrActivityOutboxInput, ErrBrowserNavigationLimit} {
|
||||
if errors.Is(err, sentinel) {
|
||||
return sentinel
|
||||
}
|
||||
}
|
||||
safe := &db.PostgreSQLError{Operation: operation}
|
||||
var pg *pgconn.PgError
|
||||
if errors.As(err, &pg) {
|
||||
safe.SQLState = pg.Code
|
||||
return safe
|
||||
}
|
||||
// Driver encoding and Scan errors can also contain private values. Only
|
||||
// explicitly marked, constant validation messages escape this boundary.
|
||||
return safe
|
||||
}
|
||||
|
||||
// namespaceWrite serializes topology validation with its mutation, including
|
||||
// callers in different processes. All future Domain writers must use this key.
|
||||
// Payload/filesystem I/O must remain outside this small transaction.
|
||||
func (s *Store) namespaceWrite(ctx context.Context, fn func(context.Context, *sql.Tx) error) error {
|
||||
return s.administrativeWrite(ctx, `UPDATE domains SET uuid=uuid WHERE 0`, fn)
|
||||
}
|
||||
|
||||
// SQLite's database-wide writer lock can be reserved using an owned table.
|
||||
// Authentication-only legacy fixtures have groups but no domains. PostgreSQL
|
||||
// always uses the same topology/administration advisory key, regardless of table.
|
||||
func (s *Store) groupWrite(ctx context.Context, fn func(context.Context, *sql.Tx) error) error {
|
||||
return s.administrativeWrite(ctx, `UPDATE groups SET uuid=uuid WHERE 0`, fn)
|
||||
}
|
||||
|
||||
func (s *Store) administrativeWrite(ctx context.Context, sqliteReservation string, fn func(context.Context, *sql.Tx) error) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
|
||||
defer cancel()
|
||||
tx, err := s.DB.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelReadCommitted})
|
||||
if err != nil {
|
||||
return s.sqlError("namespace begin", err)
|
||||
}
|
||||
defer tx.Rollback()
|
||||
if s.sqlBackend == storePostgreSQL {
|
||||
_, err = tx.ExecContext(ctx, `SELECT pg_advisory_xact_lock(1885431653, 1853058931)`)
|
||||
} else {
|
||||
_, err = tx.ExecContext(ctx, sqliteReservation)
|
||||
}
|
||||
if err != nil {
|
||||
return s.sqlError("namespace lock", err)
|
||||
}
|
||||
if err := fn(ctx, tx); err != nil {
|
||||
return s.sqlError("namespace update", err)
|
||||
}
|
||||
return s.sqlError("namespace commit", tx.Commit())
|
||||
}
|
||||
Reference in new issue
Block a user