From 8b608f6f9868309697883fb53bc6c8a79819127d Mon Sep 17 00:00:00 2001 From: xsadia Date: Wed, 11 Oct 2023 11:21:32 -0300 Subject: [PATCH 1/5] feat(generate): add support for .yml file extension --- .github/ISSUE_TEMPLATE/BUG_REPORT.yml | 2 +- docs/reference/config.md | 112 +++++++++---------- docs/tutorials/getting-started-mysql.md | 2 +- docs/tutorials/getting-started-postgresql.md | 4 +- docs/tutorials/getting-started-sqlite.md | 2 +- internal/cmd/generate.go | 26 +++-- internal/endtoend/ddl_test.go | 2 +- internal/endtoend/endtoend_test.go | 4 +- scripts/regenerate/main.go | 2 +- 9 files changed, 83 insertions(+), 73 deletions(-) diff --git a/.github/ISSUE_TEMPLATE/BUG_REPORT.yml b/.github/ISSUE_TEMPLATE/BUG_REPORT.yml index 8fb024f174..4ded0ee603 100644 --- a/.github/ISSUE_TEMPLATE/BUG_REPORT.yml +++ b/.github/ISSUE_TEMPLATE/BUG_REPORT.yml @@ -51,7 +51,7 @@ body: id: config attributes: label: Configuration - description: Please include the sqlc.yaml or sqlc.json file you using in your project. This will be automatically formatted, so no need for backticks. + description: Please include the sqlc.(yaml|yml) or sqlc.json file you using in your project. This will be automatically formatted, so no need for backticks. render: yaml - type: input id: playground diff --git a/docs/reference/config.md b/docs/reference/config.md index d4a28db612..2acedb15fb 100644 --- a/docs/reference/config.md +++ b/docs/reference/config.md @@ -1,6 +1,6 @@ # Configuration -The `sqlc` tool is configured via a `sqlc.yaml` or `sqlc.json` file. This +The `sqlc` tool is configured via a `sqlc.(yaml|yml)` or `sqlc.json` file. This file must be in the directory where the `sqlc` command is run. ## Version 2 @@ -61,24 +61,24 @@ The `codegen` mapping supports the following keys: - A mapping of plugin-specific options. ```yaml -version: '2' +version: "2" plugins: -- name: py - wasm: - url: https://github.com/sqlc-dev/sqlc-gen-python/releases/download/v0.16.0-alpha/sqlc-gen-python.wasm - sha256: 428476c7408fd4c032da4ec74e8a7344f4fa75e0f98a5a3302f238283b9b95f2 + - name: py + wasm: + url: https://github.com/sqlc-dev/sqlc-gen-python/releases/download/v0.16.0-alpha/sqlc-gen-python.wasm + sha256: 428476c7408fd4c032da4ec74e8a7344f4fa75e0f98a5a3302f238283b9b95f2 sql: -- schema: "schema.sql" - queries: "query.sql" - engine: postgresql - codegen: - - out: src/authors - plugin: py - options: - package: authors - emit_sync_querier: true - emit_async_querier: true - query_parameter_limit: 5 + - schema: "schema.sql" + queries: "query.sql" + engine: postgresql + codegen: + - out: src/authors + plugin: py + options: + package: authors + emit_sync_querier: true + emit_async_querier: true + query_parameter_limit: 5 ``` ### database @@ -93,19 +93,19 @@ syntax. In the following example, the connection string will have the value of the `PG_PASSWORD` environment variable set as its password. ```yaml -version: '2' +version: "2" sql: -- schema: schema.sql - queries: query.sql - engine: postgresql - database: - uri: postgresql://postgres:${PG_PASSWORD}@localhost:5432/authors - gen: - go: - package: authors - out: postgresql + - schema: schema.sql + queries: query.sql + engine: postgresql + database: + uri: postgresql://postgres:${PG_PASSWORD}@localhost:5432/authors + gen: + go: + package: authors + out: postgresql ``` - + ### gen The `gen` mapping supports the following keys: @@ -255,19 +255,19 @@ Each mapping in the `plugins` collection has the following keys: - The URL to fetch the WASM file. Supports the `https://` or `file://` schemes. - `sha256` - The SHA256 checksum for the downloaded file. - + ```yaml version: "2" plugins: -- name: "py" - wasm: - url: "https://github.com/sqlc-dev/sqlc-gen-python/releases/download/v0.16.0-alpha/sqlc-gen-python.wasm" - sha256: "428476c7408fd4c032da4ec74e8a7344f4fa75e0f98a5a3302f238283b9b95f2" -- name: "js" - env: - - PATH - process: - cmd: "sqlc-gen-json" + - name: "py" + wasm: + url: "https://github.com/sqlc-dev/sqlc-gen-python/releases/download/v0.16.0-alpha/sqlc-gen-python.wasm" + sha256: "428476c7408fd4c032da4ec74e8a7344f4fa75e0f98a5a3302f238283b9b95f2" + - name: "js" + env: + - PATH + process: + cmd: "sqlc-gen-json" ``` ### rules @@ -283,7 +283,7 @@ Each mapping in the `rules` collection has the following keys: See the [vet](../howto/vet.md) documentation for a list of built-in rules and help writing custom rules. - + ```yaml version: "2" sql: @@ -317,11 +317,11 @@ rules: rule: | query.cmd == "exec" ``` - + ### global overrides Sometimes, the same configuration must be done across various specifications of -code generation. Then a global definition for type overriding and field +code generation. Then a global definition for type overriding and field renaming can be done using the `overrides` mapping the following manner: ```yaml @@ -339,27 +339,27 @@ overrides: package: "null" type: "Time" sql: -- schema: "postgresql/schema.sql" - queries: "postgresql/query.sql" - engine: "postgresql" - gen: - go: - package: "authors" - out: "postgresql" -- schema: "mysql/schema.sql" - queries: "mysql/query.sql" - engine: "mysql" - gen: - go: - package: "authors" - out: "mysql" + - schema: "postgresql/schema.sql" + queries: "postgresql/query.sql" + engine: "postgresql" + gen: + go: + package: "authors" + out: "postgresql" + - schema: "mysql/schema.sql" + queries: "mysql/query.sql" + engine: "mysql" + gen: + go: + package: "authors" + out: "mysql" ``` With the previous configuration, whenever a struct field is generated from a table column that is called `id`, it will generated as `Identifier`. Also, whenever there is a nullable `timestamp with time zone` column in a -Postgres table, it will be generated as `null.Time`. Note that the mapping for +Postgres table, it will be generated as `null.Time`. Note that the mapping for global type overrides has a field called `engine` that is absent in the regular type overrides. This field is only used when there are multiple definitions using multiple engines. Otherwise, the value of the `engine` key diff --git a/docs/tutorials/getting-started-mysql.md b/docs/tutorials/getting-started-mysql.md index 2f86a7bd39..4fa55d713c 100644 --- a/docs/tutorials/getting-started-mysql.md +++ b/docs/tutorials/getting-started-mysql.md @@ -11,7 +11,7 @@ Initialize a new Go module named `tutorial.sql.dev/app` go mod init tutorial.sqlc.dev/app ``` -sqlc looks for either a `sqlc.yaml` or `sqlc.json` file in the current +sqlc looks for either a `sqlc.(yaml|yml)` or `sqlc.json` file in the current directory. In our new directory, create a file named `sqlc.yaml` with the following contents: diff --git a/docs/tutorials/getting-started-postgresql.md b/docs/tutorials/getting-started-postgresql.md index bc761be004..9f7d09a1a4 100644 --- a/docs/tutorials/getting-started-postgresql.md +++ b/docs/tutorials/getting-started-postgresql.md @@ -1,4 +1,4 @@ -# Getting started with PostgreSQL +# Getting started with PostgreSQL This tutorial assumes that the latest version of sqlc is [installed](../overview/install.md) and ready to use. @@ -11,7 +11,7 @@ Initialize a new Go module named `tutorial.sqlc.dev/app` go mod init tutorial.sqlc.dev/app ``` -sqlc looks for either a `sqlc.yaml` or `sqlc.json` file in the current +sqlc looks for either a `sqlc.(yaml|yml)` or `sqlc.json` file in the current directory. In our new directory, create a file named `sqlc.yaml` with the following contents: diff --git a/docs/tutorials/getting-started-sqlite.md b/docs/tutorials/getting-started-sqlite.md index 96bb693e23..28f0669ea3 100644 --- a/docs/tutorials/getting-started-sqlite.md +++ b/docs/tutorials/getting-started-sqlite.md @@ -11,7 +11,7 @@ Initialize a new Go module named `tutorial.sql.dev/app` go mod init tutorial.sqlc.dev/app ``` -sqlc looks for either a `sqlc.yaml` or `sqlc.json` file in the current +sqlc looks for either a `sqlc.(yaml|yml)` or `sqlc.json` file in the current directory. In our new directory, create a file named `sqlc.yaml` with the following contents: diff --git a/internal/cmd/generate.go b/internal/cmd/generate.go index b479670e5d..b1c8d756b7 100644 --- a/internal/cmd/generate.go +++ b/internal/cmd/generate.go @@ -76,8 +76,9 @@ func readConfig(stderr io.Writer, dir, filename string) (string, *config.Config, if filename != "" { configPath = filepath.Join(dir, filename) } else { - var yamlMissing, jsonMissing bool + var yamlMissing, jsonMissing, ymlMissing bool yamlPath := filepath.Join(dir, "sqlc.yaml") + ymlPath := filepath.Join(dir, "sqlc.yml") jsonPath := filepath.Join(dir, "sqlc.json") if _, err := os.Stat(yamlPath); os.IsNotExist(err) { @@ -87,18 +88,27 @@ func readConfig(stderr io.Writer, dir, filename string) (string, *config.Config, jsonMissing = true } - if yamlMissing && jsonMissing { - fmt.Fprintln(stderr, "error parsing configuration files. sqlc.yaml or sqlc.json: file does not exist") + if _, err := os.Stat(ymlPath); os.IsNotExist(err) { + ymlMissing = true + } + + if yamlMissing && ymlMissing && jsonMissing { + fmt.Fprintln(stderr, "error parsing configuration files. sqlc.(yaml|yml) or sqlc.json: file does not exist") return "", nil, errors.New("config file missing") } - if !yamlMissing && !jsonMissing { - fmt.Fprintln(stderr, "error: both sqlc.json and sqlc.yaml files present") - return "", nil, errors.New("sqlc.json and sqlc.yaml present") + if !yamlMissing && !ymlMissing && !jsonMissing { + fmt.Fprintln(stderr, "error: both sqlc.json and sqlc.(yaml|yml) files present") + return "", nil, errors.New("sqlc.json and sqlc.(yaml|yml) present") } - configPath = yamlPath - if yamlMissing { + if jsonMissing { + if yamlMissing { + configPath = ymlPath + } else { + configPath = yamlPath + } + } else { configPath = jsonPath } } diff --git a/internal/endtoend/ddl_test.go b/internal/endtoend/ddl_test.go index 39aaa0292e..2ead5841c8 100644 --- a/internal/endtoend/ddl_test.go +++ b/internal/endtoend/ddl_test.go @@ -39,7 +39,7 @@ func TestValidSchema(t *testing.T) { if err != nil { return err } - if filepath.Base(path) == "sqlc.json" || filepath.Base(path) == "sqlc.yaml" { + if filepath.Base(path) == "sqlc.json" || filepath.Base(path) == "sqlc.yaml" || filepath.Base(path) == "sqlc.yml" { stderr := filepath.Join(filepath.Dir(path), "stderr.txt") if _, err := os.Stat(stderr); !os.IsNotExist(err) { return nil diff --git a/internal/endtoend/endtoend_test.go b/internal/endtoend/endtoend_test.go index e91e9f9c56..236b37d7ad 100644 --- a/internal/endtoend/endtoend_test.go +++ b/internal/endtoend/endtoend_test.go @@ -86,7 +86,7 @@ func TestReplay(t *testing.T) { if err != nil { return err } - if info.Name() == "sqlc.json" || info.Name() == "sqlc.yaml" { + if info.Name() == "sqlc.json" || info.Name() == "sqlc.yaml" || info.Name() == "sqlc.yml" { dirs = append(dirs, filepath.Dir(path)) return filepath.SkipDir } @@ -251,7 +251,7 @@ func BenchmarkReplay(b *testing.B) { if err != nil { return err } - if info.Name() == "sqlc.json" || info.Name() == "sqlc.yaml" { + if info.Name() == "sqlc.json" || info.Name() == "sqlc.yaml" || info.Name() == "sqlc.yml" { dirs = append(dirs, filepath.Dir(path)) return filepath.SkipDir } diff --git a/scripts/regenerate/main.go b/scripts/regenerate/main.go index b2bcb2089f..c1a578b62d 100644 --- a/scripts/regenerate/main.go +++ b/scripts/regenerate/main.go @@ -39,7 +39,7 @@ func regenerate(dir string) error { if info.IsDir() { return nil } - if strings.HasSuffix(path, "sqlc.json") || strings.HasSuffix(path, "sqlc.yaml") { + if strings.HasSuffix(path, "sqlc.json") || strings.HasSuffix(path, "sqlc.yaml") || strings.HasSuffix(path, "sqlc.yml") { cwd := filepath.Dir(path) command, err := parseExecCommand(cwd) if err != nil { From 0acbec0b8de918bb26d1af535bec2e12ae0112db Mon Sep 17 00:00:00 2001 From: xsadia Date: Wed, 11 Oct 2023 11:26:10 -0300 Subject: [PATCH 2/5] fix: rollback lint --- docs/reference/config.md | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/docs/reference/config.md b/docs/reference/config.md index 2acedb15fb..2c8ccf1b4f 100644 --- a/docs/reference/config.md +++ b/docs/reference/config.md @@ -8,24 +8,24 @@ file must be in the directory where the `sqlc` command is run. ```yaml version: "2" sql: -- schema: "postgresql/schema.sql" - queries: "postgresql/query.sql" - engine: "postgresql" - gen: - go: - package: "authors" - out: "postgresql" - database: - uri: "postgresql://postgres:postgres@localhost:5432/postgres" - rules: - - sqlc/db-prepare -- schema: "mysql/schema.sql" - queries: "mysql/query.sql" - engine: "mysql" - gen: - go: - package: "authors" - out: "mysql" + - schema: "postgresql/schema.sql" + queries: "postgresql/query.sql" + engine: "postgresql" + gen: + go: + package: "authors" + out: "postgresql" + database: + uri: "postgresql://postgres:postgres@localhost:5432/postgres" + rules: + - sqlc/db-prepare + - schema: "mysql/schema.sql" + queries: "mysql/query.sql" + engine: "mysql" + gen: + go: + package: "authors" + out: "mysql" ``` ### sql From 436af332625f98f158bb21f5fba3de4411f926b1 Mon Sep 17 00:00:00 2001 From: xsadia Date: Wed, 11 Oct 2023 11:27:58 -0300 Subject: [PATCH 3/5] fix: formatting --- docs/reference/config.md | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/docs/reference/config.md b/docs/reference/config.md index 2c8ccf1b4f..2acedb15fb 100644 --- a/docs/reference/config.md +++ b/docs/reference/config.md @@ -8,24 +8,24 @@ file must be in the directory where the `sqlc` command is run. ```yaml version: "2" sql: - - schema: "postgresql/schema.sql" - queries: "postgresql/query.sql" - engine: "postgresql" - gen: - go: - package: "authors" - out: "postgresql" - database: - uri: "postgresql://postgres:postgres@localhost:5432/postgres" - rules: - - sqlc/db-prepare - - schema: "mysql/schema.sql" - queries: "mysql/query.sql" - engine: "mysql" - gen: - go: - package: "authors" - out: "mysql" +- schema: "postgresql/schema.sql" + queries: "postgresql/query.sql" + engine: "postgresql" + gen: + go: + package: "authors" + out: "postgresql" + database: + uri: "postgresql://postgres:postgres@localhost:5432/postgres" + rules: + - sqlc/db-prepare +- schema: "mysql/schema.sql" + queries: "mysql/query.sql" + engine: "mysql" + gen: + go: + package: "authors" + out: "mysql" ``` ### sql From 92543865d3f22f003d9971cf1447fa5017fc8100 Mon Sep 17 00:00:00 2001 From: xsadia Date: Wed, 11 Oct 2023 11:29:24 -0300 Subject: [PATCH 4/5] fix: formatting --- docs/reference/config.md | 110 +++++++++++++++++++-------------------- 1 file changed, 55 insertions(+), 55 deletions(-) diff --git a/docs/reference/config.md b/docs/reference/config.md index 2acedb15fb..c0ba543503 100644 --- a/docs/reference/config.md +++ b/docs/reference/config.md @@ -61,24 +61,24 @@ The `codegen` mapping supports the following keys: - A mapping of plugin-specific options. ```yaml -version: "2" +version: '2' plugins: - - name: py - wasm: - url: https://github.com/sqlc-dev/sqlc-gen-python/releases/download/v0.16.0-alpha/sqlc-gen-python.wasm - sha256: 428476c7408fd4c032da4ec74e8a7344f4fa75e0f98a5a3302f238283b9b95f2 +- name: py + wasm: + url: https://github.com/sqlc-dev/sqlc-gen-python/releases/download/v0.16.0-alpha/sqlc-gen-python.wasm + sha256: 428476c7408fd4c032da4ec74e8a7344f4fa75e0f98a5a3302f238283b9b95f2 sql: - - schema: "schema.sql" - queries: "query.sql" - engine: postgresql - codegen: - - out: src/authors - plugin: py - options: - package: authors - emit_sync_querier: true - emit_async_querier: true - query_parameter_limit: 5 +- schema: "schema.sql" + queries: "query.sql" + engine: postgresql + codegen: + - out: src/authors + plugin: py + options: + package: authors + emit_sync_querier: true + emit_async_querier: true + query_parameter_limit: 5 ``` ### database @@ -93,19 +93,19 @@ syntax. In the following example, the connection string will have the value of the `PG_PASSWORD` environment variable set as its password. ```yaml -version: "2" +version: '2' sql: - - schema: schema.sql - queries: query.sql - engine: postgresql - database: - uri: postgresql://postgres:${PG_PASSWORD}@localhost:5432/authors - gen: - go: - package: authors - out: postgresql +- schema: schema.sql + queries: query.sql + engine: postgresql + database: + uri: postgresql://postgres:${PG_PASSWORD}@localhost:5432/authors + gen: + go: + package: authors + out: postgresql ``` - + ### gen The `gen` mapping supports the following keys: @@ -255,19 +255,19 @@ Each mapping in the `plugins` collection has the following keys: - The URL to fetch the WASM file. Supports the `https://` or `file://` schemes. - `sha256` - The SHA256 checksum for the downloaded file. - + ```yaml version: "2" plugins: - - name: "py" - wasm: - url: "https://github.com/sqlc-dev/sqlc-gen-python/releases/download/v0.16.0-alpha/sqlc-gen-python.wasm" - sha256: "428476c7408fd4c032da4ec74e8a7344f4fa75e0f98a5a3302f238283b9b95f2" - - name: "js" - env: - - PATH - process: - cmd: "sqlc-gen-json" +- name: "py" + wasm: + url: "https://github.com/sqlc-dev/sqlc-gen-python/releases/download/v0.16.0-alpha/sqlc-gen-python.wasm" + sha256: "428476c7408fd4c032da4ec74e8a7344f4fa75e0f98a5a3302f238283b9b95f2" +- name: "js" + env: + - PATH + process: + cmd: "sqlc-gen-json" ``` ### rules @@ -283,7 +283,7 @@ Each mapping in the `rules` collection has the following keys: See the [vet](../howto/vet.md) documentation for a list of built-in rules and help writing custom rules. - + ```yaml version: "2" sql: @@ -317,11 +317,11 @@ rules: rule: | query.cmd == "exec" ``` - + ### global overrides Sometimes, the same configuration must be done across various specifications of -code generation. Then a global definition for type overriding and field +code generation. Then a global definition for type overriding and field renaming can be done using the `overrides` mapping the following manner: ```yaml @@ -339,27 +339,27 @@ overrides: package: "null" type: "Time" sql: - - schema: "postgresql/schema.sql" - queries: "postgresql/query.sql" - engine: "postgresql" - gen: - go: - package: "authors" - out: "postgresql" - - schema: "mysql/schema.sql" - queries: "mysql/query.sql" - engine: "mysql" - gen: - go: - package: "authors" - out: "mysql" +- schema: "postgresql/schema.sql" + queries: "postgresql/query.sql" + engine: "postgresql" + gen: + go: + package: "authors" + out: "postgresql" +- schema: "mysql/schema.sql" + queries: "mysql/query.sql" + engine: "mysql" + gen: + go: + package: "authors" + out: "mysql" ``` With the previous configuration, whenever a struct field is generated from a table column that is called `id`, it will generated as `Identifier`. Also, whenever there is a nullable `timestamp with time zone` column in a -Postgres table, it will be generated as `null.Time`. Note that the mapping for +Postgres table, it will be generated as `null.Time`. Note that the mapping for global type overrides has a field called `engine` that is absent in the regular type overrides. This field is only used when there are multiple definitions using multiple engines. Otherwise, the value of the `engine` key From dd14568c20ae70e25d3bdbd660052c4840e3f222 Mon Sep 17 00:00:00 2001 From: Felipe Arthur Date: Wed, 11 Oct 2023 12:37:17 -0300 Subject: [PATCH 5/5] update yaml missing condition Co-authored-by: Nao Yonashiro --- internal/cmd/generate.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/cmd/generate.go b/internal/cmd/generate.go index b1c8d756b7..1e1e6e54cd 100644 --- a/internal/cmd/generate.go +++ b/internal/cmd/generate.go @@ -97,7 +97,7 @@ func readConfig(stderr io.Writer, dir, filename string) (string, *config.Config, return "", nil, errors.New("config file missing") } - if !yamlMissing && !ymlMissing && !jsonMissing { + if (!yamlMissing || !ymlMissing) && !jsonMissing { fmt.Fprintln(stderr, "error: both sqlc.json and sqlc.(yaml|yml) files present") return "", nil, errors.New("sqlc.json and sqlc.(yaml|yml) present") }