From b941450e0345d574b7c643a853ad688dda073168 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Wed, 20 Nov 2019 16:14:15 +0000 Subject: [PATCH] Fix indexes --- db/define_test.go | 38 ++++++++++++++++++++++++++++++++++++++ db/document.go | 4 ++-- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/db/define_test.go b/db/define_test.go index f12403c8..24f4b7f4 100644 --- a/db/define_test.go +++ b/db/define_test.go @@ -1109,6 +1109,44 @@ func TestDefine(t *testing.T) { }) + Convey("Define a single-field unique index on a table, and ensure it prevents duplicate record values, after updating records", t, func() { + + setupDB(workerCount) + + txt := ` + USE NS test DB test; + DEFINE INDEX test ON person COLUMNS email UNIQUE; + UPDATE person:one SET account="one", email="info@demo.com"; + UPDATE person:one SET account="demo", email="info@demo.com"; + UPDATE person:one SET account="demo", email="test@demo.com"; + UPDATE person:two SET account="demo", email="info@demo.com"; + UPDATE person:tre SET account="demo", email="info@demo.com"; + UPDATE person:tre SET account="demo", email="test@demo.com"; + SELECT * FROM person ORDER BY meta.id; + ` + + res, err := Execute(permsKV(), txt, nil) + So(err, ShouldBeNil) + So(res, ShouldHaveLength, 9) + So(res[1].Status, ShouldEqual, "OK") + So(res[2].Result, ShouldHaveLength, 1) + So(res[2].Status, ShouldEqual, "OK") + So(res[3].Result, ShouldHaveLength, 1) + So(res[3].Status, ShouldEqual, "OK") + So(res[4].Result, ShouldHaveLength, 1) + So(res[4].Status, ShouldEqual, "OK") + So(res[5].Result, ShouldHaveLength, 1) + So(res[5].Status, ShouldEqual, "OK") + So(res[6].Result, ShouldHaveLength, 0) + So(res[6].Status, ShouldEqual, "ERR_IX") + So(res[7].Result, ShouldHaveLength, 0) + So(res[7].Status, ShouldEqual, "ERR_IX") + So(res[8].Result, ShouldHaveLength, 2) + So(data.Consume(res[8].Result[0]).Get("id").Data(), ShouldResemble, &sql.Thing{"person", "one"}) + So(data.Consume(res[8].Result[1]).Get("id").Data(), ShouldResemble, &sql.Thing{"person", "two"}) + + }) + Convey("Define a multiple-field unique index on a table, and ensure it prevents duplicate record values", t, func() { setupDB(workerCount) diff --git a/db/document.go b/db/document.go index 0ab173a9..a03cbfd3 100644 --- a/db/document.go +++ b/db/document.go @@ -355,7 +355,7 @@ func (d *document) storeIndex(ctx context.Context) (err error) { for _, f := range del { enfd := data.Consume(f).Encode() didx := &keys.Index{KV: d.key.KV, NS: d.key.NS, DB: d.key.DB, TB: d.key.TB, IX: ix.Name.VA, FD: enfd} - d.i.e.tx.DelC(ctx, d.i.e.time.UnixNano(), didx.Encode(), d.id.Bytes()) + d.i.e.tx.DelC(ctx, 0, didx.Encode(), d.id.Bytes()) } for _, f := range add { enfd := data.Consume(f).Encode() @@ -370,7 +370,7 @@ func (d *document) storeIndex(ctx context.Context) (err error) { for _, f := range del { enfd := data.Consume(f).Encode() didx := &keys.Point{KV: d.key.KV, NS: d.key.NS, DB: d.key.DB, TB: d.key.TB, IX: ix.Name.VA, FD: enfd, ID: d.key.ID} - d.i.e.tx.DelC(ctx, d.i.e.time.UnixNano(), didx.Encode(), d.id.Bytes()) + d.i.e.tx.DelC(ctx, 0, didx.Encode(), d.id.Bytes()) } for _, f := range add { enfd := data.Consume(f).Encode()