forked from Morabaraba/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
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
3c2e42a
commit 2da240f
Showing
15 changed files
with
588 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package action | ||
|
||
import ( | ||
"encoding/gob" | ||
"fmt" | ||
) | ||
|
||
type AlterAttribute struct { | ||
SchemaName string | ||
TypeName string | ||
Column Column | ||
NewColumn Column | ||
} | ||
|
||
// Register type for gob | ||
func init() { | ||
gob.Register(&AlterAttribute{}) | ||
} | ||
|
||
func (a *AlterAttribute) Execute(c *Context) error { | ||
if a.Column.Name != a.NewColumn.Name { | ||
_, err := c.Tx.Exec( | ||
fmt.Sprintf( | ||
"ALTER TYPE \"%s\".\"%s\" RENAME ATTRIBUTE \"%s\" TO \"%s\";", | ||
a.SchemaName, | ||
a.TypeName, | ||
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 TYPE \"%s\".\"%s\" ALTER ATTRIBUTE \"%s\" TYPE %s\"%s\";", | ||
a.SchemaName, | ||
a.TypeName, | ||
a.NewColumn.Name, | ||
a.NewColumn.GetTypeSchemaStr(a.SchemaName), | ||
a.NewColumn.Type, | ||
), | ||
) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (a *AlterAttribute) Filter(targetExpression string) bool { | ||
return true | ||
} | ||
|
||
func (a *AlterAttribute) NeedsSeparatedBatch() bool { | ||
return false | ||
} |
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,40 @@ | ||
package action | ||
|
||
import ( | ||
"encoding/gob" | ||
"fmt" | ||
) | ||
|
||
type CreateAttribute struct { | ||
SchemaName string | ||
TypeName string | ||
Column Column | ||
} | ||
|
||
// Register type for gob | ||
func init() { | ||
gob.Register(&CreateAttribute{}) | ||
} | ||
|
||
func (a *CreateAttribute) Execute(c *Context) error { | ||
_, err := c.Tx.Exec( | ||
fmt.Sprintf( | ||
"ALTER TYPE \"%s\".\"%s\" ADD ATTRIBUTE \"%s\" %s\"%s\";", | ||
a.SchemaName, | ||
a.TypeName, | ||
a.Column.Name, | ||
a.Column.GetTypeSchemaStr(a.SchemaName), | ||
a.Column.Type, | ||
), | ||
) | ||
|
||
return err | ||
} | ||
|
||
func (a *CreateAttribute) Filter(targetExpression string) bool { | ||
return true | ||
} | ||
|
||
func (a *CreateAttribute) NeedsSeparatedBatch() bool { | ||
return false | ||
} |
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,33 @@ | ||
package action | ||
|
||
import ( | ||
"encoding/gob" | ||
"fmt" | ||
) | ||
|
||
type DropAttribute struct { | ||
SchemaName string | ||
TypeName string | ||
Column Column | ||
} | ||
|
||
// Register type for gob | ||
func init() { | ||
gob.Register(&DropAttribute{}) | ||
} | ||
|
||
func (a *DropAttribute) Execute(c *Context) error { | ||
_, err := c.Tx.Exec( | ||
fmt.Sprintf("ALTER TYPE \"%s\".\"%s\" DROP ATTRIBUTE \"%s\";", a.SchemaName, a.TypeName, a.Column.Name), | ||
) | ||
|
||
return err | ||
} | ||
|
||
func (a *DropAttribute) Filter(targetExpression string) bool { | ||
return true | ||
} | ||
|
||
func (a *DropAttribute) NeedsSeparatedBatch() bool { | ||
return false | ||
} |
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
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,88 @@ | ||
package database | ||
|
||
import ( | ||
"github.com/pagarme/teleport/action" | ||
"github.com/pagarme/teleport/batcher/ddldiff" | ||
) | ||
|
||
// Define a class column | ||
type Attribute struct { | ||
Name string `json:"attr_name"` | ||
Num int `json:"attr_num"` | ||
TypeName string `json:"type_name"` | ||
TypeSchema string `json:"type_schema"` | ||
TypeOid string `json:"type_oid"` | ||
Type *Type | ||
} | ||
|
||
func (a *Attribute) IsNativeType() bool { | ||
return a.TypeSchema == "pg_catalog" | ||
} | ||
|
||
// Implements Diffable | ||
func (post *Attribute) Diff(other ddldiff.Diffable, context ddldiff.Context) []action.Action { | ||
actions := make([]action.Action, 0) | ||
|
||
if other == nil { | ||
actions = append(actions, &action.CreateAttribute{ | ||
context.Schema, | ||
post.Type.Name, | ||
action.Column{ | ||
post.Name, | ||
post.TypeName, | ||
post.IsNativeType(), | ||
}, | ||
}) | ||
} else { | ||
pre := other.(*Attribute) | ||
|
||
if pre.Name != post.Name || pre.TypeOid != post.TypeOid { | ||
actions = append(actions, &action.AlterAttribute{ | ||
context.Schema, | ||
post.Type.Name, | ||
action.Column{ | ||
pre.Name, | ||
pre.TypeName, | ||
pre.IsNativeType(), | ||
}, | ||
action.Column{ | ||
post.Name, | ||
post.TypeName, | ||
post.IsNativeType(), | ||
}, | ||
}) | ||
} | ||
} | ||
|
||
return actions | ||
} | ||
|
||
func (a *Attribute) Children() []ddldiff.Diffable { | ||
return []ddldiff.Diffable{} | ||
} | ||
|
||
func (a *Attribute) Drop(context ddldiff.Context) []action.Action { | ||
return []action.Action{ | ||
&action.DropAttribute{ | ||
context.Schema, | ||
a.Type.Name, | ||
action.Column{ | ||
a.Name, | ||
a.TypeName, | ||
a.IsNativeType(), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (a *Attribute) IsEqual(other ddldiff.Diffable) bool { | ||
if other == nil { | ||
return false | ||
} | ||
|
||
if otherAttr, ok := other.(*Attribute); ok { | ||
return (a.Num == otherAttr.Num) | ||
} | ||
|
||
return false | ||
} |
Oops, something went wrong.