episerver / content-templates

Apache License 2.0
12 stars 7 forks source link

create-db.sh for docker incorrectly reports "Creating database completed" when sqlcmd has failed #45

Open ChristopherHackett opened 1 year ago

ChristopherHackett commented 1 year ago

When setting the Alloy demo up

$ dotnet new epi-alloy-mvc --name alloy-docker  --output ./alloy-docker --enable-docker
The template "Optimizely Alloy MVC" was created successfully.

$ cd alloy-docker
$ docker-compose up

I have an issue with the database creation. This may be a local issue with docker however on reviewing the logs I noticed CREATE DATABASE failed. followed by Creating database completed.

2023-02-22 11:38:02.26 spid51      CREATE FILE encountered operating system error 87(The parameter is incorrect.) while attempting to open or create the physical file '/var/opt/mssql/host_data/cms.mdf'.
Msg 5123, Level 16, State 1, Server 9b4d2b960188, Line 1
CREATE FILE encountered operating system error 87(The parameter is incorrect.) while attempting to open or create the physical file '/var/opt/mssql/host_data/cms.mdf'.
Msg 1802, Level 16, State 4, Server 9b4d2b960188, Line 1
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.
Creating database completed

On reviewing the source code the status code is checked

https://github.com/episerver/content-templates/blob/0c72f0eea84fc20e4af5184a8ed7360e093d2527/templates/Alloy.Mvc/Docker/create-db.sh#L1-L12

However sqlcmd requires -b to return an error value https://learn.microsoft.com/en-us/sql/tools/sqlcmd/sqlcmd-utility

Error Reporting Options
-b
Specifies that sqlcmd exits and returns a DOS ERRORLEVEL value when an error occurs. The value that is returned to the DOS ERRORLEVEL variable is 1 when the SQL Server error message has a severity level greater than 10; otherwise, the value returned is 0. If the -V option has been set in addition to -b, sqlcmd will not report an error if the severity level is lower than the values set using -V. Command prompt batch files can test the value of ERRORLEVEL and handle the error appropriately. sqlcmd does not report errors for severity level 10 (informational messages).

If the sqlcmd script contains an incorrect comment, syntax error, or is missing a scripting variable, ERRORLEVEL returned is 1.

The following files appear to be affected by this behaviour:

ChristopherHackett commented 1 year ago

The underlying issue that caused the command to fail was related to local docker setup however the reported problem re error checking is still a relevant issue to resolve.

JohanPetersson commented 1 year ago

Thanks for reporting this. We will add -b to improve the error-handling.