6
6
"context"
7
7
"fmt"
8
8
9
- "github.com/lib/pq"
10
-
11
9
"github.com/xataio/pgroll/pkg/db"
12
10
"github.com/xataio/pgroll/pkg/schema"
13
11
)
@@ -26,9 +24,39 @@ func (o *OpSetForeignKey) Start(ctx context.Context, l Logger, conn db.DB, lates
26
24
l .LogOperationStart (o )
27
25
28
26
table := s .GetTable (o .Table )
27
+ if table == nil {
28
+ return nil , TableDoesNotExistError {Name : o .Table }
29
+ }
30
+ column := table .GetColumn (o .Column )
31
+ if column == nil {
32
+ return nil , ColumnDoesNotExistError {Table : o .Table , Name : o .Column }
33
+ }
34
+ referencedTable := s .GetTable (o .References .Table )
35
+ if referencedTable == nil {
36
+ return nil , TableDoesNotExistError {Name : o .References .Table }
37
+ }
38
+
39
+ referencedColumn := referencedTable .GetColumn (o .References .Column )
40
+ if referencedColumn == nil {
41
+ return nil , ColumnDoesNotExistError {Table : o .References .Table , Name : o .References .Column }
42
+ }
29
43
30
44
// Create a NOT VALID foreign key constraint on the new column.
31
- if err := o .addForeignKeyConstraint (ctx , conn , s ); err != nil {
45
+ if err := NewCreateFKConstraintAction (conn ,
46
+ o .Table ,
47
+ o .References .Name ,
48
+ []string {o .Column },
49
+ & TableForeignKeyReference {
50
+ Table : o .References .Table ,
51
+ Columns : []string {o .References .Column },
52
+ MatchType : o .References .MatchType ,
53
+ OnDelete : o .References .OnDelete ,
54
+ OnUpdate : o .References .OnUpdate ,
55
+ },
56
+ o .References .InitiallyDeferred ,
57
+ o .References .Deferrable ,
58
+ true ,
59
+ ).Execute (ctx ); err != nil {
32
60
return nil , fmt .Errorf ("failed to add foreign key constraint: %w" , err )
33
61
}
34
62
@@ -84,40 +112,3 @@ func (o *OpSetForeignKey) Validate(ctx context.Context, s *schema.Schema) error
84
112
85
113
return nil
86
114
}
87
-
88
- func (o * OpSetForeignKey ) addForeignKeyConstraint (ctx context.Context , conn db.DB , s * schema.Schema ) error {
89
- table := s .GetTable (o .Table )
90
- if table == nil {
91
- return TableDoesNotExistError {Name : o .Table }
92
- }
93
- column := table .GetColumn (o .Column )
94
- if column == nil {
95
- return ColumnDoesNotExistError {Table : o .Table , Name : o .Column }
96
- }
97
- referencedTable := s .GetTable (o .References .Table )
98
- if referencedTable == nil {
99
- return TableDoesNotExistError {Name : o .References .Table }
100
- }
101
-
102
- referencedColumn := referencedTable .GetColumn (o .References .Column )
103
- if referencedColumn == nil {
104
- return ColumnDoesNotExistError {Table : o .References .Table , Name : o .References .Column }
105
- }
106
-
107
- sql := fmt .Sprintf ("ALTER TABLE %s ADD " , pq .QuoteIdentifier (table .Name ))
108
- writer := & ConstraintSQLWriter {
109
- Name : o .References .Name ,
110
- Columns : []string {column .Name },
111
- SkipValidation : true ,
112
- }
113
- sql += writer .WriteForeignKey (
114
- referencedTable .Name ,
115
- []string {referencedColumn .Name },
116
- o .References .OnDelete ,
117
- o .References .OnUpdate ,
118
- nil ,
119
- o .References .MatchType )
120
-
121
- _ , err := conn .ExecContext (ctx , sql )
122
- return err
123
- }
0 commit comments