145 lines
5.0 KiB
Go
145 lines
5.0 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"pageswiki/internal/metadata"
|
|
)
|
|
|
|
// NewPostgreSQLWithDocumentRoot assembles the native repositories for isolated
|
|
// runtime verification. It does not import SQLite data, generate/replace keys,
|
|
// hydrate legacy body caches, or start background workers. The caller owns the
|
|
// pool and must hold the instance filesystem lock. Main deliberately does not
|
|
// expose this backend until the full import/restore release gate is satisfied.
|
|
func NewPostgreSQLWithDocumentRoot(ctx context.Context, database *sql.DB, root string) (*Store, error) {
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
if database == nil {
|
|
return nil, errors.New("PostgreSQL database is required")
|
|
}
|
|
root = strings.TrimSpace(root)
|
|
if root == "" {
|
|
return nil, ErrDocumentStoreRequired
|
|
}
|
|
root, err := filepath.Abs(root)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
info, err := os.Stat(root)
|
|
if err != nil || !info.IsDir() {
|
|
return nil, ErrDocumentStoreRequired
|
|
}
|
|
key, err := existingAuthKey(root)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
s := &Store{DB: database, sqlBackend: storePostgreSQL, DocumentRoot: root, authKey: key, childCache: map[string][]string{}}
|
|
// Raw schema initialization does not repair namespace ownership or create
|
|
// fallback Domains in an already populated installation.
|
|
if err = s.ensurePostgreSQLNamespaces(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
core, err := metadata.New(database, metadata.PostgreSQL)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err = core.InitializeFresh(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
if err = s.initializeNativeNamespaces(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
if err = s.prepareNativeApplicationSchemas(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
if err = s.EnsureBuiltinAdminGroups(); err != nil {
|
|
return nil, err
|
|
}
|
|
return s, nil
|
|
}
|
|
|
|
func (s *Store) prepareNativeApplicationSchemas(ctx context.Context) error {
|
|
var err error
|
|
steps := []struct {
|
|
name string
|
|
run func() error
|
|
}{
|
|
{"paths", s.EnsureDomainPathsSchema},
|
|
{"object listing", s.EnsureObjectListingSchema},
|
|
{"assets", s.EnsureAssetsSchema},
|
|
{"federation", s.EnsureFederationSchema},
|
|
{"event jobs", s.EnsureEventJobsSchema},
|
|
{"activity outbox", s.EnsureActivityOutboxSchema},
|
|
{"notifications", s.EnsureNotificationSchema},
|
|
{"publication approvals", s.EnsurePublicationApprovalSchema},
|
|
{"broadcasts", s.EnsureBroadcastSchema},
|
|
{"projection metadata", s.EnsureProjectionMetadataSchema},
|
|
{"resource history", s.EnsureResourceHistorySchema},
|
|
{"hosted editor storage", s.EnsureAdapterSessionsSchema},
|
|
{"authentication", s.EnsureAuthSchema},
|
|
{"tasks", s.EnsureTasksSchema},
|
|
{"contacts", func() error { return s.ensureSchemaComponent("contacts") }},
|
|
{"feeds", func() error { return s.ensureSchemaComponent("feeds") }},
|
|
{"browser navigation", s.EnsureBrowserNavigationSchema},
|
|
{"city", s.EnsureCitySchema},
|
|
{"periodicals", s.EnsurePeriodicalSchema},
|
|
{"user space", s.EnsureUserSpaceSchema},
|
|
{"profile contacts", func() error { return s.ensureSchemaComponent("profile-contacts") }},
|
|
{"search", s.EnsureSearchIndexSchema},
|
|
{"import journals", s.EnsureImportSchema},
|
|
{"migration retention", func() error { return s.ensureNativeImportRetention(ctx) }},
|
|
}
|
|
for _, step := range steps {
|
|
if err = ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
if err = step.run(); err != nil {
|
|
return fmt.Errorf("native startup %s: %w", step.name, err)
|
|
}
|
|
}
|
|
// No zombie GC schema/worker, legacy body backfill, or Push registration.
|
|
return nil
|
|
}
|
|
|
|
func existingAuthKey(root string) ([]byte, error) {
|
|
if encoded := strings.TrimSpace(os.Getenv("PAGESWIKI_AUTH_KEY")); encoded != "" {
|
|
sum := sha256.Sum256([]byte(encoded))
|
|
return sum[:], nil
|
|
}
|
|
key, err := os.ReadFile(filepath.Join(filepath.Dir(root), "auth.key"))
|
|
if err != nil || len(key) != 32 {
|
|
return nil, errors.New("native startup requires the original 32-byte auth.key or PAGESWIKI_AUTH_KEY")
|
|
}
|
|
return key, nil
|
|
}
|
|
|
|
func (s *Store) initializeNativeNamespaces(ctx context.Context) error {
|
|
return s.namespaceWrite(ctx, func(ctx context.Context, tx *sql.Tx) error {
|
|
var campuses, domains int
|
|
if err := tx.QueryRowContext(ctx, `SELECT (SELECT count(*) FROM campuses),(SELECT count(*) FROM domains)`).Scan(&campuses, &domains); err != nil {
|
|
return err
|
|
}
|
|
// Provisioning/import is explicit. Even an empty instance must have its
|
|
// namespaces installed beforehand; startup never guesses their identity.
|
|
if campuses == 0 || domains == 0 {
|
|
return errors.New("native startup requires provisioned Campus and Domain records")
|
|
}
|
|
var invalid bool
|
|
if err := tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM domains d LEFT JOIN campuses c ON c.uuid=d.campus_uuid WHERE c.uuid IS NULL) OR EXISTS(SELECT 1 FROM paths p LEFT JOIN domains d ON d.uuid=p.domain_uuid WHERE d.uuid IS NULL)`).Scan(&invalid); err != nil {
|
|
return err
|
|
}
|
|
if invalid {
|
|
return errors.New("native startup found unbound namespaces; import or repair must complete before serving")
|
|
}
|
|
return nil
|
|
})
|
|
}
|