SQL Project Dynamically set Recovery Model - visual-studio-2012

We have a SQL Server Database Project (.sqlproj) in Visual Studio 2012 that we use as source control for our database schema. One of the cool things that it does is generate the SQL to update the schema when we release code.
We have 3 profiles setup - dev, test, live - which is all working fine.
Recently we changed our live database from "Simple" Recovery to "Full" Recovery. Everything was great until we tried to run our next deploy to dev and test. We don't want to change the recovery mode from Simple to Full on dev and test - there is no need for us to change it. However when we publish the database project it now wants to set it.
I want to set the recovery model based on which publish config I am using. I have tried creating a variable and assigning that in the projects xml:
<Recovery>$(RecoveryModel)</Recovery>
but it still tries to set it to "Full" in the deploy script:
:setvar DefaultDataPath "C:\Program Files\Microsoft SQL Server\MSSQL11.SQL2012\MSSQL\DATA\"
:setvar DefaultLogPath "C:\Program Files\Microsoft SQL Server\MSSQL11.SQL2012\MSSQL\DATA\"
:setvar RecoveryModel "Simple"
GO
:on error exit
GO
/*
Detect SQLCMD mode and disable script execution if SQLCMD mode is not supported.
To re-enable the script after enabling SQLCMD mode, execute the following:
SET NOEXEC OFF;
*/
:setvar __IsSqlCmdEnabled "True"
GO
IF N'$(__IsSqlCmdEnabled)' NOT LIKE N'True'
BEGIN
PRINT N'SQLCMD mode must be enabled to successfully execute this script.';
SET NOEXEC ON;
END
GO
IF EXISTS (SELECT 1
FROM [master].[dbo].[sysdatabases]
WHERE [name] = N'$(DatabaseName)')
BEGIN
ALTER DATABASE [$(DatabaseName)]
SET RECOVERY FULL
WITH ROLLBACK IMMEDIATE;
END
My current work around is to create a script in the Post-Deployment folder to work out which server I'm on and then set the recovery model back to simple if it's dev or test. This doesn't seem like the best solution.
Is there a way to set database properties with SQLCMD Variables?

I know this is an old question but I thought I would suggest this solution. Why can't you just run a Post Deployment script to set the recovery mode.
USE [MYDATABASE];
IF ##SERVERNAME= 'DEVSQLSERVER'
BEGIN;
ALTER DATABASE [MYDATABASE] SET RECOVERY SIMPLE ;
END;

