Skip to content

Commit

Permalink
Stop using pointers for OpCreateIndex fields (#565)
Browse files Browse the repository at this point in the history
Set JSON schema defaults for the optional fields in `OpCreateIndex` so
that the generated types don't use pointers.

This simplifies the use of those fields.
  • Loading branch information
andrew-farries authored Jan 2, 2025
1 parent c451b8d commit eadef66
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
14 changes: 7 additions & 7 deletions pkg/migrations/op_create_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,27 @@ func (o *OpCreateIndex) Start(ctx context.Context, conn db.DB, latestSchema stri

// create index concurrently
stmtFmt := "CREATE INDEX CONCURRENTLY %s ON %s"
if o.Unique != nil && *o.Unique {
if o.Unique {
stmtFmt = "CREATE UNIQUE INDEX CONCURRENTLY %s ON %s"
}
stmt := fmt.Sprintf(stmtFmt,
pq.QuoteIdentifier(o.Name),
pq.QuoteIdentifier(table.Name))

if o.Method != nil {
stmt += fmt.Sprintf(" USING %s", string(*o.Method))
if o.Method != "" {
stmt += fmt.Sprintf(" USING %s", string(o.Method))
}

stmt += fmt.Sprintf(" (%s)", strings.Join(
quoteColumnNames(table.PhysicalColumnNamesFor(o.Columns...)), ", "),
)

if o.StorageParameters != nil {
stmt += fmt.Sprintf(" WITH (%s)", *o.StorageParameters)
if o.StorageParameters != "" {
stmt += fmt.Sprintf(" WITH (%s)", o.StorageParameters)
}

if o.Predicate != nil {
stmt += fmt.Sprintf(" WHERE %s", *o.Predicate)
if o.Predicate != "" {
stmt += fmt.Sprintf(" WHERE %s", o.Predicate)
}

_, err := conn.ExecContext(ctx, stmt)
Expand Down
6 changes: 3 additions & 3 deletions pkg/migrations/op_create_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func TestCreateIndex(t *testing.T) {
Name: "idx_users_name_after_2019",
Table: "users",
Columns: []string{"registered_at_year"},
Predicate: ptr("registered_at_year > 2019"),
Predicate: "registered_at_year > 2019",
},
},
},
Expand Down Expand Up @@ -235,8 +235,8 @@ func TestCreateIndex(t *testing.T) {
Name: "idx_users_name_hash",
Table: "users",
Columns: []string{"name"},
Method: ptr(migrations.OpCreateIndexMethodHash),
StorageParameters: ptr("fillfactor = 70"),
Method: migrations.OpCreateIndexMethodHash,
StorageParameters: "fillfactor = 70",
},
},
},
Expand Down
8 changes: 4 additions & 4 deletions pkg/migrations/types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -238,20 +238,24 @@
},
"predicate": {
"description": "Conditional expression for defining a partial index",
"type": "string"
"type": "string",
"default": ""
},
"method": {
"description": "Index method to use for the index: btree, hash, gist, spgist, gin, brin",
"type": "string",
"enum": ["btree", "hash", "gist", "spgist", "gin", "brin"]
"enum": ["btree", "hash", "gist", "spgist", "gin", "brin"],
"default": "btree"
},
"storage_parameters": {
"description": "Storage parameters for the index",
"type": "string"
"type": "string",
"default": ""
},
"unique": {
"description": "Indicates if the index is unique",
"type": "boolean"
"type": "boolean",
"default": false
}
},
"required": ["columns", "name", "table"],
Expand Down

0 comments on commit eadef66

Please sign in to comment.