views:

440

answers:

3

Hi, I'm using VSTS Database Edition GDR Version 9.1.31024.02

I've got a project where we will be creating multiple databases with identical schema, on the fly, as customers are added to the system. It's one DB per customer. I thought I should be able to use the deploy script to do this. Unfortunately I always get the full filenames specified on the CREATE DATABASE statement. For example:

CREATE DATABASE [$(DatabaseName)]
ON 
PRIMARY(NAME = [targetDBName], FILENAME = N'$(DefaultDataPath)targetDBName.mdf')
LOG ON (NAME = [targetDBName_log], FILENAME = N'$(DefaultDataPath)targetDBName_log.ldf')
GO

I'd expected something more like this

CREATE DATABASE [$(DatabaseName)]
ON 
PRIMARY(NAME = [targetDBName], FILENAME = N'$(DefaultDataPath)$(DatabaseName).mdf')
LOG ON (NAME = [targetDBName_log], FILENAME = N'$(DefaultDataPath)$(DatabaseName)_log.ldf')
GO

Or even

CREATE DATABASE [$(DatabaseName)]

I'm not going to be running this on an on-going basis so I'd like to make it as simple as possible, for the next guy. There are a bunch of options for deployment in the project properties, but I can't get this to work the way I'd like.

Any one know how to set this up?

A: 

Hmm, well it seems that the best answer so far (given the over whelming response) is to edit the file after the fact... Still looking

baralong
+1  A: 

We do this using a template database, that we back up, copy, and restore as new customers are brought online. We don't do any of the schema creation with scripts but with a live, empty DB.

Joe
+1  A: 

Better late than never, I know how to get the $(DefaultDataPath)$(DatabaseName) file names from your second example.

The SQL you're showing in your first code snippet suggests that you don't have scripts for creating the database files in your VSTS:DB project, perhaps by deliberately excluded them from any schema comparisons you've done. I found it a little counter-intuitive, but the solution is to let VSTS:DB script the MDF and LDF in you development environment, then edit those scripts to use the SQLCMD variables.

In your database project, go to the folder Schema Objects > Database Level Objects > Storage > Files. In there, add these two files:

Database.sqlfile.sql

ALTER DATABASE [$(DatabaseName)]
ADD FILE (NAME = [$(DatabaseName)], 
    FILENAME = '$(DefaultDataPath)$(DatabaseName).mdf',
    SIZE = 2304 KB, MAXSIZE = UNLIMITED, FILEGROWTH = 1024 KB) 
    TO FILEGROUP [PRIMARY];

Database_log.sqlfile.sql

ALTER DATABASE [$(DatabaseName)]
    ADD LOG FILE (NAME = [$(DatabaseName)_log], 
    FILENAME = '$(DefaultDataPath)$(DatabaseName)_log.ldf', 
    SIZE = 1024 KB, MAXSIZE = 2097152 MB, FILEGROWTH = 10 %);

The full database creation script that VSTS:DB, or for that matter VSDBCMD.exe, generates will now use the SQLCMD variables for naming the MDF and LDF files, allowing you to specify them on the command line, or in MSBuild.

Rory MacLeod

related questions