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
@@ -51,6 +51,7 @@ type Repository struct {
|
||||
Root string
|
||||
DB *sql.DB
|
||||
PublicAllowed func(string) bool
|
||||
postgres bool
|
||||
}
|
||||
|
||||
func (r *Repository) canRead(domain string, c Calendar, actor string) bool {
|
||||
@@ -66,8 +67,8 @@ func New(root string, db *sql.DB) (*Repository, error) {
|
||||
}
|
||||
_, err := db.Exec(`CREATE TABLE IF NOT EXISTS calendar_index_state(domain TEXT NOT NULL,calendar TEXT NOT NULL,version TEXT NOT NULL,PRIMARY KEY(domain,calendar));
|
||||
CREATE TABLE IF NOT EXISTS calendar_month_cache(domain TEXT NOT NULL,calendar TEXT NOT NULL,month TEXT NOT NULL,hash TEXT NOT NULL,PRIMARY KEY(domain,calendar,month));
|
||||
CREATE TABLE IF NOT EXISTS calendar_event_cache(domain TEXT NOT NULL,calendar TEXT NOT NULL,uid TEXT NOT NULL,month TEXT NOT NULL,start INTEGER NOT NULL,end INTEGER NOT NULL,etag TEXT NOT NULL,data TEXT NOT NULL,PRIMARY KEY(domain,calendar,uid));
|
||||
CREATE INDEX IF NOT EXISTS calendar_event_range ON calendar_event_cache(domain,calendar,start,end);`)
|
||||
CREATE TABLE IF NOT EXISTS calendar_event_cache(domain TEXT NOT NULL,calendar TEXT NOT NULL,uid TEXT NOT NULL,month TEXT NOT NULL,start INTEGER NOT NULL,"end" INTEGER NOT NULL,etag TEXT NOT NULL,data TEXT NOT NULL,PRIMARY KEY(domain,calendar,uid));
|
||||
CREATE INDEX IF NOT EXISTS calendar_event_range ON calendar_event_cache(domain,calendar,start,"end");`)
|
||||
return &Repository{Root: root, DB: db}, err
|
||||
}
|
||||
func validID(s string) bool { u, err := uuid.Parse(s); return err == nil && u.String() == s }
|
||||
@@ -347,7 +348,10 @@ func (r *Repository) Settings(ctx context.Context, domain, id, actor, version, n
|
||||
// with event/source writes. Cache rows are disposable, but deleting them
|
||||
// before the atomic directory rename means a failed rename can safely rebuild
|
||||
// them from the still-visible canonical files on the next access.
|
||||
func (r *Repository) Delete(ctx context.Context, domain, id, actor, version string) error {
|
||||
func (r *Repository) Delete(ctx context.Context, domain, id, actor, version string) (resultErr error) {
|
||||
defer func() { resultErr = r.failure(resultErr) }()
|
||||
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
||||
defer cancel()
|
||||
dir, err := r.dir(domain, id)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -382,7 +386,7 @@ func (r *Repository) Delete(ctx context.Context, domain, id, actor, version stri
|
||||
}
|
||||
defer tx.Rollback()
|
||||
for _, table := range []string{"calendar_event_cache", "calendar_month_cache", "calendar_index_state"} {
|
||||
if _, err = tx.ExecContext(ctx, `DELETE FROM `+table+` WHERE domain=? AND calendar=?`, domain, id); err != nil {
|
||||
if _, err = tx.ExecContext(ctx, `DELETE FROM `+table+` WHERE domain=`+r.p(1)+` AND calendar=`+r.p(2), domain, id); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -413,9 +417,12 @@ func syncDirectory(path string) error {
|
||||
|
||||
// refresh reconciles only changed monthly generations. A crash after manifest
|
||||
// commit but before index commit repairs itself on the next request.
|
||||
func (r *Repository) refresh(ctx context.Context, domain string, c Calendar, dir string) error {
|
||||
func (r *Repository) refresh(ctx context.Context, domain string, c Calendar, dir string) (resultErr error) {
|
||||
defer func() { resultErr = r.failure(resultErr) }()
|
||||
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
||||
defer cancel()
|
||||
var version string
|
||||
if err := r.DB.QueryRowContext(ctx, `SELECT version FROM calendar_index_state WHERE domain=? AND calendar=?`, domain, c.ID).Scan(&version); err == nil && version == c.Version {
|
||||
if err := r.DB.QueryRowContext(ctx, `SELECT version FROM calendar_index_state WHERE domain=`+r.p(1)+` AND calendar=`+r.p(2), domain, c.ID).Scan(&version); err == nil && version == c.Version {
|
||||
return nil
|
||||
} else if err != nil && err != sql.ErrNoRows {
|
||||
return err
|
||||
@@ -425,7 +432,7 @@ func (r *Repository) refresh(ctx context.Context, domain string, c Calendar, dir
|
||||
return e
|
||||
}
|
||||
defer tx.Rollback()
|
||||
rows, e := tx.QueryContext(ctx, `SELECT month,hash FROM calendar_month_cache WHERE domain=? AND calendar=?`, domain, c.ID)
|
||||
rows, e := tx.QueryContext(ctx, `SELECT month,hash FROM calendar_month_cache WHERE domain=`+r.p(1)+` AND calendar=`+r.p(2), domain, c.ID)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
@@ -445,10 +452,10 @@ func (r *Repository) refresh(ctx context.Context, domain string, c Calendar, dir
|
||||
}
|
||||
for m, h := range known {
|
||||
if c.Months[m] != h {
|
||||
if _, e = tx.ExecContext(ctx, `DELETE FROM calendar_event_cache WHERE domain=? AND calendar=? AND month=?`, domain, c.ID, m); e != nil {
|
||||
if _, e = tx.ExecContext(ctx, `DELETE FROM calendar_event_cache WHERE domain=`+r.p(1)+` AND calendar=`+r.p(2)+` AND month=`+r.p(3), domain, c.ID, m); e != nil {
|
||||
return e
|
||||
}
|
||||
if _, e = tx.ExecContext(ctx, `DELETE FROM calendar_month_cache WHERE domain=? AND calendar=? AND month=?`, domain, c.ID, m); e != nil {
|
||||
if _, e = tx.ExecContext(ctx, `DELETE FROM calendar_month_cache WHERE domain=`+r.p(1)+` AND calendar=`+r.p(2)+` AND month=`+r.p(3), domain, c.ID, m); e != nil {
|
||||
return e
|
||||
}
|
||||
}
|
||||
@@ -480,15 +487,15 @@ func (r *Repository) refresh(ctx context.Context, domain string, c Calendar, dir
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
if _, e = tx.ExecContext(ctx, `INSERT INTO calendar_event_cache(domain,calendar,uid,month,start,end,etag,data) VALUES(?,?,?,?,?,?,?,?)`, domain, c.ID, event.UID, m, event.Start.Unix(), event.End.Unix(), event.ETag, event.Data); e != nil {
|
||||
if _, e = tx.ExecContext(ctx, `INSERT INTO calendar_event_cache(domain,calendar,uid,month,start,"end",etag,data) VALUES(`+r.p(1)+`,`+r.p(2)+`,`+r.p(3)+`,`+r.p(4)+`,`+r.p(5)+`,`+r.p(6)+`,`+r.p(7)+`,`+r.p(8)+`)`, domain, c.ID, event.UID, m, event.Start.Unix(), event.End.Unix(), event.ETag, event.Data); e != nil {
|
||||
return e
|
||||
}
|
||||
}
|
||||
if _, e = tx.ExecContext(ctx, `INSERT INTO calendar_month_cache(domain,calendar,month,hash) VALUES(?,?,?,?)`, domain, c.ID, m, h); e != nil {
|
||||
if _, e = tx.ExecContext(ctx, `INSERT INTO calendar_month_cache(domain,calendar,month,hash) VALUES(`+r.p(1)+`,`+r.p(2)+`,`+r.p(3)+`,`+r.p(4)+`)`, domain, c.ID, m, h); e != nil {
|
||||
return e
|
||||
}
|
||||
}
|
||||
if _, e = tx.ExecContext(ctx, `INSERT INTO calendar_index_state(domain,calendar,version) VALUES(?,?,?) ON CONFLICT(domain,calendar) DO UPDATE SET version=excluded.version`, domain, c.ID, c.Version); e != nil {
|
||||
if _, e = tx.ExecContext(ctx, `INSERT INTO calendar_index_state(domain,calendar,version) VALUES(`+r.p(1)+`,`+r.p(2)+`,`+r.p(3)+`) ON CONFLICT(domain,calendar) DO UPDATE SET version=excluded.version`, domain, c.ID, c.Version); e != nil {
|
||||
return e
|
||||
}
|
||||
return tx.Commit()
|
||||
@@ -540,7 +547,8 @@ func (r *Repository) query(ctx context.Context, domain, id, actor string, start,
|
||||
func (r *Repository) Upcoming(ctx context.Context, domain, id, actor string, start, end time.Time) ([]Event, error) {
|
||||
return r.queryLimit(ctx, domain, id, actor, start, end, true, 20)
|
||||
}
|
||||
func (r *Repository) queryLimit(ctx context.Context, domain, id, actor string, start, end time.Time, withData bool, limit int) ([]Event, error) {
|
||||
func (r *Repository) queryLimit(ctx context.Context, domain, id, actor string, start, end time.Time, withData bool, limit int) (result []Event, resultErr error) {
|
||||
defer func() { resultErr = r.failure(resultErr) }()
|
||||
if !end.After(start) || end.Sub(start) > 370*24*time.Hour {
|
||||
return nil, ErrInvalid
|
||||
}
|
||||
@@ -553,7 +561,7 @@ func (r *Repository) queryLimit(ctx context.Context, domain, id, actor string, s
|
||||
if withData {
|
||||
columns = "data"
|
||||
}
|
||||
rows, e := r.DB.QueryContext(ctx, `SELECT `+columns+` FROM calendar_event_cache WHERE domain=? AND calendar=? AND start<? AND (end>? OR (start=end AND start>=?)) ORDER BY start,uid LIMIT ?`, domain, id, end.Unix(), start.Unix(), start.Unix(), limit)
|
||||
rows, e := r.DB.QueryContext(ctx, `SELECT `+columns+` FROM calendar_event_cache WHERE domain=`+r.p(1)+` AND calendar=`+r.p(2)+` AND start<`+r.p(3)+` AND ("end">`+r.p(4)+` OR (start="end" AND start>=`+r.p(5)+`)) ORDER BY start,uid LIMIT `+r.p(6), domain, id, end.Unix(), start.Unix(), start.Unix(), limit)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
@@ -583,14 +591,15 @@ func (r *Repository) queryLimit(ctx context.Context, domain, id, actor string, s
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
func (r *Repository) Event(ctx context.Context, domain, id, actor, uid string) (Event, error) {
|
||||
func (r *Repository) Event(ctx context.Context, domain, id, actor, uid string) (result Event, resultErr error) {
|
||||
defer func() { resultErr = r.failure(resultErr) }()
|
||||
_, _, unlock, e := r.locked(ctx, domain, id, actor)
|
||||
if e != nil {
|
||||
return Event{}, e
|
||||
}
|
||||
defer unlock()
|
||||
var data string
|
||||
e = r.DB.QueryRowContext(ctx, `SELECT data FROM calendar_event_cache WHERE domain=? AND calendar=? AND uid=?`, domain, id, uid).Scan(&data)
|
||||
e = r.DB.QueryRowContext(ctx, `SELECT data FROM calendar_event_cache WHERE domain=`+r.p(1)+` AND calendar=`+r.p(2)+` AND uid=`+r.p(3), domain, id, uid).Scan(&data)
|
||||
if e == sql.ErrNoRows {
|
||||
e = ErrMissing
|
||||
}
|
||||
@@ -611,13 +620,14 @@ func (r *Repository) Change(ctx context.Context, domain, id, actor, uid, match s
|
||||
return r.changeLocked(ctx, domain, actor, uid, match, item, c, dir)
|
||||
}
|
||||
|
||||
func (r *Repository) changeLocked(ctx context.Context, domain, actor, uid, match string, item *Event, c Calendar, dir string) (Event, error) {
|
||||
func (r *Repository) changeLocked(ctx context.Context, domain, actor, uid, match string, item *Event, c Calendar, dir string) (result Event, resultErr error) {
|
||||
defer func() { resultErr = r.failure(resultErr) }()
|
||||
id := c.ID
|
||||
if !c.CanWrite(actor) {
|
||||
return Event{}, ErrForbidden
|
||||
}
|
||||
var oldData, oldMonth, oldTag string
|
||||
e := r.DB.QueryRowContext(ctx, `SELECT data,month,etag FROM calendar_event_cache WHERE domain=? AND calendar=? AND uid=?`, domain, id, uid).Scan(&oldData, &oldMonth, &oldTag)
|
||||
e := r.DB.QueryRowContext(ctx, `SELECT data,month,etag FROM calendar_event_cache WHERE domain=`+r.p(1)+` AND calendar=`+r.p(2)+` AND uid=`+r.p(3), domain, id, uid).Scan(&oldData, &oldMonth, &oldTag)
|
||||
exists := e == nil
|
||||
if e != nil && e != sql.ErrNoRows {
|
||||
return Event{}, e
|
||||
@@ -650,7 +660,7 @@ func (r *Repository) changeLocked(ctx context.Context, domain, actor, uid, match
|
||||
months[newMonth] = true
|
||||
}
|
||||
for month := range months {
|
||||
rows, e := r.DB.QueryContext(ctx, `SELECT data FROM calendar_event_cache WHERE domain=? AND calendar=? AND month=? AND uid<>? ORDER BY uid`, domain, id, month, uid)
|
||||
rows, e := r.DB.QueryContext(ctx, `SELECT data FROM calendar_event_cache WHERE domain=`+r.p(1)+` AND calendar=`+r.p(2)+` AND month=`+r.p(3)+` AND uid<>`+r.p(4)+` ORDER BY uid`, domain, id, month, uid)
|
||||
if e != nil {
|
||||
return Event{}, e
|
||||
}
|
||||
@@ -717,7 +727,7 @@ func (r *Repository) changeLocked(ctx context.Context, domain, actor, uid, match
|
||||
_ = os.Remove(file)
|
||||
}
|
||||
}
|
||||
// The committed files, not SQLite, determine success. A subsequent read
|
||||
// The committed files, not the database cache, determine success. A subsequent read
|
||||
// reconciles the cache; retries with the old ETag correctly get 412.
|
||||
return next, nil
|
||||
}
|
||||
Reference in new issue
Block a user