Skip to content

Commit 0a8ac1c

Browse files
committed
Saving changes
1 parent f0b9f3b commit 0a8ac1c

File tree

12 files changed

+142
-130
lines changed

12 files changed

+142
-130
lines changed

internal/container/controller.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ func (c Controller) RunCmdInContainer(id string, cmd []string) ([]byte, []byte)
305305
AttachStderr: true,
306306
AttachStdout: true,
307307
Cmd: cmd,
308+
Env: []string{"DOTNET_ROOT=/root/.dotnet"},
308309
},
309310
)
310311
checkErr(err)

internal/databaseurl/factory.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package databaseurl
2+
3+
import (
4+
"fmt"
5+
url2 "net/url"
6+
"path/filepath"
7+
"strings"
8+
)
9+
10+
func NewDatabaseUrl(url string) *DatabaseUrl {
11+
databaseUrl := DatabaseUrl{}
12+
13+
// To enable URL.Parse, switch to / from \\
14+
url = strings.Replace(url, "\\", "/", -1)
15+
16+
// Cope with a URL that has no schema, e.g. in local dir, or local folder \foo.mdf
17+
if !strings.Contains(url, "/") {
18+
url = "./" + url
19+
}
20+
21+
var err error
22+
parsedUrl, err := url2.Parse(url)
23+
if err != nil {
24+
panic(err)
25+
}
26+
27+
databaseUrl.URL = parsedUrl
28+
29+
fmt.Println("databaseUrl.URL.Path: " + databaseUrl.URL.Path)
30+
31+
databaseUrl.Filename = filepath.Base(databaseUrl.URL.Path)
32+
databaseUrl.FileExtension = strings.TrimLeft(filepath.Ext(databaseUrl.Filename), ".")
33+
34+
split := strings.Split(databaseUrl.URL.Path, ",")
35+
if len(split) > 1 {
36+
databaseUrl.DatabaseName = split[1]
37+
38+
// Remove the database name (specified after the comma) from the URL, and reparse it
39+
url = strings.Replace(url, ","+split[1], "", 1)
40+
databaseUrl.URL, err = databaseUrl.URL.Parse(url)
41+
if err != nil {
42+
panic(err)
43+
}
44+
45+
split := strings.Split(databaseUrl.FileExtension, ",")
46+
databaseUrl.FileExtension = split[0]
47+
48+
split = strings.Split(databaseUrl.Filename, ",")
49+
databaseUrl.Filename = split[0]
50+
} else {
51+
52+
databaseUrl.DatabaseName = strings.TrimSuffix(
53+
databaseUrl.Filename,
54+
"."+databaseUrl.FileExtension,
55+
)
56+
}
57+
58+
fmt.Println("databaseUrl.Filename: " + databaseUrl.Filename)
59+
fmt.Println("databaseUrl.FileExtension: " + databaseUrl.FileExtension)
60+
fmt.Println("databaseUrl.DatabaseName: " + databaseUrl.DatabaseName)
61+
62+
databaseUrl.IsLocal = databaseUrl.URL.Scheme == "file" || len(databaseUrl.URL.Scheme) < 3
63+
64+
escapedDbName := strings.ReplaceAll(databaseUrl.DatabaseName, "'", "''")
65+
databaseUrl.DatabaseNameAsTsqlIdentifier = strings.ReplaceAll(escapedDbName, "]", "]]")
66+
databaseUrl.DatabaseNameAsNonTsqlIdentifier = strings.ReplaceAll(databaseUrl.DatabaseName, "]", "]]")
67+
68+
return &databaseUrl
69+
}

internal/databaseurl/type.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package databaseurl
2+
3+
import "net/url"
4+
5+
type DatabaseUrl struct {
6+
*url.URL
7+
Filename string
8+
FileExtension string
9+
IsLocal bool
10+
11+
// DatabaseName returns the databaseName from --using arg
12+
// It sets database name to the specified database name
13+
// or in absence of it, it is set to the filename without
14+
// extension.
15+
DatabaseName string
16+
17+
DatabaseNameAsTsqlIdentifier string
18+
DatabaseNameAsNonTsqlIdentifier string
19+
}

internal/uri/uri_test.go renamed to internal/databaseurl/uri_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package uri
1+
package databaseurl
22

