views:

109

answers:

3

I am development a app multi tenant. I chose the approach Shared Database/ Separeted Schema.

My idea is has a schema default (dbo) and when deploy this schema, do update schema of outhers tenants (tanantA, tenantB, tenantC).

In other words, make sincronization schemas.

How can sincronized this schemas of tenants with schema default?

I am using SQL Server 2008.

Thank´s

+1  A: 

There is no magic command to synchronize the schemas as far as I know. You would need to use a tool - either built in house or bought (Check out Red Gate's SQL Compare and SQL Examiner - you need to tweak them to compare different schemas).

Just synchronizing can often be tricky business though. If you added a column, do you need to also fill that column with data? If you split a column into two new columns there has to be conversion code for something like that.

My suggestion would be to very carefully track any scripts that you run against the dbo schema and make sure that they also get run against the other schemas when appropriate. You can then use a tool like SQL Compare as an occasional sanity check to look for any unexpected differences.

Tom H.
My idea is automate this process. Using a program like Red Gate´s SQL Compare, the process are manual. Thanks.
Juliano Oliveira
Actually, SQL Compare has an API as well as command-line functionality, so if it's just keeping structures in sync AND (here's the important part no matter which method you use) if you can come up with well defined rules on how the synchronization works, then you should be able to automate it with SQL Compare.
Tom H.
+1  A: 

First thing you will need is a table or other mechanism to store the version information of the schema. If nothing else so that you can bind your application and schema together. There is nothing more painful than a version of the application against the wrong schema - failing, corrupting data etc.

The application should reject or shutdown if its not the right version - you might get some blowback when its not right, but protects you from the really bad day when the database corrumpts the valuable data

You'll need a way to track changes such as SVN or something else - from SQL you can export the intial schema. From here you will need a mechanism to track changes using a nice tool like SQL comapre and then track the schema changes and match to an update in version number in the target database.

we keep each delta in a separate folder beneath the upgrade utility we built. This utility signs onto the server, reads the version info and then applies the transform scripts from the next version in the database until it can find no more upgrade scripts in its sub folder. This gives us the ability upgrade a database no matter how old it is to the current version. If there are data transforms unique the tenant, thse are going to get tricky.

of course you should always make a backup of the databse that writes to an external file preferrrable with an human identifiable version number so you can find it and restore it when the script(s) go bad. And eventually it will so just plan on figuring out how to recover and restore.

--

I saw there is some sort of schema upgrader tool in the new VS 2010 but I havent used it. that might also be useful to you.

MikeJ
I was thinking in using a framework like Migrator.Net or RikMigrations.So, I can in my Build Server create a build that get lastest version and executes a command that check and update my schemas.One problem that I see is when the application have many tenants, this process of update schemas will be slow and dangerous.What you think?Thanks Mike
Juliano Oliveira
If you can close down the app off during a maintenance window this is best/easiest. not worries of data updates etc. you can have multiple instances backing up and and migrating against your sql servers(s).what we did was setup a front door page than that redirects users of a particular version to a particular serer/path. If a request to authenticate comes in adn their db is marked as in upgrade, we redirect them to "down for maintenance" page and dont pass them on to the app. once its back up for them, they can be redirected from the authentication to servers withthe new version
MikeJ
I looked at rikMigrations. The bummer I saw was that it doesnt have a way to work with SPs or Views, probably because these update when any DDL code executes. we include in our current scripts code to drop all of these artifacts and then rebuild these for the new schema.
MikeJ
A: 

How do you handle a web application where there is a generic application user logging in to the SQL Server?

What about an instance where you have a corporate HR dept that can see everyone and then regional HR that should only see the employees in that location?

Oxy