DATABASE PROJECT SQLCMD -v - sql-server

To control the database version, I use the Visual Studio database project but to execute the script generated by the project I use the sqlcmd.exe.
There is a variable called $(environment) in my script.postdeployment.sql that depends on the server environment (prd,uat,dev) and I use the command sqlcmd.exe -v environment="dev" to set this value to this variable. The problem is the Visual Studio throw a compilation error
SQL72008: Variable Environment is not defined.
Ok, I know why this error is trown, It is because the variable is not defined in the Prject properties (SQLCMD Variables tab), BUT if I add this variable in the project properties the value will be setted in the script overriding the value I passed in the sqlcmd.exe -v environment="dev".
For example
, If I set the $(environment) variable in the project properties as "uat" and execute sqlcmd.exe -v environment="dev", the highest precedence is the value I set in the project properties, so UAT, because the project generate the following statment in my script:
:setvar app_environment "uat"
How could I handle this issue between visual studio database project and sqlcmd.exe?

The way I have fixed this is to Set the properties of the post deployment script as follows:
Build Action: None
Copy to Output Directory: Copy always
When you do this, Visual Studio won't build the post script file, but will copy it to the /bin folder. You can then call SQLCMD twice. Once for the database creation script, and then a second time for the post build script using the -v option.

Related

How to run batch file inside Visual Studio 2022 Developer Command Prompt

I recently upgraded to VS2022 I cannot find a way to start developer command prompt and run a batch file inside it. Previously I called vsvars32.bat and it set all variables and paths, allowing me to continue in my .bat file. In the current version it does not work and when I call VsDevCmd.bat, the rest of commands in my batch file are not executed.
Is there a way to call developer command prompt or set paths and variables from batch file and continue?
I did not find any other solution so ended up using pipe like this:
type cmds.txt | "c:\Program Files\Microsoft Visual
And cmds.txt contains batch file invocation:
buildall.bat
Not pretty, but works.
I just want to thank you and confirm that this was the only way to automate compilation of a C++ project trhough VS Code.
I tried setting up tasks.json in VS Code and multiple other setups but after 4 days of that nothing worked except your solution PiotrK.
So I ended up preparing a TXT file with a list of commands like so:
set PATH=%PATH%;"C:\Users\DEKOLEV\AppData\Local\Programs\Microsoft VS Code\bin"
cd "C:\path\to\some\folder\with\C++\source\code\files"
code .
}
and I piped the text file directly to the Developer Console link. So now I have a batch file containing the following
type "C:\Some\path\to\file\commands.txt" | "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2022\Visual Studio Tools\VC\x64 Native Tools Command Prompt for VS 2022.lnk"
Starting the batch file starts the x64 developer command prompt and runs VS Code from it which enables VS Code to compile the source C++ files with the environment x64 Native Tools Command Prompt for VS 2022 provides.
I don't have enough points to post a comment so I am posting this feedback as an answer. :)

How to get vscode-remote-ssh extension connects via git bash in windows?

