Skip to content

Commit 8dfde57

Browse files
committed
feat(vet): Run rules against a managed database
1 parent 0d8d73d commit 8dfde57

File tree

10 files changed

+27
-29
lines changed

10 files changed

+27
-29
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,6 @@ jobs:
3232
runs-on: ubuntu-latest
3333

3434
services:
35-
postgres:
36-
image: "postgres:15"
37-
env:
38-
POSTGRES_DB: postgres
39-
POSTGRES_PASSWORD: postgres
40-
POSTGRES_USER: postgres
41-
ports:
42-
- 5432:5432
43-
# needed because the postgres container does not provide a healthcheck
44-
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
4535
mysql:
4636
image: "mysql/mysql-server:8.0"
4737
env:
@@ -69,17 +59,13 @@ jobs:
6959
- name: test ./...
7060
run: gotestsum --junitfile junit.xml -- --tags=examples ./...
7161
env:
72-
PG_USER: postgres
73-
PG_HOST: localhost
74-
PG_DATABASE: postgres
75-
PG_PASSWORD: postgres
76-
PG_PORT: ${{ job.services.postgres.ports['5432'] }}
7762
MYSQL_DATABASE: mysql
7863
MYSQL_HOST: localhost
7964
MYSQL_PORT: ${{ job.services.mysql.ports['3306'] }}
8065
MYSQL_ROOT_PASSWORD: mysecretpassword
8166
CI_SQLC_PROJECT_ID: ${{ secrets.CI_SQLC_PROJECT_ID }}
8267
CI_SQLC_AUTH_TOKEN: ${{ secrets.CI_SQLC_AUTH_TOKEN }}
68+
SQLC_AUTH_TOKEN: ${{ secrets.CI_SQLC_AUTH_TOKEN }}
8369

8470
- name: build internal/endtoend
8571
run: go build ./...

examples/authors/sqlc.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
version: '2'
2+
cloud:
3+
project: "01HAQMMECEYQYKFJN8MP16QC41"
24
sql:
35
- schema: postgresql/schema.sql
46
queries: postgresql/query.sql
57
engine: postgresql
68
database:
7-
uri: postgresql://${PG_USER}:${PG_PASSWORD}@${PG_HOST}:${PG_PORT}/authors
9+
managed: true
810
rules:
911
- sqlc/db-prepare
1012
- postgresql-query-too-costly

