Skip to content

Commit

Permalink
Merge pull request #6356 from kedarkale27/master
Browse files Browse the repository at this point in the history
Update mssql.go
  • Loading branch information
tyrannosaurus-becks authored Apr 23, 2019
2 parents decdbeb + 779a4f9 commit be70748
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
7 changes: 2 additions & 5 deletions physical/mssql/mssql.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,13 @@ func NewMSSQLBackend(conf map[string]string, logger log.Logger) (physical.Backen
"') CREATE TABLE " + dbTable + " (Path VARCHAR(512) PRIMARY KEY, Value VARBINARY(MAX))"

if schema != "dbo" {
if _, err := db.Exec("USE " + database); err != nil {
return nil, errwrap.Wrapf("failed to switch mssql database: {{err}}", err)
}

var num int
err = db.QueryRow("SELECT 1 FROM sys.schemas WHERE name = '" + schema + "'").Scan(&num)
err = db.QueryRow("SELECT 1 FROM " + database + ".sys.schemas WHERE name = '" + schema + "'").Scan(&num)

switch {
case err == sql.ErrNoRows:
if _, err := db.Exec("CREATE SCHEMA " + schema); err != nil {
if _, err := db.Exec("USE " + database + "; EXEC ('CREATE SCHEMA " + schema + "')"); err != nil {
return nil, errwrap.Wrapf("failed to create mssql schema: {{err}}", err)
}

Expand Down
58 changes: 58 additions & 0 deletions physical/mssql/mssql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,63 @@ func TestMSSQLBackend(t *testing.T) {
table = "test"
}

schema := os.Getenv("MSSQL_SCHEMA")
if schema == "" {
schema = "test"
}

username := os.Getenv("MSSQL_USERNAME")
password := os.Getenv("MSSQL_PASSWORD")

// Run vault tests
logger := logging.NewVaultLogger(log.Debug)

b, err := NewMSSQLBackend(map[string]string{
"server": server,
"database": database,
"table": table,
"schema": schema,
"username": username,
"password": password,
}, logger)

if err != nil {
t.Fatalf("Failed to create new backend: %v", err)
}

defer func() {
mssql := b.(*MSSQLBackend)
_, err := mssql.client.Exec("DROP TABLE " + mssql.dbTable)
if err != nil {
t.Fatalf("Failed to drop table: %v", err)
}
}()

physical.ExerciseBackend(t, b)
physical.ExerciseBackend_ListPrefix(t, b)
}

func TestMSSQLBackend_schema(t *testing.T) {
server := os.Getenv("MSSQL_SERVER")
if server == "" {
t.SkipNow()
}

database := os.Getenv("MSSQL_DB")
if database == "" {
database = "test"
}

table := os.Getenv("MSSQL_TABLE")
if table == "" {
table = "test"
}

schema := os.Getenv("MSSQL_SCHEMA")
if schema == "" {
schema = "test"
}

username := os.Getenv("MSSQL_USERNAME")
password := os.Getenv("MSSQL_PASSWORD")

Expand All @@ -36,6 +93,7 @@ func TestMSSQLBackend(t *testing.T) {
b, err := NewMSSQLBackend(map[string]string{
"server": server,
"database": database,
"schema": schema,
"table": table,
"username": username,
"password": password,
Expand Down

0 comments on commit be70748

Please sign in to comment.