I have vs code configured to use the git bash as a terminal in windows 7.
"terminal.integrated.shell.windows": "C:\Program Files\Git\bin\bash.exe"
I have enabled SSH key based authentication to remotely access a host. All this works fine from within the terminal in VS Code.
However, when using the vscode-remote SSH extension to connect to host I get an error because it tries to connect using "The terminal process command 'cmd.exe'" instead of git bash.
I've checked my terminal settings configuration in vs code and it points to git bash.exe
I've used the terminal extension in vs code and it opens a git bash and successfully connects to the host
Is there a setting that I'm missing to force Remote-SSH to use the git bash for the connection?
JerryL's answer lead me to realize, that I can simply set GIT's ssh path c:\Program Files\Git\usr\bin\ssh.exe in the remote.SSH.path setting of VS Code Preferences:
Then it just worked like a charm.
Just for clarity my VS Code version is: 1.40.0-insider (system setup)
I ran into a similar issue trying to get MS VS Code Studio Remote-SSH working with Putty's Pageant. I had Git for Windows installed and in a Git Bash shell, I could ssh and pick up the Pageant keys and no password was needed.
But VS Code Remote-SSH, while using the Git ssh in C:\Program Files\Git\usr\bin\ssh.exe was using Windows 7 cmd.exe shell which didn't talk to Pageant.
What worked for me on Windows 7, VS Code 1.36.1 with (Remote Development 0.15.0, Remote-SSH 0.44.0) and Git for Windows 2.22:
Start Pageant (C:\Program Files\PuTTY\pageant.exe) and Add key.
Start the ssh agent shim (C:\Program Files\Git\cmd\start-ssh-pageant.cmd). This takes care of the communication between Git ssh, which looks for ssh-agent, and Pageant.
Create the SSH_AUTH_SOCK environment variable
Control Panel / System / Advanced Settings / Environment Variables..
User variables for username / New..:
Variable name: SSH_AUTH_SOCK
Variable value: /tmp/.ssh-pageant-username (e.g. /tmp/.ssh-pageant-bill) (Environment Variable assignment screenshot)
Test ssh:
Open a command prompt: Enter set to view the list of Environment Variables. Is the SSH_AUTH_SOCK variable set correctly to something like /tmp/.ssh-pageant-bill?
Try ssh to your host using Git's ssh.exe: c:\Program Files\Git\usr\bin\ssh.exe user#host If this works, then VS Code Remote-SSH should work.
Finally, I added Pageant and start-ssh-pageant.cmd to my Windows 7 Startup so this persists across reboots.
Hope that helps.
Jerry.
I had a similar problem trying to get VS Code Remote use Putty Pageant.
1. Create .bat file somewhere with the following content:
echo OpenSSH
"C:\YOUR_PATH_HERE\PLINK.EXE" -ssh %*
2. Open VS Code settings, type remote ssh path in search and find Remote.SSH: Path settings
3. Past here path to your .bat file
4. Now VS Code Remote will use Pageant correctly.

Executing batch script from ansible control host does not work properly

I have following very simple batch script to install Visual Basic 2017 Build Tools.
C:\packages\visualstudio2017buildtools\vs_buildtools.exe --add Microsoft.VisualStudio.Workload.DataScience --add Microsoft.VisualStudio.Workload.ManagedDesktop --add Microsoft.VisualStudio.Workload.NativeDesktop --add Microsoft.VisualStudio.Workload.NetCoreTools --add Microsoft.VisualStudio.Workload.Universal --includeOptional --includeRecommended --noweb --quiet
When I execute this script locally on the server, everything works as expected while executing it from ansible control host does not install the software. Tried many things e.g. invoke script from ansible control host and also tried to execute it locally after copying it to the remote server. It looks like the script is executing and copies few dlls from the installer but never installs the actual software/components.
Any idea what I maybe missing?
Example ansible task:
- name: Invoke batch script to install Build Tools
win_command: install.bat
args:
executable: cmd
chdir: '{{ buildtools_dst_dir }}\visualstudio2017buildtools\'
creates: '{{ buildtools_dst_dir }}\do_not_run_buildtools\'
Update:
It looks like I was missing --wait parameter in batch script when running from ansible. Adding that solved the issue.

Deploy the database to Docker Container microsoft/mssql-server-linux