examples/batch/sqlc.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"version": "1",
3+
"cloud": {
4+
"project": "01HAQMMECEYQYKFJN8MP16QC41"
5+
},
36
"packages": [
47
{
58
"path": "postgresql",
@@ -8,7 +11,7 @@
811
"queries": "postgresql/query.sql",
912
"engine": "postgresql",
1013
"database": {
11-
"uri": "postgresql://${PG_USER}:${PG_PASSWORD}@${PG_HOST}:${PG_PORT}/batch"
14+
"managed": true
1215
},
1316
"rules": [
1417
"sqlc/db-prepare"

examples/booktest/sqlc.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"version": "1",
3+
"cloud": {
4+
"project": "01HAQMMECEYQYKFJN8MP16QC41"
5+
},
36
"packages": [
47
{
58
"name": "booktest",
@@ -8,7 +11,7 @@
811
"queries": "postgresql/query.sql",
912
"engine": "postgresql",
1013
"database": {
11-
"uri": "postgresql://${PG_USER}:${PG_PASSWORD}@${PG_HOST}:${PG_PORT}/booktest"
14+
"managed": true
1215
},
1316
"rules": [
1417
"sqlc/db-prepare"

examples/jets/sqlc.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"version": "1",
3+
"cloud": {
4+
"project": "01HAQMMECEYQYKFJN8MP16QC41"
5+
},
36
"packages": [
47
{
58
"path": "postgresql",
@@ -8,7 +11,7 @@
811
"queries": "postgresql/query-building.sql",
912
"engine": "postgresql",
1013
"database": {
11-
"uri": "postgresql://${PG_USER}:${PG_PASSWORD}@${PG_HOST}:${PG_PORT}/jets"
14+
"managed": true
1215
},
1316
"rules": [
1417
"sqlc/db-prepare"

examples/ondeck/sqlc.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"version": "1",
3+
"cloud": {
4+
"project": "01HAQMMECEYQYKFJN8MP16QC41"
5+
},
36
"packages": [
47
{
58
"path": "postgresql",
@@ -8,7 +11,7 @@
811
"queries": "postgresql/query",
912
"engine": "postgresql",
1013
"database": {
11-
"uri": "postgresql://${PG_USER}:${PG_PASSWORD}@${PG_HOST}:${PG_PORT}/ondeck"
14+
"managed": true
1215
},
1316
"rules": [
1417
"sqlc/db-prepare"

internal/cmd/cmd.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func Do(args []string, stdin io.Reader, stdout io.Writer, stderr io.Writer) int
3737
rootCmd.PersistentFlags().StringP("file", "f", "", "specify an alternate config file (default: sqlc.yaml)")
3838
rootCmd.PersistentFlags().BoolP("experimental", "x", false, "DEPRECATED: enable experimental features (default: false)")
3939
rootCmd.PersistentFlags().Bool("no-remote", false, "disable remote execution (default: false)")
40+
rootCmd.PersistentFlags().Bool("remote", false, "enable remote execution (default: false)")
4041
rootCmd.PersistentFlags().Bool("no-database", false, "disable database connections (default: false)")
4142

4243
rootCmd.AddCommand(checkCmd)
@@ -136,17 +137,20 @@ var initCmd = &cobra.Command{
136137
type Env struct {
137138
DryRun bool
138139
Debug opts.Debug
140+
Remote bool
139141
NoRemote bool
140142
NoDatabase bool
141143
}
142144

143145
func ParseEnv(c *cobra.Command) Env {
144146
dr := c.Flag("dry-run")
147+
r := c.Flag("remote")
145148
nr := c.Flag("no-remote")
146149
nodb := c.Flag("no-database")
147150
return Env{
148151
DryRun: dr != nil && dr.Changed,
149152
Debug: opts.DebugFromEnv(),
153+
Remote: r != nil && nr.Value.String() == "true",
150154
NoRemote: nr != nil && nr.Value.String() == "true",
151155
NoDatabase: nodb != nil && nodb.Value.String() == "true",
152156
}

internal/cmd/generate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func Generate(ctx context.Context, e Env, dir, filename string, stderr io.Writer
145145
return nil, err
146146
}
147147

148-
if conf.Cloud.Project != "" && !e.NoRemote {
148+
if conf.Cloud.Project != "" && e.Remote && !e.NoRemote {
149149
return remoteGenerate(ctx, configPath, conf, dir, stderr)
150150
}
151151

internal/cmd/vet.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,10 +415,9 @@ func (c *checker) fetchDatabaseUri(ctx context.Context, s config.SQL) (string, f
415415
migrations = append(migrations, string(contents))
416416
}
417417

418-
start := time.Now()
419418
resp, err := c.Client.CreateEphemeralDatabase(ctx, &pb.CreateEphemeralDatabaseRequest{
420419
Engine: "postgresql",
421-
Region: "sjc",
420+
Region: quickdb.GetClosestRegion(),
422421
Migrations: migrations,
423422
})
424423
if err != nil {
@@ -432,7 +431,6 @@ func (c *checker) fetchDatabaseUri(ctx context.Context, s config.SQL) (string, f
432431
return err
433432
}
434433

435-
fmt.Println("createdb+template", time.Since(start))
436434
return resp.Uri, cleanup, nil
437435
}
438436

internal/endtoend/vet_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,8 @@ func TestExamplesVet(t *testing.T) {
5151
path := filepath.Join(examples, tc)
5252

5353
if tc != "kotlin" && tc != "python" {
54-
if s, found := findSchema(t, filepath.Join(path, "postgresql")); found {
55-
db, cleanup := sqltest.CreatePostgreSQLDatabase(t, tc, false, []string{s})
56-
defer db.Close()
57-
defer cleanup()
58-
}
5954
if s, found := findSchema(t, filepath.Join(path, "mysql")); found {
55+
t.Skip("local")
6056
db, cleanup := sqltest.CreateMySQLDatabase(t, tc, []string{s})
6157
defer db.Close()
6258
defer cleanup()

0 commit comments

Comments
 (0)