From c46c90e123e8b8c8daa06748171db2e2c624a812 Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Sun, 27 Mar 2022 19:21:22 -0700 Subject: [PATCH 1/2] feat: Add sqlc version to generated Python code --- examples/python/src/authors/models.py | 2 + examples/python/src/authors/query.py | 2 + examples/python/src/booktest/models.py | 2 + examples/python/src/booktest/query.py | 2 + examples/python/src/jets/models.py | 2 + examples/python/src/jets/query-building.py | 2 + examples/python/src/ondeck/city.py | 2 + examples/python/src/ondeck/models.py | 2 + examples/python/src/ondeck/venue.py | 2 + internal/codegen/python/gen.go | 39 +++++++++---------- .../python_postgresql/python/models.py | 2 + .../python_postgresql/python/query.py | 2 + .../python_postgresql/python/models.py | 2 + .../python_postgresql/python/query.py | 2 + 14 files changed, 45 insertions(+), 20 deletions(-) diff --git a/examples/python/src/authors/models.py b/examples/python/src/authors/models.py index 9cac9b2798..d645ba374b 100644 --- a/examples/python/src/authors/models.py +++ b/examples/python/src/authors/models.py @@ -1,4 +1,6 @@ # Code generated by sqlc. DO NOT EDIT. +# versions: +# sqlc v1.12.0 import dataclasses from typing import Optional diff --git a/examples/python/src/authors/query.py b/examples/python/src/authors/query.py index d764438121..5e1afbb270 100644 --- a/examples/python/src/authors/query.py +++ b/examples/python/src/authors/query.py @@ -1,4 +1,6 @@ # Code generated by sqlc. DO NOT EDIT. +# versions: +# sqlc v1.12.0 from typing import AsyncIterator, Iterator, Optional import sqlalchemy diff --git a/examples/python/src/booktest/models.py b/examples/python/src/booktest/models.py index 4ab5c6a28b..44acf04ce0 100644 --- a/examples/python/src/booktest/models.py +++ b/examples/python/src/booktest/models.py @@ -1,4 +1,6 @@ # Code generated by sqlc. DO NOT EDIT. +# versions: +# sqlc v1.12.0 import dataclasses import datetime import enum diff --git a/examples/python/src/booktest/query.py b/examples/python/src/booktest/query.py index 20ffa65be6..a7f9da81c2 100644 --- a/examples/python/src/booktest/query.py +++ b/examples/python/src/booktest/query.py @@ -1,4 +1,6 @@ # Code generated by sqlc. DO NOT EDIT. +# versions: +# sqlc v1.12.0 import dataclasses import datetime from typing import AsyncIterator, List, Optional diff --git a/examples/python/src/jets/models.py b/examples/python/src/jets/models.py index 67e2d4bffb..40a641fb7c 100644 --- a/examples/python/src/jets/models.py +++ b/examples/python/src/jets/models.py @@ -1,4 +1,6 @@ # Code generated by sqlc. DO NOT EDIT. +# versions: +# sqlc v1.12.0 import dataclasses diff --git a/examples/python/src/jets/query-building.py b/examples/python/src/jets/query-building.py index d7afd2b8ee..4c26746756 100644 --- a/examples/python/src/jets/query-building.py +++ b/examples/python/src/jets/query-building.py @@ -1,4 +1,6 @@ # Code generated by sqlc. DO NOT EDIT. +# versions: +# sqlc v1.12.0 from typing import AsyncIterator, Optional import sqlalchemy diff --git a/examples/python/src/ondeck/city.py b/examples/python/src/ondeck/city.py index b8e3700d90..bdef26ce0d 100644 --- a/examples/python/src/ondeck/city.py +++ b/examples/python/src/ondeck/city.py @@ -1,4 +1,6 @@ # Code generated by sqlc. DO NOT EDIT. +# versions: +# sqlc v1.12.0 from typing import AsyncIterator, Optional import sqlalchemy diff --git a/examples/python/src/ondeck/models.py b/examples/python/src/ondeck/models.py index 8ea4e6835a..8d4cc789ea 100644 --- a/examples/python/src/ondeck/models.py +++ b/examples/python/src/ondeck/models.py @@ -1,4 +1,6 @@ # Code generated by sqlc. DO NOT EDIT. +# versions: +# sqlc v1.12.0 import dataclasses import datetime import enum diff --git a/examples/python/src/ondeck/venue.py b/examples/python/src/ondeck/venue.py index d51f3175b5..5813830598 100644 --- a/examples/python/src/ondeck/venue.py +++ b/examples/python/src/ondeck/venue.py @@ -1,4 +1,6 @@ # Code generated by sqlc. DO NOT EDIT. +# versions: +# sqlc v1.12.0 import dataclasses from typing import AsyncIterator, List, Optional diff --git a/internal/codegen/python/gen.go b/internal/codegen/python/gen.go index b9ff9c7a7f..9df1ca4e62 100644 --- a/internal/codegen/python/gen.go +++ b/internal/codegen/python/gen.go @@ -10,6 +10,7 @@ import ( "github.com/kyleconroy/sqlc/internal/codegen/sdk" "github.com/kyleconroy/sqlc/internal/inflection" + "github.com/kyleconroy/sqlc/internal/info" "github.com/kyleconroy/sqlc/internal/metadata" "github.com/kyleconroy/sqlc/internal/plugin" pyast "github.com/kyleconroy/sqlc/internal/python/ast" @@ -481,6 +482,22 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error) return qs, nil } +func moduleNode() *pyast.Module { + return &pyast.Module{ + Body: []*pyast.Node{ + poet.Comment( + "Code generated by sqlc. DO NOT EDIT.", + ), + poet.Comment( + "versions:", + ), + poet.Comment( + " sqlc " + info.Version, + ), + }, + } +} + func importNode(name string) *pyast.Node { return &pyast.Node{ Node: &pyast.Node_Import{ @@ -636,18 +653,7 @@ func buildImportGroup(specs map[string]importSpec) *pyast.Node { } func buildModelsTree(ctx *pyTmplCtx, i *importer) *pyast.Node { - mod := &pyast.Module{ - Body: []*pyast.Node{ - { - Node: &pyast.Node_Comment{ - Comment: &pyast.Comment{ - Text: "Code generated by sqlc. DO NOT EDIT.", - }, - }, - }, - }, - } - + mod := moduleNode() std, pkg := i.modelImportSpecs() mod.Body = append(mod.Body, buildImportGroup(std), buildImportGroup(pkg)) @@ -779,14 +785,7 @@ func asyncQuerierClassDef() *pyast.ClassDef { } func buildQueryTree(ctx *pyTmplCtx, i *importer, source string) *pyast.Node { - mod := &pyast.Module{ - Body: []*pyast.Node{ - poet.Comment( - "Code generated by sqlc. DO NOT EDIT.", - ), - }, - } - + mod := moduleNode() std, pkg := i.queryImportSpecs(source) mod.Body = append(mod.Body, buildImportGroup(std), buildImportGroup(pkg)) mod.Body = append(mod.Body, &pyast.Node{ diff --git a/internal/endtoend/testdata/exec_result/python_postgresql/python/models.py b/internal/endtoend/testdata/exec_result/python_postgresql/python/models.py index 8d9fd2fb5d..7a8ba38174 100644 --- a/internal/endtoend/testdata/exec_result/python_postgresql/python/models.py +++ b/internal/endtoend/testdata/exec_result/python_postgresql/python/models.py @@ -1,4 +1,6 @@ # Code generated by sqlc. DO NOT EDIT. +# versions: +# sqlc v1.12.0 import dataclasses diff --git a/internal/endtoend/testdata/exec_result/python_postgresql/python/query.py b/internal/endtoend/testdata/exec_result/python_postgresql/python/query.py index 1de523d8e7..97af1e062f 100644 --- a/internal/endtoend/testdata/exec_result/python_postgresql/python/query.py +++ b/internal/endtoend/testdata/exec_result/python_postgresql/python/query.py @@ -1,4 +1,6 @@ # Code generated by sqlc. DO NOT EDIT. +# versions: +# sqlc v1.12.0 import sqlalchemy import sqlalchemy.ext.asyncio diff --git a/internal/endtoend/testdata/exec_rows/python_postgresql/python/models.py b/internal/endtoend/testdata/exec_rows/python_postgresql/python/models.py index 8d9fd2fb5d..7a8ba38174 100644 --- a/internal/endtoend/testdata/exec_rows/python_postgresql/python/models.py +++ b/internal/endtoend/testdata/exec_rows/python_postgresql/python/models.py @@ -1,4 +1,6 @@ # Code generated by sqlc. DO NOT EDIT. +# versions: +# sqlc v1.12.0 import dataclasses diff --git a/internal/endtoend/testdata/exec_rows/python_postgresql/python/query.py b/internal/endtoend/testdata/exec_rows/python_postgresql/python/query.py index fef4752418..e1d6aad822 100644 --- a/internal/endtoend/testdata/exec_rows/python_postgresql/python/query.py +++ b/internal/endtoend/testdata/exec_rows/python_postgresql/python/query.py @@ -1,4 +1,6 @@ # Code generated by sqlc. DO NOT EDIT. +# versions: +# sqlc v1.12.0 import sqlalchemy import sqlalchemy.ext.asyncio From e900343e0d2097bbdc4f9b59cd81fadab44e1258 Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Sun, 27 Mar 2022 19:26:21 -0700 Subject: [PATCH 2/2] feat: Add source file to Python code comments --- examples/python/src/authors/query.py | 1 + examples/python/src/booktest/query.py | 1 + examples/python/src/jets/query-building.py | 1 + examples/python/src/ondeck/city.py | 1 + examples/python/src/ondeck/venue.py | 1 + internal/codegen/python/gen.go | 16 ++++++++++++---- .../python_postgresql/python/query.py | 1 + .../exec_rows/python_postgresql/python/query.py | 1 + 8 files changed, 19 insertions(+), 4 deletions(-) diff --git a/examples/python/src/authors/query.py b/examples/python/src/authors/query.py index 5e1afbb270..72862d2da6 100644 --- a/examples/python/src/authors/query.py +++ b/examples/python/src/authors/query.py @@ -1,6 +1,7 @@ # Code generated by sqlc. DO NOT EDIT. # versions: # sqlc v1.12.0 +# source: query.sql from typing import AsyncIterator, Iterator, Optional import sqlalchemy diff --git a/examples/python/src/booktest/query.py b/examples/python/src/booktest/query.py index a7f9da81c2..557a256b0d 100644 --- a/examples/python/src/booktest/query.py +++ b/examples/python/src/booktest/query.py @@ -1,6 +1,7 @@ # Code generated by sqlc. DO NOT EDIT. # versions: # sqlc v1.12.0 +# source: query.sql import dataclasses import datetime from typing import AsyncIterator, List, Optional diff --git a/examples/python/src/jets/query-building.py b/examples/python/src/jets/query-building.py index 4c26746756..8754985d18 100644 --- a/examples/python/src/jets/query-building.py +++ b/examples/python/src/jets/query-building.py @@ -1,6 +1,7 @@ # Code generated by sqlc. DO NOT EDIT. # versions: # sqlc v1.12.0 +# source: query-building.sql from typing import AsyncIterator, Optional import sqlalchemy diff --git a/examples/python/src/ondeck/city.py b/examples/python/src/ondeck/city.py index bdef26ce0d..3f029b554f 100644 --- a/examples/python/src/ondeck/city.py +++ b/examples/python/src/ondeck/city.py @@ -1,6 +1,7 @@ # Code generated by sqlc. DO NOT EDIT. # versions: # sqlc v1.12.0 +# source: city.sql from typing import AsyncIterator, Optional import sqlalchemy diff --git a/examples/python/src/ondeck/venue.py b/examples/python/src/ondeck/venue.py index 5813830598..390d3a7b3c 100644 --- a/examples/python/src/ondeck/venue.py +++ b/examples/python/src/ondeck/venue.py @@ -1,6 +1,7 @@ # Code generated by sqlc. DO NOT EDIT. # versions: # sqlc v1.12.0 +# source: venue.sql import dataclasses from typing import AsyncIterator, List, Optional diff --git a/internal/codegen/python/gen.go b/internal/codegen/python/gen.go index 9df1ca4e62..eea575b9ac 100644 --- a/internal/codegen/python/gen.go +++ b/internal/codegen/python/gen.go @@ -482,8 +482,8 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error) return qs, nil } -func moduleNode() *pyast.Module { - return &pyast.Module{ +func moduleNode(source string) *pyast.Module { + mod := &pyast.Module{ Body: []*pyast.Node{ poet.Comment( "Code generated by sqlc. DO NOT EDIT.", @@ -496,6 +496,14 @@ func moduleNode() *pyast.Module { ), }, } + if source != "" { + mod.Body = append(mod.Body, + poet.Comment( + "source: "+source, + ), + ) + } + return mod } func importNode(name string) *pyast.Node { @@ -653,7 +661,7 @@ func buildImportGroup(specs map[string]importSpec) *pyast.Node { } func buildModelsTree(ctx *pyTmplCtx, i *importer) *pyast.Node { - mod := moduleNode() + mod := moduleNode("") std, pkg := i.modelImportSpecs() mod.Body = append(mod.Body, buildImportGroup(std), buildImportGroup(pkg)) @@ -785,7 +793,7 @@ func asyncQuerierClassDef() *pyast.ClassDef { } func buildQueryTree(ctx *pyTmplCtx, i *importer, source string) *pyast.Node { - mod := moduleNode() + mod := moduleNode(source) std, pkg := i.queryImportSpecs(source) mod.Body = append(mod.Body, buildImportGroup(std), buildImportGroup(pkg)) mod.Body = append(mod.Body, &pyast.Node{ diff --git a/internal/endtoend/testdata/exec_result/python_postgresql/python/query.py b/internal/endtoend/testdata/exec_result/python_postgresql/python/query.py index 97af1e062f..f2a094f968 100644 --- a/internal/endtoend/testdata/exec_result/python_postgresql/python/query.py +++ b/internal/endtoend/testdata/exec_result/python_postgresql/python/query.py @@ -1,6 +1,7 @@ # Code generated by sqlc. DO NOT EDIT. # versions: # sqlc v1.12.0 +# source: query.sql import sqlalchemy import sqlalchemy.ext.asyncio diff --git a/internal/endtoend/testdata/exec_rows/python_postgresql/python/query.py b/internal/endtoend/testdata/exec_rows/python_postgresql/python/query.py index e1d6aad822..9689ec189b 100644 --- a/internal/endtoend/testdata/exec_rows/python_postgresql/python/query.py +++ b/internal/endtoend/testdata/exec_rows/python_postgresql/python/query.py @@ -1,6 +1,7 @@ # Code generated by sqlc. DO NOT EDIT. # versions: # sqlc v1.12.0 +# source: query.sql import sqlalchemy import sqlalchemy.ext.asyncio