views:

1576

answers:

5

We have a common problem of moving our development SQL 2005 database onto shared web servers at website hosting companies.

Ideally we would like a system that transfers the database structure and data as an exact replica.

This would be commonly achieved by restoring a backup. But because they are shared SQL servers, we cannot restore backups – we are not given access to the actual machine.

We could generate a script to create the database structure, but then we could not do a data transfer through the menu item Tasks/Import Data because we might violate foreign key constraints as tables are imported in an order the conflicts with the database schema. Also, indexes might not be replicated if they are set to auto generate.

Thus we are left with a messy operation:

  1. Create a script in SQL 2005 that generates the database in SQL 2000 format.
  2. Run the script to create a SQL 2000 database in SQL 2000.
  3. Create a script in SQL 2000 that generates the database structure WITHOUT indexes and foreign keys.
  4. Run this script on the production server. You now have a database structure to upload data to.
  5. Use SQL 2005 to transfer the data to the production server with Tasks/Import data.
  6. Use SQL 2000 to generate a script that creates the database with indexes and keys.
  7. Copy the commands that generate the indexes and foreign keys only. These are located after the table creation commands. Note: In SQL 2005, the indexes and foreign keys are generated as one and cannot be easily separated.
  8. Run this script on the production database.

Voila! The database is uploaded with all data and keys/constraints in place. What a messy and error prone system.

Is there something better?

+4  A: 

Scott Gu had written few posts on this topic :

SQL Server Database Publishing Toolkit for Web Hosting

Gulzar
This looks good.I was not aware that there was an option in the Database Explorer to Publish to Provider. We will give this a go on our next job.
Petras
+1  A: 

You should not rely on restoring backups for copying / transferring databases. You need to use scripts - trust me you will get better at it.

DJ
A: 

I have used the RedGate Compare tools with shared hosting and it works well.

Craig
+1  A: 

Database-generation scripts are messy, but they also have several advantages that ... well, make the pain more tolerable.

First, if you treat the DB scripts as real programming tasks in and of themselves, you can encapsulate the messiness. If you generate a script once (using a database tool), you can split the table structure aspects from the constraint aspects (keys, indices, etc.). Similarly, you can export the data once, but split it it into "system" data that's not frequently changed but is necessary for correct operation (stuff like tax or shipping rates, etc.), 'test' data that's easily identifiable, and 'operational' data that needs to be moved from DB version Old to DB version New (last week's Orders).

The first 3 minutes after you've accomplished that, things are wonderful: you can regenerate a new database with or without test data in a few minutes. Unfortunately, after 3 minutes, the databases are out of synch, at least in terms of data, if not quite as frequently in terms of structure.

I personally like to have each table's structure as a separate SQL file (and it's constraints as a separate file in a separate directory, and it's test data in one file, it's system data in another, etc.). On the one hand, this means that several different files have to be touched when making a change, but on the other hand, it makes it much easier to see the granularity of what's been changed: it's all right there in the version control logs. (I could probably be convinced that many-files is a mistaken strategy...)

All of this is predicated on the assumption that you have some facility for actually running a complex script involving many files and are not just constrained to some Web-based control panel, which may be what you're describing when you say "we are not given access to the actual machine." I feel that you can't do custom software development and not have some kind of shell access on the server; the hosting business is competitive enough that you can certainly find a script-friendly host easily enough.

Larry OBrien
A: 

Generation scripts are fine for creating the database objects, but not for transporting database information. For example, client-specific databases where the developer is required to pre-populate some data.

One of the issues I've run into with this is the new MAX types in SQL Server 2005+. (nvarchar(max), varchar(max), etc.) Of course, this is worse when you are actually using Sql Server Express, which doesn't allow for exporting other than creating your own scripts to create the data.

I would recommend switching to a hosting company that allows you to have the ability to FTP backup files and does NOT require you to use your own scripts. That's the whole point of SQL Server, right? To provide more tools that are friendlier to use. If the hosting company takes that away, you may as well move to MySql for its ease in dumping information.

WebHost4Life is a life saver in this category. They offer FTP to the database server to upload your backup file or MDF and LDF files for attachment! I was so upset when I saw GoDaddy had the similar restriction you mentioned. Their tool didn't tell me it was a bad import, and I couldn't figure out why my site was coming back with 500 errors.

One other note: I'm not sure which is considered more secure. I enabled external connections in GoDaddy and connected with Management Studio, and I was able to see every database on that server! I couldn't access them, but I now have that info. A double whammy is that GoDaddy requires that the user name for the DB be the same as the DB! now all you need to do is spam passwords against those hundreds of DBs!

Webhost4life, on the other hand, has only your specific database shown in Management Studio. And they let you pick your own DB name and user name, independent of each other. They only append the same unique id on the end of the user & db names in order to keep them from conflicting with others.