-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
758ea6e
commit 974a3f9
Showing
2 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package migrations | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/lib/pq" | ||
|
||
"pg-roll/pkg/schema" | ||
) | ||
|
||
type OpCreateIndex struct { | ||
Table string `json:"table"` | ||
Columns []string `json:"columns"` | ||
} | ||
|
||
var _ Operation = (*OpCreateIndex)(nil) | ||
|
||
func (o *OpCreateIndex) Start(ctx context.Context, conn *sql.DB, schemaName string, stateSchema string, s *schema.Schema) error { | ||
// create index concurrently | ||
_, err := conn.ExecContext(ctx, fmt.Sprintf("CREATE INDEX CONCURRENTLY IF NOT EXISTS %s ON %s.%s (%s)", | ||
pq.QuoteIdentifier(IndexName(o.Table, o.Columns)), | ||
pq.QuoteIdentifier(schemaName), | ||
pq.QuoteIdentifier(o.Table), | ||
strings.Join(quoteColumnNames(o.Columns), ", "))) | ||
return err | ||
} | ||
|
||
func (o *OpCreateIndex) Complete(ctx context.Context, conn *sql.DB) error { | ||
// No-op | ||
return nil | ||
} | ||
|
||
func (o *OpCreateIndex) Rollback(ctx context.Context, conn *sql.DB) error { | ||
// drop the index concurrently | ||
_, err := conn.ExecContext(ctx, fmt.Sprintf("DROP INDEX CONCURRENTLY IF EXISTS %s", | ||
IndexName(o.Table, o.Columns))) | ||
|
||
return err | ||
} | ||
|
||
func (o *OpCreateIndex) Validate(ctx context.Context, s *schema.Schema) error { | ||
table := s.GetTable(o.Table) | ||
|
||
if table == nil { | ||
return TableDoesNotExistError{Name: o.Table} | ||
} | ||
|
||
for _, column := range o.Columns { | ||
if table.GetColumn(column) == nil { | ||
return ColumnDoesNotExistError{Table: o.Table, Name: column} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func IndexName(table string, columns []string) string { | ||
return "_pgroll_idx_" + table + "_" + strings.Join(columns, "_") | ||
} | ||
|
||
func quoteColumnNames(columns []string) (quoted []string) { | ||
for _, col := range columns { | ||
quoted = append(quoted, pq.QuoteIdentifier(col)) | ||
} | ||
return quoted | ||
} |