Skip to content

feat(postgres): add copyfrom support - #92

Open
kellen-miller wants to merge 5 commits into
sqlc-dev:mainfrom
kellen-miller:add-copyfrom
Open

feat(postgres): add copyfrom support#92
kellen-miller wants to merge 5 commits into
sqlc-dev:mainfrom
kellen-miller:add-copyfrom

Conversation

@kellen-miller

@kellen-miller kellen-miller commented Aug 23, 2025

Copy link
Copy Markdown

Closes #63

@kellen-miller kellen-miller changed the title Add copyfrom Aug 23, 2025

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements support for the :copyfrom command in sqlc-gen-python, enabling efficient bulk insert operations. The implementation leverages SQLAlchemy's executemany behavior by passing lists of parameter dictionaries to Connection.execute().

Key Changes:

  • Added :copyfrom command support with automatic handling of both struct and non-struct parameters
  • Removed the previous error that blocked CopyFrom usage
  • Added comprehensive test coverage and examples demonstrating both sync and async usage

Reviewed changes

Copilot reviewed 44 out of 45 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
internal/gen.go Core implementation: adds BuildCopyFromBody, addCopyFromArgs, and buildStructToDictList methods; removes CopyFrom error; adds switch cases for :copyfrom in sync/async queriers
internal/imports.go Adds import logic for typing.List and typing.Any types needed by copyfrom methods
internal/endtoend/testdata/copyfrom/* New end-to-end test case with schema, queries, and expected generated code covering multiple copyfrom scenarios
examples/src/authors/query.sql Adds example CreateAuthorsBatch copyfrom query
examples/src/authors/query.py Generated code demonstrating copyfrom method with List[Any] parameter
examples/src/tests/test_authors.py Test cases validating both sync and async copyfrom functionality
README.md Documentation explaining copyfrom usage with examples for both List[Any] and typed struct parameters
internal/endtoend/testdata/*/sqlc.yaml Updated sha256 hashes for new WASM binary version
internal/endtoend/testdata//python/.py Updated generated file version comments from v1.28.0 to v1.29.0
.gitignore Adds .idea/ IDE directory to gitignore

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread internal/gen.go
Comment on lines +251 to +254
body = append(body, assignNode(targetVar, poet.Node(&pyast.Call{
Func: poet.Name("list"),
Args: []*pyast.Node{},
})))

Copilot AI Nov 24, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using list() to create an empty list is less efficient than using a list literal []. In Python, [] is more idiomatic and has better performance since it doesn't require a function call. Consider updating the code generation to use [] instead of list().

Copilot uses AI. Check for mistakes.

@kellen-miller kellen-miller Dec 3, 2025

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m not sure if the custom AST/printer that generates the python supports [] without adding a new node/printing logic. It currently emits list() for empty lists so I’ve left list() for now. Happy to change if the generator can do literals though.

@kellen-miller kellen-miller changed the title Add :copyfrom support Mar 26, 2026
@kellen-miller kellen-miller changed the title feat: add pg copyfrom support Mar 26, 2026
@devstein

Copy link
Copy Markdown
Collaborator

Hey @kellen-miller - will try take a look at this soon. Feel free to ping me if you don't get a review in the next week or so!

@kellen-miller

Copy link
Copy Markdown
Author

Hey @devstein have you had a chance to review this pr?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants