@@ -15,6 +15,9 @@ DROP INDEX title_idx;
15
15
ALTER TABLE distributors
16
16
ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5);
17
17
18
+ ALTER TABLE distributors
19
+ ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES addresses (address);
20
+
18
21
ALTER TABLE pgbench_accounts
19
22
ADD COLUMN test integer NOT NULL DEFAULT 0;
20
23
`
@@ -27,7 +30,10 @@ CREATE UNIQUE INDEX CONCURRENTLY title_idx ON films USING btree (title);
27
30
DROP INDEX CONCURRENTLY title_idx;
28
31
29
32
ALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5) NOT VALID;
30
- ALTER TABLE distributors VALIDATE CONSTRAINT zipchk;
33
+ BEGIN; ALTER TABLE distributors VALIDATE CONSTRAINT zipchk; COMMIT;
34
+
35
+ ALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES addresses (address) NOT VALID;
36
+ BEGIN; ALTER TABLE distributors VALIDATE CONSTRAINT distfk; COMMIT;
31
37
32
38
ALTER TABLE pgbench_accounts ADD COLUMN test int;
33
39
ALTER TABLE pgbench_accounts ALTER COLUMN test SET DEFAULT 0;
@@ -146,11 +152,11 @@ func AlterStmt(node *pg_query.Node) []*pg_query.Node {
146
152
147
153
alterStmts = append (alterStmts , node )
148
154
149
- defaultDefinitionTemp := fmt .Sprintf (`ALTER TABLE %s ALTER COLUMN %s SET DEFAULT %v;` ,
155
+ defaultDefinitionTemp := fmt .Sprintf (`alter table %s alter column %s set default %v;` ,
150
156
alterTableStmt .GetRelation ().GetRelname (), def .Colname ,
151
157
constraints [index ].GetConstraint ().GetRawExpr ().GetAConst ().GetVal ().GetInteger ().GetIval ())
152
158
153
- alterStmts = append (alterStmts , generateNode (defaultDefinitionTemp ))
159
+ alterStmts = append (alterStmts , generateNodes (defaultDefinitionTemp )... )
154
160
155
161
// TODO: Update rows
156
162
@@ -165,10 +171,10 @@ func AlterStmt(node *pg_query.Node) []*pg_query.Node {
165
171
166
172
alterStmts = append (alterStmts , node )
167
173
168
- validationTemp := fmt .Sprintf (`ALTER TABLE %s VALIDATE CONSTRAINT %s;` ,
174
+ validationTemp := fmt .Sprintf (`begin; alter table %s validate constraint %s; commit ;` ,
169
175
alterTableStmt .GetRelation ().GetRelname (), constraint .GetConname ())
170
176
171
- alterStmts = append (alterStmts , generateNode (validationTemp ))
177
+ alterStmts = append (alterStmts , generateNodes (validationTemp )... )
172
178
173
179
default :
174
180
alterStmts = append (alterStmts , node )
@@ -184,11 +190,16 @@ func AlterStmt(node *pg_query.Node) []*pg_query.Node {
184
190
return alterStmts
185
191
}
186
192
187
- func generateNode (nodeTemplate string ) * pg_query.Node {
193
+ func generateNodes (nodeTemplate string ) [] * pg_query.Node {
188
194
defDefinition , err := pg_query .Parse (nodeTemplate )
189
195
if err != nil {
190
196
log .Fatal (err )
191
197
}
192
198
193
- return defDefinition .Stmts [0 ].Stmt
199
+ nodes := []* pg_query.Node {}
200
+ for _ , rawStmt := range defDefinition .Stmts {
201
+ nodes = append (nodes , rawStmt .Stmt )
202
+ }
203
+
204
+ return nodes
194
205
}
0 commit comments