Skip to content

Conversation

@DeanPDX
Copy link
Contributor

@DeanPDX DeanPDX commented Jan 9, 2026

Adds support for creating/dropping table indexes. Closes #5. Closes #6.

This depends on #2 because it builds on the functionality of warnings. As part of the integration tests, we are now verifying we are getting warnings when we filter on non-indexed columns, then creating an index and verifying warnings go away. So, before reading this diff probably best to merge that PR.

Creating an index

// Create (if not exists) an index named is_checked_out_idx on our table, on the column
// is_checked_out.
if err := tbl.CreateIndex(ctx, "is_checked_out_idx", "is_checked_out", options.WithIndexIfNotExists(true)); err != nil {
	return err
}

Dropping an index

// Drop an index in our DB called is_checked_out_idx. Index names are db-specific, not table-specific.
if err := db.DropTableIndex(ctx, "is_checked_out_idx"); err != nil {
	return err
}

Unit test coverage

To make sure our structs are marshalling into proper JSON, we added tests that construct commands, marshal them into JSON, and match them to the examples in the docs. Here's an example:

// This example was taken from the documentation here:
// https://docs.datastax.com/en/astra-db-serverless/api-reference/table-index-methods/create-index.html#example-ascii
const exampleIndexASCIIPayloadJSON = `{
  "createIndex": {
    "name": "example_index_name",
    "definition": {
      "column": "example_column",
      "options": {
        "ascii": true
      }
    }
  }
}`

// TestCreateIndexASCIICommandMarshal verifies that the resulting command from createIndexCommand
// with the ascii option matches the payload in the docs.
func TestCreateIndexASCIICommandMarshal(t *testing.T) {
	cmd := createIndexCommand(getTestTable(t), "example_index_name", "example_column", options.WithAscii(true))
	// MarshalIndent and match the indentation of the example JSON
	cmdBytes, err := json.MarshalIndent(cmd, "", "  ")
	if err != nil {
		t.Fatalf("json.MarshalIndent: %v", err)
	}
	if string(cmdBytes) != exampleIndexASCIIPayloadJSON {
		t.Errorf("expected JSON:\n%s\nGot:\n%s", exampleIndexASCIIPayloadJSON, string(cmdBytes))
	}
}

Future enhancements

Probably should do a better job in the godoc of documenting that you want your index names to be unique in the DB not unique per table. Most Postgres devs would probably be familiar with this, but, we shouldn't assume familiarity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support for drop table index Support for create table index

1 participant