44 lines
1.7 KiB
Go
44 lines
1.7 KiB
Go
package v071
|
|
|
|
import (
|
|
"knowledge-governance/internal/libraryrelease"
|
|
"testing"
|
|
)
|
|
|
|
// Cross-database snapshots may reuse a local serial value already owned by an
|
|
// unrelated KB. UUIDs remain stable; the destination allocates its own serial.
|
|
func TestLibraryChunkSequenceIsolated(t *testing.T) {
|
|
fx := openWriteSetFixture(t)
|
|
seedTargetData(t, fx)
|
|
if _, err := fx.pool.Exec(fx.ctx, `ALTER TABLE chunks ADD COLUMN seq_id bigserial UNIQUE`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var occupied int64
|
|
if err := fx.pool.QueryRow(fx.ctx, `SELECT seq_id FROM chunks WHERE knowledge_base_id=$1 LIMIT 1`, wsOtherKB).Scan(&occupied); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
before := universeDump(t, fx.pool)
|
|
snapshot := sealSourceSnapshot(t, fx, func(parts map[string][]map[string]any) { parts["chunks"][0]["seq_id"] = occupied })
|
|
target := fx.target(t)
|
|
outcome := libraryrelease.Run(fx.ctx, requestFor(snapshot), target, fx.backups(t, target), nil)
|
|
if outcome.State != "succeeded" {
|
|
t.Fatalf("cross-database publish must allocate local sequence: %+v", outcome)
|
|
}
|
|
var allocated int64
|
|
if err := fx.pool.QueryRow(fx.ctx, `SELECT seq_id FROM chunks WHERE id='chunk-src'`).Scan(&allocated); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if allocated == occupied || allocated <= 0 {
|
|
t.Fatalf("invalid destination serial %d (occupied %d)", allocated, occupied)
|
|
}
|
|
if err := target.Verify(fx.ctx, libraryrelease.Expectation{SnapshotID: snapshot}); err != nil {
|
|
t.Fatalf("content verification: %v", err)
|
|
}
|
|
if err := target.Restore(fx.ctx, outcome.Backup); err != nil {
|
|
t.Fatalf("restore must preserve original serial values: %v", err)
|
|
}
|
|
if diff := diffUniverse(before, universeDump(t, fx.pool)); diff != "" {
|
|
t.Fatalf("restored universe differs: %s", diff)
|
|
}
|
|
}
|