Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed table schema using with SQL Server. #30

Merged
merged 2 commits into from
Jul 3, 2018

Conversation

ekhabarov
Copy link
Contributor

Hi!

This PR fixes next bug. If you create any table in database in SQL Server within schema different to dbo, for instance x.empty_table_without_fixtures, and run tests you'll get an error

% go test -tags sqlserver -v                                                                
=== RUN   TestFixtureFile                                                                                                                                                                     
--- PASS: TestFixtureFile (0.00s)                                                                                
=== RUN   TestLoadFixtures                                                                                           
Test for mssql                                           
--- FAIL: TestLoadFixtures (0.06s)                      
        testfixtures_test.go:150: Error on loading fixtures: mssql: Cannot find the object "empty_table_without_fixtures" because it does not exist or you do not have permissions.
        testfixtures_test.go:139: posts should have 2, but has 0          
        testfixtures_test.go:139: comments should have 4, but has 0                                           
        testfixtures_test.go:139: tags should have 3, but has 0      
        testfixtures_test.go:139: posts_tags should have 2, but has 0
        testfixtures_test.go:139: users should have 2, but has 0          
        testfixtures_test.go:200: Error on loading fixtures: mssql: Cannot find the object "empty_table_without_fixtures" because it does not exist or you do not have permissions.           
        testfixtures_test.go:139: posts should have 2, but has 1
        testfixtures_test.go:139: comments should have 4, but has 0                                                
        testfixtures_test.go:139: tags should have 3, but has 0                                                                                                                               
        testfixtures_test.go:139: posts_tags should have 2, but has 0                                                                                                                         
        testfixtures_test.go:74: mssql: Invalid object name 'empty_table_without_fixtures'.
        testfixtures_test.go:82: mssql: Cannot find the object "empty_table_without_fixtures" because it does not exist or you do not have permissions.
=== RUN   TestQuoteKeyword                           
--- PASS: TestQuoteKeyword (0.00s)                                                             
=== RUN   TestDatabaseNameHelperSurfacesErrors                                                                                                                                                
Test for mssql                                                                                                                                
--- PASS: TestDatabaseNameHelperSurfacesErrors (0.00s)                                                                                                        
FAIL                                                      
exit status 1                                     
FAIL    github.com/go-testfixtures/testfixtures 0.067s

... because function tableName does not support table schema.

Copy link
Contributor

@andreynering andreynering left a comment

Choose a reason for hiding this comment

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

Hi @ekhabarov!

Thanks a lot for your PR! A few comments and this is good to go

sqlserver.go Outdated
// Splitter is a BatchSplitter interface implementation. We need it for
// SQL Server because commands like a `CREATE SCHEMA...` and a `CREATE TABLE...`
// could not be executed in the same batch.
// See https://docs.microsoft.com/en-us/previous-versions/sql/sql-server-2008-r2/ms175502(v=sql.105)#rules-for-using-batches
Copy link
Contributor

Choose a reason for hiding this comment

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

Ouch!

I think I should move the schema files to either a function or an YAML file (so arrays are allowed to run multiple statements) to prevent logic like this

But for this PR this is good enough as is, we can do that later

@@ -18,6 +21,15 @@ CREATE TABLE tags (
,created_at DATETIME NOT NULL
,updated_at DATETIME NOT NULL
);
GO

CREATE SCHEMA x AUTHORIZATION dbo;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we rename x to something more descriptive, like non_default_schema?

GO

CREATE TABLE x.empty_table_without_fixtures (
id INT IDENTITY PRIMARY KEY
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's keep indentation with tabs for now, to be consistent with the rest of the file

@@ -52,14 +51,25 @@ func TestLoadFixtures(t *testing.T) {
log.Fatalf("Failed to ping database: %v\n", err)
}

bytes, err = ioutil.ReadFile(database.schemaFile)
batches := [][]byte{}
Copy link
Contributor

Choose a reason for hiding this comment

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

var batches [][]byte

if err != nil {
log.Fatalf("Failed to create schema: %v\n", err)
h, ok := database.helper.(BatchSplitter)
if ok {
Copy link
Contributor

Choose a reason for hiding this comment

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

if h, ok := database.helper.(BatchSplitter); ok {


for _, b := range batches {
_, err = db.Exec(string(b))
if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

if _, err = db.Exec(string(b)); err != nil {

helper.go Outdated
// For Microsoft SQL Server batch splitter is "GO". For details see
// https://docs.microsoft.com/en-us/sql/t-sql/language-elements/sql-server-utilities-statements-go
type BatchSplitter interface {
Splitter() []byte
Copy link
Contributor

Choose a reason for hiding this comment

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

What about moving the interface and method to lower case? I don't think this need to be exported

@ekhabarov
Copy link
Contributor Author

Hi @andreynering!

Fixed.

@andreynering
Copy link
Contributor

@ekhabarov Thanks! 🙂

@andreynering andreynering merged commit 689bce4 into go-testfixtures:master Jul 3, 2018
@ekhabarov ekhabarov deleted the sqlserver-schema-fix branch July 11, 2023 03:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants