@@ -225,6 +225,29 @@ func Update(c *pg.Catalog, stmt nodes.Node) error {
225225 }
226226 }
227227
228+ case nodes.CompositeTypeStmt :
229+ fqn , err := ParseRange (n .Typevar )
230+ if err != nil {
231+ return err
232+ }
233+ schema , exists := c .Schemas [fqn .Schema ]
234+ if ! exists {
235+ return wrap (pg .ErrorSchemaDoesNotExist (fqn .Schema ), raw .StmtLocation )
236+ }
237+ // Because tables have associated data types, the type name must also
238+ // be distinct from the name of any existing table in the same
239+ // schema.
240+ // https://www.postgresql.org/docs/current/sql-createtype.html
241+ if _ , exists := schema .Tables [fqn .Rel ]; exists {
242+ return wrap (pg .ErrorRelationAlreadyExists (fqn .Rel ), raw .StmtLocation )
243+ }
244+ if _ , exists := schema .Types [fqn .Rel ]; exists {
245+ return wrap (pg .ErrorRelationAlreadyExists (fqn .Rel ), raw .StmtLocation )
246+ }
247+ schema .Types [fqn .Rel ] = pg.CompositeType {
248+ Name : fqn .Rel ,
249+ }
250+
228251 case nodes.CreateStmt :
229252 fqn , err := ParseRange (n .Relation )
230253 if err != nil {
@@ -264,10 +287,17 @@ func Update(c *pg.Catalog, stmt nodes.Node) error {
264287 if ! exists {
265288 return wrap (pg .ErrorSchemaDoesNotExist (fqn .Schema ), raw .StmtLocation )
266289 }
267- if _ , exists := schema .Enums [fqn .Rel ]; exists {
290+ // Because tables have associated data types, the type name must also
291+ // be distinct from the name of any existing table in the same
292+ // schema.
293+ // https://www.postgresql.org/docs/current/sql-createtype.html
294+ if _ , exists := schema .Tables [fqn .Rel ]; exists {
295+ return wrap (pg .ErrorRelationAlreadyExists (fqn .Rel ), raw .StmtLocation )
296+ }
297+ if _ , exists := schema .Types [fqn .Rel ]; exists {
268298 return wrap (pg .ErrorTypeAlreadyExists (fqn .Rel ), raw .StmtLocation )
269299 }
270- schema .Enums [fqn .Rel ] = pg.Enum {
300+ schema .Types [fqn .Rel ] = pg.Enum {
271301 Name : fqn .Rel ,
272302 Vals : stringSlice (n .Vals ),
273303 }
@@ -311,8 +341,8 @@ func Update(c *pg.Catalog, stmt nodes.Node) error {
311341 }
312342
313343 case nodes .OBJECT_TYPE :
314- if _ , exists := schema .Enums [fqn .Rel ]; exists {
315- delete (schema .Enums , fqn .Rel )
344+ if _ , exists := schema .Types [fqn .Rel ]; exists {
345+ delete (schema .Types , fqn .Rel )
316346 } else if ! n .MissingOk {
317347 return wrap (pg .ErrorTypeDoesNotExist (fqn .Rel ), raw .StmtLocation )
318348 }
@@ -507,16 +537,19 @@ func Update(c *pg.Catalog, stmt nodes.Node) error {
507537 if ! exists {
508538 return wrap (pg .ErrorSchemaDoesNotExist (fqn .Schema ), raw .StmtLocation )
509539 }
510- enum , exists := schema .Enums [fqn .Rel ]
540+ typ , exists := schema .Types [fqn .Rel ]
511541 if ! exists {
512542 return wrap (pg .ErrorRelationDoesNotExist (fqn .Rel ), raw .StmtLocation )
513543 }
514- if n .Comment != nil {
515- enum .Comment = * n .Comment
516- } else {
517- enum .Comment = ""
544+ switch t := typ .(type ) {
545+ case pg.Enum :
546+ if n .Comment != nil {
547+ t .Comment = * n .Comment
548+ } else {
549+ t .Comment = ""
550+ }
551+ schema .Types [fqn .Rel ] = t
518552 }
519- schema .Enums [fqn .Rel ] = enum
520553
521554 }
522555
0 commit comments