sqlc icon indicating copy to clipboard operation
sqlc copied to clipboard

rename for uuid to UUID not working

Open pushkar-anand opened this issue 3 years ago • 3 comments

Version

1.14.0

What happened?

I have a column name uuid in my tables with UUID type. By default sqlc generates the struct field name as Uuid.

I have added a rename as follows:

        rename:
          # Override the field name for column "uuid". By default, sqlc is generating it as "Uuid".
          uuid: "UUID"

Expected: The struct field name should be UUID. Actual: The struct field name should is Uuid.

Relevant log output

No response

Database schema

CREATE TABLE authors (
  id   BIGSERIAL PRIMARY KEY,
  uuid UUID NOT NULL,
  name text      NOT NULL,
  bio  text
);

SQL queries

-- name: GetAuthor :one
SELECT * FROM authors
WHERE id = $1 LIMIT 1;

-- name: ListAuthors :many
SELECT * FROM authors
ORDER BY name;

-- name: CreateAuthor :one
INSERT INTO authors (
  name, bio
) VALUES (
  $1, $2
)
RETURNING *;

-- name: DeleteAuthor :exec
DELETE FROM authors
WHERE id = $1;

Configuration

{
  "version": 2,
  "sql": [
    {
      "schema": "query.sql",
      "queries": "query.sql",
      "engine": "postgresql",
      "gen": {
        "go": {
          "package": "models",
          "out": "./models",
          "output_db_file_name": "db_gen.go",
          "output_files_suffix": "_gen",
          "sql_package": "pgx/v4",
          "emit_db_tags": true,
          "emit_prepared_queries": true,
          "emit_interface": true,
          "emit_empty_slices": false,
          "emit_json_tags": true,
          "json_tags_case_style": "snake",
          "emit_result_struct_pointers": true,
          "emit_enum_valid_method": true,
          "emit_all_enum_values": true,
          "rename": {
            "uuid": "UUID"
          },
          "overrides": [
            {
              "db_type": "uuid",
              "go_type": "string"
            }
          ]
        }
      }
    }
  ]
}

Playground URL

https://play.sqlc.dev/p/f61aa5a0842d25dbb45cac7aea3926561172dc282c8d61fc487d3d7611b924cd

What operating system are you using?

macOS

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

pushkar-anand avatar Jun 29 '22 14:06 pushkar-anand

I see this behavior, too, with v1.14.0.

jkakar avatar Jul 21 '22 23:07 jkakar

It seems that config files of version 1 and version 2 put the the rename configuration in different spots of the config. The following test fails when added to config_test.go:

const v2WithRename = `
{
    "version": "2",
    "sql": [
      {
	"engine": "postgresql",
	"gen": {
	    "go": {
		"out": "./models",
		"rename": {
		      "uuid": "UUID"
		}
	    }
	}
      }
    ]
}
`

const v1WithRename = `
{
	"version": "1",
	"packages": [
	{
		"engine": "postgresql",
		"path": "./models",
	}
	],
	"rename": {
		"uuid": "UUID"
	}
}
`

func TestConfigVersion1And2(t *testing.T) {
	config1, err := ParseConfig(strings.NewReader(v1WithRename))
	if err != nil {
		t.Log(err)
		t.FailNow()
	}
	config2, err := ParseConfig(strings.NewReader(v2WithRename))
	if err != nil {
		t.Log(err)
		t.FailNow()
	}
	if diff := cmp.Diff(config1, config2, cmpopts.IgnoreFields(Config{}, "Version")); diff != "" {
		t.Errorf("differed (-want +got):\n%s", diff)
	}
}

I would expect the two test configs to refer to the same configuration, but you can see that the rename config is in a different place. Right now, only the v1 file configuration is used when renaming.

    config_test.go:242: differed (-want +got):
          config.Config{
          	... // 1 ignored field
          	Project: {},
          	SQL: []config.SQL{
          		{
          			... // 4 identical fields
          			Gen: config.SQLGen{
          				Go: &config.SQLGo{
          					... // 16 identical fields
        - 					Rename:           nil,
        + 					Rename:           map[string]string{"uuid": "UUID"},
          					... // 5 identical fields
          				},
          				Kotlin: nil,
          				Python: nil,
          				JSON:   nil,
          			},
          			Codegen: nil,
          		},
          	},
          	Gen: config.Gen{
        - 		Go:     &config.GenGo{Rename: map[string]string{"uuid": "UUID"}},
        + 		Go:     nil,
          		Kotlin: nil,
          	},
          	Plugins: nil,
          }

@kyleconroy I am willing to put together a PR. I just don't know what to fix:

  • should the config struct be the same for version 1 and version 2 (i.e. should the test above be green?)
  • if so, what is the expected location for the rename mapping?
  • should I keep the config as is and just get the mapping from the current location when putting together a CombinedSetting?

akutschera avatar Jul 22 '22 14:07 akutschera

The configuration options don't need to be changed. Version 2 of the configuration files supports per-package renames and global renames.

The bug is that we aren't pulling in the rename settings from per-package setting.

kyleconroy avatar Aug 29 '22 03:08 kyleconroy

This appears to be resolved now. Running the original playground example with v1.20.0 produces the correct UUID struct field name: https://play.sqlc.dev/p/b20feb6dd6693d01bc0955496c5591679ad6f08c89aaf1d66aea8423a562afab

andrewmbenton avatar Aug 30 '23 17:08 andrewmbenton