Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
fa05d57
Updated testing environment.
simukka Nov 6, 2025
351a6dc
Working connection.
simukka Nov 6, 2025
b1b982d
Updated fixture to use pgx. Simplified how we introspect the DSN to d…
simukka Dec 3, 2025
2d9462f
Wrote initial sqlcode migration for postgres.
simukka Dec 3, 2025
6643158
Add security definer role.
simukka Dec 3, 2025
fd8c76f
[wip] working through changes for EnsureUploaded to support postgresql.
simukka Dec 3, 2025
b6681a2
Working EnsureUpload!
simukka Dec 4, 2025
da5b115
[wip] update parser and scanner
simukka Dec 5, 2025
18309f0
Fixed issue with Preprocess. Passing pgsql tests.
simukka Dec 9, 2025
b651814
Updated GO workflow to test both drivers.
simukka Dec 9, 2025
160df17
Fixed typo in GH workflow.
simukka Dec 9, 2025
11659ed
Updated Dockerfile
simukka Dec 9, 2025
37fd588
Use build tags to exclude examples from bulid & test
simukka Dec 9, 2025
ca444bc
Exclude example test
simukka Dec 9, 2025
c483fb7
Fixed failing test.
simukka Dec 9, 2025
4b803bf
Moved Document structs to a separate file for better organization.
simukka Dec 11, 2025
f202e18
Updated go-mssql depedency to use microsoft fork. DropAndUpload now s…
simukka Dec 11, 2025
ad129b8
Initial unit tests for T-SQL syntax parsing.
simukka Dec 11, 2025
352ed9f
Refactored to use a Document interface.
simukka Dec 11, 2025
5e807d5
Renamed the existing Document struct to be specific for T-SQL.
simukka Dec 11, 2025
494dca9
Created initial PGSqlDocument for PostgreSQL.
simukka Dec 11, 2025
1f7b6b7
Updated unit test.
simukka Dec 11, 2025
af75628
Updated tests.
simukka Dec 11, 2025
6916d34
Simplified Document interface. Created Pragma struct.
simukka Dec 11, 2025
1a91556
[wip] pgsql document parsing
simukka Dec 11, 2025
fb56414
Simplify the interfaces for parsing a SQL document.
simukka Dec 16, 2025
5e29dc9
Refactored pgsql document to use node parser.
simukka Dec 16, 2025
6e114a9
[wip]
simukka Dec 18, 2025
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
17 changes: 7 additions & 10 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: go-querysql-test
name: sqlcode

on:
pull_request:
Expand All @@ -11,19 +11,16 @@ jobs:

build:
runs-on: ubuntu-latest
env:
SQLSERVER_DSN: "sqlserver://127.0.0.1:1433?database=master&user id=sa&password=VippsPw1"
strategy:
matrix:
driver: ['mssql','pgsql']
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v5

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.23'

- name: Start db
run: docker compose -f docker-compose.test.yml up -d
go-version: '1.25'

- name: Test
# Skip the example folder because it has examples of what-not-to-do
run: go test -v $(go list ./... | grep -v './example')
run: docker compose -f docker-compose.${{ matrix.driver }}.yml run test
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
test: test_mssql test_pgsql


test_mssql:
docker compose --progress plain -f docker-compose.mssql.yml run test

test_pgsql:
docker compose --progress plain -f docker-compose.pgsql.yml run test
4 changes: 3 additions & 1 deletion cli/cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package cmd
import (
"errors"
"fmt"

mssql "github.com/microsoft/go-mssqldb"
"github.com/spf13/cobra"
"github.com/vippsas/sqlcode"
)
Expand All @@ -23,7 +25,7 @@ var (
return err
}

preprocessed, err := sqlcode.Preprocess(d.CodeBase, schemasuffix)
preprocessed, err := sqlcode.Preprocess(d.CodeBase, schemasuffix, &mssql.Driver{})
Copy link
Contributor Author

Choose a reason for hiding this comment

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

TODO: The cli module is not yet compatible with postgres.

