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
334 stars 58 forks source link

Container not deleted with DELETE #322

Closed way0utwest closed 1 year ago

way0utwest commented 1 year ago

I ran the DELETE command and thought I'd have the container removed, but it isn't. It also isn't re-used, so when I start with a new context, I get a new container.

I would expect if I lose context, that you also delete the container. Preferably, I'd have the option to specify if I delete a container and a way to specify which container to use on start.

stuartpa commented 1 year ago

sqlcmd does delete the container with:

sqlcmd delete

Per previous feedback from a user, we do allow the sqlcmd delete to succeed if the container has already been deleted.

If there is a situation where the container isn't being deleted, please run sqlcmd delete --verbosity 4 and add the output to this issue.

way0utwest commented 1 year ago

Doesn't work. Ran delete, then docker container ls. I see this:

2023-04-17 09_28_41-cmd - MoveSocFiles - MoveSocFiles - MoveSocFiles bat - MoveSocFiles bat

Delete command:

sqlcmd delete --verbosity 4 TRACE: Config loaded from file: C:\Users\Steve.sqlcmd\sqlconfig Current context is "mssql2". Do you want to continue? (Y/N) y TRACE: [{e78a8204f0b4a16b2f6245388e3dd6b436139e80567bfbb66365ae264f0c55a9 [/pedantic_keldysh] mcr.microsoft.com/mssql/server:latest sha256:c8f4b70e38bf5755c665781cd9395fa1347b23c3b0122e07fa62c4769e61c342 /opt/mssql/bin/permissions_check.sh /opt/mssql/bin/sqlservr 1681427744 [{0.0.0.0 1433 1436 tcp}] 0 0 map[com.microsoft.product:Microsoft SQL Server com.microsoft.version:16.0.4025.1 org.opencontainers.image.ref.name:ubuntu org.opencontainers.image.version:20.04 vendor:Microsoft] running Up 3 days {default} 0xc00044a1b8 []}] TRACE: Up Verifying no user (non-system) database (.mdf) files INFO: HINT: INFO: 1. If the database is mounted, run sqlcmd query "use master; DROP DATABASE [<database_name>]" INFO: 2. Pass in the flag --force to override this safety check for user (non-system) databases INFO: panic: Unable to continue, a user (non-system) database (/var/opt/mssql/AdventureWorksLT2012_Data.mdf) is present

goroutine 1 [running]: main.checkErr({0xfdd140?, 0xc000149548?}) D:/a/1/s/cmd/modern/main.go:148 +0x65 github.com/microsoft/go-sqlcmd/internal/output.Output.fatalf({0xeb7ea8, 0xeb7eb0, {0xfe01d8, 0xc0002c2b58}, 0x2, {0xfe0de0, 0xc000006018}}, {0xc0001394a0, 0x2, 0x2}, ...) D:/a/1/s/internal/output/output.go:186 +0x11b github.com/microsoft/go-sqlcmd/internal/output.Output.FatalfWithHints(...) D:/a/1/s/internal/output/output.go:63 github.com/microsoft/go-sqlcmd/cmd/modern/root.(Uninstall).userDatabaseSafetyCheck(0x1646e90?, 0xc000274200?, {0xc000274200?, 0x0?}) D:/a/1/s/cmd/modern/root/uninstall.go:161 +0x3a6 github.com/microsoft/go-sqlcmd/cmd/modern/root.(Uninstall).run(0xc000339180) D:/a/1/s/cmd/modern/root/uninstall.go:112 +0x96b github.com/microsoft/go-sqlcmd/internal/cmdparser.(Cmd).run(0xc000339180, 0x0?, {0xc0001381c0?, 0x0?, 0x0?}) D:/a/1/s/internal/cmdparser/cmd.go:286 +0x256 github.com/spf13/cobra.(Command).execute(0xc000339220, {0xc0001381a0, 0x2, 0x2}) C:/Users/VssAdministrator/go/pkg/mod/github.com/spf13/cobra@v1.6.1/command.go:920 +0x847 github.com/spf13/cobra.(Command).ExecuteC(0xc0005acca0) C:/Users/VssAdministrator/go/pkg/mod/github.com/spf13/cobra@v1.6.1/command.go:1044 +0x3bc github.com/spf13/cobra.(Command).Execute(...) C:/Users/VssAdministrator/go/pkg/mod/github.com/spf13/cobra@v1.6.1/command.go:968 github.com/microsoft/go-sqlcmd/internal/cmdparser.(Cmd).Execute(0xc0005acc00) D:/a/1/s/internal/cmdparser/cmd.go:154 +0x2a main.(Root).Execute(...) D:/a/1/s/cmd/modern/root.go:84 main.main() D:/a/1/s/cmd/modern/main.go:49 +0x205

Steve@ARISTOTLE C:\Users\Steve $

stuartpa commented 1 year ago

Ah, I see the function:

userDatabaseSafetyCheck.

Is being called... the error is:

"Unable to continue, a user (non-system) database ... is present"

Sqlcmd is failing the delete because it has detected a user database is present in the container. It is outputting the following hint:

"Pass in the flag --force to override this safety check for user (non-system) databases"

This check gives the user a chance to confirm they really want to delete left over user data.

way0utwest commented 1 year ago

Would you like me to submit doc PRs for some of this? Either here and/or MS learn?

felipeschneider88 commented 1 year ago

The error is crystal clear, you cannot DELETE with a user DB

PS C:\Users\felip> sqlcmd delete
Current context is "mssql". Do you want to continue? (Y/N)
Y
Verifying no user (non-system) database (.mdf) files

HINT:
  1. If the database is mounted, run `sqlcmd query "use master; DROP DATABASE [<database_name>]"`
  2. Pass in the flag --force to override this safety check for user (non-system) databases

Error: Unable to continue, a user (non-system) database (/var/opt/mssql/data/testDB.mdf) is present
PS C:\Users\felip>

I tested with the --flag and works fine

stuartpa commented 1 year ago

Would you like me to submit doc PRs for some of this? Either here and/or MS learn?

We are working on auto-generating documentation from sqlcmd --help for learn.microsoft.com. This should help with the docs aspects (we do cover this safety check in sqlcmd delete --help examples)