🐛 Keep tombstones when replacing data in the legacy import

Clients only pull changed rows and never learn about hard deletes, so
a row they already pulled survived as a ghost: it kept winning against
imported rows with the same effective_from. Existing rows now become
tombstones. Closed days and weeks are overwritten in place when an
imported row has the same key, so their unique index holds.
This commit is contained in:
2026-12-01 16:37:26 +01:00
parent 20df2918c9
commit 8898d976e6
2 changed files with 310 additions and 78 deletions
+65 -13
View File
@@ -17,6 +17,8 @@ import (
"github.com/pocketbase/pocketbase/core"
"github.com/spf13/cobra"
_ "modernc.org/sqlite"
"github.com/wotra/wotra/internal/collections"
)
// Summary reports the outcome of an import.
@@ -35,8 +37,11 @@ func Register(app core.App, rootCmd *cobra.Command) {
// Every imported row gets a new id, rev 1 and client_updated_at set to the
// legacy updated_at. Rows that fail the PocketBase field validation are
// skipped with a printed warning and counted in Summary.Skipped. Unless
// dryRun is set, all existing records of the owner in the synced collections
// are deleted and the mapped rows inserted in one transaction.
// dryRun is set, the existing records of the owner in the synced collections
// are replaced by the mapped rows in one transaction. Existing rows become
// tombstones so that the clients learn about the replacement, except closed
// days and weeks whose key an imported row has: they are overwritten in place
// because of their unique index.
func Import(app core.App, db *sql.DB, owner string, tzOverride string, dryRun bool) (Summary, error) {
var summary Summary
if err := checkSchema(db); err != nil {
@@ -86,18 +91,12 @@ func Import(app core.App, db *sql.DB, owner string, tzOverride string, dryRun bo
}
err := app.RunInTransaction(func(tx core.App) error {
now := time.Now().UnixMilli()
for _, t := range tables {
if err := deleteOwned(tx, t.collection, owner); err != nil {
if err := replaceOwned(tx, t.collection, t.records, owner, now); err != nil {
return err
}
}
for _, t := range tables {
for _, record := range t.records {
if err := tx.Save(record); err != nil {
return fmt.Errorf("insert %s: %w", t.collection, err)
}
}
}
return nil
})
if err != nil {
@@ -395,19 +394,72 @@ func loadAdjustments(app core.App, db *sql.DB, owner string) ([]*core.Record, in
})
}
func deleteOwned(tx core.App, collection, owner string) error {
// keyField names the field that identifies a row of a collection with a unique
// index per owner. The other synced collections have no such key.
var keyField = map[string]string{"closed_days": "day_key", "closed_weeks": "week_key"}
// replaceOwned makes every existing record of the owner a tombstone and inserts
// the imported records of the collection.
//
// The existing rows are not hard deleted because clients only pull changed rows:
// a row they already have would never learn about the delete and survive as a
// ghost. A closed day or week whose key an imported record has is overwritten in
// place instead, because the unique (owner, day_key) and (owner, week_key) indexes
// leave no room for a second row with the same key.
func replaceOwned(tx core.App, collection string, imported []*core.Record, owner string, now int64) error {
records, err := tx.FindAllRecords(collection, dbx.HashExp{"owner": owner})
if err != nil {
return fmt.Errorf("find %s: %w", collection, err)
}
key := keyField[collection]
byKey := make(map[string]*core.Record, len(imported))
for _, r := range imported {
if key != "" {
byKey[r.GetString(key)] = r
}
}
overwritten := make(map[*core.Record]bool)
for _, record := range records {
if err := tx.Delete(record); err != nil {
return fmt.Errorf("delete %s %s: %w", collection, record.Id, err)
var match *core.Record
if key != "" {
match = byKey[record.GetString(key)]
}
if match != nil {
overwritten[match] = true
overwrite(record, match)
} else {
record.Set("deleted", true)
record.Set("client_updated_at", now)
}
record.Set("rev", record.GetInt("rev")+1)
if err := tx.Save(record); err != nil {
return fmt.Errorf("update %s %s: %w", collection, record.Id, err)
}
}
for _, record := range imported {
if overwritten[record] {
continue
}
if err := tx.Save(record); err != nil {
return fmt.Errorf("insert %s: %w", collection, err)
}
}
return nil
}
// overwrite copies the data of the imported record onto record, which keeps its id.
// The client_updated_at is newer than the one of the client's copy of the row so
// that the client takes the imported values.
func overwrite(record, imported *core.Record) {
for _, field := range collections.DataFields[record.Collection().Name] {
record.Set(field, imported.GetRaw(field))
}
record.Set("deleted", false)
record.Set("client_updated_at", max(imported.GetInt64("client_updated_at"), record.GetInt64("client_updated_at")+1))
}
const legacyCopyName = "legacy.db"
type legacyFile struct {