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..72862d2da6 100644 --- a/examples/python/src/authors/query.py +++ b/examples/python/src/authors/query.py @@ -1,4 +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/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..557a256b0d 100644 --- a/examples/python/src/booktest/query.py +++ b/examples/python/src/booktest/query.py @@ -1,4 +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/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..8754985d18 100644 --- a/examples/python/src/jets/query-building.py +++ b/examples/python/src/jets/query-building.py @@ -1,4 +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 b8e3700d90..3f029b554f 100644 --- a/examples/python/src/ondeck/city.py +++ b/examples/python/src/ondeck/city.py @@ -1,4 +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/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..390d3a7b3c 100644 --- a/examples/python/src/ondeck/venue.py +++ b/examples/python/src/ondeck/venue.py @@ -1,4 +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 b9ff9c7a7f..eea575b9ac 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,30 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error) return qs, nil } +func moduleNode(source string) *pyast.Module { + mod := &pyast.Module{ + Body: []*pyast.Node{ + poet.Comment( + "Code generated by sqlc. DO NOT EDIT.", + ), + poet.Comment( + "versions:", + ), + poet.Comment( + " sqlc " + info.Version, + ), + }, + } + if source != "" { + mod.Body = append(mod.Body, + poet.Comment( + "source: "+source, + ), + ) + } + return mod +} + func importNode(name string) *pyast.Node { return &pyast.Node{ Node: &pyast.Node_Import{ @@ -636,18 +661,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 +793,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(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/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..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,4 +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/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..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,4 +1,7 @@ # Code generated by sqlc. DO NOT EDIT. +# versions: +# sqlc v1.12.0 +# source: query.sql import sqlalchemy import sqlalchemy.ext.asyncio