@@ -9,12 +9,11 @@ import (
99 "fmt"
1010 "github.com/microsoft/go-sqlcmd/internal/cmdparser/dependency"
1111 "github.com/microsoft/go-sqlcmd/internal/tools"
12+ "github.com/microsoft/go-sqlcmd/pkg/mssqlcontainer/ingest"
1213 "os"
1314 "runtime"
1415 "strings"
1516
16- "github.com/microsoft/go-sqlcmd/pkg/mssqlcontainer"
17-
1817 "github.com/microsoft/go-sqlcmd/cmd/modern/root/open"
1918
2019 "github.com/microsoft/go-sqlcmd/cmd/modern/sqlconfig"
@@ -59,9 +58,11 @@ type MssqlBase struct {
5958
6059 port int
6160
62- usingDatabaseUrl string
63- openTool string
64- openFile string
61+ useDatabaseUrl string
62+ useMechanism string
63+
64+ openTool string
65+ openFile string
6566
6667 unitTesting bool
6768
@@ -219,12 +220,26 @@ func (c *MssqlBase) AddFlags(
219220 })
220221
221222 addFlag (cmdparser.FlagOptions {
222- String : & c .usingDatabaseUrl ,
223+ String : & c .useDatabaseUrl ,
223224 DefaultString : "" ,
224225 Name : "using" ,
225226 Usage : "Download and use database from .bak/.bacpac/.mdf/.7z URL" ,
226227 })
227228
229+ addFlag (cmdparser.FlagOptions {
230+ String : & c .useDatabaseUrl ,
231+ DefaultString : "" ,
232+ Name : "use" ,
233+ Usage : "Download and use database from .bak/.bacpac/.mdf/.7z URL" ,
234+ })
235+
236+ addFlag (cmdparser.FlagOptions {
237+ String : & c .useMechanism ,
238+ DefaultString : "" ,
239+ Name : "use-mechanism" ,
240+ Usage : "Mechanism to use to make --use database online (attach, restore, dacfx)" ,
241+ })
242+
228243 addFlag (cmdparser.FlagOptions {
229244 String : & c .openTool ,
230245 DefaultString : "" ,
@@ -281,6 +296,7 @@ func (c *MssqlBase) Run() {
281296// command-line and the program will exit.
282297func (c * MssqlBase ) createContainer (imageName string , contextName string ) {
283298 output := c .Cmd .Output ()
299+ controller := container .NewController ()
284300 saPassword := c .generatePassword ()
285301
286302 env := []string {
@@ -294,8 +310,39 @@ func (c *MssqlBase) createContainer(imageName string, contextName string) {
294310 }
295311
296312 // Do an early exit if url doesn't exist
297- if c .usingDatabaseUrl != "" {
298- mssqlcontainer .ValidateUsingUrlExists (c .usingDatabaseUrl , output )
313+ var useDatabase ingest.Ingest
314+ if c .useDatabaseUrl != "" {
315+ useDatabase = ingest .NewIngest (c .useDatabaseUrl , controller , ingest.IngestOptions {
316+ Mechanism : c .useMechanism ,
317+ })
318+
319+ if ! useDatabase .IsValidFileExtension () {
320+ output .FatalfWithHints (
321+ []string {
322+ fmt .Sprintf (
323+ "--using must be a path to a file with a %q extension" ,
324+ strings .Join (useDatabase .ValidFileExtensions (), ", " ),
325+ ),
326+ },
327+ "%q is not a valid file extension for --using flag" , useDatabase .UserProvidedFileExt ())
328+ }
329+
330+ if useDatabase .IsRemoteUrl () && ! useDatabase .IsValidScheme () {
331+ output .FatalfWithHints (
332+ []string {
333+ fmt .Sprintf (
334+ "--using URL must one of %q" ,
335+ strings .Join (useDatabase .ValidSchemes (), ", " ),
336+ ),
337+ },
338+ "%q is not a valid URL for --using flag" , c .useDatabaseUrl )
339+ }
340+
341+ if ! useDatabase .SourceFileExists () {
342+ output .FatalfWithHints (
343+ []string {fmt .Sprintf ("File does not exist at URL %q" , c .useDatabaseUrl )},
344+ "Unable to download file" )
345+ }
299346 }
300347
301348 if c .defaultDatabase != "" {
@@ -304,8 +351,6 @@ func (c *MssqlBase) createContainer(imageName string, contextName string) {
304351 }
305352 }
306353
307- controller := container .NewController ()
308-
309354 if ! c .useCached {
310355 c .downloadImage (imageName , output , controller )
311356 }
@@ -344,8 +389,7 @@ func (c *MssqlBase) createContainer(imageName string, contextName string) {
344389 config .CurrentContextName (),
345390 config .GetConfigFileUsed ())
346391
347- controller .ContainerWaitForLogEntry (
348- containerId , c .errorLogEntryToWaitFor )
392+ controller .ContainerWaitForLogEntry (containerId , c .errorLogEntryToWaitFor )
349393
350394 output .Infof (
351395 "Disabled %q account (and rotated %q password). Creating user %q" ,
@@ -374,24 +418,23 @@ func (c *MssqlBase) createContainer(imageName string, contextName string) {
374418 Name : "sa" }
375419
376420 c .sql .Connect (endpoint , saUser , sql.ConnectOptions {Database : "master" , Interactive : false })
377-
378421 c .createNonSaUser (userName , password )
379422
380423 // Download and restore DB if asked
381- if c .usingDatabaseUrl != "" {
382- mssqlcontainer .DownloadAndRestoreDb (
383- controller ,
384- containerId ,
385- c .usingDatabaseUrl ,
386- userName ,
387- password ,
388- c .sql .Query ,
389- c .Cmd .Output (),
390- )
424+ if useDatabase != nil {
425+ output .Infof ("Copying to container" )
426+ useDatabase .CopyToContainer (containerId )
427+
428+ if useDatabase .IsExtractionNeeded () {
429+ output .Infof ("Extracting files from archive" )
430+ useDatabase .Extract ()
431+ }
432+
433+ output .Infof ("Bringing database online" )
434+ useDatabase .BringOnline (c .sql .Query , userName , password )
391435 }
392436
393437 if c .openTool == "" {
394-
395438 hints := [][]string {}
396439
397440 // TODO: sqlcmd open ads only support on Windows right now, add Mac support
0 commit comments