diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index f19161a..07d79d9 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -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 diff --git a/migrations/0001.sqlcode.sql b/migrations/0001.sqlcode.sql index d67fc7f..60c5c2d 100644 --- a/migrations/0001.sqlcode.sql +++ b/migrations/0001.sqlcode.sql @@ -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 \ No newline at end of file +grant create type to [sqlcode-deploy-role]; \ No newline at end of file diff --git a/migrations/0002.sqlcode.sql b/migrations/0002.sqlcode.sql index c397683..e94aeb9 100644 --- a/migrations/0002.sqlcode.sql +++ b/migrations/0002.sqlcode.sql @@ -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; diff --git a/sqlparser/parser.go b/sqlparser/parser.go index 4ed6562..40eebe9 100644 --- a/sqlparser/parser.go +++ b/sqlparser/parser.go @@ -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 { @@ -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) diff --git a/sqlparser/parser_test.go b/sqlparser/parser_test.go index 9e5f830..7bd20b8 100644 --- a/sqlparser/parser_test.go +++ b/sqlparser/parser_test.go @@ -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{ @@ -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 diff --git a/sqltest/sql.go b/sqltest/sql.go new file mode 100644 index 0000000..15d7995 --- /dev/null +++ b/sqltest/sql.go @@ -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, +) diff --git a/sqltest/sqlcode_test.go b/sqltest/sqlcode_test.go new file mode 100644 index 0000000..0f209df --- /dev/null +++ b/sqltest/sqlcode_test.go @@ -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) +} diff --git a/sqltest/test.sql b/sqltest/test.sql new file mode 100644 index 0000000..d56ac9e --- /dev/null +++ b/sqltest/test.sql @@ -0,0 +1,4 @@ +create procedure [code].Test as +begin +select 1 +end \ No newline at end of file