Skip to content

Commit

Permalink
Remove RoutineName, fix and test rowcount (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
dfava authored Dec 18, 2024
1 parent c605e20 commit f19bcdb
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ jobs:

build:
runs-on: ubuntu-latest
env:
SQLSERVER_DSN: "sqlserver://127.0.0.1:1433?database=master&user id=sa&password=VippsPw1"
steps:
- uses: actions/checkout@v4

Expand Down
5 changes: 1 addition & 4 deletions migrations/0001.sqlcode.sql
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,4 @@ grant execute on sqlcode.CreateCodeSchema to [sqlcode-deploy-role];
grant execute on sqlcode.DropCodeSchema to [sqlcode-deploy-role];
grant create procedure to [sqlcode-deploy-role];
grant create function to [sqlcode-deploy-role];
grant create type to [sqlcode-deploy-role];


alter user
grant create type to [sqlcode-deploy-role];
2 changes: 1 addition & 1 deletion migrations/0002.sqlcode.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- We want to re-create the procedure DropCodeSchema.
-- Frist, everything related to this procedure must be dropped
-- First, everything related to this procedure must be dropped
-- before it is re-created in the end.

drop procedure sqlcode.DropCodeSchema;
Expand Down
45 changes: 34 additions & 11 deletions sqlparser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ func (d *Document) parseCreate(s *Scanner, createCountInBatch int) (result Creat
// point we copy the rest until the batch ends; *but* track dependencies
// + some other details mentioned below

firstAs := true
//firstAs := true // See comment below on rowcount

tailloop:
for {
Expand Down Expand Up @@ -473,18 +473,41 @@ tailloop:
case tt == ReservedWordToken && s.Token() == "as":
CopyToken(s, &result.Body)
NextTokenCopyingWhitespace(s, &result.Body)
if firstAs {
// Add the `RoutineName` token as a convenience, so that we can refer to the procedure/function name
// from inside the procedure (for example, when logging)
if result.CreateType == "procedure" {
procNameToken := Unparsed{
Type: OtherToken,
RawValue: fmt.Sprintf(templateRoutineName, strings.Trim(result.QuotedName.Value, "[]")),
/*
TODO: Fix and re-enable
This code add RoutineName for convenience. So:
create procedure [code@5420c0269aaf].Test as
begin
select 1
end
go
becomes:
create procedure [code@5420c0269aaf].Test as
declare @RoutineName nvarchar(128)
set @RoutineName = 'Test'
begin
select 1
end
go
However, for some very strange reason, @@rowcount is 1 with the first version,
and it is 2 with the second version.
if firstAs {
// Add the `RoutineName` token as a convenience, so that we can refer to the procedure/function name
// from inside the procedure (for example, when logging)
if result.CreateType == "procedure" {
procNameToken := Unparsed{
Type: OtherToken,
RawValue: fmt.Sprintf(templateRoutineName, strings.Trim(result.QuotedName.Value, "[]")),
}
result.Body = append(result.Body, procNameToken)
}
result.Body = append(result.Body, procNameToken)
firstAs = false
}
firstAs = false
}
*/

default:
CopyToken(s, &result.Body)
Expand Down
7 changes: 4 additions & 3 deletions sqlparser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ end;

assert.Equal(t, "[TestFunc]", c.QuotedName.Value)
assert.Equal(t, []string{"[HelloFunc]", "[OtherFunc]"}, c.DependsOnStrings())
assert.Equal(t, fmt.Sprintf(`-- preceding comment 1
assert.Equal(t, `-- preceding comment 1
/* preceding comment 2
asdfasdf */create procedure [code].TestFunc as %sbegin
asdfasdf */create procedure [code].TestFunc as begin
refers to [code].OtherFunc [code].HelloFunc;
create table x ( int x not null ); -- should be ok
end;
/* trailing comment */
`, fmt.Sprintf(templateRoutineName, "TestFunc")), c.String())
`, c.String())

assert.Equal(t,
[]Error{
Expand Down Expand Up @@ -274,6 +274,7 @@ create procedure [code].FirstProc as table (x int)
}

func TestCreateProcsAndCheckForRoutineName(t *testing.T) {
t.Skip() // Routine name is disabled for now
testcases := []struct {
name string
doc Document
Expand Down
15 changes: 15 additions & 0 deletions sqltest/sql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package sqltest

import (
"embed"

"github.com/vippsas/sqlcode"
)

//go:embed *.sql
var sqlfs embed.FS

var SQL = sqlcode.MustInclude(
sqlcode.Options{},
sqlfs,
)
26 changes: 26 additions & 0 deletions sqltest/sqlcode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package sqltest

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func Test_RowsAffected(t *testing.T) {
fixture := NewFixture()
defer fixture.Teardown()
fixture.RunMigrationFile("../migrations/0001.sqlcode.sql")

ctx := context.Background()

require.NoError(t, SQL.EnsureUploaded(ctx, fixture.DB))
patched := SQL.Patch(`[code].Test`)

res, err := fixture.DB.ExecContext(ctx, patched)
require.NoError(t, err)
rowsAffected, err := res.RowsAffected()
require.NoError(t, err)
assert.Equal(t, int64(1), rowsAffected)
}
4 changes: 4 additions & 0 deletions sqltest/test.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
create procedure [code].Test as
begin
select 1
end

0 comments on commit f19bcdb

Please sign in to comment.