Skip to content

Commit

Permalink
Merge pull request #77 from schemahero/unique-fixtures
Browse files Browse the repository at this point in the history
Index comparison
  • Loading branch information
marccampbell committed Aug 14, 2019
2 parents 30304b2 + 42fb2ae commit 84dfada
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 25 deletions.
6 changes: 1 addition & 5 deletions pkg/database/mysql/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ func AddIndexStatement(tableName string, schemaIndex *schemasv1alpha2.SQLTableIn
name = types.GenerateIndexName(tableName, schemaIndex)
}

return fmt.Sprintf("create %sindex %s on %s (%s)",
unique,
name,
tableName,
strings.Join(schemaIndex.Columns, ", "))
return fmt.Sprintf("create %sindex %s on %s (%s)", unique, name, tableName, strings.Join(schemaIndex.Columns, ", "))
}

func RenameIndexStatement(tableName string, index *types.Index, schemaIndex *schemasv1alpha2.SQLTableIndex) string {
Expand Down
15 changes: 4 additions & 11 deletions pkg/database/postgres/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,6 @@ func DeployPostgresTable(uri string, tableName string, postgresTableSchema *sche
return err
}

if postgresTableSchema.Indexes != nil {
for _, index := range postgresTableSchema.Indexes {
createIndex := AddIndexStatement(tableName, index)

fmt.Printf("Executing query: %q\n", createIndex)
_, err := p.db.Exec(query)
if err != nil {
return err
}
}
}
return nil
}

Expand Down Expand Up @@ -271,6 +260,10 @@ func buildIndexStatements(p *PostgresConnection, tableName string, postgresTable
}

for _, index := range postgresTableSchema.Indexes {
if index.Name == "" {
index.Name = types.GenerateIndexName(tableName, index)
}

var statement string
var matchedIndex *types.Index
for _, currentIndex := range currentIndexes {
Expand Down
9 changes: 2 additions & 7 deletions pkg/database/postgres/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import (
)

func RemoveIndexStatement(tableName string, index *types.Index) string {
return fmt.Sprintf(
"drop index %s",
pq.QuoteIdentifier(index.Name))
return fmt.Sprintf("drop index %s", pq.QuoteIdentifier(index.Name))
}

func AddIndexStatement(tableName string, schemaIndex *schemasv1alpha2.SQLTableIndex) string {
Expand All @@ -34,8 +32,5 @@ func AddIndexStatement(tableName string, schemaIndex *schemasv1alpha2.SQLTableIn
}

func RenameIndexStatement(tableName string, index *types.Index, schemaIndex *schemasv1alpha2.SQLTableIndex) string {
return fmt.Sprintf(
"alter index %s rename to %s",
pq.QuoteIdentifier(index.Name),
pq.QuoteIdentifier(schemaIndex.Name))
return fmt.Sprintf("alter index %s rename to %s", pq.QuoteIdentifier(index.Name), pq.QuoteIdentifier(schemaIndex.Name))
}
25 changes: 23 additions & 2 deletions pkg/database/types/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,30 @@ type Index struct {
}

func (idx *Index) Equals(other *Index) bool {
// TODO
if idx.Name != other.Name {
return false
}

if idx.IsUnique != other.IsUnique {
return false
}

if len(idx.Columns) != len(other.Columns) {
return false
}

for _, otherColumn := range other.Columns {
for _, col := range idx.Columns {
if col == otherColumn {
goto NextColumn
}
}

return false
return false

NextColumn:
}
return true
}

func IndexToSchemaIndex(index *Index) *schemasv1alpha2.SQLTableIndex {
Expand Down

0 comments on commit 84dfada

Please sign in to comment.