feat(storage): add native PostgreSQL runtime and verified offline application import
Build and publish container images / images (push) Failing after 12m29s

This commit is contained in:
Anya Tokugawa committed 2026-11-15 05:47:14 +09:00
1 parent 96065d1e89
commit 0ba1dcfef0
243 files changed
+21441 -3090

No files matched your search

+48 -31
View File
@@ -46,6 +46,9 @@ type PublicationChange struct {
}
func (s *Store) ensurePublicationSchema() error {
if s.sqlBackend == storePostgreSQL {
return s.ensurePostgreSQLPublication(context.Background())
}
for _, query := range []string{
`CREATE TABLE IF NOT EXISTS locality_publication_versions(domain_uuid TEXT NOT NULL,node_kind TEXT NOT NULL,node_uuid TEXT NOT NULL,version INTEGER NOT NULL,PRIMARY KEY(domain_uuid,node_kind,node_uuid))`,
`CREATE TABLE IF NOT EXISTS locality_publication_manifests(domain_uuid TEXT NOT NULL,node_kind TEXT NOT NULL,node_uuid TEXT NOT NULL,epoch INTEGER NOT NULL,topology_version INTEGER NOT NULL,actor_uuid TEXT NOT NULL,created_at_ms INTEGER NOT NULL,PRIMARY KEY(domain_uuid,node_kind,node_uuid))`,
@@ -101,20 +104,20 @@ func installPublicationGuards(ctx context.Context, tx *sql.Tx) error {
return nil
}
func publicationImpact(ctx context.Context, tx *sql.Tx, domain, actor, kind, id string, g CityGovernance) (LocalityPublication, error) {
return publicationImpactBounded(ctx, tx, domain, actor, kind, id, g, 512, "")
func (s *Store) publicationImpact(ctx context.Context, tx *sql.Tx, domain, actor, kind, id string, g CityGovernance) (LocalityPublication, error) {
return s.publicationImpactBounded(ctx, tx, domain, actor, kind, id, g, 512, "")
}
func publicationImpactBounded(ctx context.Context, tx *sql.Tx, domain, actor, kind, id string, g CityGovernance, limit int, onlyPath string) (LocalityPublication, error) {
func (s *Store) publicationImpactBounded(ctx context.Context, tx *sql.Tx, domain, actor, kind, id string, g CityGovernance, limit int, onlyPath string) (LocalityPublication, error) {
p := LocalityPublication{PublicationVersion: g.PublicationVersion, Epoch: g.Epoch, TopologyVersion: g.TopologyVersion, Entries: []PublicationEntry{}, Excluded: []string{}}
var mode string
if err := tx.QueryRowContext(ctx, `SELECT value FROM config WHERE domain_uuid=? AND key='authorization_mode'`, domain).Scan(&mode); err != nil {
if err := tx.QueryRowContext(ctx, `SELECT value FROM config WHERE domain_uuid=`+s.sqlParam(1)+` AND key='authorization_mode'`, domain).Scan(&mode); err != nil {
return p, err
}
if mode != "enforcing" {
return p, ErrAccessDenied
}
if err := tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM locality_publication_manifests m WHERE domain_uuid=? AND node_kind=? AND node_uuid=? AND epoch=? AND topology_version=? AND EXISTS(SELECT 1 FROM locality_publication_entries e WHERE e.domain_uuid=m.domain_uuid AND e.node_kind=m.node_kind AND e.node_uuid=m.node_uuid))`, domain, kind, id, g.Epoch, g.TopologyVersion).Scan(&p.Enabled); err != nil {
if err := tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM locality_publication_manifests m WHERE domain_uuid=`+s.sqlParam(1)+` AND node_kind=`+s.sqlParam(2)+` AND node_uuid=`+s.sqlParam(3)+` AND epoch=`+s.sqlParam(4)+` AND topology_version=`+s.sqlParam(5)+` AND EXISTS(SELECT 1 FROM locality_publication_entries e WHERE e.domain_uuid=m.domain_uuid AND e.node_kind=m.node_kind AND e.node_uuid=m.node_uuid))`, domain, kind, id, g.Epoch, g.TopologyVersion).Scan(&p.Enabled); err != nil {
return p, err
}
// A Domain ceiling is never a publication grant.
@@ -126,20 +129,20 @@ func publicationImpactBounded(ctx context.Context, tx *sql.Tx, domain, actor, ki
if kind == "spot" {
table = "spots"
}
if err := tx.QueryRowContext(ctx, `SELECT path_prefix FROM `+table+` WHERE domain_uuid=? AND uuid=?`, domain, id).Scan(&scope); err != nil {
if err := tx.QueryRowContext(ctx, `SELECT path_prefix FROM `+table+` WHERE domain_uuid=`+s.sqlParam(1)+` AND uuid=`+s.sqlParam(2)+``, domain, id).Scan(&scope); err != nil {
return p, err
}
policy, err := loadReadPathPolicy(cityReadQuery{ctx, tx}, domain, actor)
policy, err := s.loadReadPathPolicy(cityReadQuery{ctx, tx}, domain, actor)
if err != nil {
return p, err
}
// Literal segment predicates: '%' and '_' are valid names, never wildcards.
rows, err := tx.QueryContext(ctx, `SELECT p.path,p.object_uuid,
(EXISTS(SELECT 1 FROM locality_access_policies a WHERE a.domain_uuid=p.domain_uuid AND `+cityPathScopeSQL("a.scope_path", "p.path")+`)
OR EXISTS(SELECT 1 FROM locality_publication_policies a JOIN zones z ON z.uuid=a.node_uuid AND z.domain_uuid=a.domain_uuid AND z.state!='archived' WHERE a.domain_uuid=p.domain_uuid AND a.node_kind IN ('quartier','street') AND a.policy='authenticated_acl' AND `+cityPathScopeSQL("z.path_prefix", "p.path")+`)
OR EXISTS(SELECT 1 FROM locality_publication_policies a JOIN spots z ON z.uuid=a.node_uuid AND z.domain_uuid=a.domain_uuid AND z.state!='archived' WHERE a.domain_uuid=p.domain_uuid AND a.node_kind='spot' AND a.policy='authenticated_acl' AND `+cityPathScopeSQL("z.path_prefix", "p.path")+`)
(EXISTS(SELECT 1 FROM locality_access_policies a WHERE a.domain_uuid=p.domain_uuid AND `+s.pathScopeSQL("a.scope_path", "p.path")+`)
OR EXISTS(SELECT 1 FROM locality_publication_policies a JOIN zones z ON z.uuid=a.node_uuid AND z.domain_uuid=a.domain_uuid AND z.state!='archived' WHERE a.domain_uuid=p.domain_uuid AND a.node_kind IN ('quartier','street') AND a.policy='authenticated_acl' AND `+s.pathScopeSQL("z.path_prefix", "p.path")+`)
OR EXISTS(SELECT 1 FROM locality_publication_policies a JOIN spots z ON z.uuid=a.node_uuid AND z.domain_uuid=a.domain_uuid AND z.state!='archived' WHERE a.domain_uuid=p.domain_uuid AND a.node_kind='spot' AND a.policy='authenticated_acl' AND `+s.pathScopeSQL("z.path_prefix", "p.path")+`)
OR EXISTS(SELECT 1 FROM paths other WHERE other.object_uuid=p.object_uuid AND (other.domain_uuid!=p.domain_uuid OR other.path!=p.path)))
FROM paths p CROSS JOIN (SELECT ? AS scope) input WHERE p.domain_uuid=? AND `+cityPathScopeSQL("input.scope", "p.path")+` AND (?='' OR p.path=?) ORDER BY p.path LIMIT ?`, scope, domain, onlyPath, onlyPath, limit+1)
FROM paths p CROSS JOIN (SELECT CAST(`+s.sqlParam(1)+` AS TEXT) AS scope) input WHERE p.domain_uuid=`+s.sqlParam(2)+` AND `+s.pathScopeSQL("input.scope", "p.path")+` AND (CAST(`+s.sqlParam(3)+` AS TEXT)='' OR p.path=`+s.sqlParam(4)+`) ORDER BY p.path LIMIT `+s.sqlParam(5), scope, domain, onlyPath, onlyPath, limit+1)
if err != nil {
return p, err
}
@@ -181,11 +184,11 @@ func publicationImpactBounded(ctx context.Context, tx *sql.Tx, domain, actor, ki
if len(args) > 0 {
// Read all scoped language heads in one metadata-only window query,
// rather than repeating an authorization/head query for every Page.
marks := strings.TrimSuffix(strings.Repeat("?,", len(args)), ",")
marks := s.sqlValues(len(args))
rows, err = tx.QueryContext(ctx, `SELECT object_uuid,uuid,document_hash,lang FROM (
SELECT object_uuid,uuid,COALESCE(document_hash,'') AS document_hash,COALESCE(NULLIF(lang,''),'und') AS lang,
ROW_NUMBER() OVER(PARTITION BY object_uuid,COALESCE(NULLIF(lang,''),'und') ORDER BY created_at_ms DESC,rowid DESC) AS head
FROM revisions WHERE object_uuid IN (`+marks+`)) WHERE head=1 ORDER BY object_uuid,lang LIMIT `+fmt.Sprint(limit*4+1), args...)
ROW_NUMBER() OVER(PARTITION BY object_uuid,COALESCE(NULLIF(lang,''),'und') ORDER BY `+s.revisionHeadOrder("")+`) AS head
FROM revisions WHERE object_uuid IN (`+marks+`)) heads WHERE head=1 ORDER BY object_uuid,lang LIMIT `+fmt.Sprint(limit*4+1), args...)
if err != nil {
return p, err
}
@@ -235,20 +238,28 @@ func publicationImpactBounded(ctx context.Context, tx *sql.Tx, domain, actor, ki
return p, nil
}
func (s *Store) LocalityPublication(ctx context.Context, domain, actor, kind, id string, change *PublicationChange) (LocalityPublication, error) {
func (s *Store) LocalityPublication(ctx context.Context, domain, actor, kind, id string, change *PublicationChange) (_ LocalityPublication, resultErr error) {
defer func() { resultErr = s.sqlError("LocalityPublication", resultErr) }()
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
tx, err := s.DB.BeginTx(ctx, nil)
options := s.policyReadOptions()
if change != nil {
options = &sql.TxOptions{Isolation: sql.LevelReadCommitted}
}
tx, err := s.DB.BeginTx(ctx, options)
if err != nil {
return LocalityPublication{}, err
}
defer tx.Rollback()
if change != nil {
if _, err = tx.ExecContext(ctx, `INSERT INTO city_governance_epochs(domain_uuid,version) VALUES(?,0) ON CONFLICT(domain_uuid) DO UPDATE SET version=version`, domain); err != nil {
if err = s.lockPolicyWrite(ctx, tx); err != nil {
return LocalityPublication{}, err
}
if _, err = tx.ExecContext(ctx, `INSERT INTO city_governance_epochs(domain_uuid,version) VALUES(`+s.sqlParam(1)+`,0) ON CONFLICT(domain_uuid) DO UPDATE SET version=city_governance_epochs.version`, domain); err != nil {
return LocalityPublication{}, err
}
}
g, err := governanceSnapshot(ctx, tx, domain, actor, kind, id)
g, err := s.governanceSnapshot(ctx, tx, domain, actor, kind, id)
if err != nil {
return LocalityPublication{}, err
}
@@ -258,7 +269,7 @@ func (s *Store) LocalityPublication(ctx context.Context, domain, actor, kind, id
// Revocation must work even when content is now unreadable or over budget.
p := LocalityPublication{PublicationVersion: g.PublicationVersion, Epoch: g.Epoch, TopologyVersion: g.TopologyVersion, Entries: []PublicationEntry{}, Excluded: []string{}}
if change == nil || change.Enabled {
p, err = publicationImpact(ctx, tx, domain, actor, kind, id, g)
p, err = s.publicationImpact(ctx, tx, domain, actor, kind, id, g)
if err != nil {
return p, err
}
@@ -270,40 +281,40 @@ func (s *Store) LocalityPublication(ctx context.Context, domain, actor, kind, id
return p, ErrPublicationImpact
}
var old bool
if err = tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM locality_publication_manifests WHERE domain_uuid=? AND node_kind=? AND node_uuid=?)`, domain, kind, id).Scan(&old); err != nil {
if err = tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM locality_publication_manifests WHERE domain_uuid=`+s.sqlParam(1)+` AND node_kind=`+s.sqlParam(2)+` AND node_uuid=`+s.sqlParam(3)+`)`, domain, kind, id).Scan(&old); err != nil {
return p, err
}
for _, table := range []string{"locality_publication_entries", "locality_publication_manifests"} {
if _, err = tx.ExecContext(ctx, `DELETE FROM `+table+` WHERE domain_uuid=? AND node_kind=? AND node_uuid=?`, domain, kind, id); err != nil {
if _, err = tx.ExecContext(ctx, `DELETE FROM `+table+` WHERE domain_uuid=`+s.sqlParam(1)+` AND node_kind=`+s.sqlParam(2)+` AND node_uuid=`+s.sqlParam(3)+``, domain, kind, id); err != nil {
return p, err
}
}
if change.Enabled {
if err = installPublicationGuards(ctx, tx); err != nil {
if err = s.installPublicationGuards(ctx, tx); err != nil {
return p, err
}
if _, err = tx.ExecContext(ctx, `INSERT INTO locality_publication_manifests VALUES(?,?,?,?,?,?,?)`, domain, kind, id, g.Epoch, g.TopologyVersion, actor, time.Now().UnixMilli()); err != nil {
if _, err = tx.ExecContext(ctx, `INSERT INTO locality_publication_manifests VALUES(`+s.sqlParam(1)+`,`+s.sqlParam(2)+`,`+s.sqlParam(3)+`,`+s.sqlParam(4)+`,`+s.sqlParam(5)+`,`+s.sqlParam(6)+`,`+s.sqlParam(7)+`)`, domain, kind, id, g.Epoch, g.TopologyVersion, actor, time.Now().UnixMilli()); err != nil {
return p, err
}
for _, e := range p.Entries {
if _, err = tx.ExecContext(ctx, `INSERT INTO locality_publication_entries VALUES(?,?,?,?,?,?,?,?)`, domain, kind, id, e.Path, e.Object, e.Revision, e.Hash, e.Language); err != nil {
if _, err = tx.ExecContext(ctx, `INSERT INTO locality_publication_entries VALUES(`+s.sqlParam(1)+`,`+s.sqlParam(2)+`,`+s.sqlParam(3)+`,`+s.sqlParam(4)+`,`+s.sqlParam(5)+`,`+s.sqlParam(6)+`,`+s.sqlParam(7)+`,`+s.sqlParam(8)+`)`, domain, kind, id, e.Path, e.Object, e.Revision, e.Hash, e.Language); err != nil {
return p, err
}
}
}
if !change.Enabled {
if err = requireManualPublication(ctx, tx, domain, actor, kind, id); err != nil {
if err = s.requireManualPublication(ctx, tx, domain, actor, kind, id); err != nil {
return p, err
}
}
p.Enabled = change.Enabled
if _, err = tx.ExecContext(ctx, `INSERT INTO locality_publication_versions VALUES(?,?,?,1) ON CONFLICT(domain_uuid,node_kind,node_uuid) DO UPDATE SET version=version+1`, domain, kind, id); err != nil {
if _, err = tx.ExecContext(ctx, `INSERT INTO locality_publication_versions VALUES(`+s.sqlParam(1)+`,`+s.sqlParam(2)+`,`+s.sqlParam(3)+`,1) ON CONFLICT(domain_uuid,node_kind,node_uuid) DO UPDATE SET version=locality_publication_versions.version+1`, domain, kind, id); err != nil {
return p, err
}
p.PublicationVersion++
after, _ := json.Marshal(map[string]any{"publication": change.Enabled, "impact_token": p.ImpactToken, "entries": len(p.Entries)})
before, _ := json.Marshal(map[string]bool{"publication": old})
if _, err = tx.ExecContext(ctx, `INSERT INTO city_governance_events(domain_uuid,epoch,actor_uuid,node_kind,node_uuid,before_json,after_json,created_at_ms) VALUES(?,?,?,?,?,?,?,?)`, domain, g.Epoch, actor, kind, id, string(before), string(after), time.Now().UnixMilli()); err != nil {
if _, err = tx.ExecContext(ctx, `INSERT INTO city_governance_events(domain_uuid,epoch,actor_uuid,node_kind,node_uuid,before_json,after_json,created_at_ms) VALUES(`+s.sqlParam(1)+`,`+s.sqlParam(2)+`,`+s.sqlParam(3)+`,`+s.sqlParam(4)+`,`+s.sqlParam(5)+`,`+s.sqlParam(6)+`,`+s.sqlParam(7)+`,`+s.sqlParam(8)+`)`, domain, g.Epoch, actor, kind, id, string(before), string(after), time.Now().UnixMilli()); err != nil {
return p, err
}
// Reconfirmation needs a fresh impact token, even if content is unchanged.
@@ -320,14 +331,20 @@ func PublicSemanticPath(path string) bool {
// Lookup is metadata-only and Path-indexed. The manifest is fenced by current
// governance/topology epochs, live binding, and the current language head.
func (s *Store) AnonymousRevision(ctx context.Context, domain, path string, languages []string) (*Revision, error) {
func (s *Store) AnonymousRevision(ctx context.Context, domain, path string, languages []string) (_ *Revision, resultErr error) {
defer func() { resultErr = s.sqlError("AnonymousRevision", resultErr) }()
if !PublicSemanticPath(path) {
return nil, sql.ErrNoRows
}
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
defer cancel()
rows, err := s.DB.QueryContext(ctx, `SELECT DISTINCT r.uuid,r.object_uuid,r.document_hash,COALESCE(NULLIF(r.lang,''),'und'),COALESCE(NULLIF(r.content_type,''),'text/html; charset=utf-8'),COALESCE(r.filename,''),r.created_at_ms
`+anonymousRevisionFromSQL+` AND e.path=?
tx, err := s.DB.BeginTx(ctx, s.policyReadOptions())
if err != nil {
return nil, s.sqlError("anonymous publication", err)
}
defer tx.Rollback()
rows, err := tx.QueryContext(ctx, `SELECT DISTINCT r.uuid,r.object_uuid,r.document_hash,COALESCE(NULLIF(r.lang,''),'und'),COALESCE(NULLIF(r.content_type,''),'text/html; charset=utf-8'),COALESCE(r.filename,''),r.created_at_ms
`+s.anonymousRevisionFromSQL()+` AND e.path=`+s.sqlParam(2)+`
ORDER BY r.created_at_ms DESC,r.uuid LIMIT 33`, domain, path)
if err != nil {
return nil, err
@@ -347,7 +364,7 @@ func (s *Store) AnonymousRevision(ctx context.Context, domain, path string, lang
}
rows.Close()
if len(items) == 0 {
pages, e := s.automaticPublicPages(ctx, domain, path)
pages, e := s.automaticPublicPagesTx(ctx, tx, domain, path)
if e != nil {
return nil, e
}