if err != nil {
return err
}
Expand Down
11 changes: 6 additions & 5 deletions cli/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ import (
"database/sql"
"errors"
"fmt"
mssql "github.com/denisenkom/go-mssqldb"
"github.com/denisenkom/go-mssqldb/azuread"
"golang.org/x/net/proxy"
"io/ioutil"
"os"
"path"
"strings"

_ "github.com/denisenkom/go-mssqldb/azuread"
"github.com/denisenkom/go-mssqldb/msdsn"
mssql "github.com/microsoft/go-mssqldb"
"github.com/microsoft/go-mssqldb/azuread"
"golang.org/x/net/proxy"

_ "github.com/microsoft/go-mssqldb/azuread"
"github.com/microsoft/go-mssqldb/msdsn"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)
Expand Down
8 changes: 4 additions & 4 deletions cli/cmd/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ var (
if err != nil {
return err
}
if len(d.CodeBase.Creates) == 0 && len(d.CodeBase.Declares) == 0 {
if d.CodeBase.Empty() {
fmt.Println("No SQL code found in given paths")
}
if len(d.CodeBase.Errors) > 0 {
if d.CodeBase.HasErrors() {
fmt.Println("Errors:")
for _, e := range d.CodeBase.Errors {
for _, e := range d.CodeBase.Errors() {
fmt.Printf("%s:%d:%d: %s\n", e.Pos.File, e.Pos.Line, e.Pos.Line, e.Message)
}
return nil
}
fmt.Println("declare")
for i, c := range d.CodeBase.Declares {
for i, c := range d.CodeBase.Declares() {
var prefix string
if i == 0 {
prefix = " "
Expand Down
8 changes: 4 additions & 4 deletions cli/cmd/dep.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ var (
fmt.Println()
err = nil
}
if len(d.CodeBase.Creates) == 0 && len(d.CodeBase.Declares) == 0 {
if d.CodeBase.Empty() {
fmt.Println("No SQL code found in given paths")
}
if len(d.CodeBase.Errors) > 0 {
if d.CodeBase.HasErrors() {
fmt.Println("Errors:")
for _, e := range d.CodeBase.Errors {
for _, e := range d.CodeBase.Errors() {
fmt.Printf("%s:%d:%d: %s\n", e.Pos.File, e.Pos.Line, e.Pos.Line, e.Message)
}
}
for _, c := range d.CodeBase.Creates {
for _, c := range d.CodeBase.Creates() {
fmt.Println(c.QuotedName.String() + ":")
if len(c.DependsOn) > 0 {
fmt.Println(" Uses:")
Expand Down
2 changes: 2 additions & 0 deletions dbintf.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sqlcode
import (
"context"
"database/sql"
"database/sql/driver"
)

type DB interface {
Expand All @@ -11,6 +12,7 @@ type DB interface {
QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
Conn(ctx context.Context) (*sql.Conn, error)
BeginTx(ctx context.Context, txOptions *sql.TxOptions) (*sql.Tx, error)
Driver() driver.Driver
Copy link
Contributor Author

@simukka simukka Dec 9, 2025

Choose a reason for hiding this comment

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

This is a required interface for sql.DB and shouldn't be too much of a hassle to support in internal libraries.

}

var _ DB = &sql.DB{}
37 changes: 34 additions & 3 deletions dbops.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,26 @@ package sqlcode
import (
"context"
"database/sql"

"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/stdlib"
mssql "github.com/microsoft/go-mssqldb"
)

func Exists(ctx context.Context, dbc DB, schemasuffix string) (bool, error) {
var schemaID int
err := dbc.QueryRowContext(ctx, `select isnull(schema_id(@p1), 0)`, SchemaName(schemasuffix)).Scan(&schemaID)

driver := dbc.Driver()
var qs string

if _, ok := driver.(*mssql.Driver); ok {
qs = `select isnull(schema_id(@p1), 0)`
}
if _, ok := driver.(*stdlib.Driver); ok {
qs = `select coalesce((select oid from pg_namespace where nspname = $1),0)`
}

err := dbc.QueryRowContext(ctx, qs, SchemaName(schemasuffix)).Scan(&schemaID)
if err != nil {
return false, err
}
Expand All @@ -19,8 +34,24 @@ func Drop(ctx context.Context, dbc DB, schemasuffix string) error {
if err != nil {
return err
}
_, err = tx.ExecContext(ctx, `sqlcode.DropCodeSchema`,
sql.Named("schemasuffix", schemasuffix))

var qs string
var arg = []interface{}{}
driver := dbc.Driver()

if _, ok := driver.(*mssql.Driver); ok {
qs = `sqlcode.DropCodeSchema`
arg = []interface{}{sql.Named("schemasuffix", schemasuffix)}
}

if _, ok := dbc.Driver().(*stdlib.Driver); ok {
qs = `call sqlcode.dropcodeschema(@schemasuffix)`
arg = []interface{}{
pgx.NamedArgs{"schemasuffix": schemasuffix},
}
}

_, err = tx.ExecContext(ctx, qs, arg...)
if err != nil {
_ = tx.Rollback()
return err
Expand Down
Loading