33
import (
44
"github.com/stretchr/testify/assert"
@@ -19,7 +19,7 @@ func TestExtractUrl(t *testing.T) {
1919

2020
for _, testcase := range tests {
2121
u := NewUri(testcase.inputURL)
22-
assert.Equal(t, testcase.expectedURL, u.ActualUrl(),
22+
assert.Equal(t, testcase.expectedURL, u.ActualUrl,
2323
"Extracted URL does not match expected URL")
2424
}
2525
}
@@ -54,7 +54,7 @@ func TestParseDbName(t *testing.T) {
5454

5555
for _, testcase := range tests {
5656
u := NewUri(testcase.inputURL)
57-
assert.Equal(t, testcase.expectedURL, u.ParseDbName(),
57+
assert.Equal(t, testcase.expectedURL, u.DatabaseName,
5858
"Extracted DB Name does not match expected DB Name")
5959
}
6060
}
@@ -92,9 +92,9 @@ func TestGetDbNameIfExists(t *testing.T) {
9292
for _, testcase := range tests {
9393
u := NewUri(testcase.input)
9494

95-
assert.Equal(t, testcase.expectedIdentifierOp, u.GetDbNameAsIdentifier(),
95+
assert.Equal(t, testcase.expectedIdentifierOp, u.DatabaseNameAsTsqlIdentifier,
9696
"Unexpected database name as identifier")
97-
assert.Equal(t, testcase.expectedNonIdentifierOp, u.GetDbNameAsNonIdentifier(),
97+
assert.Equal(t, testcase.expectedNonIdentifierOp, u.DatabaseNameAsNonTsqlIdentifier,
9898
"Unexpected database name as non-identifier")
9999
}
100100
}

internal/uri/factory.go

Lines changed: 0 additions & 17 deletions
This file was deleted.

internal/uri/type.go

Lines changed: 0 additions & 8 deletions
This file was deleted.

internal/uri/uri.go

Lines changed: 0 additions & 71 deletions
This file was deleted.

pkg/mssqlcontainer/ingest/extract/7zip.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func (e *sevenZip) Extract(srcFile string, destFolder string) (string, string) {
2929
e.controller.RunCmdInContainer(e.containerId, []string{
3030
"/opt/7-zip/7zz",
3131
"x",
32+
"-aoa",
3233
"-o" + destFolder,
3334
"/var/opt/mssql/backup/" + srcFile,
3435
})

pkg/mssqlcontainer/ingest/factory.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@ package ingest
22

33
import (
44
"github.com/microsoft/go-sqlcmd/internal/container"
5-
"github.com/microsoft/go-sqlcmd/internal/uri"
5+
"github.com/microsoft/go-sqlcmd/internal/databaseurl"
66
"github.com/microsoft/go-sqlcmd/pkg/mssqlcontainer/ingest/extract"
77
"github.com/microsoft/go-sqlcmd/pkg/mssqlcontainer/ingest/location"
88
"github.com/microsoft/go-sqlcmd/pkg/mssqlcontainer/ingest/mechanism"
99
"strings"
1010
)
1111

1212
func NewIngest(databaseUri string, controller *container.Controller, options IngestOptions) Ingest {
13-
uri := uri.NewUri(databaseUri)
13+
databaseUrl := databaseurl.NewDatabaseUrl(databaseUri)
1414

1515
return &ingest{
16-
uri: uri,
16+
uri: databaseUrl,
1717
controller: controller,
18-
location: location.NewLocation(uri.IsLocal(), uri.ActualUrl(), controller),
19-
mechanism: mechanism.NewMechanism(uri.FileExtension(), options.Mechanism, controller),
18+
location: location.NewLocation(databaseUrl.IsLocal, databaseUrl.String(), controller),
19+
mechanism: mechanism.NewMechanism(databaseUrl.FileExtension, options.Mechanism, controller),
2020
}
2121
}
2222

pkg/mssqlcontainer/ingest/ingest.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package ingest
33
import (
44
"fmt"
55
"github.com/microsoft/go-sqlcmd/internal/container"
6-
"github.com/microsoft/go-sqlcmd/internal/uri"
6+
"github.com/microsoft/go-sqlcmd/internal/databaseurl"
77
"github.com/microsoft/go-sqlcmd/pkg/mssqlcontainer/ingest/extract"
88
"github.com/microsoft/go-sqlcmd/pkg/mssqlcontainer/ingest/location"
99
"github.com/microsoft/go-sqlcmd/pkg/mssqlcontainer/ingest/mechanism"
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
type ingest struct {
15-
uri uri.Uri
15+
uri *databaseurl.DatabaseUrl
1616
location location.Location
1717
controller *container.Controller
1818
mechanism mechanism.Mechanism
@@ -23,7 +23,7 @@ type ingest struct {
2323
}
2424

2525
func (i *ingest) IsExtractionNeeded() bool {
26-
i.extractor = extract.NewExtractor(i.uri.FileExtension(), i.controller)
26+
i.extractor = extract.NewExtractor(i.uri.FileExtension, i.controller)
2727
if i.extractor == nil {
2828
return false
2929
} else {
@@ -36,12 +36,12 @@ func (i *ingest) IsRemoteUrl() bool {
3636
}
3737

3838
func (i *ingest) UrlFilename() string {
39-
return i.uri.Filename()
39+
return i.uri.Filename
4040
}
4141

4242
func (i *ingest) IsValidScheme() bool {
4343
for _, s := range i.location.ValidSchemes() {
44-
if s == i.uri.Scheme() {
44+
if s == i.uri.Scheme {
4545
return true
4646
}
4747
}
@@ -59,7 +59,7 @@ func (i *ingest) CopyToContainer(containerId string) {
5959

6060
i.containerId = containerId
6161
i.location.CopyToContainer(containerId, destFolder)
62-
i.options.Filename = i.uri.Filename()
62+
i.options.Filename = i.uri.Filename
6363

6464
if i.options.Filename == "" {
6565
panic("filename is empty")
@@ -76,7 +76,7 @@ func (i *ingest) Extract() {
7676
}
7777

7878
i.options.Filename, i.options.LdfFilename =
79-
i.extractor.Extract(i.uri.Filename(), "/var/opt/mssql/data")
79+
i.extractor.Extract(i.uri.Filename, "/var/opt/mssql/data")
8080

8181
if i.mechanism == nil {
8282
ext := strings.TrimLeft(filepath.Ext(i.options.Filename), ".")
@@ -88,11 +88,18 @@ func (i *ingest) BringOnline(query func(string), username string, password strin
8888
if i.options.Filename == "" {
8989
panic("filename is empty, did you call CopyToContainer()?")
9090
}
91+
if query == nil {
92+
panic("query is nil")
93+
}
94+
if i.mechanism == nil {
95+
panic("mechanism is nil")
96+
}
9197

9298
i.query = query
9399
i.options.Username = username
94100
i.options.Password = password
95-
i.mechanism.BringOnline(i.uri.GetDbNameAsIdentifier(), i.containerId, i.query, i.options)
101+
fmt.Println(i.uri.DatabaseNameAsTsqlIdentifier)
102+
i.mechanism.BringOnline(i.uri.DatabaseNameAsTsqlIdentifier, i.containerId, i.query, i.options)
96103
i.setDefaultDatabase(username)
97104
}
98105

@@ -104,18 +111,18 @@ func (i *ingest) setDefaultDatabase(username string) {
104111
alterDefaultDb := fmt.Sprintf(
105112
"ALTER LOGIN [%s] WITH DEFAULT_DATABASE = [%s]",
106113
username,
107-
i.uri.GetDbNameAsNonIdentifier())
114+
i.uri.DatabaseNameAsNonTsqlIdentifier)
108115
i.query(alterDefaultDb)
109116
}
110117

111118
func (i *ingest) IsValidFileExtension() bool {
112119
for _, m := range mechanism.FileTypes() {
113-
if m == i.uri.FileExtension() {
120+
if m == i.uri.FileExtension {
114121
return true
115122
}
116123
}
117124
for _, e := range extract.FileTypes() {
118-
if e == i.uri.FileExtension() {
125+
if e == i.uri.FileExtension {
119126
return true
120127
}
121128
}
@@ -127,7 +134,7 @@ func (i *ingest) SourceFileExists() bool {
127134
}
128135

129136
func (i *ingest) UserProvidedFileExt() string {
130-
return i.uri.FileExtension()
137+
return i.uri.FileExtension
131138
}
132139

133140
func (i *ingest) ValidSchemes() []string {

0 commit comments

Comments
 (0)