Fix indexes

This commit is contained in:
Tobie Morgan Hitchcock 2019-11-20 16:14:15 +00:00
parent 9c00cc7484
commit b941450e03
2 changed files with 40 additions and 2 deletions

View file

@ -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)

View file

@ -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()