-
Notifications
You must be signed in to change notification settings - Fork 75
/
alter.go
51 lines (39 loc) · 1.32 KB
/
alter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package cassandra
import (
"fmt"
"strings"
schemasv1alpha4 "github.com/schemahero/schemahero/pkg/apis/schemas/v1alpha4"
"github.com/schemahero/schemahero/pkg/database/types"
)
func AlterColumnStatements(keyspace string, tableName string, desiredColumns []*schemasv1alpha4.CassandraColumn, existingColumn *types.Column) ([]string, error) {
alterStatement := fmt.Sprintf("alter column %s", existingColumn.Name)
for _, desiredColumn := range desiredColumns {
if desiredColumn.Name == existingColumn.Name {
column, err := schemaColumnToColumn(desiredColumn)
if err != nil {
return nil, err
}
if columnsMatch(*existingColumn, *column) {
return []string{}, nil
}
changes := []string{}
if existingColumn.DataType != column.DataType {
changes = append(changes, fmt.Sprintf("%s type %s", alterStatement, column.DataType))
}
if len(changes) == 0 {
return []string{}, nil
}
return []string{fmt.Sprintf(`alter table "%s.%s" %s`, keyspace, tableName, strings.Join(changes, ", "))}, nil
}
}
return []string{fmt.Sprintf(`alter table "%s.%s" drop column %s`, keyspace, tableName, existingColumn.Name)}, nil
}
func columnsMatch(col1 types.Column, col2 types.Column) bool {
if col1.DataType != col2.DataType {
return false
}
if col1.IsStatic != col2.IsStatic {
return false
}
return true
}