@@ -2,37 +2,40 @@ package ingest
22
33import (
44 "fmt"
5+ "github.com/microsoft/go-sqlcmd/internal/container"
56 "github.com/microsoft/go-sqlcmd/internal/uri"
67 "github.com/microsoft/go-sqlcmd/pkg/mssqlcontainer/ingest/extract"
78 "github.com/microsoft/go-sqlcmd/pkg/mssqlcontainer/ingest/location"
89 "github.com/microsoft/go-sqlcmd/pkg/mssqlcontainer/ingest/mechanism"
910 "path/filepath"
11+ "strings"
1012)
1113
1214type ingest struct {
1315 uri uri.Uri
1416 location location.Location
17+ controller * container.Controller
1518 mechanism mechanism.Mechanism
1619 options mechanism.BringOnlineOptions
1720 extractor extract.Extractor
1821 containerId string
1922 query func (text string )
2023}
2124
22- func (i ingest ) IsExtractionNeeded () bool {
23- i .extractor = extract .NewExtractor (i .uri .FileExtension ())
25+ func (i * ingest ) IsExtractionNeeded () bool {
26+ i .extractor = extract .NewExtractor (i .uri .FileExtension (), i . controller )
2427 if i .extractor == nil {
2528 return false
2629 } else {
2730 return true
2831 }
2932}
3033
31- func (i ingest ) IsRemoteUrl () bool {
34+ func (i * ingest ) IsRemoteUrl () bool {
3235 return ! i .location .IsLocal ()
3336}
3437
35- func (i ingest ) IsValidScheme () bool {
38+ func (i * ingest ) IsValidScheme () bool {
3639 for _ , s := range i .location .ValidSchemes () {
3740 if s == i .uri .Scheme () {
3841 return true
@@ -41,17 +44,25 @@ func (i ingest) IsValidScheme() bool {
4144 return false
4245}
4346
44- func (i ingest ) CopyToLocation () string {
45- return i .mechanism .CopyToLocation ()
46- }
47+ func (i * ingest ) CopyToContainer (containerId string ) {
48+ destFolder := "/var/opt/mssql/backup"
49+ if i .mechanism != nil {
50+ destFolder = i .mechanism .CopyToLocation ()
51+ }
52+ if i .location == nil {
53+ panic ("location is nil, did you call NewIngest()?" )
54+ }
4755
48- func (i ingest ) CopyToContainer (containerId string ) {
4956 i .containerId = containerId
50- i .location .CopyToContainer (containerId , i . CopyToLocation () )
57+ i .location .CopyToContainer (containerId , destFolder )
5158 i .options .Filename = i .uri .Filename ()
59+
60+ if i .options .Filename == "" {
61+ panic ("filename is empty" )
62+ }
5263}
5364
54- func (i ingest ) Extract () {
65+ func (i * ingest ) Extract () {
5566 if i .extractor == nil {
5667 panic ("extractor is nil" )
5768 }
@@ -61,30 +72,42 @@ func (i ingest) Extract() {
6172 }
6273
6374 i .options .Filename , i .options .LdfFilename =
64- i .extractor .Extract (i .uri .ActualUrl (), i . CopyToLocation () )
75+ i .extractor .Extract (i .uri .Filename (), "/var/opt/mssql/data" )
6576
6677 if i .mechanism == nil {
67- ext := filepath .Ext (i .options .Filename )
68- i .mechanism = mechanism .NewMechanismByFileExt (ext )
78+ fmt .Println ("FOO: " + i .options .Filename )
79+ ext := strings .TrimLeft (filepath .Ext (i .options .Filename ), "." )
80+ i .mechanism = mechanism .NewMechanismByFileExt (ext , i .controller )
6981 }
82+ fmt .Println ("FOO: " + i .mechanism .Name ())
83+
7084}
7185
72- func (i ingest ) BringOnline (query func (string ), username string , password string ) {
86+ func (i * ingest ) BringOnline (query func (string ), username string , password string ) {
87+ if i .options .Filename == "" {
88+ panic ("filename is empty, did you call CopyToContainer()?" )
89+ }
90+
91+ i .query = query
7392 i .options .Username = username
7493 i .options .Password = password
75- i .mechanism .BringOnline (i .uri .GetDbNameAsIdentifier (), i .containerId , query , i .options )
94+ i .mechanism .BringOnline (i .uri .GetDbNameAsIdentifier (), i .containerId , i . query , i .options )
7695 i .setDefaultDatabase (username )
7796}
7897
79- func (i ingest ) setDefaultDatabase (username string ) {
98+ func (i * ingest ) setDefaultDatabase (username string ) {
99+ if i .query == nil {
100+ panic ("query is nil, did you call BringOnline()?" )
101+ }
102+
80103 alterDefaultDb := fmt .Sprintf (
81104 "ALTER LOGIN [%s] WITH DEFAULT_DATABASE = [%s]" ,
82105 username ,
83106 i .uri .GetDbNameAsNonIdentifier ())
84107 i .query (alterDefaultDb )
85108}
86109
87- func (i ingest ) IsValidFileExtension () bool {
110+ func (i * ingest ) IsValidFileExtension () bool {
88111 for _ , m := range mechanism .FileTypes () {
89112 if m == i .uri .FileExtension () {
90113 return true
@@ -98,19 +121,19 @@ func (i ingest) IsValidFileExtension() bool {
98121 return false
99122}
100123
101- func (i ingest ) SourceFileExists () bool {
124+ func (i * ingest ) SourceFileExists () bool {
102125 return i .location .Exists ()
103126}
104127
105- func (i ingest ) UserProvidedFileExt () string {
128+ func (i * ingest ) UserProvidedFileExt () string {
106129 return i .uri .FileExtension ()
107130}
108131
109- func (i ingest ) ValidSchemes () []string {
132+ func (i * ingest ) ValidSchemes () []string {
110133 return i .location .ValidSchemes ()
111134}
112135
113- func (i ingest ) ValidFileExtensions () []string {
136+ func (i * ingest ) ValidFileExtensions () []string {
114137 extensions := []string {}
115138
116139 for _ , m := range mechanism .FileTypes () {
0 commit comments