I have a database running on SQL Server (13.01) on Windows. I like to deploy it to the Docker Container on Linux using SSDT.
I can perfectly connect to the server running on Docker and create/drop database manually and play with the data.
The problem is I can not publish it. I'm executing following script on Powershell
PS: SqlPackage.exe /Action:Publish /SourceFile:"d.dacpac" /TargetConnectionString:"server=containeraddress;database=thedatabase;user id=sa;password=thepassword;
and getting the following error.
Unable to connect to master or target server 'the database'. You must have a user with the same password in master or target server 'the database'. (Microsoft.Data.Tools.Schema.Sql)
I have the same user and same password on target and source servers.
Is there anybody has the same problem and know how to solve it?
I'll post this here as most of the answers refer to having an existing compiled dacpac file, which may not always be possible. I haven't seen similar ideas posted elsewhere to the solution I'm suggesting here.
Given your usage of docker and if you wish to compile your visual studio project inside the container, given certain combinations of the container base OS and image may not be possible to create a dacpac file with msbuild.
You can work around restoring the database using a series of unix based commands, taking note that the visual studio database project is usually just a series of SQL files, below I show an example of this, where I concat the SQL files into a single file and call sqlcmd to run the script;
FROM mcr.microsoft.com/mssql/server
WORKDIR /init
ENV ACCEPT_EULA=Y
ENV MSSQL_SA_PASSWORD=MyPassword
EXPOSE 1433:1433
RUN apt-get update && apt-get install dos2unix
COPY /solution_folder/database/Tables/*.sql /init/
WORKDIR /database
RUN echo "CREATE DATABASE [database_name];\nGO\nUSE [database_name];\n” >> /database/create.sql
RUN for f in /init/*.sql; do dos2unix $f; cat $f >> /database/create.sql; echo "\nGO\n" >> /database/create.sql; done
RUN ( /opt/mssql/bin/sqlservr --accept-eula & ) | grep -q "Service Broker manager has started" && /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P ‘MyPassword’ -i /database/create.sql && pkill sqlservr
The reason for "dos2unix" is that the SQL files created within visual studio have unique hidden cr/lf (and other characters) which the linux version of sqlcmd won't interpret successfully and will cause errors (which is kind of bizarre and this is exactly the kind of thing you'd want a cross platform database to be able to cope with)
Also, within the final run command you have to start-up the sql server service temporarily otherwise you'll also get errors; it's a little bit of work-around, and a bit fiddly and I'm not sure entirely that the microsoft sql server linux container is well designed enough for the simple task of restoring a database like this, the nuances are the differences between building and running a container and needing some sort of happy middle ground of both concepts for it to work.
Given here isn't a complete solution to restore, it only deals with Tables from the project file, although it should be trivial to expand to scalar functions and stored procedures.
Which version of SqlPackage.exe are you using? Only the most recent release candidate versions of SqlPackage.exe support SQL Server vNext CTP. The SqlPackage release candidate can be downloaded here: https://www.microsoft.com/en-us/download/details.aspx?id=54273

appveyor - how to set environment variable using %HOMEPATH%

I'm working on a node.js native addon project, instead of using binding.gyp, I'm using cmake, as my project depends on another cmake project.
I can use cmake to build the target(on my own windows 7), which needs to setup environment variables to specify library path include node-gyp and mysql. I got the mysql path(from appveyor doc), please correct me if I'm wrong.
But I don't know how to set node-gyp dir in appveyor environment, which is located in ~/.node-gyp windows equivalent path. I tried below script, and it errored out at NODE_GYP_DIR line.
environment:
# set variables
NODE_GYP_VER: 0.12.7
NODE_GYP_DIR: %HOMEDRIVE%%HOMEPATH%\.node-gyp
LIBMYSQL_INCLUDE_DIR: C:\Program Files\MySql\MySQL Server 5.6\include
LIBMYSQL_LIBRARY: C:\Program Files\MySql\MySQL Server 5.6\lib
My question is, can I assume ~/.node-gyp windows equivalent path exists? How to set the path to my environment variable for my cmake? Thanks!
Use %USERPROFILE% instead:
environment:
# set variables
NODE_GYP_VER: 0.12.7
NODE_GYP_DIR: '%USERPROFILE%\.node-gyp'
LIBMYSQL_INCLUDE_DIR: C:\Program Files\MySql\MySQL Server 5.6\include
LIBMYSQL_LIBRARY: C:\Program Files\MySql\MySQL Server 5.6\lib

Resources