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

Update mssql.go #6356

Merged
merged 7 commits into from
Apr 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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