Skip to content

Commit

Permalink
Fix and test for rowcount
Browse files Browse the repository at this point in the history
  • Loading branch information
dfava committed Dec 18, 2024
1 parent 517e77b commit 165b55b
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 16 deletions.
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
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 165b55b

Please sign in to comment.