Example #1
0
func TestAllocateIDs(t *testing.T) {
	defer leaktest.AfterTest(t)

	desc := sql.TableDescriptor{
		ID:       keys.MaxReservedDescID + 2,
		ParentID: keys.MaxReservedDescID + 1,
		Name:     "foo",
		Columns: []sql.ColumnDescriptor{
			{Name: "a"},
			{Name: "b"},
			{Name: "c"},
		},
		PrimaryIndex: sql.IndexDescriptor{Name: "c", ColumnNames: []string{"a", "b"}},
		Indexes: []sql.IndexDescriptor{
			{Name: "d", ColumnNames: []string{"b", "a"}},
			{Name: "e", ColumnNames: []string{"b"}},
		},
		Privileges: sql.NewDefaultPrivilegeDescriptor(),
	}
	if err := desc.AllocateIDs(); err != nil {
		t.Fatal(err)
	}

	expected := sql.TableDescriptor{
		ID:       keys.MaxReservedDescID + 2,
		ParentID: keys.MaxReservedDescID + 1,
		Version:  1,
		Name:     "foo",
		Columns: []sql.ColumnDescriptor{
			{ID: 1, Name: "a"},
			{ID: 2, Name: "b"},
			{ID: 3, Name: "c"},
		},
		PrimaryIndex: sql.IndexDescriptor{
			ID: 1, Name: "c", ColumnIDs: []sql.ColumnID{1, 2}, ColumnNames: []string{"a", "b"}},
		Indexes: []sql.IndexDescriptor{
			{ID: 2, Name: "d", ColumnIDs: []sql.ColumnID{2, 1}, ColumnNames: []string{"b", "a"}},
			{ID: 3, Name: "e", ColumnIDs: []sql.ColumnID{2}, ColumnNames: []string{"b"},
				ImplicitColumnIDs: []sql.ColumnID{1}},
		},
		Privileges:   sql.NewDefaultPrivilegeDescriptor(),
		NextColumnID: 4,
		NextIndexID:  4,
	}
	if !reflect.DeepEqual(expected, desc) {
		a, _ := json.MarshalIndent(expected, "", "  ")
		b, _ := json.MarshalIndent(desc, "", "  ")
		t.Fatalf("expected %s, but found %s", a, b)
	}

	if err := desc.AllocateIDs(); err != nil {
		t.Fatal(err)
	}
	if !reflect.DeepEqual(expected, desc) {
		a, _ := json.MarshalIndent(expected, "", "  ")
		b, _ := json.MarshalIndent(desc, "", "  ")
		t.Fatalf("expected %s, but found %s", a, b)
	}
}
Example #2
0
func TestAllocateIDs(t *testing.T) {
	defer leaktest.AfterTest(t)

	desc := sql.TableDescriptor{
		ID:   sql.MaxReservedDescID + 1,
		Name: "foo",
		Columns: []sql.ColumnDescriptor{
			{Name: "a"},
			{Name: "b"},
		},
		PrimaryIndex: sql.IndexDescriptor{Name: "c", ColumnNames: []string{"a"}},
		Indexes: []sql.IndexDescriptor{
			{Name: "d", ColumnNames: []string{"b", "a"}},
		},
		Privileges: sql.NewDefaultDatabasePrivilegeDescriptor(),
	}
	if err := desc.AllocateIDs(); err != nil {
		t.Fatal(err)
	}

	expected := sql.TableDescriptor{
		ID:   sql.MaxReservedDescID + 1,
		Name: "foo",
		Columns: []sql.ColumnDescriptor{
			{ID: 1, Name: "a"},
			{ID: 2, Name: "b"},
		},
		PrimaryIndex: sql.IndexDescriptor{ID: 1, Name: "c", ColumnIDs: []sql.ColumnID{1}, ColumnNames: []string{"a"}},
		Indexes: []sql.IndexDescriptor{
			{ID: 2, Name: "d", ColumnIDs: []sql.ColumnID{2, 1}, ColumnNames: []string{"b", "a"}},
		},
		Privileges:   sql.NewDefaultDatabasePrivilegeDescriptor(),
		NextColumnID: 3,
		NextIndexID:  3,
	}
	if !reflect.DeepEqual(expected, desc) {
		t.Fatalf("expected %+v, but found %+v", expected, desc)
	}
}
Example #3
0
func TestDropIndex(t *testing.T) {
	defer leaktest.AfterTest(t)
	s, sqlDB, kvDB := setup(t)
	defer cleanup(s, sqlDB)

	if _, err := sqlDB.Exec(`
CREATE DATABASE t;
CREATE TABLE t.kv (k CHAR PRIMARY KEY, v CHAR);
CREATE INDEX foo on t.kv (v);
INSERT INTO t.kv VALUES ('c', 'e'), ('a', 'c'), ('b', 'd');
`); err != nil {
		t.Fatal(err)
	}

	nameKey := sql.MakeNameMetadataKey(keys.MaxReservedDescID+1, "kv")
	gr, err := kvDB.Get(nameKey)
	if err != nil {
		t.Fatal(err)
	}

	if !gr.Exists() {
		t.Fatalf("Name entry %q does not exist", nameKey)
	}

	descKey := sql.MakeDescMetadataKey(sql.ID(gr.ValueInt()))
	desc := sql.TableDescriptor{}
	if err := kvDB.GetProto(descKey, &desc); err != nil {
		t.Fatal(err)
	}

	i, err := desc.FindIndexByName("foo")
	if err != nil {
		t.Fatal(err)
	}
	indexPrefix := sql.MakeIndexKeyPrefix(desc.ID, desc.Indexes[i].ID)

	indexStartKey := roachpb.Key(indexPrefix)
	indexEndKey := indexStartKey.PrefixEnd()
	if kvs, err := kvDB.Scan(indexStartKey, indexEndKey, 0); err != nil {
		t.Fatal(err)
	} else if l := 3; len(kvs) != l {
		t.Fatalf("expected %d key value pairs, but got %d", l, len(kvs))
	}

	if _, err := sqlDB.Exec(`DROP INDEX t.kv@foo`); err != nil {
		t.Fatal(err)
	}

	if kvs, err := kvDB.Scan(indexStartKey, indexEndKey, 0); err != nil {
		t.Fatal(err)
	} else if l := 0; len(kvs) != l {
		t.Fatalf("expected %d key value pairs, but got %d", l, len(kvs))
	}

	if err := kvDB.GetProto(descKey, &desc); err != nil {
		t.Fatal(err)
	} else {
		if _, err := desc.FindIndexByName("foo"); err == nil {
			t.Fatalf("table descriptor still contains index after index is dropped")
		}
		if err != nil {
			t.Fatal(err)
		}
	}
}