@@ -28,8 +28,9 @@ func (e *Enum) isType() {
28
28
}
29
29
30
30
type CompositeType struct {
31
- Name string
32
- Comment string
31
+ Name string
32
+ ColTypeNames []* ast.TypeName
33
+ Comment string
33
34
}
34
35
35
36
func (ct * CompositeType ) isType () {
@@ -101,6 +102,16 @@ func stringSlice(list *ast.List) []string {
101
102
return items
102
103
}
103
104
105
+ func columnTypeNamesSlice (list * ast.List ) []* ast.TypeName {
106
+ items := []* ast.TypeName {}
107
+ for _ , item := range list .Items {
108
+ if n , ok := item .(* ast.ColumnDef ); ok {
109
+ items = append (items , n .TypeName )
110
+ }
111
+ }
112
+ return items
113
+ }
114
+
104
115
func (c * Catalog ) getType (rel * ast.TypeName ) (Type , int , error ) {
105
116
ns := rel .Schema
106
117
if ns == "" {
@@ -136,7 +147,8 @@ func (c *Catalog) createCompositeType(stmt *ast.CompositeTypeStmt) error {
136
147
return sqlerr .TypeExists (tbl .Name )
137
148
}
138
149
schema .Types = append (schema .Types , & CompositeType {
139
- Name : stmt .TypeName .Name ,
150
+ Name : stmt .TypeName .Name ,
151
+ ColTypeNames : columnTypeNamesSlice (stmt .ColDefList ),
140
152
})
141
153
return nil
142
154
}
@@ -277,16 +289,11 @@ func (c *Catalog) alterTypeSetSchema(stmt *ast.AlterTypeSetSchemaStmt) error {
277
289
oldSchema .Types = append (oldSchema .Types [:idx ], oldSchema .Types [idx + 1 :]... )
278
290
newSchema .Types = append (newSchema .Types , typ )
279
291
280
- // Update all the table columns with the new type
281
- for _ , schema := range c .Schemas {
282
- for _ , table := range schema .Tables {
283
- for _ , column := range table .Columns {
284
- if column .Type == oldType {
285
- column .Type .Schema = * stmt .NewSchema
286
- }
287
- }
292
+ c .updateTypeNames (func (t * ast.TypeName ) {
293
+ if * t == oldType {
294
+ t .Schema = * stmt .NewSchema
288
295
}
289
- }
296
+ })
290
297
return nil
291
298
}
292
299
@@ -343,8 +350,9 @@ func (c *Catalog) renameType(stmt *ast.RenameTypeStmt) error {
343
350
344
351
case * CompositeType :
345
352
schema .Types [idx ] = & CompositeType {
346
- Name : newName ,
347
- Comment : typ .Comment ,
353
+ Name : newName ,
354
+ ColTypeNames : typ .ColTypeNames ,
355
+ Comment : typ .Comment ,
348
356
}
349
357
350
358
case * Enum :
@@ -359,16 +367,33 @@ func (c *Catalog) renameType(stmt *ast.RenameTypeStmt) error {
359
367
360
368
}
361
369
362
- // Update all the table columns with the new type
370
+ c .updateTypeNames (func (t * ast.TypeName ) {
371
+ if * t == * stmt .Type {
372
+ t .Name = newName
373
+ }
374
+ })
375
+
376
+ return nil
377
+ }
378
+
379
+ func (c * Catalog ) updateTypeNames (typeUpdater func (t * ast.TypeName )) error {
363
380
for _ , schema := range c .Schemas {
381
+ // Update all the table columns with the new type
364
382
for _ , table := range schema .Tables {
365
383
for _ , column := range table .Columns {
366
- if column .Type == * stmt .Type {
367
- column .Type .Name = newName
368
- }
384
+ typeUpdater (& column .Type )
385
+ }
386
+ }
387
+ // Update all the composite fields with the new type
388
+ for _ , typ := range schema .Types {
389
+ composite , ok := typ .(* CompositeType )
390
+ if ! ok {
391
+ continue
392
+ }
393
+ for _ , fieldType := range composite .ColTypeNames {
394
+ typeUpdater (fieldType )
369
395
}
370
396
}
371
397
}
372
-
373
398
return nil
374
399
}
0 commit comments