Skip to content

Commit

Permalink
Stop using pointers for fields
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-farries committed Dec 23, 2024
1 parent ac6c7cc commit d912b21
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
18 changes: 8 additions & 10 deletions pkg/sql2pgroll/create_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,39 +34,37 @@ func convertCreateIndexStmt(stmt *pgq.IndexStmt) (migrations.Operations, error)
}

// Get index uniqueness
var unique *bool
unique := false
if stmt.GetUnique() {
unique = ptr(true)
unique = true
}

// Deparse WHERE clause
var predicate *string
var predicate string
if where := stmt.GetWhereClause(); where != nil {
deparsed, err := pgq.DeparseExpr(where)
predicate, err = pgq.DeparseExpr(where)
if err != nil {
return nil, fmt.Errorf("parsing where clause: %w", err)
}
predicate = &deparsed
}

// Deparse storage parameters
var storageParams *string
var storageParams string
if len(stmt.GetOptions()) > 0 {
str, err := pgq.DeparseRelOptions(stmt.GetOptions())
storageParams, err = pgq.DeparseRelOptions(stmt.GetOptions())
if err != nil {
return nil, fmt.Errorf("parsing options: %w", err)
}
// strip outer parentheses
str = str[1 : len(str)-1]
storageParams = &str
storageParams = storageParams[1 : len(storageParams)-1]
}

return migrations.Operations{
&migrations.OpCreateIndex{
Table: tableName,
Columns: columns,
Name: stmt.GetIdxname(),
Method: &method,
Method: method,
Unique: unique,
Predicate: predicate,
StorageParameters: storageParams,
Expand Down
20 changes: 10 additions & 10 deletions pkg/sql2pgroll/expect/create_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var CreateIndexOp1 = &migrations.OpCreateIndex{
Name: "idx_name",
Table: "foo",
Columns: []string{"bar"},
Method: ptr(migrations.OpCreateIndexMethodBtree),
Method: migrations.OpCreateIndexMethodBtree,
}

func CreateIndexOp1WithMethod(method string) *migrations.OpCreateIndex {
Expand All @@ -22,46 +22,46 @@ func CreateIndexOp1WithMethod(method string) *migrations.OpCreateIndex {
Name: "idx_name",
Table: "foo",
Columns: []string{"bar"},
Method: ptr(parsed),
Method: parsed,
}
}

var CreateIndexOp2 = &migrations.OpCreateIndex{
Name: "idx_name",
Table: "schema.foo",
Columns: []string{"bar"},
Method: ptr(migrations.OpCreateIndexMethodBtree),
Method: migrations.OpCreateIndexMethodBtree,
}

var CreateIndexOp3 = &migrations.OpCreateIndex{
Name: "idx_name",
Table: "foo",
Columns: []string{"bar", "baz"},
Method: ptr(migrations.OpCreateIndexMethodBtree),
Method: migrations.OpCreateIndexMethodBtree,
}

var CreateIndexOp4 = &migrations.OpCreateIndex{
Name: "idx_name",
Table: "foo",
Columns: []string{"bar"},
Method: ptr(migrations.OpCreateIndexMethodBtree),
Unique: ptr(true),
Method: migrations.OpCreateIndexMethodBtree,
Unique: true,
}

var CreateIndexOp5 = &migrations.OpCreateIndex{
Name: "idx_name",
Table: "foo",
Columns: []string{"bar"},
Method: ptr(migrations.OpCreateIndexMethodBtree),
Predicate: ptr("foo > 0"),
Method: migrations.OpCreateIndexMethodBtree,
Predicate: "foo > 0",
}

func CreateIndexOpWithStorageParam(param string) *migrations.OpCreateIndex {
return &migrations.OpCreateIndex{
Name: "idx_name",
Table: "foo",
Columns: []string{"bar"},
Method: ptr(migrations.OpCreateIndexMethodBtree),
StorageParameters: ptr(param),
Method: migrations.OpCreateIndexMethodBtree,
StorageParameters: param,
}
}

0 comments on commit d912b21

Please sign in to comment.