microsoft / go-sqlcmd

The new sqlcmd, CLI for SQL Server and Azure SQL (winget install sqlcmd / sqlcmd create mssql / sqlcmd open ads)
https://learn.microsoft.com/sql/tools/sqlcmd/go-sqlcmd-utility
MIT License
343 stars 60 forks source link

Add use command, add support for local/remote files, add .mdf file attach support #363

Open stuartpa opened 1 year ago

stuartpa commented 1 year ago

DRAFT: Not ready for code review yet.


This PR is a partial merge of PR #319, which rounds out the "use" scenario for containers, to allow the casual user who doesn't know containers/linux well, to be able to get going quickly with already existing databases.

This PR adds supports for local files (remote http(s) was already supported), and .mdf files, and adds the sqlcmd use sub-command. To follow later will be .bacpac support, and compressed file support. (there are issues right now with these features, in how to reliably do them with the existing mssql container structure)

  1. --use now supports local and remote files sqlcmd create mssql --use [<local file>|<remote file>]

The T/SQL database name by default becomes the --use file name (without extension). This can be overridden with a ",", e.g.

sqlcmd create mssql --use https://aka.ms/AdventureWorksLT.bak,adventure_works
sqlcmd create mssql --use c:\users\alias\downloads\AdventureWorksLT.bak,adventure_works

This will result in the restored database being named [adventure_works] instead of [AdventureWorksLT]

  1. --use now supports .mdf and .bak files sqlcmd create mssql --use [.mdf|.bak]

  2. the new sqlcmd use subcommand allows all of the above, to add a database to an already existing container sqlcmd use [<local file>|<remote file>]

Examples:

sqlcmd install mssql --accept-eula

# Restore a backup (override database name to be [adventure_works], instead of [AdventureWorksLT])
sqlcmd use https://aka.ms/AdventureWorksLT.bak,adventure_works
sqlcmd query "DROP DATABASE [adventure_works]"
sqlcmd use c:\Users\alias\Downloads\AdventureWorksLT.bak,adventure_works