If one were using VSTS: As I see it there are three ways to go about handling this situation (i.e. Recovery mode setting in DACPAC overwriting Recovery Mode in your database(s)).
Alter your VSTS Build definition to replace your 'Database Option:Recovery' with the desired state in your *.sqlproj before the msbuild step (I'm not sure you can change this per Release environment or based on the branch you're building on, so kinda ugly and unworkable to start with unless you have a separate build definition per environment):
Replace <Recovery>SIMPLE</Recovery> with <Recovery>FULL</Recovery> or vice versa in the *.sqlproj file
Modify your VSTS Release to use the "SQL Server Database Deploy" task (i.e. don't use "Run a DACPAC file") AND specify in "Additional Arguments":
/p:ScriptDatabaseOptions=false
Modify your VSTS Release to use "SQL Server Database Deploy" AND specify a "Publish Profile". I think the "Deploy database properties" option in the profile is equivalent to "Script Database Options" but I've not tested it because 2 was a reasonable solution for me. One could pre-setup the different profiles before build time or attempt to change a master profile with variables for each Release environment. I really didn't want to complicate my life with this...
So in the end I chose option (2) above and tested with and without the /p:ScriptDatabaseOptions=false setting. It worked as expected. Choosing option (2) means that you'll need to setup your environment databases ahead of time. We use a Recovery mode of SIMPLE in our non-Prod environments and FULL in our Prod environment. To verify either open up the database options in MSSQL Mgt Studio or view the SQL Server Logs to see if a message like so appeared:
Setting database option RECOVERY to SIMPLE for datatbase 'WhatACoolNameForADatabase'.
If I needed to be able to stand up new databases as part of my release process then I may have to venture into the realm of option 3. If that is where you're at, then good luck and let us know what you had to do to make it work.

You can go to the project settings in visual studio. Click Database Settings > Operational and change the recovery model to SIMPLE.

Related

How do you tell entity framework to create / deploy a database in Azure?

I'm using Entity Framework with a code first model; I've got my InitialCreate migration setup and working locally, I can run code against my database context, and everything works.
But when I deploy my project to Azure, I just get a connection string error ("Format of the initialization string does not conform to specification starting at index 0.").
I can't seem to find where in the Publish dialog are the options to create the Azure database. -- Do I have to create the database separately and hook them up manually? -- If so, what exact process should I follow. Does the database need to have contents?
I thought Microsoft was making a big deal that this could all be done in a single deploy step, but that doesn't seem to be the case from my current experience.
When you publish your project in the publish dialog, there is an option for the code first migration in the Settings tab, it will automatically show your data context and it will give you the option to set the remote connection string, and this will add a section in web.config to specify the data context and the Migration class to run during the migration process.
It will also allow you to set if you want to run the code first Migration or not.
You can also take a backup from the dev and clear the data then upload it to Azure SQL DB, this way the code first data context will check at first connection and it will find the code an database the same

ASP.NET 5 Azure template deployment failed creating SQL Server

I have added a new default ASP.NET 5 web app, and chosen to deploy to AZURE. This works fine, but if I retry and add a azure sql db, then I get the error below as it tries to create the new resource group.
Microsoft Visual Studio
Template deployment failed. Deployment operation statuses:
Failed: /subscriptions/81368473107b/resourceGroups/DJWTestDb/providers/Microsoft.Sql/servers/djwtestdbdbserver ()
error (InvalidApiVersionParameter): The api-version '2.0' is invalid. The api version must be of the following format: yyyy-MM-dd. This format supports the following suffixes: '-preview,-alpha,-beta,-rc,-privatepreview'.
Succeeded: /subscriptions/81368473107b/resourceGroups/DJWTestDb/providers/Microsoft.Web/serverfarms/DJWTestDbPlan ()
I've been running into the same problem, just registered to let you know. It seems that it isn't possible right now (who knows why, but I'm looking at you, ASP.NET RC1) to publish while deploying an SQL server. However, it DOES work to publish without one and make one in the portal, and then connect them through your Web.config.
At first I thought it was all of the new updates to Visual Studio (as of 5/12/15), but I was still getting the same problems after updating. I'm not sure if the new updates are required for this workaround, but that's what I was working with for this.
Steps:
1. Publish your API app as normal, without including an SQL server.
2. Go to your Azure portal and navigate to the "SQL Databases" tab.
3. Create a new database using your existing server, or just make a new database server if you don't have one yet.
4. When it's done (it may take a second for it to show up in the list), click on the new database and it's settings will appear. You want to go to the "Show Database Connection Strings" option.
5. Save the ADO.NET connection string!
6. Go back to your project in Visual Studio and navigate to the Web.config file.
7. In the "connectionStrings" header, find your database context. It should have a name formatted like YOURDATABASENAMEContext, or something similar. It'll probably be the second entry there. Replace (save a copy first, see note below for why) the inside of the "connectionString" with the ADO.NET connection string of the database you just created.
8. Publish and it should work!
NOTE: This workaround, if followed literally, means the program won't function locally anymore (e.g. starting the app rather than publishing it) since it will be looking for that database in the connection strings. You could return it to it's original state for testing, and then use the azure connection string when publishing, although I'm sure there's a more elegant way to do it.

Publish to Azure from Vs2013 with 'Execute Code First Migrations' checked overwrote remote database

While regular publish to Azure with WebDeploy, had checked Execute Code First Migrations, which i did before.
But this time the Use this connection string at runtime, was also checked, and i published without noticing it. as a result the remote azure db was wiped and instead is seeded with what looks like a default database with aspnetmemembership tables and _Migrations table that only has migrations related to identity tables.
The production data w db structures is gone and I did not yet setup backup on azure, doing it now.
Is there way to restore the database from some sort of auto backup on azure, i have web version w 1Gb size selected, I do not see any options
this suggests that web version would not have any daily backup, but also that web version is discountinued as of april, but i still have it. http://msdn.microsoft.com/en-us/library/jj650016.aspx
and another questions, i understand everything that happened? But it seems extremly dangerous that its so easy to wipe out the whole database and VS shows no warning nor publishing to azure notifies of anything. Is there anything that can be done to prevent dumb but yet very costly erros like this ?
TIA

Creating SQL Agent JOBs in SQL Server 2012 Database Projects from VS 2012

I am working on Visual Studio 2012 with Update 2 + SQL Server Data Tools and I am able work on the SQL Server Database Projects.
How do I create/add new SQL Agent JOB into this database project? Because if I click Add - New Item... I don't see anything for Job.
I know Jobs are related to SQL Server and the Database Project is related to a SQL Database. But I was wondering how do manage the SQL Jobs?
Thanks,
Prabhat
Jobs aren't supported natively. It might be possible to add these from the post-deployment script, although it's not something I've tried.
Creating server agent objects are not supported by visual studio database projects; however you can add your job by calling dbo.sp_add_job stored procedure.
The easiest way will be:
Create your job by SSMS and copy the script.
Control the job is already exist at database or not at your post deployment script. if it is exist delete at first; or you are going to have an error during deployment. you can do something like below:
DECLARE #JobID BINARY(16)
SELECT #JobID = job_id
FROM msdb.dbo.sysjob
WHERE (name = N'NameOfYourJob')
IF (#JobIDdel IS NOT NULL)
BEGIN
EXECUTE msdb.dbo.sp_delete_job #job_name = N'NameOfYourJob'
END
Paste your script for creating job to post deployment script.
For feeling more safe you can check the job is working at the moment or not and
you can disable job also before deleting.
Disable a job
Check job status

t-sql database restore without security

Is there a way to ignore the security portion (users) of a database when doing a restore? I know there is a way to script them all out, but we are restoring production databases on multiple dev machines, each having their particular set of users that we need to keep. Currently they are overwritten by production users.
[Microsoft SQL Server 2008 (SP1) Developer Edition (64-bit)]
There's no way to eliminate the security portion from a backup. It is possible to do a schema comparison using Visual Studio 2010 or higher before doing the restore and generate a script from that to do the permission changes.
See Compare and Synchronize Database Schemas.

Resources