forked from Morabaraba/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaltercolumn.go
63 lines (53 loc) · 1.07 KB
/
altercolumn.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
52
53
54
55
56
57
58
59
60
61
62
63
package action
import (
"encoding/gob"
"fmt"
)
type AlterColumn struct {
SchemaName string
TableName string
Column Column
NewColumn Column
}
// Register type for gob
func init() {
gob.Register(&AlterColumn{})
}
func (a *AlterColumn) Execute(c *Context) error {
if a.Column.Name != a.NewColumn.Name {
_, err := c.Tx.Exec(
fmt.Sprintf(
"ALTER TABLE \"%s\".\"%s\" RENAME COLUMN \"%s\" TO \"%s\";",
a.SchemaName,
a.TableName,
a.Column.Name,
a.NewColumn.Name,
),
)
if err != nil {
return err
}
}
if a.Column.Type != a.NewColumn.Type {
_, err := c.Tx.Exec(
fmt.Sprintf(
"ALTER TABLE \"%s\".\"%s\" ALTER COLUMN \"%s\" TYPE %s\"%s\";",
a.SchemaName,
a.TableName,
a.NewColumn.Name,
a.NewColumn.GetTypeSchemaStr(a.SchemaName),
a.NewColumn.Type,
),
)
if err != nil {
return err
}
}
return nil
}
func (a *AlterColumn) Filter(targetExpression string) bool {
return IsInTargetExpression(&targetExpression, &a.SchemaName, &a.TableName)
}
func (a *AlterColumn) NeedsSeparatedBatch() bool {
return false
}