Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(vet): Run rules against a managed database
  • Loading branch information
kyleconroy committed Sep 21, 2023
commit 8dfde57dafa749c47961614aa6196730ecc51625
16 changes: 1 addition & 15 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,6 @@ jobs:
runs-on: ubuntu-latest

services:
postgres:
image: "postgres:15"
env:
POSTGRES_DB: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
ports:
- 5432:5432
# needed because the postgres container does not provide a healthcheck
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
mysql:
image: "mysql/mysql-server:8.0"
env:
Expand Down Expand Up @@ -69,17 +59,13 @@ jobs:
- name: test ./...
run: gotestsum --junitfile junit.xml -- --tags=examples ./...
env:
PG_USER: postgres
PG_HOST: localhost
PG_DATABASE: postgres
PG_PASSWORD: postgres
PG_PORT: ${{ job.services.postgres.ports['5432'] }}
MYSQL_DATABASE: mysql
MYSQL_HOST: localhost
MYSQL_PORT: ${{ job.services.mysql.ports['3306'] }}
MYSQL_ROOT_PASSWORD: mysecretpassword
CI_SQLC_PROJECT_ID: ${{ secrets.CI_SQLC_PROJECT_ID }}
CI_SQLC_AUTH_TOKEN: ${{ secrets.CI_SQLC_AUTH_TOKEN }}
SQLC_AUTH_TOKEN: ${{ secrets.CI_SQLC_AUTH_TOKEN }}

- name: build internal/endtoend
run: go build ./...
Expand Down
4 changes: 3 additions & 1 deletion examples/authors/sqlc.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
version: '2'
cloud:
project: "01HAQMMECEYQYKFJN8MP16QC41"
sql:
- schema: postgresql/schema.sql
queries: postgresql/query.sql
engine: postgresql
database:
uri: postgresql://${PG_USER}:${PG_PASSWORD}@${PG_HOST}:${PG_PORT}/authors
managed: true
rules:
- sqlc/db-prepare
- postgresql-query-too-costly
Expand Down
5 changes: 4 additions & 1 deletion examples/batch/sqlc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"version": "1",
"cloud": {
"project": "01HAQMMECEYQYKFJN8MP16QC41"
},
"packages": [
{
"path": "postgresql",
Expand All @@ -8,7 +11,7 @@
"queries": "postgresql/query.sql",
"engine": "postgresql",
"database": {
"uri": "postgresql://${PG_USER}:${PG_PASSWORD}@${PG_HOST}:${PG_PORT}/batch"
"managed": true
},
"rules": [
"sqlc/db-prepare"
Expand Down
5 changes: 4 additions & 1 deletion examples/booktest/sqlc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"version": "1",
"cloud": {
"project": "01HAQMMECEYQYKFJN8MP16QC41"
},
"packages": [
{
"name": "booktest",
Expand All @@ -8,7 +11,7 @@
"queries": "postgresql/query.sql",
"engine": "postgresql",
"database": {
"uri": "postgresql://${PG_USER}:${PG_PASSWORD}@${PG_HOST}:${PG_PORT}/booktest"
"managed": true
},
"rules": [
"sqlc/db-prepare"
Expand Down
5 changes: 4 additions & 1 deletion examples/jets/sqlc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"version": "1",
"cloud": {
"project": "01HAQMMECEYQYKFJN8MP16QC41"
},
"packages": [
{
"path": "postgresql",
Expand All @@ -8,7 +11,7 @@
"queries": "postgresql/query-building.sql",
"engine": "postgresql",
"database": {
"uri": "postgresql://${PG_USER}:${PG_PASSWORD}@${PG_HOST}:${PG_PORT}/jets"
"managed": true
},
"rules": [
"sqlc/db-prepare"
Expand Down
5 changes: 4 additions & 1 deletion examples/ondeck/sqlc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"version": "1",
"cloud": {
"project": "01HAQMMECEYQYKFJN8MP16QC41"
},
"packages": [
{
"path": "postgresql",
Expand All @@ -8,7 +11,7 @@
"queries": "postgresql/query",
"engine": "postgresql",
"database": {
"uri": "postgresql://${PG_USER}:${PG_PASSWORD}@${PG_HOST}:${PG_PORT}/ondeck"
"managed": true
},
"rules": [
"sqlc/db-prepare"
Expand Down
4 changes: 4 additions & 0 deletions internal/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func Do(args []string, stdin io.Reader, stdout io.Writer, stderr io.Writer) int
rootCmd.PersistentFlags().StringP("file", "f", "", "specify an alternate config file (default: sqlc.yaml)")
rootCmd.PersistentFlags().BoolP("experimental", "x", false, "DEPRECATED: enable experimental features (default: false)")
rootCmd.PersistentFlags().Bool("no-remote", false, "disable remote execution (default: false)")
rootCmd.PersistentFlags().Bool("remote", false, "enable remote execution (default: false)")
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Now that vet is also using the project configuration, we can't default to remote generation. Instead, allow users to opt in with the --remote flag.

rootCmd.PersistentFlags().Bool("no-database", false, "disable database connections (default: false)")

rootCmd.AddCommand(checkCmd)
Expand Down Expand Up @@ -136,17 +137,20 @@ var initCmd = &cobra.Command{
type Env struct {
DryRun bool
Debug opts.Debug
Remote bool
NoRemote bool
NoDatabase bool
}

func ParseEnv(c *cobra.Command) Env {
dr := c.Flag("dry-run")
r := c.Flag("remote")
nr := c.Flag("no-remote")
nodb := c.Flag("no-database")
return Env{
DryRun: dr != nil && dr.Changed,
Debug: opts.DebugFromEnv(),
Remote: r != nil && nr.Value.String() == "true",
NoRemote: nr != nil && nr.Value.String() == "true",
NoDatabase: nodb != nil && nodb.Value.String() == "true",
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func Generate(ctx context.Context, e Env, dir, filename string, stderr io.Writer
return nil, err
}

if conf.Cloud.Project != "" && !e.NoRemote {
if conf.Cloud.Project != "" && e.Remote && !e.NoRemote {
return remoteGenerate(ctx, configPath, conf, dir, stderr)
}

Expand Down
4 changes: 1 addition & 3 deletions internal/cmd/vet.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,10 +415,9 @@ func (c *checker) fetchDatabaseUri(ctx context.Context, s config.SQL) (string, f
migrations = append(migrations, string(contents))
}

start := time.Now()
resp, err := c.Client.CreateEphemeralDatabase(ctx, &pb.CreateEphemeralDatabaseRequest{
Engine: "postgresql",
Region: "sjc",
Region: quickdb.GetClosestRegion(),
Migrations: migrations,
})
if err != nil {
Expand All @@ -432,7 +431,6 @@ func (c *checker) fetchDatabaseUri(ctx context.Context, s config.SQL) (string, f
return err
}

fmt.Println("createdb+template", time.Since(start))
return resp.Uri, cleanup, nil
}

Expand Down
6 changes: 1 addition & 5 deletions internal/endtoend/vet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,8 @@ func TestExamplesVet(t *testing.T) {
path := filepath.Join(examples, tc)

if tc != "kotlin" && tc != "python" {
if s, found := findSchema(t, filepath.Join(path, "postgresql")); found {
db, cleanup := sqltest.CreatePostgreSQLDatabase(t, tc, false, []string{s})
defer db.Close()
defer cleanup()
}
if s, found := findSchema(t, filepath.Join(path, "mysql")); found {
t.Skip("local")
db, cleanup := sqltest.CreateMySQLDatabase(t, tc, []string{s})
defer db.Close()
defer cleanup()
Expand Down