(constraint *pgquery.Constraint, tableName parser.TableName)
| 1015 | } |
| 1016 | |
| 1017 | func (p PostgresParser) parseConstraint(constraint *pgquery.Constraint, tableName parser.TableName) (parser.Statement, error) { |
| 1018 | switch constraint.Contype { |
| 1019 | case pgquery.ConstrType_CONSTR_UNIQUE: |
| 1020 | cols := util.TransformSlice(constraint.Keys, func(key *pgquery.Node) parser.IndexColumn { |
| 1021 | return parser.IndexColumn{ |
| 1022 | Column: parser.NewIdent(key.Node.(*pgquery.Node_String_).String_.Sval, false), |
| 1023 | Direction: "asc", |
| 1024 | } |
| 1025 | }) |
| 1026 | return &parser.DDL{ |
| 1027 | Action: parser.AddIndex, |
| 1028 | Table: tableName, |
| 1029 | NewName: tableName, |
| 1030 | IndexSpec: &parser.IndexSpec{ |
| 1031 | Name: parser.NewIdent(constraint.Conname, false), |
| 1032 | Constraint: true, |
| 1033 | Unique: true, |
| 1034 | ConstraintOptions: &parser.ConstraintOptions{ |
| 1035 | Deferrable: constraint.Deferrable, |
| 1036 | InitiallyDeferred: constraint.Initdeferred, |
| 1037 | }, |
| 1038 | }, |
| 1039 | IndexCols: cols, |
| 1040 | }, nil |
| 1041 | case pgquery.ConstrType_CONSTR_FOREIGN: |
| 1042 | fk, err := p.parseForeignKey(constraint) |
| 1043 | if err != nil { |
| 1044 | return nil, err |
| 1045 | } |
| 1046 | return &parser.DDL{ |
| 1047 | Action: parser.AddForeignKey, |
| 1048 | Table: tableName, |
| 1049 | NewName: tableName, |
| 1050 | ForeignKey: fk, |
| 1051 | }, nil |
| 1052 | case pgquery.ConstrType_CONSTR_EXCLUSION: |
| 1053 | ex, err := p.parseExclusion(constraint) |
| 1054 | if err != nil { |
| 1055 | return nil, err |
| 1056 | } |
| 1057 | return &parser.DDL{ |
| 1058 | Action: parser.AddExclusion, |
| 1059 | Table: tableName, |
| 1060 | NewName: tableName, |
| 1061 | Exclusion: ex, |
| 1062 | }, nil |
| 1063 | default: |
| 1064 | return nil, fmt.Errorf("unhandled constraint type in parseAlterTableStmt: %d", constraint.Contype) |
| 1065 | } |
| 1066 | } |
| 1067 | |
| 1068 | func (p PostgresParser) parseExclusion(constraint *pgquery.Constraint) (*parser.ExclusionDefinition, error) { |
| 1069 | var exs []parser.ExclusionPair |
no test coverage detected