@@ -373,3 +373,71 @@ func TestHalfFloat(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Two records listing the same cid in cids/* with no cid: index row (the same
|
||||
// content stored twice): the first keeps it, a record left with no cid of its
|
||||
// own folds into it, and the duplicate (cid, key) claims are skipped.
|
||||
func TestSharedUnindexedCIDFolds(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
oldPath := filepath.Join(dir, "v1.db")
|
||||
db, err := sql.Open("sqlite", "file:"+oldPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := db.Exec(`CREATE TABLE kv(k TEXT PRIMARY KEY, v TEXT NOT NULL) WITHOUT ROWID`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for k, v := range map[string]string{
|
||||
"meta-core:schema-version": "2",
|
||||
"file:" + ridA + "/cids/" + cidSHA: "true",
|
||||
"file:" + ridA + "/title": "A",
|
||||
"file:" + ridB + "/cids/" + cidSHA: "true",
|
||||
"file:" + ridB + "/title": "B",
|
||||
"file:" + ridB + "/genres/Drama": "true",
|
||||
"file:" + ridC + "/cids/" + cidSHA: "true",
|
||||
"file:" + ridC + "/cids/" + cidB: "true",
|
||||
"file:" + ridC + "/title": "C",
|
||||
} {
|
||||
if _, err := db.Exec(`INSERT INTO kv(k, v) VALUES(?, ?)`, k, v); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
db.Close()
|
||||
truthPath := filepath.Join(dir, "truth.db")
|
||||
st, err := migrate(options{oldPath: oldPath, truthPath: truthPath, workers: 2})
|
||||
if err != nil {
|
||||
t.Fatalf("migrate: %v", err)
|
||||
}
|
||||
if st.Rids != 2 || st.RecordsFolded != 1 || st.CIDShared != 2 || st.DupClaims != 1 {
|
||||
t.Errorf("rids %d folded %d shared %d dups %d", st.Rids, st.RecordsFolded, st.CIDShared, st.DupClaims)
|
||||
}
|
||||
out, err := sql.Open("sqlite", "file:"+truthPath+"?mode=ro")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer out.Close()
|
||||
groups := map[string]string{}
|
||||
rows, err := out.Query(`SELECT cid, rid FROM cid_group`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for rows.Next() {
|
||||
var c, r string
|
||||
if err := rows.Scan(&c, &r); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
groups[c] = r
|
||||
}
|
||||
rows.Close()
|
||||
if groups[cidSHA] != ridA || groups[cidB] != ridC || len(groups) != 2 {
|
||||
t.Errorf("cid_group %v", groups)
|
||||
}
|
||||
var n int
|
||||
if err := out.QueryRow(`SELECT count(*) FROM claim WHERE cid = ? AND key IN ('title', 'genres/Drama')`, cidSHA).Scan(&n); err != nil || n != 2 {
|
||||
t.Errorf("claims on the shared cid: %d (%v)", n, err)
|
||||
}
|
||||
var title string
|
||||
if err := out.QueryRow(`SELECT value FROM claim WHERE cid = ? AND key = 'title'`, cidB).Scan(&title); err != nil || title != "C" {
|
||||
t.Errorf("record C title on its own cid: %q (%v)", title, err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user