Skip to content

Releases: sqlc-dev/sqlc

v1.19.0

06 Jul 18:12
b643642
Compare
Choose a tag to compare

What's new

sqlc vet

sqlc vet runs queries through a set of lint rules.

Rules are defined in the sqlc configuration file. They consist of a name, message, and a Common Expression Language (CEL) expression. Expressions are evaluated using cel-go. If an expression evaluates to true, an error is reported using the given message.

While these examples are simplistic, they give you a flavor of the types of rules you can write.

version: 2
sql:
  - schema: "query.sql"
    queries: "query.sql"
    engine: "postgresql"
    gen:
      go:
        package: "authors"
        out: "db"
    rules:
      - no-pg
      - no-delete
      - only-one-param
      - no-exec
rules:
  - name: no-pg
    message: "invalid engine: postgresql"
    rule: |
      config.engine == "postgresql"
  - name: no-delete
    message: "don't use delete statements"
    rule: |
      query.sql.contains("DELETE")
  - name: only-one-param
    message: "too many parameters"
    rule: |
      query.params.size() > 1
  - name: no-exec
    message: "don't use exec"
    rule: |
      query.cmd == "exec"

Database connectivity

vet also marks the first time that sqlc can connect to a live, running database server. We'll expand this functionality over time, but for now it powers the sqlc/db-prepare built-in rule.

When a database is configured, the sqlc/db-prepare rule will attempt to prepare each of your queries against the connected database and report any failures.

version: 2
sql:
  - schema: "query.sql"
    queries: "query.sql"
    engine: "postgresql"
    gen:
      go:
        package: "authors"
        out: "db"
    database:
      uri: "postgresql://postgres:password@localhost:5432/postgres"
    rules:
      - sqlc/db-prepare

To see this in action, check out the authors example.

Please note that sqlc does not manage or migrate your database. Use your migration tool of choice to create the necessary database tables and objects before running sqlc vet.

Omit unused structs

Added a new configuration parameter omit_unused_structs which, when set to true, filters out table and enum structs that aren't used in queries for a given package.

Suggested CI/CD setup

With the addition of sqlc diff and sqlc vet, we encourage users to run sqlc in your CI/CD pipelines. See our suggested CI/CD setup for more information.

Simplified plugin development

The sqlc-gen-kotlin and sqlc-gen-python plugins have been updated use the upcoming WASI support in Go 1.21. Building these plugins no longer requires TinyGo.

What's Changed

Read more

v1.18.0

27 Apr 17:51
e4b1c18
Compare
Choose a tag to compare

What's New

Remote code generation

Developed by @andrewmbenton

At its core, sqlc is powered by SQL engines, which include parsers, formatters,
analyzers and more. While our goal is to support each engine on each operating
system, it's not always possible. For example, the PostgreSQL engine does not
work on Windows.

To bridge that gap, we're announcing remote code generation, currently in
private alpha. To join the private alpha, sign up for the waitlist.

To configure remote generation, configure a cloud block in sqlc.json.

{
  "version": "2",
  "cloud": {
    "organization": "<org-id>",
    "project": "<project-id>",
  },
  ...
}

You'll also need to the SQLC_AUTH_TOKEN environment variable.

export SQLC_AUTH_TOKEN=<token>

When the cloud configuration exists, sqlc generate will default to remote
generation. If you'd like to generate code locally, pass the --no-remote
option.

sqlc generate --no-remote

Remote generation is off by default and requires an opt-in to use.

sqlc.embed

Developed by @nickjackson

Embedding allows you to reuse existing model structs in more queries, resulting
in less manual serilization work. First, imagine we have the following schema
with students and test scores.

CREATE TABLE students (
  id   bigserial PRIMARY KEY,
  name text,
  age  integer
)

CREATE TABLE test_scores (
  student_id bigint,
  score integer,
  grade text
)

We want to select the student record and the highest score they got on a test.
Here's how we'd usually do that:

-- name: HighScore :many
WITH high_scores AS (
  SELECT student_id, max(score) as high_score
  FROM test_scores
  GROUP BY 1
)
SELECT students.*, high_score::integer
FROM students
JOIN high_scores ON high_scores.student_id = students.id;

When using Go, sqlc will produce a struct like this:

type HighScoreRow struct {
	ID        int64
	Name      sql.NullString
	Age       sql.NullInt32
	HighScore int32
}

With embedding, the struct will contain a model for the table instead of a
flattened list of columns.

-- name: HighScoreEmbed :many
WITH high_scores AS (
  SELECT student_id, max(score) as high_score
  FROM test_scores
  GROUP BY 1
)
SELECT sqlc.embed(students), high_score::integer
FROM students
JOIN high_scores ON high_scores.student_id = students.id;
type HighScoreRow struct {
	Student   Student
	HighScore int32
}

sqlc.slice

Developed by Paul Cameron and Jille Timmermans

The MySQL Go driver does not support passing slices to the IN operator. The
sqlc.slice function generates a dynamic query at runtime with the correct
number of parameters.

/* name: SelectStudents :many */
SELECT * FROM students 
WHERE age IN (sqlc.slice("ages"))
func (q *Queries) SelectStudents(ctx context.Context, arges []int32) ([]Student, error) {

This feature is only supported in MySQL and cannot be used with prepared
queries.

Batch operation improvements

When using batches with pgx, the error returned when a batch is closed is
exported by the generated package. This change allows for cleaner error
handling using errors.Is.

errors.Is(err, generated_package.ErrBatchAlreadyClosed)

Previously, you would have had to check match on the error message itself.

err.Error() == "batch already closed"

The generated code for batch operations always lived in batch.go. This file
name can now be configured via the output_batch_file_name configuration
option.

Configurable query parameter limits for Go

By default, sqlc will limit Go functions to a single parameter. If a query
includes more than one parameter, the generated method will use an argument
struct instead of positional arguments. This behavior can now be changed via
the query_parameter_limit configuration option. If set to 0, every
generated method will use a argument struct.

What's Changed

Read more

v1.17.2

22 Feb 18:02
140db73
Compare
Choose a tag to compare

What's Changed

New Contributors

Full Changelog: v1.17.0...v1.17.1

v1.17.1

22 Feb 17:18
fc6ed52
Compare
Choose a tag to compare
v1.17.1 Pre-release
Pre-release

Note

This release contained a build failure for Windows and will not be released. Please see v1.17.2

What's Changed

New Contributors

Full Changelog: v1.17.0...v1.17.1

v1.17.0

14 Feb 07:14
fa985d0
Compare
Choose a tag to compare

## What's Changed

New Contributors

Full Changelog: v1.16.0...v1.17.0

v1.16.0

09 Nov 04:41
89922d4
Compare
Choose a tag to compare

What's Changed

New Contributors

Full Changelog: v1.15.0...v1.16.0

v1.15.0

07 Aug 18:14
61f1758
Compare
Choose a tag to compare

What's Changed

New Contributors

Full Changelog: v1.14.0...v1.15.0

v1.14.0

10 Jun 06:14
d20ca82
Compare
Choose a tag to compare

What's Changed

New Contributors

Full Changelog: v1.13.0...v1.14.0

v1.13.0

03 Apr 02:33
e7aa4bd
Compare
Choose a tag to compare

What's Changed

New Contributors

Full Changelog: v1.12.0...v1.13.0

v1.12.0

06 Feb 05:03
45bd150
Compare
Choose a tag to compare

What's Changed

New Contributors

Full Changelog: v1.11.0...v1.12.0