Skip to content

Commit f19bcdb

Browse files
authored
Remove RoutineName, fix and test rowcount (#30)
1 parent c605e20 commit f19bcdb

File tree

8 files changed

+87
-19
lines changed

8 files changed

+87
-19
lines changed

.github/workflows/go.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ jobs:
1111

1212
build:
1313
runs-on: ubuntu-latest
14+
env:
15+
SQLSERVER_DSN: "sqlserver://127.0.0.1:1433?database=master&user id=sa&password=VippsPw1"
1416
steps:
1517
- uses: actions/checkout@v4
1618

migrations/0001.sqlcode.sql

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,4 @@ grant execute on sqlcode.CreateCodeSchema to [sqlcode-deploy-role];
132132
grant execute on sqlcode.DropCodeSchema to [sqlcode-deploy-role];
133133
grant create procedure to [sqlcode-deploy-role];
134134
grant create function to [sqlcode-deploy-role];
135-
grant create type to [sqlcode-deploy-role];
136-
137-
138-
alter user
135+
grant create type to [sqlcode-deploy-role];

migrations/0002.sqlcode.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
-- We want to re-create the procedure DropCodeSchema.
2-
-- Frist, everything related to this procedure must be dropped
2+
-- First, everything related to this procedure must be dropped
33
-- before it is re-created in the end.
44

55
drop procedure sqlcode.DropCodeSchema;

sqlparser/parser.go

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ func (d *Document) parseCreate(s *Scanner, createCountInBatch int) (result Creat
417417
// point we copy the rest until the batch ends; *but* track dependencies
418418
// + some other details mentioned below
419419

420-
firstAs := true
420+
//firstAs := true // See comment below on rowcount
421421

422422
tailloop:
423423
for {
@@ -473,18 +473,41 @@ tailloop:
473473
case tt == ReservedWordToken && s.Token() == "as":
474474
CopyToken(s, &result.Body)
475475
NextTokenCopyingWhitespace(s, &result.Body)
476-
if firstAs {
477-
// Add the `RoutineName` token as a convenience, so that we can refer to the procedure/function name
478-
// from inside the procedure (for example, when logging)
479-
if result.CreateType == "procedure" {
480-
procNameToken := Unparsed{
481-
Type: OtherToken,
482-
RawValue: fmt.Sprintf(templateRoutineName, strings.Trim(result.QuotedName.Value, "[]")),
476+
/*
477+
TODO: Fix and re-enable
478+
This code add RoutineName for convenience. So:
479+
480+
create procedure [code@5420c0269aaf].Test as
481+
begin
482+
select 1
483+
end
484+
go
485+
486+
becomes:
487+
488+
create procedure [code@5420c0269aaf].Test as
489+
declare @RoutineName nvarchar(128)
490+
set @RoutineName = 'Test'
491+
begin
492+
select 1
493+
end
494+
go
495+
496+
However, for some very strange reason, @@rowcount is 1 with the first version,
497+
and it is 2 with the second version.
498+
if firstAs {
499+
// Add the `RoutineName` token as a convenience, so that we can refer to the procedure/function name
500+
// from inside the procedure (for example, when logging)
501+
if result.CreateType == "procedure" {
502+
procNameToken := Unparsed{
503+
Type: OtherToken,
504+
RawValue: fmt.Sprintf(templateRoutineName, strings.Trim(result.QuotedName.Value, "[]")),
505+
}
506+
result.Body = append(result.Body, procNameToken)
483507
}
484-
result.Body = append(result.Body, procNameToken)
508+
firstAs = false
485509
}
486-
firstAs = false
487-
}
510+
*/
488511

489512
default:
490513
CopyToken(s, &result.Body)

sqlparser/parser_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,16 @@ end;
4646

4747
assert.Equal(t, "[TestFunc]", c.QuotedName.Value)
4848
assert.Equal(t, []string{"[HelloFunc]", "[OtherFunc]"}, c.DependsOnStrings())
49-
assert.Equal(t, fmt.Sprintf(`-- preceding comment 1
49+
assert.Equal(t, `-- preceding comment 1
5050
/* preceding comment 2
5151
52-
asdfasdf */create procedure [code].TestFunc as %sbegin
52+
asdfasdf */create procedure [code].TestFunc as begin
5353
refers to [code].OtherFunc [code].HelloFunc;
5454
create table x ( int x not null ); -- should be ok
5555
end;
5656
5757
/* trailing comment */
58-
`, fmt.Sprintf(templateRoutineName, "TestFunc")), c.String())
58+
`, c.String())
5959

6060
assert.Equal(t,
6161
[]Error{
@@ -274,6 +274,7 @@ create procedure [code].FirstProc as table (x int)
274274
}
275275

276276
func TestCreateProcsAndCheckForRoutineName(t *testing.T) {
277+
t.Skip() // Routine name is disabled for now
277278
testcases := []struct {
278279
name string
279280
doc Document

sqltest/sql.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package sqltest
2+
3+
import (
4+
"embed"
5+
6+
"github.com/vippsas/sqlcode"
7+
)
8+
9+
//go:embed *.sql
10+
var sqlfs embed.FS
11+
12+
var SQL = sqlcode.MustInclude(
13+
sqlcode.Options{},
14+
sqlfs,
15+
)

sqltest/sqlcode_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package sqltest
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func Test_RowsAffected(t *testing.T) {
12+
fixture := NewFixture()
13+
defer fixture.Teardown()
14+
fixture.RunMigrationFile("../migrations/0001.sqlcode.sql")
15+
16+
ctx := context.Background()
17+
18+
require.NoError(t, SQL.EnsureUploaded(ctx, fixture.DB))
19+
patched := SQL.Patch(`[code].Test`)
20+
21+
res, err := fixture.DB.ExecContext(ctx, patched)
22+
require.NoError(t, err)
23+
rowsAffected, err := res.RowsAffected()
24+
require.NoError(t, err)
25+
assert.Equal(t, int64(1), rowsAffected)
26+
}

sqltest/test.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
create procedure [code].Test as
2+
begin
3+
select 1
4+
end

0 commit comments

Comments
 (0)