sqlc icon indicating copy to clipboard operation
sqlc copied to clipboard

AFTER and BEFORE is ignored when adding values to an ENUM

Open christeredvartsen opened this issue 3 years ago • 3 comments

Version

1.15.0

What happened?

PostgreSQL supports specifying BEFORE or AFTER when adding a value to an existing ENUM, see docs. sqlc does not pick this up and simply appends the new values, so when the emit_all_enum_values configuration parameter is set to true, the order of the values returned from the All*Values() function is not correct.

I have attached a playground URL where the necessary statements and configuration can be found. If you look at the generated implementation of AllEnumTypeValues in db/models.go, you can see the order is not correct.

Given the following statements:

CREATE TYPE enum_type AS ENUM ('first', 'last');
ALTER TYPE enum_type ADD VALUE 'second' AFTER 'first';
ALTER TYPE enum_type ADD VALUE 'third' BEFORE 'last';

I would expect the AllEnumTypeValues implementation to look like this:

func AllEnumTypeValues() []EnumType {
	return []EnumType{
		EnumTypeFirst,
		EnumTypeSecond,
		EnumTypeThird,
		EnumTypeLast,
	}
}

but instead, it looks like this:

func AllEnumTypeValues() []EnumType {
	return []EnumType{
		EnumTypeFirst,
		EnumTypeLast,
		EnumTypeSecond,
		EnumTypeThird,
	}
}

Relevant log output

No response

Database schema

No response

SQL queries

No response

Configuration

No response

Playground URL

https://play.sqlc.dev/p/746548c45a823783171d5a09b59680132882e1e6786137e2e0dba3f8bcf5b1e2

What operating system are you using?

macOS

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

christeredvartsen avatar Sep 07 '22 07:09 christeredvartsen

@kyleconroy If you give me some pointers as to where in the code base to start looking I might be able to fix it myself, and provide a PR.

christeredvartsen avatar Sep 08 '22 06:09 christeredvartsen

I have been poking around in the codebase and have an idea where to start, but, I'm unable to get debugging to work. I keep getting internal compiler error: NewBulk too big: nbit=22255 count=1028433 nword=696 size=715789368 (macOS).

Any tips on how to get debugging to work I can probably figure out how to fix my initial issue.

christeredvartsen avatar Sep 08 '22 07:09 christeredvartsen

It looks like you are also affected by #1849

akutschera avatar Sep 17 '22 12:09 akutschera